Skip to content

Commit f1675fb

Browse files
authored
Fix regex anchor #69 (#80)
* Always wrap regex pattern in ^(..)
1 parent eb3ac59 commit f1675fb

File tree

5 files changed

+8
-9
lines changed

5 files changed

+8
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Breaking changes:
99
New features:
1010

1111
Bugfixes:
12+
- Issue #69: Fix regex parser to always wrap pattern inside `^(..)` (#80 by @chtenb)
1213

1314
Other improvements:
1415
- Added `purs-tidy` formatter (#76 by @thomashoneyman)

src/Text/Parsing/StringParser/CodePoints.purs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import Data.Either (Either(..))
3131
import Data.Enum (fromEnum)
3232
import Data.Foldable (class Foldable, foldMap, elem, notElem)
3333
import Data.Maybe (Maybe(..))
34-
import Data.String.CodePoints (codePointAt, drop, indexOf', length, stripPrefix)
34+
import Data.String.CodePoints (codePointAt, drop, indexOf', length)
3535
import Data.String.CodeUnits (singleton)
3636
import Data.String.Pattern (Pattern(..))
3737
import Data.String.Regex as Regex
@@ -132,10 +132,7 @@ regex pat =
132132
matchRegex r
133133
where
134134
-- ensure the pattern only matches the current position in the parse
135-
pattern =
136-
case stripPrefix (Pattern "^") pat of
137-
Nothing -> "^" <> pat
138-
_ -> pat
135+
pattern = "^(" <> pat <> ")"
139136

140137
matchRegex :: Regex.Regex -> Parser String
141138
matchRegex r = Parser \{ str, pos } -> do

src/Text/Parsing/StringParser/CodeUnits.purs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,7 @@ regex pat =
127127
matchRegex r
128128
where
129129
-- ensure the pattern only matches the current position in the parse
130-
pattern =
131-
case SCU.stripPrefix (Pattern "^") pat of
132-
Nothing -> "^" <> pat
133-
_ -> pat
130+
pattern = "^(" <> pat <> ")"
134131

135132
matchRegex :: Regex.Regex -> Parser String
136133
matchRegex r = Parser \{ str, pos } -> do

test/CodePoints.purs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ testCodePoints = do
9999
assert $ expectResult (NonEmptyList ('0' :| '1' : '2' : '3' : '4' : Nil)) (many1 anyDigit) "01234/"
100100
assert $ expectResult (NonEmptyList ('5' :| '6' : '7' : '8' : '9' : Nil)) (many1 anyDigit) "56789:"
101101
assert $ expectResult "aaaa" (regex "a+") "aaaab"
102+
assert $ expectResult "aaaa" (regex "^a+") "aaaab"
103+
assert $ parseFail (regex "a|b") "xb"
102104
assert $ expectResult ("a" : "a" : "a" : Nil) (manyTill (string "a") (string "b")) "aaab"
103105
assert $ expectResult Nil (manyTill (string "a") (string "b")) "b"
104106
assert $ expectResult (NonEmptyList ("a" :| "a" : "a" : Nil)) (many1Till (string "a") (string "b")) "aaab"

test/CodeUnits.purs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ testCodeUnits = do
9999
assert $ expectResult (NonEmptyList ('0' :| '1' : '2' : '3' : '4' : Nil)) (many1 anyDigit) "01234/"
100100
assert $ expectResult (NonEmptyList ('5' :| '6' : '7' : '8' : '9' : Nil)) (many1 anyDigit) "56789:"
101101
assert $ expectResult "aaaa" (regex "a+") "aaaab"
102+
assert $ expectResult "aaaa" (regex "^a+") "aaaab"
103+
assert $ parseFail (regex "a|b") "xb"
102104
assert $ expectResult ("a" : "a" : "a" : Nil) (manyTill (string "a") (string "b")) "aaab"
103105
assert $ expectResult Nil (manyTill (string "a") (string "b")) "b"
104106
assert $ expectResult (NonEmptyList ("a" :| "a" : "a" : Nil)) (many1Till (string "a") (string "b")) "aaab"

0 commit comments

Comments
 (0)