Skip to content

minor improvements #144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 23, 2024
Merged
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
54 changes: 27 additions & 27 deletions src/regex.nim
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ when not defined(forceRegexAtRuntime):
func re2*(
s: static string,
flags: static RegexFlags = {}
): static[Regex2] {.inline.} =
): static[Regex2] =
## Parse and compile a regular expression at compile-time
toRegex2 reCt(s, flags)

Expand Down Expand Up @@ -575,7 +575,7 @@ func match*(
pattern: Regex2,
m: var RegexMatch2,
start = 0
): bool {.inline, raises: [].} =
): bool {.raises: [].} =
## return a match if the whole string
## matches the regular expression. This
## is similar to ``find(text, re"^regex$", m)``
Expand All @@ -588,7 +588,7 @@ func match*(
debugCheckUtf8(s, pattern)
result = matchImpl(s, pattern.toRegex, m, start)

func match*(s: string, pattern: Regex2): bool {.inline, raises: [].} =
func match*(s: string, pattern: Regex2): bool {.raises: [].} =
debugCheckUtf8(s, pattern)
var m: RegexMatch2
result = matchImpl(s, pattern.toRegex, m)
Expand Down Expand Up @@ -648,7 +648,7 @@ func findAll*(
s: string,
pattern: Regex2,
start = 0
): seq[RegexMatch2] {.inline, raises: [].} =
): seq[RegexMatch2] {.raises: [].} =
for m in findAll(s, pattern, start):
result.add m

Expand Down Expand Up @@ -686,7 +686,7 @@ func findAllBounds*(
s: string,
pattern: Regex2,
start = 0
): seq[Slice[int]] {.inline, raises: [].} =
): seq[Slice[int]] {.raises: [].} =
for m in findAllBounds(s, pattern, start):
result.add m

Expand All @@ -695,7 +695,7 @@ func find*(
pattern: Regex2,
m: var RegexMatch2,
start = 0
): bool {.inline, raises: [].} =
): bool {.raises: [].} =
## search through the string looking for the first
## location where there is a match
runnableExamples:
Expand All @@ -715,7 +715,7 @@ func find*(
return false

# XXX find shortest match; disable captures
func contains*(s: string, pattern: Regex2): bool {.inline, raises: [].} =
func contains*(s: string, pattern: Regex2): bool {.raises: [].} =
runnableExamples:
doAssert re2"bc" in "abcd"
doAssert re2"(23)+" in "23232"
Expand Down Expand Up @@ -751,7 +751,7 @@ iterator split*(s: string, sep: Regex2): string {.inline, raises: [].} =
yield substr(s, first, last-1)
first = ab.b+1

func split*(s: string, sep: Regex2): seq[string] {.inline, raises: [].} =
func split*(s: string, sep: Regex2): seq[string] {.raises: [].} =
## return not matched substrings
runnableExamples:
doAssert split("11a22Ϊ33Ⓐ44弢55", re2"\d+") ==
Expand All @@ -760,7 +760,7 @@ func split*(s: string, sep: Regex2): seq[string] {.inline, raises: [].} =
for w in split(s, sep):
result.add w

func splitIncl*(s: string, sep: Regex2): seq[string] {.inline, raises: [].} =
func splitIncl*(s: string, sep: Regex2): seq[string] {.raises: [].} =
## return not matched substrings, including captured groups
runnableExamples:
let
Expand Down Expand Up @@ -795,7 +795,7 @@ func startsWith*(
s: string,
pattern: Regex2,
start = 0
): bool {.inline, raises: [].} =
): bool {.raises: [].} =
## return whether the string
## starts with the pattern or not
runnableExamples:
Expand All @@ -805,7 +805,7 @@ func startsWith*(
debugCheckUtf8(s, pattern)
startsWithImpl2(s, pattern.toRegex, start)

func endsWith*(s: string, pattern: Regex2): bool {.inline, raises: [].} =
func endsWith*(s: string, pattern: Regex2): bool {.raises: [].} =
## return whether the string
## ends with the pattern or not
runnableExamples:
Expand Down Expand Up @@ -842,7 +842,7 @@ func replace*(
pattern: Regex2,
by: string,
limit = 0
): string {.inline, raises: [ValueError].} =
): string {.raises: [ValueError].} =
## Replace matched substrings.
##
## Matched groups can be accessed with ``$N``
Expand Down Expand Up @@ -887,7 +887,7 @@ func replace*(
pattern: Regex2,
by: proc (m: RegexMatch2, s: string): string,
limit = 0
): string {.inline, raises: [], effectsOf: by.} =
): string {.raises: [], effectsOf: by.} =
## Replace matched substrings.
##
## If ``limit`` is given, at most ``limit``
Expand Down Expand Up @@ -988,7 +988,7 @@ func re*(
when not defined(forceRegexAtRuntime):
func re*(
s: static string
): static[Regex] {.inline, deprecated: "use re2(static string) instead".} =
): static[Regex] {.deprecated: "use re2(static string) instead".} =
reCt(s)

func toPattern*(
Expand Down Expand Up @@ -1077,11 +1077,11 @@ func match*(
pattern: Regex,
m: var RegexMatch,
start = 0
): bool {.inline, raises: [], deprecated: "use match(string, Regex2, var RegexMatch2) instead".} =
): bool {.raises: [], deprecated: "use match(string, Regex2, var RegexMatch2) instead".} =
debugCheckUtf8 s
result = matchImpl(s, pattern, m, start)

func match*(s: string, pattern: Regex): bool {.inline, raises: [], deprecated: "use match(string, Regex2) instead".} =
func match*(s: string, pattern: Regex): bool {.raises: [], deprecated: "use match(string, Regex2) instead".} =
debugCheckUtf8 s
var m: RegexMatch
result = matchImpl(s, pattern, m)
Expand Down Expand Up @@ -1111,7 +1111,7 @@ func findAll*(
s: string,
pattern: Regex,
start = 0
): seq[RegexMatch] {.inline, raises: [], deprecated: "use findAll(string, Regex2) instead".} =
): seq[RegexMatch] {.raises: [], deprecated: "use findAll(string, Regex2) instead".} =
for m in findAll(s, pattern, start):
result.add m

Expand All @@ -1138,17 +1138,17 @@ func findAllBounds*(
s: string,
pattern: Regex,
start = 0
): seq[Slice[int]] {.inline, raises: [], deprecated: "use findAllBounds(string, Regex2) instead".} =
): seq[Slice[int]] {.raises: [], deprecated: "use findAllBounds(string, Regex2) instead".} =
for m in findAllBounds(s, pattern, start):
result.add m

func findAndCaptureAll*(
s: string, pattern: Regex
): seq[string] {.inline, raises: [], deprecated: "use findAll(string, Regex2) instead".} =
): seq[string] {.raises: [], deprecated: "use findAll(string, Regex2) instead".} =
for m in s.findAll(pattern):
result.add s[m.boundaries]

func contains*(s: string, pattern: Regex): bool {.inline, raises: [], deprecated: "use contains(string, Regex2) instead".} =
func contains*(s: string, pattern: Regex): bool {.raises: [], deprecated: "use contains(string, Regex2) instead".} =
for _ in findAllBounds(s, pattern):
return true
return false
Expand All @@ -1158,7 +1158,7 @@ func find*(
pattern: Regex,
m: var RegexMatch,
start = 0
): bool {.inline, raises: [], deprecated: "use find(string, Regex2, var RegexMatch2) instead".} =
): bool {.raises: [], deprecated: "use find(string, Regex2, var RegexMatch2) instead".} =
m.clear()
for m2 in findAll(s, pattern, start):
m.captures.add m2.captures
Expand All @@ -1185,11 +1185,11 @@ iterator split*(s: string, sep: Regex): string {.inline, raises: [], deprecated:
yield substr(s, first, last-1)
first = ab.b+1

func split*(s: string, sep: Regex): seq[string] {.inline, raises: [], deprecated: "use split(string, Regex2) instead".} =
func split*(s: string, sep: Regex): seq[string] {.raises: [], deprecated: "use split(string, Regex2) instead".} =
for w in split(s, sep):
result.add w

func splitIncl*(s: string, sep: Regex): seq[string] {.inline, raises: [], deprecated: "use splitIncl(string, Regex2) instead".} =
func splitIncl*(s: string, sep: Regex): seq[string] {.raises: [], deprecated: "use splitIncl(string, Regex2) instead".} =
template ab: untyped = m.boundaries
debugCheckUtf8 s
var
Expand All @@ -1215,7 +1215,7 @@ func splitIncl*(s: string, sep: Regex): seq[string] {.inline, raises: [], deprec

func startsWith*(
s: string, pattern: Regex, start = 0
): bool {.inline, raises: [], deprecated: "use startsWith(string, Regex2) instead".} =
): bool {.raises: [], deprecated: "use startsWith(string, Regex2) instead".} =
debugCheckUtf8 s
startsWithImpl(s, pattern, start)

Expand All @@ -1227,7 +1227,7 @@ template runeIncAt(s: string, n: var int) =
else:
n = s.len+1

func endsWith*(s: string, pattern: Regex): bool {.inline, raises: [], deprecated: "use endsWith(string, Regex2) instead".} =
func endsWith*(s: string, pattern: Regex): bool {.raises: [], deprecated: "use endsWith(string, Regex2) instead".} =
debugCheckUtf8 s
result = false
var
Expand Down Expand Up @@ -1263,7 +1263,7 @@ func replace*(
pattern: Regex,
by: string,
limit = 0
): string {.inline, raises: [ValueError], deprecated: "use replace(string, Regex2, string) instead".} =
): string {.raises: [ValueError], deprecated: "use replace(string, Regex2, string) instead".} =
debugCheckUtf8 s
result = ""
var
Expand All @@ -1289,7 +1289,7 @@ func replace*(
pattern: Regex,
by: proc (m: RegexMatch, s: string): string,
limit = 0
): string {.inline, raises: [], effectsOf: by, deprecated: "use replace(string, Regex2, proc(RegexMatch2, string): string) instead".} =
): string {.raises: [], effectsOf: by, deprecated: "use replace(string, Regex2, proc(RegexMatch2, string): string) instead".} =
debugCheckUtf8 s
result = ""
var i, j = 0
Expand Down
2 changes: 1 addition & 1 deletion src/regex/compiler.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import ./litopt
when defined(regexDotDir):
import ./dotgraph

func reImpl*(s: string, flags: RegexFlags = {}): Regex {.inline.} =
func reImpl*(s: string, flags: RegexFlags = {}): Regex =
if regexArbitraryBytes notin flags and verifyUtf8(s) != -1:
raise newException(RegexError, "Invalid utf-8 regex")
var groups: GroupsCapture
Expand Down
7 changes: 4 additions & 3 deletions src/regex/exptransformation.nim
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ func swapCase(r: Rune): Rune =
else:
result = r

func check(cond: bool, msg: string) =
if not cond:
raise newException(RegexError, msg)
template check(cond, msg: untyped): untyped =
{.line: instantiationInfo(fullPaths = true).}:
if not cond:
raise newException(RegexError, msg)

func fixEmptyOps(exp: Exp): Exp =
## Handle "|", "(|)", "a|", "|b", "||", "a||b", ...
Expand Down
7 changes: 4 additions & 3 deletions src/regex/nfa.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import std/algorithm
import ./types
import ./common

func check(cond: bool, msg: string) =
if not cond:
raise newException(RegexError, msg)
template check(cond, msg: untyped): untyped =
{.line: instantiationInfo(fullPaths = true).}:
if not cond:
raise newException(RegexError, msg)

type
End = seq[int16]
Expand Down
4 changes: 2 additions & 2 deletions src/regex/nfamatch2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type
look: var Lookaround,
start: int,
flags: MatchFlags
): bool {.noSideEffect, raises: [].}
): bool {.nimcall, noSideEffect, raises: [].}
BehindSig = proc (
smA, smB: var Submatches,
capts: var Capts3,
Expand All @@ -28,7 +28,7 @@ type
look: var Lookaround,
start, limit: int,
flags: MatchFlags
): int {.noSideEffect, raises: [].}
): int {.nimcall, noSideEffect, raises: [].}
Lookaround* = object
ahead*: AheadSig
behind*: BehindSig
Expand Down
35 changes: 19 additions & 16 deletions src/regex/nfatype.nim
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,9 @@ func `[]`*(capts: var Capts3, i, j: Natural): var Slice[int] {.inline.} =
doAssert j <= capts.blockSize-1
result = capts.s[(i shl capts.blockSizeL2) + j] # i * blockSize

func `[]=`(capts: var Capts3, i, j: Natural, x: Slice[int]) {.inline.} =
doAssert i <= capts.len-1
doAssert j <= capts.blockSize-1
capts.s[(i shl capts.blockSizeL2) + j] = x
func blockIdx(capts: Capts3, blockNum: Natural): int {.inline.} =
assert blockNum <= capts.len-1
blockNum shl capts.blockSizeL2

when defined(js):
func jsLog2(x: Natural): int {.importjs: "Math.log2(@)".}
Expand Down Expand Up @@ -133,18 +132,20 @@ func unfreeze*(capts: var Capts3, freezeId: CaptState) =
func diverge*(capts: var Capts3, captIdx: CaptIdx): CaptIdx =
if capts.free.len > 0:
result = capts.free.pop
for i in 0 .. capts.blockSize-1:
capts[result, i] = nonCapture
capts.states[result].to stsInitial
else:
result = capts.len.CaptIdx
for _ in 0 .. capts.blockSize-1:
capts.s.add nonCapture
capts.s.setLen(capts.s.len+capts.blockSize)
capts.states.add stsInitial
doAssert result == capts.states.len-1
let idx = capts.blockIdx(result)
if captIdx != -1:
let cidx = capts.blockIdx(captIdx)
for i in 0 .. capts.blockSize-1:
capts[result, i] = capts[captIdx, i]
capts.s[idx+i] = capts.s[cidx+i]
else:
for i in 0 .. capts.blockSize-1:
capts.s[idx+i] = nonCapture

func recycle*(capts: var Capts3) =
## Free recyclable entries
Expand Down Expand Up @@ -263,13 +264,11 @@ when defined(js) and (NimMajor, NimMinor) >= (1, 6) and (NimMajor, NimMinor) <=
#flags*: set[RegexFlag]
litOpt*: LitOpt

{.push inline, noSideEffect.}
converter toRegex2*(r: Regex): Regex2 =
func toRegex2*(r: Regex): Regex2 =
Regex2(nfa: r.nfa, groupsCount: r.groupsCount, namedGroups: r.namedGroups, litOpt: r.litOpt)

converter toRegex*(r: Regex2): Regex =
func toRegex*(r: Regex2): Regex =
Regex(nfa: r.nfa, groupsCount: r.groupsCount, namedGroups: r.namedGroups, litOpt: r.litOpt)
{.pop.}
else:
type
Regex2* = distinct Regex
Expand Down Expand Up @@ -374,13 +373,13 @@ func setLen*(item: var SmLookaroundItem, size: int) {.inline.} =
item.a.setLen size
item.b.setLen size

template last*(sm: var SmLookaround): untyped =
template last*(sm: SmLookaround): untyped =
sm.s[sm.i-1]

template lastA*(sm: var SmLookaround): untyped =
template lastA*(sm: SmLookaround): untyped =
last(sm).a

template lastB*(sm: var SmLookaround): untyped =
template lastB*(sm: SmLookaround): untyped =
last(sm).b

func grow*(sm: var SmLookaround) {.inline.} =
Expand All @@ -394,6 +393,10 @@ func removeLast*(sm: var SmLookaround) {.inline.} =
sm.i -= 1

when isMainModule:
func `[]=`(capts: var Capts3, i, j: Natural, x: Slice[int]) =
doAssert i <= capts.len-1
doAssert j <= capts.blockSize-1
capts.s[(i shl capts.blockSizeL2) + j] = x
block:
var capts = initCapts3(2)
doAssert capts.len == 0
Expand Down
Loading
Loading