Skip to content

Commit e1e2658

Browse files
committed
avoid copy
1 parent 279dab7 commit e1e2658

File tree

2 files changed

+18
-22
lines changed

2 files changed

+18
-22
lines changed

src/regex.nim

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ func re2*(s: string): Regex2 {.raises: [RegexError].} =
333333
let pat = r"abc\w"
334334
let abcx3 = re2(pat)
335335

336-
reImpl(s).toRegex2
336+
toRegex2 reImpl(s)
337337

338338
# Workaround Nim/issues/14515
339339
# ideally only `re2(string): Regex`
@@ -342,9 +342,9 @@ when not defined(forceRegexAtRuntime):
342342
func re2*(s: static string): static[Regex2] {.inline.} =
343343
## Parse and compile a regular expression at compile-time
344344
when canUseMacro: # VM dies on Nim < 1.1
345-
reCt(s).toRegex2
345+
toRegex2 reCt(s)
346346
else:
347-
reImpl(s).toRegex2
347+
toRegex2 reImpl(s)
348348

349349
func group*(m: RegexMatch2, i: int): Slice[int] {.inline, raises: [].} =
350350
## return slice for a given group.
@@ -430,11 +430,11 @@ func match*(
430430
doAssert "abcd".match(re2"abcd", m)
431431
doAssert not "abcd".match(re2"abc", m)
432432

433-
result = matchImpl(s, pattern.toRegex, m, start)
433+
result = matchImpl(s, toRegex(pattern), m, start)
434434

435435
func match*(s: string, pattern: Regex2): bool {.inline, raises: [].} =
436436
var m: RegexMatch2
437-
result = matchImpl(s, pattern.toRegex, m)
437+
result = matchImpl(s, toRegex(pattern), m)
438438

439439
when defined(noRegexOpt):
440440
template findSomeOptTpl(s, pattern, ms, i): untyped =
@@ -470,11 +470,11 @@ iterator findAll*(
470470
var ms: RegexMatches2
471471
while i <= len(s):
472472
doAssert(i > i2); i2 = i
473-
i = findSomeOptTpl(s, pattern.toRegex, ms, i)
473+
i = findSomeOptTpl(s, toRegex(pattern), ms, i)
474474
#debugEcho i
475475
if i < 0: break
476476
for mi in ms:
477-
fillMatchImpl(m, mi, ms, pattern.toRegex)
477+
fillMatchImpl(m, mi, ms, toRegex(pattern))
478478
yield m
479479
if i == len(s):
480480
break
@@ -507,7 +507,7 @@ iterator findAllBounds*(
507507
var ms: RegexMatches2
508508
while i <= len(s):
509509
doAssert(i > i2); i2 = i
510-
i = findSomeOptTpl(s, pattern.toRegex, ms, i)
510+
i = findSomeOptTpl(s, toRegex(pattern), ms, i)
511511
#debugEcho i
512512
if i < 0: break
513513
for ab in ms.bounds:
@@ -573,7 +573,7 @@ iterator split*(s: string, sep: Regex2): string {.inline, raises: [].} =
573573
ms: RegexMatches2
574574
while not done:
575575
doAssert(i > i2); i2 = i
576-
i = findSomeOptTpl(s, sep.toRegex, ms, i)
576+
i = findSomeOptTpl(s, toRegex(sep), ms, i)
577577
done = i < 0 or i >= len(s)
578578
if done: ms.dummyMatch(s.len)
579579
for ab in ms.bounds:
@@ -608,11 +608,11 @@ func splitIncl*(s: string, sep: Regex2): seq[string] {.inline, raises: [].} =
608608
ms: RegexMatches2
609609
while not done:
610610
doAssert(i > i2); i2 = i
611-
i = findSomeOptTpl(s, sep.toRegex, ms, i)
611+
i = findSomeOptTpl(s, toRegex(sep), ms, i)
612612
done = i < 0 or i >= len(s)
613613
if done: ms.dummyMatch(s.len)
614614
for mi in ms:
615-
fillMatchImpl(m, mi, ms, sep.toRegex)
615+
fillMatchImpl(m, mi, ms, toRegex(sep))
616616
last = ab.a
617617
if ab.a > 0 or ab.a <= ab.b: # skip first empty match
618618
result.add substr(s, first, last-1)
@@ -630,7 +630,7 @@ func startsWith*(
630630
doAssert "abc".startsWith(re2"\w")
631631
doAssert not "abc".startsWith(re2"\d")
632632

633-
startsWithImpl2(s, pattern.toRegex, start)
633+
startsWithImpl2(s, toRegex(pattern), start)
634634

635635
template runeIncAt(s: string, n: var int) =
636636
## increment ``n`` up to
@@ -703,7 +703,7 @@ func replace*(
703703
result = ""
704704
var
705705
i, j = 0
706-
capts = newSeqOfCap[string](pattern.toRegex.groupsCount)
706+
capts = newSeqOfCap[string](toRegex(pattern).groupsCount)
707707
for m in findAll(s, pattern):
708708
result.addsubstr(s, i, m.boundaries.a-1)
709709
capts.setLen 0
@@ -758,7 +758,7 @@ func isInitialized*(re: Regex2): bool {.inline, raises: [].} =
758758
re = re2"foo"
759759
doAssert re.isInitialized
760760

761-
re.toRegex.nfa.s.len > 0
761+
toRegex(re).nfa.s.len > 0
762762

763763
func escapeRe*(s: string): string {.raises: [].} =
764764
## Escape special regex characters in ``s``
@@ -793,7 +793,7 @@ proc toString(
793793
result = "[...]"
794794
return
795795
visited.incl(nIdx)
796-
let n = pattern.toRegex.nfa.s[nIdx]
796+
let n = toRegex(pattern).nfa.s[nIdx]
797797
result = "["
798798
result.add($n)
799799
for nn in n.next:
@@ -1368,7 +1368,7 @@ when isMainModule:
13681368
doAssert(not match("A", re2"((?xi)) a"))
13691369
doAssert(not match("A", re2"(?xi:(?xi) )a"))
13701370

1371-
doAssert graph(re2"^a+$".toRegex) == """digraph graphname {
1371+
doAssert graph(toRegex(re2"^a+$")) == """digraph graphname {
13721372
0 [label="q0";color=blue];
13731373
2 [label="q1";color=black];
13741374
4 [label="q2";color=blue];

src/regex/nfatype.nim

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -268,12 +268,8 @@ else:
268268
Regex2* = distinct Regex
269269
## a compiled regular expression
270270

271-
{.push inline, noSideEffect.}
272-
converter toRegex2*(r: Regex): Regex2 = Regex2(r)
273-
274-
converter toRegex*(r: Regex2): Regex = Regex(r)
275-
{.pop.}
276-
271+
template toRegex2*(r): untyped = Regex2(r)
272+
template toRegex*(r): untyped = Regex(r)
277273

278274
func clear*(m: var RegexMatch) {.inline.} =
279275
if m.captures.len > 0:

0 commit comments

Comments
 (0)