diff --git a/jester.nim b/jester.nim index 647146b..7f57658 100644 --- a/jester.nim +++ b/jester.nim @@ -1,6 +1,6 @@ # Copyright (C) 2015 Dominik Picheta # MIT License - Look at license.txt for details. -import net, strtabs, re, tables, os, strutils, uri, +import net, strtabs, regex, tables, os, strutils, uri, times, mimetypes, asyncnet, asyncdispatch, macros, md5, logging, httpcore, asyncfile, macrocache, json, options, strformat @@ -889,13 +889,13 @@ proc createRegexPattern( routeNode, reMatchesSym, patternMatchSym: NimNode, pathPrefix: string ): NimNode {.compileTime.} = - # -> let = find(request.pathInfo, , ) + # -> let = findRe(request.pathInfo, , ) var strNode = routeNode[1].copyNimTree() strNode[1].strVal = escapeRegex(pathPrefix) & strNode[1].strVal return newLetStmt( patternMatchSym, newCall( - bindSym"find", + bindSym"findRe", parseExpr("request.pathInfo"), strNode, reMatchesSym @@ -913,7 +913,7 @@ proc determinePatternType(pattern: NimNode): MatchType {.compileTime.} = of nnkCallStrLit: expectKind(pattern[0], nnkIdent) case ($pattern[0]).normalize - of "re": return MRegex + of "re2": return MRegex else: macros.error("Invalid pattern type: " & $pattern[0]) else: diff --git a/jester.nimble b/jester.nimble index d17be37..e3e0c13 100644 --- a/jester.nimble +++ b/jester.nimble @@ -11,9 +11,8 @@ skipDirs = @["tests"] # Deps requires "nim >= 1.0.0" - -when not defined(windows): - requires "httpbeast >= 0.4.0" +requires "regex >= 0.21.0" +requires "httpbeast >= 0.4.0" task test, "Runs the test suite.": exec "nimble install -y asynctools@#0e6bdc3ed5bae8c7cc9" diff --git a/jester/request.nim b/jester/request.nim index 205f832..e3e2593 100644 --- a/jester/request.nim +++ b/jester/request.nim @@ -1,7 +1,13 @@ -import uri, cgi, tables, logging, strutils, re, options +import uri, cgi, tables, logging, strutils, options + +import regex import jester/private/utils +# Maximum number of capture groups stored for regex routes; mirrors std/re's +# limit, which this module previously pulled in (and with it, PCRE1). +const MaxSubpatterns* = 20 + when useHttpBeast: import httpbeast except Settings import options, httpcore @@ -196,3 +202,20 @@ proc setPatternParams*(req: var Request, p: Table[string, string]) = proc setReMatches*(req: var Request, r: array[MaxSubpatterns, string]) = req.reMatches = r + +proc findRe*(s: string, pattern: Regex2, + matches: var array[MaxSubpatterns, string]): int = + ## std/re-compatible ``find`` backed by nitely regex: returns the start + ## index of the first match or -1, and fills ``matches`` with the capture + ## group texts (group 1 ends up in ``matches[0]``, and so on). + var m: RegexMatch2 + if find(s, pattern, m): + result = m.boundaries.a + var i = 0 + while i < m.groupsCount and i < MaxSubpatterns: + let sl = m.group(i) + if sl.a <= sl.b: + matches[i] = s[sl] + inc i + else: + result = -1