Skip to content

Commit

Permalink
Drop support for Nim < 1.6
Browse files Browse the repository at this point in the history
  • Loading branch information
nitely committed Mar 7, 2024
1 parent 9085db4 commit 2da4b86
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 126 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ jobs:
strategy:
matrix:
# xxx trim to on use latest in each branch eg 1.0.10, 1.2.8 etc
nim: [1.0.10, 1.2.12, 1.4.8, 1.6.14, 2.0.0]
nim: [1.6.18, 2.0.0]
steps:
- uses: actions/checkout@v2
- name: Run Tests
run: |
docker pull nimlang/nim:${{ matrix.nim }}
docker run --rm -v `pwd`:/usr/src/app -w /usr/src/app nimlang/nim:${{ matrix.nim }} /bin/bash -c "git config --global --add safe.directory /usr/src/app; nimble install -y; nimble test"
- name: Build docs
if: matrix.nim == '1.6.14'
if: matrix.nim == '1.6.18'
run: |
docker pull nimlang/nim:${{ matrix.nim }}
docker run --rm -v `pwd`:/usr/src/app -w /usr/src/app nimlang/nim:${{ matrix.nim }} /bin/bash -c "git config --global --add safe.directory /usr/src/app; nimble install -y; nimble docs"
Expand All @@ -31,7 +31,7 @@ jobs:
# and remove `github.ref == 'refs/heads/master'` below
if: |
github.event_name == 'push' && github.ref == 'refs/heads/master' &&
matrix.nim == '1.6.14'
matrix.nim == '1.6.18'
uses: crazy-max/ghaction-github-pages@v1
with:
build_dir: docs
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
v0.25.0
==================

* Drop support for Nim < 1.6

v0.24.1
==================

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ nimble install regex

# Compatibility

Nim +1.0.0
Nim +1.6.0

## Docs

Expand Down
38 changes: 13 additions & 25 deletions regex.nimble
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Package

version = "0.24.1"
version = "0.24.2"
author = "Esteban Castro Borsani (@nitely)"
description = "Linear time regex matching"
license = "MIT"
srcDir = "src"
skipDirs = @["tests", "bench", "docs"]

requires "nim >= 1.0.0"
requires "nim >= 1.6.0"
requires "unicodedb >= 0.7.2"

task test2, "Test":
Expand All @@ -18,44 +18,32 @@ task test2, "Test":
exec "nim c -r -d:forceRegexAtRuntime tests/tests2.nim"
exec "nim c -r -d:forceRegexAtRuntime -d:noRegexOpt tests/tests2.nim"
exec "nim c -r -d:noRegexOpt tests/tests2.nim"
when (NimMajor, NimMinor, NimPatch) >= (0, 20, 2):
exec "nim c -d:runTestAtCT tests/tests2.nim"
exec "nim c -d:runTestAtCT tests/tests2.nim"
exec "nim c -r tests/tests_misc.nim"
exec "nim c -r -d:forceRegexAtRuntime tests/tests_misc.nim"
exec "nim c -r -d:forceRegexAtRuntime -d:noRegexOpt tests/tests_misc.nim"
exec "nim c -r -d:noRegexOpt tests/tests_misc.nim"
# js target should work in older versions, but
# the docker image for CI has it since Nim 1.0.4,
# so I'll only test it there
when (NimMajor, NimMinor, NimPatch) >= (1, 0, 4) and
(NimMajor, NimMinor) != (1, 4): # issue #88
exec "nim js -r src/regex.nim"
exec "nim js -r tests/tests2.nim"
exec "nim js -r -d:forceRegexAtRuntime tests/tests2.nim"
exec "nim js -r tests/tests_misc.nim"
exec "nim js -r -d:forceRegexAtRuntime tests/tests_misc.nim"
# JS
exec "nim js -r src/regex.nim"
exec "nim js -r tests/tests2.nim"
exec "nim js -r -d:forceRegexAtRuntime tests/tests2.nim"
exec "nim js -r tests/tests_misc.nim"
exec "nim js -r -d:forceRegexAtRuntime tests/tests_misc.nim"
# test release/danger mode
exec "nim c -r -d:release -o:bin/regex src/regex.nim"
exec "nim c -r -d:danger -o:bin/regex src/regex.nim"
# Test runnable examples
when (NimMajor, NimMinor) >= (1, 6):
exec "nim doc -o:./docs/ugh/ugh.html ./src/regex.nim"
exec "nim doc -o:./docs/ugh/ugh.html ./src/regex.nim"

task oldtest, "Test":
# OLD DEPRECATED API TESTS
exec "nim c -r tests/tests.nim"
exec "nim c -r -d:forceRegexAtRuntime tests/tests.nim"
exec "nim c -r -d:forceRegexAtRuntime -d:noRegexOpt tests/tests.nim"
exec "nim c -r -d:noRegexOpt tests/tests.nim"
when (NimMajor, NimMinor, NimPatch) >= (0, 20, 2):
exec "nim c -d:runTestAtCT tests/tests.nim"
# js target should work in older versions, but
# the docker image for CI has it since Nim 1.0.4,
# so I'll only test it there
when (NimMajor, NimMinor, NimPatch) >= (1, 0, 4) and
(NimMajor, NimMinor) != (1, 4): # issue #88
exec "nim js -r tests/tests.nim"
exec "nim js -r -d:forceRegexAtRuntime tests/tests.nim"
exec "nim c -d:runTestAtCT tests/tests.nim"
exec "nim js -r tests/tests.nim"
exec "nim js -r -d:forceRegexAtRuntime tests/tests.nim"

task test, "Test":
exec "nimble test2"
Expand Down
182 changes: 85 additions & 97 deletions src/regex.nim
Original file line number Diff line number Diff line change
Expand Up @@ -357,11 +357,8 @@ import ./regex/nfamatch2
when not defined(noRegexOpt):
import ./regex/litopt

const canUseMacro = (NimMajor, NimMinor) >= (1, 1)

when canUseMacro:
import ./regex/nfamacro
export RegexLit
import ./regex/nfamacro
export RegexLit

export
Regex,
Expand Down Expand Up @@ -393,10 +390,9 @@ template debugCheckUtf8(s: string, pat: Regex2): untyped =
"Invalid utf-8 input"
)

when canUseMacro:
func rex*(s: string): RegexLit =
## Raw regex literal string
RegexLit s
func rex*(s: string): RegexLit =
## Raw regex literal string
RegexLit s

func re2*(s: string, flags: RegexFlags = {}): Regex2 {.raises: [RegexError].} =
## Parse and compile a regular expression at run-time
Expand All @@ -417,10 +413,7 @@ when not defined(forceRegexAtRuntime):
flags: static RegexFlags = {}
): static[Regex2] {.inline.} =
## Parse and compile a regular expression at compile-time
when canUseMacro: # VM dies on Nim < 1.1
toRegex2 reCt(s, flags)
else:
toRegex2 reImpl(s, flags)
toRegex2 reCt(s, flags)

func group*(m: RegexMatch2, i: int): Slice[int] {.inline, raises: [].} =
## return slice for a given group.
Expand Down Expand Up @@ -466,30 +459,29 @@ func groupNames*(m: RegexMatch2): seq[string] {.inline, raises: [].} =

result = toSeq(m.namedGroups.keys)

when canUseMacro:
macro match*(
text: string,
regex: RegexLit,
body: untyped
): untyped =
## return a match if the whole string
## matches the regular expression. This is
## similar to the ``match`` function, but
## faster. Notice it requires a raw regex *literal*
## string as second parameter; the regex must be
## known at compile time, and cannot be a var/let/const
##
## A ``matches: seq[string]`` variable is injected into
## the scope, and it contains the submatches for every capture
## group. If a group is repeated (ex: `(\\w)+`), it will
## contain the last capture for that group.
##
## Note: Only available in Nim +1.1
runnableExamples:
match "abc", rex"(a(b)c)":
doAssert matches == @["abc", "b"]
macro match*(
text: string,
regex: RegexLit,
body: untyped
): untyped =
## return a match if the whole string
## matches the regular expression. This is
## similar to the ``match`` function, but
## faster. Notice it requires a raw regex *literal*
## string as second parameter; the regex must be
## known at compile time, and cannot be a var/let/const
##
## A ``matches: seq[string]`` variable is injected into
## the scope, and it contains the submatches for every capture
## group. If a group is repeated (ex: `(\\w)+`), it will
## contain the last capture for that group.
##
## Note: Only available in Nim +1.1
runnableExamples:
match "abc", rex"(a(b)c)":
doAssert matches == @["abc", "b"]

matchImpl(text, regex, body)
matchImpl(text, regex, body)

func match*(
s: string,
Expand Down Expand Up @@ -901,10 +893,7 @@ when not defined(forceRegexAtRuntime):
func re*(
s: static string
): static[Regex] {.inline, deprecated: "use re2(static string) instead".} =
when canUseMacro: # VM dies on Nim < 1.1
reCt(s)
else:
reImpl(s)
reCt(s)

func toPattern*(
s: string
Expand Down Expand Up @@ -1604,62 +1593,61 @@ when isMainModule:
m.captures == @[0 .. 3, reNonCapture]
doAssert match("aaab", re2"(\w+)|\w+(?<=^(\w)(\w+))b", m) and
m.captures == @[0 .. 3, reNonCapture, reNonCapture]
when canUseMacro:
block:
var m = false
var matches: seq[string]
match "abc", rex"(\w+)":
doAssert matches == @["abc"]
m = true
doAssert m
doAssert matches.len == 0
block:
var m = false
match "abc", rex"(\w)+":
doAssert matches == @["c"]
m = true
doAssert m
block:
var m = false
match "abc", rex"(a(b)c)":
doAssert matches == @["abc", "b"]
m = true
doAssert m
block:
var m = false
match "x", rex"y":
m = true
doAssert not m
match "y", rex"y":
m = true
doAssert m
block:
template myRegex: untyped =
rex"""(?x)
abc # verbose mode
"""
var m = false
match "abc", myRegex:
m = true
doAssert m
block:
var m = false
var txt = "abc"
match txt, rex"(\w)+":
m = true
doAssert m
block:
var matched = false
let text = "[my link](https://example.com)"
match text, rex"\[([a-z ]*)\]\((https?://[^)]+)\)":
doAssert matches == @["my link", "https://example.com"]
matched = true
doAssert matched
block:
var matched = false
match "abcdefg", rex"\w+(?<=(ab)(?=(cd)))\w+":
doAssert matches == @["ab", "cd"]
matched = true
doAssert matched
block:
var m = false
var matches: seq[string]
match "abc", rex"(\w+)":
doAssert matches == @["abc"]
m = true
doAssert m
doAssert matches.len == 0
block:
var m = false
match "abc", rex"(\w)+":
doAssert matches == @["c"]
m = true
doAssert m
block:
var m = false
match "abc", rex"(a(b)c)":
doAssert matches == @["abc", "b"]
m = true
doAssert m
block:
var m = false
match "x", rex"y":
m = true
doAssert not m
match "y", rex"y":
m = true
doAssert m
block:
template myRegex: untyped =
rex"""(?x)
abc # verbose mode
"""
var m = false
match "abc", myRegex:
m = true
doAssert m
block:
var m = false
var txt = "abc"
match txt, rex"(\w)+":
m = true
doAssert m
block:
var matched = false
let text = "[my link](https://example.com)"
match text, rex"\[([a-z ]*)\]\((https?://[^)]+)\)":
doAssert matches == @["my link", "https://example.com"]
matched = true
doAssert matched
block:
var matched = false
match "abcdefg", rex"\w+(?<=(ab)(?=(cd)))\w+":
doAssert matches == @["ab", "cd"]
matched = true
doAssert matched

echo "ok regex.nim"

0 comments on commit 2da4b86

Please sign in to comment.