Skip to content

Commit

Permalink
feat: enable negative match patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
haltcase committed Jan 14, 2021
1 parent 923a224 commit 83bdafc
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 19 deletions.
2 changes: 1 addition & 1 deletion glob.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ skipDirs = @["docsrc"]
skipFiles = @["tests.nim"]

requires "nim >= 1.0.0 & < 2.0.0"
requires "regex >= 0.17.1 & < 0.18.0"
requires "regex >= 0.19.0 & < 0.20.0"

task test, "Run the test suite":
exec "nimble c -y --hints:off --verbosity:0 -r tests.nim"
Expand Down
5 changes: 1 addition & 4 deletions src/glob.nim
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,9 @@ bash's ``extglob`` flag:
``*(...patterns)`` match zero or more occurrences of the given patterns
``+(...patterns)`` match one or more occurrences of the given patterns
``@(...patterns)`` match one of the given patterns
``!(...patterns)`` match anything *except* the given patterns
=================== =====================================================
Note that the ``!(...patterns)`` form that allows for matching anything *except*
the given patterns is not currently supported. This is a limitation in the regex
backend.
Examples
********
Expand Down
7 changes: 3 additions & 4 deletions src/glob/regexer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,9 @@ proc globToRegexString* (
if inRange:
add('^')
elif isNext('('):
fail("Negated patterns are currently not supported", pattern, i + 1)
# stack.add(c)
# add("(?!")
# inc i
stack.add(c)
add("(?!")
inc i
else:
add('\\' & c)
of '?':
Expand Down
16 changes: 6 additions & 10 deletions tests.nim
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ proc createStructure (dir: string, files: seq[string]): () -> void =

result = () => removeDir(dir)


proc seqsEqual (seq1, seq2: seq[string], ignoreCase = false): bool =
if ignoreCase:
seqsEqual(seq1.mapIt(toLower(it)), seq2.mapIt(toLower(it)))
Expand Down Expand Up @@ -291,15 +290,12 @@ suite "regex matching":
check isMatchTest("*(foo|ba[rt]).nim", "baz.nim").not
check isMatchTest("*(foo|ba[rt]).nim", "fun.nim").not

test "extended (!) - currently unsupported":
# currently unsupported in regex implementation
expect GlobSyntaxError: discard globToRegexString("!(boo).txt")

# check isMatchTest("!(boo).nim", "foo.nim")
# check isMatchTest("!(foo|baz)bar.nim", "buzbar.nim")
# check isMatchTest("!bar.nim", "!bar.nim")
# check isMatchTest("!({foo,bar})baz.nim", "notbaz.nim")
# check isMatchTest("!({foo,bar})baz.nim", "foobaz.nim").not
test "extended (!)":
check isMatchTest("!bar.nim", "!bar.nim")

This comment has been minimized.

Copy link
@timotheecour

timotheecour Jan 16, 2021

Collaborator

@citycide how about adding some negative tests, eg check not isMatchTest... ?

also, can you explain or add a doc comment for 1st example check isMatchTest("!bar.nim", "!bar.nim")? is ! same as !() ? this is confusing; does it match usual behavior?

This comment has been minimized.

Copy link
@haltcase

haltcase Jan 17, 2021

Author Owner

@timotheecour there is one negative test at the end but we could add more.

!x isn't the same as !(x) - the parentheses are required to form a negative match pattern, otherwise you're just literally matching a !.

check isMatchTest("!(boo).nim", "foo.nim")
check isMatchTest("!(foo|baz)bar.nim", "buzbar.nim")
check isMatchTest("!({foo,bar})baz.nim", "notbaz.nim")
check isMatchTest("!({foo,bar})baz.nim", "foobaz.nim").not

test "extended (+)":
check isMatchTest("+foo.nim", "+foo.nim")
Expand Down

0 comments on commit 83bdafc

Please sign in to comment.