Merge branch 'bigbreak' of https://github.com/Araq/Nimrod into bigbreak
This commit is contained in:
commit
bbb16713c1
3 changed files with 34 additions and 4 deletions
|
|
@ -92,7 +92,7 @@ proc matchOrFind(s: string, pattern: Regex, matches: var openArray[string],
|
||||||
var a = rawMatches[i * 2]
|
var a = rawMatches[i * 2]
|
||||||
var b = rawMatches[i * 2 + 1]
|
var b = rawMatches[i * 2 + 1]
|
||||||
if a >= 0'i32: matches[i-1] = substr(s, int(a), int(b)-1)
|
if a >= 0'i32: matches[i-1] = substr(s, int(a), int(b)-1)
|
||||||
else: matches[i-1] = ""
|
else: matches[i-1] = nil
|
||||||
return rawMatches[1] - rawMatches[0]
|
return rawMatches[1] - rawMatches[0]
|
||||||
|
|
||||||
proc findBounds*(s: string, pattern: Regex, matches: var openArray[string],
|
proc findBounds*(s: string, pattern: Regex, matches: var openArray[string],
|
||||||
|
|
@ -110,7 +110,7 @@ proc findBounds*(s: string, pattern: Regex, matches: var openArray[string],
|
||||||
var a = rawMatches[i * 2]
|
var a = rawMatches[i * 2]
|
||||||
var b = rawMatches[i * 2 + 1]
|
var b = rawMatches[i * 2 + 1]
|
||||||
if a >= 0'i32: matches[i-1] = substr(s, int(a), int(b)-1)
|
if a >= 0'i32: matches[i-1] = substr(s, int(a), int(b)-1)
|
||||||
else: matches[i-1] = ""
|
else: matches[i-1] = nil
|
||||||
return (rawMatches[0].int, rawMatches[1].int - 1)
|
return (rawMatches[0].int, rawMatches[1].int - 1)
|
||||||
|
|
||||||
proc findBounds*(s: string, pattern: Regex,
|
proc findBounds*(s: string, pattern: Regex,
|
||||||
|
|
@ -190,7 +190,7 @@ proc find*(s: string, pattern: Regex, matches: var openArray[string],
|
||||||
var a = rawMatches[i * 2]
|
var a = rawMatches[i * 2]
|
||||||
var b = rawMatches[i * 2 + 1]
|
var b = rawMatches[i * 2 + 1]
|
||||||
if a >= 0'i32: matches[i-1] = substr(s, int(a), int(b)-1)
|
if a >= 0'i32: matches[i-1] = substr(s, int(a), int(b)-1)
|
||||||
else: matches[i-1] = ""
|
else: matches[i-1] = nil
|
||||||
return rawMatches[0]
|
return rawMatches[0]
|
||||||
|
|
||||||
proc find*(s: string, pattern: Regex, start = 0): int =
|
proc find*(s: string, pattern: Regex, start = 0): int =
|
||||||
|
|
@ -310,6 +310,8 @@ proc replacef*(s: string, sub: Regex, by: string): string =
|
||||||
while true:
|
while true:
|
||||||
var match = findBounds(s, sub, caps, prev)
|
var match = findBounds(s, sub, caps, prev)
|
||||||
if match.first < 0: break
|
if match.first < 0: break
|
||||||
|
assert result != nil
|
||||||
|
assert s != nil
|
||||||
add(result, substr(s, prev, match.first-1))
|
add(result, substr(s, prev, match.first-1))
|
||||||
addf(result, by, caps)
|
addf(result, by, caps)
|
||||||
prev = match.last + 1
|
prev = match.last + 1
|
||||||
|
|
@ -451,6 +453,14 @@ when isMainModule:
|
||||||
else:
|
else:
|
||||||
assert false
|
assert false
|
||||||
|
|
||||||
|
if "abc" =~ re"(cba)?.*":
|
||||||
|
assert matches[0] == nil
|
||||||
|
else: assert false
|
||||||
|
|
||||||
|
if "abc" =~ re"().*":
|
||||||
|
assert matches[0] == ""
|
||||||
|
else: assert false
|
||||||
|
|
||||||
assert "var1=key; var2=key2".endsWith(re"\w+=\w+")
|
assert "var1=key; var2=key2".endsWith(re"\w+=\w+")
|
||||||
assert("var1=key; var2=key2".replacef(re"(\w+)=(\w+)", "$1<-$2$2") ==
|
assert("var1=key; var2=key2".replacef(re"(\w+)=(\w+)", "$1<-$2$2") ==
|
||||||
"var1<-keykey; var2<-key2key2")
|
"var1<-keykey; var2<-key2key2")
|
||||||
|
|
|
||||||
|
|
@ -737,7 +737,12 @@ proc rawMatch*(s: string, p: Peg, start: int, c: var Captures): int {.
|
||||||
|
|
||||||
template fillMatches(s, caps, c: expr) =
|
template fillMatches(s, caps, c: expr) =
|
||||||
for k in 0..c.ml-1:
|
for k in 0..c.ml-1:
|
||||||
caps[k] = substr(s, c.matches[k][0], c.matches[k][1])
|
let startIdx = c.matches[k][0]
|
||||||
|
let endIdx = c.matches[k][1]
|
||||||
|
if startIdx != -1:
|
||||||
|
caps[k] = substr(s, startIdx, endIdx)
|
||||||
|
else:
|
||||||
|
caps[k] = nil
|
||||||
|
|
||||||
proc match*(s: string, pattern: Peg, matches: var openArray[string],
|
proc match*(s: string, pattern: Peg, matches: var openArray[string],
|
||||||
start = 0): bool {.nosideEffect, rtl, extern: "npegs$1Capture".} =
|
start = 0): bool {.nosideEffect, rtl, extern: "npegs$1Capture".} =
|
||||||
|
|
@ -1767,3 +1772,14 @@ when isMainModule:
|
||||||
|
|
||||||
assert match("prefix/start", peg"^start$", 7)
|
assert match("prefix/start", peg"^start$", 7)
|
||||||
|
|
||||||
|
if "foo" =~ peg"{'a'}?.*":
|
||||||
|
assert matches[0] == nil
|
||||||
|
else: assert false
|
||||||
|
|
||||||
|
if "foo" =~ peg"{''}.*":
|
||||||
|
assert matches[0] == ""
|
||||||
|
else: assert false
|
||||||
|
|
||||||
|
if "foo" =~ peg"{'foo'}":
|
||||||
|
assert matches[0] == "foo"
|
||||||
|
else: assert false
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,10 @@ News
|
||||||
- String case (or any non-ordinal case) statements
|
- String case (or any non-ordinal case) statements
|
||||||
without 'else' are deprecated.
|
without 'else' are deprecated.
|
||||||
- Recursive tuple types are not allowed anymore. Use ``object`` instead.
|
- Recursive tuple types are not allowed anymore. Use ``object`` instead.
|
||||||
|
- The PEGS module returns ``nil`` instead of ``""`` when an optional capture
|
||||||
|
fails to match
|
||||||
|
- The re module returns ``nil`` instead of ``""`` when an optional capture
|
||||||
|
fails to match
|
||||||
|
|
||||||
Language Additions
|
Language Additions
|
||||||
------------------
|
------------------
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue