Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions jester.nim
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -889,13 +889,13 @@ proc createRegexPattern(
routeNode, reMatchesSym, patternMatchSym: NimNode,
pathPrefix: string
): NimNode {.compileTime.} =
# -> let <patternMatchSym> = find(request.pathInfo, <pattern>, <reMatches>)
# -> let <patternMatchSym> = findRe(request.pathInfo, <pattern>, <reMatches>)
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
Expand All @@ -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

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious about why you're changing this

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std/re in nim is deprecated due to using pcre1 and it's recommended to use nitely regex which uses re2. See nim-lang/Nim@8b44b9d for more info.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else:
macros.error("Invalid pattern type: " & $pattern[0])
else:
Expand Down
5 changes: 2 additions & 3 deletions jester.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
25 changes: 24 additions & 1 deletion jester/request.nim
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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