Skip to content

Commit 1108a60

Browse files
no-longer-on-githu-bpaf31
authored andcommitted
Add unsafeRegex (purescript#74)
* Add unsafeRegex Fixes purescript#73. * Make purescript-partial a non-dev dependency * Move unsafeRegex to Unsafe module
1 parent 94d6526 commit 1108a60

File tree

3 files changed

+35
-26
lines changed

3 files changed

+35
-26
lines changed

bower.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
],
1919
"dependencies": {
2020
"purescript-either": "^2.0.0",
21-
"purescript-maybe": "^2.0.0"
21+
"purescript-maybe": "^2.0.0",
22+
"purescript-partial": "^1.1.2"
2223
},
2324
"devDependencies": {
2425
"purescript-assert": "^2.0.0",
25-
"purescript-console": "^2.0.0",
26-
"purescript-partial": "^1.1.2"
26+
"purescript-console": "^2.0.0"
2727
}
2828
}

src/Data/String/Regex/Unsafe.purs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module Data.String.Regex.Unsafe
2+
( unsafeRegex
3+
) where
4+
5+
import Data.Either (fromRight)
6+
import Data.String.Regex (Regex, regex)
7+
import Data.String.Regex.Flags (RegexFlags)
8+
9+
import Partial.Unsafe (unsafePartial)
10+
11+
-- | Constructs a `Regex` from a pattern string and flags. Fails with
12+
-- | an exception if the pattern contains a syntax error.
13+
unsafeRegex :: String -> RegexFlags -> Regex
14+
unsafeRegex s f = unsafePartial fromRight (regex s f)

test/Test/Data/String/Regex.purs

+18-23
Original file line numberDiff line numberDiff line change
@@ -5,53 +5,48 @@ import Prelude (Unit, ($), (<>), bind, (==), not)
55
import Control.Monad.Eff (Eff)
66
import Control.Monad.Eff.Console (CONSOLE, log)
77

8-
import Data.Either (isLeft, fromRight)
8+
import Data.Either (isLeft)
99
import Data.Maybe (Maybe(..))
1010
import Data.String.Regex
11-
import Data.String.Regex.Flags (RegexFlags, global, ignoreCase, noFlags)
12-
13-
import Partial.Unsafe (unsafePartial)
11+
import Data.String.Regex.Flags (global, ignoreCase, noFlags)
12+
import Data.String.Regex.Unsafe (unsafeRegex)
1413

1514
import Test.Assert (ASSERT, assert)
1615

17-
-- | Unsafe version of `regex`.
18-
regex' :: String -> RegexFlags -> Regex
19-
regex' pattern flags = unsafePartial $ fromRight (regex pattern flags)
20-
2116
testStringRegex :: forall eff. Eff (console :: CONSOLE, assert :: ASSERT | eff) Unit
2217
testStringRegex = do
2318
log "regex"
24-
assert $ test (regex' "^a" noFlags) "abc"
25-
assert $ not (test (regex' "^b" noFlags) "abc")
19+
assert $ test (unsafeRegex "^a" noFlags) "abc"
20+
assert $ not (test (unsafeRegex "^b" noFlags) "abc")
2621
assert $ isLeft (regex "+" noFlags)
2722

2823
log "flags"
29-
assert $ "quxbarfoobaz" == replace (regex' "foo" noFlags) "qux" "foobarfoobaz"
30-
assert $ "quxbarquxbaz" == replace (regex' "foo" global) "qux" "foobarfoobaz"
31-
assert $ "quxbarquxbaz" == replace (regex' "foo" (global <> ignoreCase)) "qux" "foobarFOObaz"
24+
assert $ "quxbarfoobaz" == replace (unsafeRegex "foo" noFlags) "qux" "foobarfoobaz"
25+
assert $ "quxbarquxbaz" == replace (unsafeRegex "foo" global) "qux" "foobarfoobaz"
26+
assert $ "quxbarquxbaz" == replace (unsafeRegex "foo" (global <> ignoreCase)) "qux" "foobarFOObaz"
3227

3328
log "match"
34-
assert $ match (regex' "^abc$" noFlags) "abc" == Just [Just "abc"]
29+
assert $ match (unsafeRegex "^abc$" noFlags) "abc" == Just [Just "abc"]
3530

3631
log "replace"
37-
assert $ replace (regex' "-" noFlags) "!" "a-b-c" == "a!b-c"
32+
assert $ replace (unsafeRegex "-" noFlags) "!" "a-b-c" == "a!b-c"
3833

3934
log "replace'"
40-
assert $ replace' (regex' "-" noFlags) (\s xs -> "!") "a-b-c" == "a!b-c"
35+
assert $ replace' (unsafeRegex "-" noFlags) (\s xs -> "!") "a-b-c" == "a!b-c"
4136

4237
log "search"
43-
assert $ search (regex' "b" noFlags) "abc" == Just 1
44-
assert $ search (regex' "d" noFlags) "abc" == Nothing
38+
assert $ search (unsafeRegex "b" noFlags) "abc" == Just 1
39+
assert $ search (unsafeRegex "d" noFlags) "abc" == Nothing
4540

4641
log "split"
47-
assert $ split (regex' "" noFlags) "" == []
48-
assert $ split (regex' "" noFlags) "abc" == ["a", "b", "c"]
49-
assert $ split (regex' "b" noFlags) "" == [""]
50-
assert $ split (regex' "b" noFlags) "abc" == ["a", "c"]
42+
assert $ split (unsafeRegex "" noFlags) "" == []
43+
assert $ split (unsafeRegex "" noFlags) "abc" == ["a", "b", "c"]
44+
assert $ split (unsafeRegex "b" noFlags) "" == [""]
45+
assert $ split (unsafeRegex "b" noFlags) "abc" == ["a", "c"]
5146

5247
log "test"
5348
-- Ensure that we have referential transparency for calls to 'test'. No
5449
-- global state should be maintained between these two calls:
55-
let pattern = regex' "a" (parseFlags "g")
50+
let pattern = unsafeRegex "a" (parseFlags "g")
5651
assert $ test pattern "a"
5752
assert $ test pattern "a"

0 commit comments

Comments
 (0)