Skip to content

Add unsafeRegex #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
],
"dependencies": {
"purescript-either": "^2.0.0",
"purescript-maybe": "^2.0.0"
"purescript-maybe": "^2.0.0",
"purescript-partial": "^1.1.2"
},
"devDependencies": {
"purescript-assert": "^2.0.0",
"purescript-console": "^2.0.0",
"purescript-partial": "^1.1.2"
"purescript-console": "^2.0.0"
}
}
14 changes: 14 additions & 0 deletions src/Data/String/Regex/Unsafe.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Data.String.Regex.Unsafe
( unsafeRegex
) where

import Data.Either (fromRight)
import Data.String.Regex (Regex, regex)
import Data.String.Regex.Flags (RegexFlags)

import Partial.Unsafe (unsafePartial)

-- | Constructs a `Regex` from a pattern string and flags. Fails with
-- | an exception if the pattern contains a syntax error.
unsafeRegex :: String -> RegexFlags -> Regex
unsafeRegex s f = unsafePartial fromRight (regex s f)
41 changes: 18 additions & 23 deletions test/Test/Data/String/Regex.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,48 @@ import Prelude (Unit, ($), (<>), bind, (==), not)
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log)

import Data.Either (isLeft, fromRight)
import Data.Either (isLeft)
import Data.Maybe (Maybe(..))
import Data.String.Regex
import Data.String.Regex.Flags (RegexFlags, global, ignoreCase, noFlags)

import Partial.Unsafe (unsafePartial)
import Data.String.Regex.Flags (global, ignoreCase, noFlags)
import Data.String.Regex.Unsafe (unsafeRegex)

import Test.Assert (ASSERT, assert)

-- | Unsafe version of `regex`.
regex' :: String -> RegexFlags -> Regex
regex' pattern flags = unsafePartial $ fromRight (regex pattern flags)

testStringRegex :: forall eff. Eff (console :: CONSOLE, assert :: ASSERT | eff) Unit
testStringRegex = do
log "regex"
assert $ test (regex' "^a" noFlags) "abc"
assert $ not (test (regex' "^b" noFlags) "abc")
assert $ test (unsafeRegex "^a" noFlags) "abc"
assert $ not (test (unsafeRegex "^b" noFlags) "abc")
assert $ isLeft (regex "+" noFlags)

log "flags"
assert $ "quxbarfoobaz" == replace (regex' "foo" noFlags) "qux" "foobarfoobaz"
assert $ "quxbarquxbaz" == replace (regex' "foo" global) "qux" "foobarfoobaz"
assert $ "quxbarquxbaz" == replace (regex' "foo" (global <> ignoreCase)) "qux" "foobarFOObaz"
assert $ "quxbarfoobaz" == replace (unsafeRegex "foo" noFlags) "qux" "foobarfoobaz"
assert $ "quxbarquxbaz" == replace (unsafeRegex "foo" global) "qux" "foobarfoobaz"
assert $ "quxbarquxbaz" == replace (unsafeRegex "foo" (global <> ignoreCase)) "qux" "foobarFOObaz"

log "match"
assert $ match (regex' "^abc$" noFlags) "abc" == Just [Just "abc"]
assert $ match (unsafeRegex "^abc$" noFlags) "abc" == Just [Just "abc"]

log "replace"
assert $ replace (regex' "-" noFlags) "!" "a-b-c" == "a!b-c"
assert $ replace (unsafeRegex "-" noFlags) "!" "a-b-c" == "a!b-c"

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

log "search"
assert $ search (regex' "b" noFlags) "abc" == Just 1
assert $ search (regex' "d" noFlags) "abc" == Nothing
assert $ search (unsafeRegex "b" noFlags) "abc" == Just 1
assert $ search (unsafeRegex "d" noFlags) "abc" == Nothing

log "split"
assert $ split (regex' "" noFlags) "" == []
assert $ split (regex' "" noFlags) "abc" == ["a", "b", "c"]
assert $ split (regex' "b" noFlags) "" == [""]
assert $ split (regex' "b" noFlags) "abc" == ["a", "c"]
assert $ split (unsafeRegex "" noFlags) "" == []
assert $ split (unsafeRegex "" noFlags) "abc" == ["a", "b", "c"]
assert $ split (unsafeRegex "b" noFlags) "" == [""]
assert $ split (unsafeRegex "b" noFlags) "abc" == ["a", "c"]

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