diff options
author | bptato <nincsnevem662@gmail.com> | 2024-06-03 20:29:10 +0200 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2024-06-03 20:29:10 +0200 |
commit | 3e12a95ab34e120fb958ba0eeebaada5def7cd11 (patch) | |
tree | ff46a2d73d32c2a2a72df82801483dc7fd0134a2 /src/js | |
parent | 5acbab703ba074e20dbc0ce729d9f57f467901e0 (diff) | |
download | chawan-3e12a95ab34e120fb958ba0eeebaada5def7cd11.tar.gz |
js: improve jsregex interface
It's easier to just use nested seqs here. (This also fixes reverse-search highlighting the last capture group instead of the whole match.)
Diffstat (limited to 'src/js')
-rw-r--r-- | src/js/jsregex.nim | 29 |
1 files changed, 13 insertions, 16 deletions
diff --git a/src/js/jsregex.nim b/src/js/jsregex.nim index f24633d1..9fb09872 100644 --- a/src/js/jsregex.nim +++ b/src/js/jsregex.nim @@ -10,23 +10,19 @@ export LREFlags type Regex* = object bytecode: seq[uint8] - buf: string + when defined(debug): + buf: string - RegexCapture* = tuple # start, end, index + RegexCapture* = tuple # start, end s, e: int - i: int32 RegexResult* = object success*: bool - captures*: seq[RegexCapture] + captures*: seq[seq[RegexCapture]] - RegexReplace* = object - regex: Regex - rule: string - global: bool - -func `$`*(regex: Regex): string = - regex.buf +when defined(debug): + func `$`*(regex: Regex): string = + regex.buf # this is hardcoded into quickjs, so we must override it here. proc lre_realloc(opaque, p: pointer; size: csize_t): pointer {.exportc.} = @@ -43,10 +39,9 @@ proc compileRegex*(buf: string; flags: LREFlags = {}): Result[Regex, string] = var bcseq = newSeqUninitialized[uint8](plen) copyMem(addr bcseq[0], bytecode, plen) dealloc(bytecode) - let regex = Regex( - buf: buf, - bytecode: bcseq - ) + var regex = Regex(bytecode: bcseq) + when defined(debug): + regex.buf = buf return ok(regex) func countBackslashes(buf: string; i: int): int = @@ -133,13 +128,15 @@ proc exec*(regex: Regex; str: string; start = 0; length = -1; nocaps = false): result.success = true if captureCount == 0 or nocaps: break + var caps: seq[RegexCapture] = @[] let cstrAddress = cast[int](cstr) let ps = start start = capture[1] - cstrAddress for i in 0 ..< captureCount: let s = capture[i * 2] - cstrAddress let e = capture[i * 2 + 1] - cstrAddress - result.captures.add((s, e, i)) + caps.add((s, e)) + result.captures.add(caps) if LRE_FLAG_GLOBAL notin flags: break if start >= str.len: |