File tree Expand file tree Collapse file tree 2 files changed +28
-10
lines changed Expand file tree Collapse file tree 2 files changed +28
-10
lines changed Original file line number Diff line number Diff line change @@ -312,18 +312,10 @@ def _translate_segment_glob(pattern: str) -> str:
312312 j += 1
313313 expr = '['
314314
315- if pattern [i ] == '!' :
316- # Braket expression needs to be negated.
315+ if pattern [i ] == '!' or pattern [ i ] == '^' :
316+ # Bracket expression needs to be negated.
317317 expr += '^'
318318 i += 1
319- elif pattern [i ] == '^' :
320- # POSIX declares that the regex bracket expression negation
321- # "[^...]" is undefined in a glob pattern. Python's
322- # `fnmatch.translate()` escapes the caret ('^') as a
323- # literal. To maintain consistency with undefined behavior,
324- # I am escaping the '^' as well.
325- expr += '\\ ^'
326- i += 1
327319
328320 # Build regex bracket expression. Escape slashes so they are
329321 # treated as literal slashes by regex as defined by POSIX.
Original file line number Diff line number Diff line change @@ -773,3 +773,29 @@ def test_12_asterisk_4_descendant(self):
773773 self .assertEqual (results , {
774774 'anydir/file.txt' ,
775775 })
776+
777+ def test_13_negate_with_caret (self ):
778+ """
779+ Test negation using the caret symbol (^)
780+ """
781+ pattern = GitWildMatchPattern ("a[^gy]c" )
782+ results = set (filter (pattern .match_file , [
783+ "agc" ,
784+ "ayc" ,
785+ "abc" ,
786+ "adc" ,
787+ ]))
788+ self .assertEqual (results , {"abc" , "adc" })
789+
790+ def test_13_negate_with_exclamation_mark (self ):
791+ """
792+ Test negation using the exclamation mark (!)
793+ """
794+ pattern = GitWildMatchPattern ("a[!gy]c" )
795+ results = set (filter (pattern .match_file , [
796+ "agc" ,
797+ "ayc" ,
798+ "abc" ,
799+ "adc" ,
800+ ]))
801+ self .assertEqual (results , {"abc" , "adc" })
You can’t perform that action at this time.
0 commit comments