Skip to content

Commit 30f87da

Browse files
authored
Merge pull request #26 from joneshf/remove-ffi
Remove FFI
2 parents fb06076 + f24affa commit 30f87da

File tree

3 files changed

+41
-33
lines changed

3 files changed

+41
-33
lines changed

src/Data/URI/Common.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/Data/URI/Common.purs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ module Data.URI.Common where
33
import Prelude
44

55
import Control.Alt ((<|>))
6-
import Data.Array (fromFoldable)
6+
import Control.MonadZero (guard)
7+
import Data.Array (fromFoldable, head)
78
import Data.Either (Either(..), fromRight)
89
import Data.List (List)
910
import Data.Maybe (Maybe(..))
@@ -78,12 +79,12 @@ anyMatch rx = Parser \{ str: str, pos: i } → case match1From rx i str of
7879
NothingLeft { error: (ParseError $ "Expected " <> show rx), pos: i }
7980

8081
match1From RX.Regex Int String Maybe String
81-
match1From = match1FromImpl Just Nothing
82-
83-
foreign import match1FromImpl
84-
( a. a Maybe a)
85-
( a. Maybe a)
86-
RX.Regex
87-
Int
88-
String
89-
(Maybe String)
82+
match1From rx' n str' =
83+
case RX.regex (RX.source rx') (RXF.global <> RX.flags rx') of
84+
Left _ -> Nothing
85+
Right rx -> do
86+
let str = S.drop n str'
87+
i <- RX.search rx str
88+
guard $ i == 0
89+
matches <- RX.match rx str
90+
join $ head matches

test/Main.purs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ import Data.List (List(..), singleton, (:))
1313
import Data.Maybe (Maybe(Nothing, Just))
1414
import Data.Monoid (mempty)
1515
import Data.Path.Pathy (currentDir, parentDir', file, dir, rootDir, (</>))
16+
import Data.String.Regex (Regex, regex)
17+
import Data.String.Regex.Flags (global, noFlags)
1618
import Data.Tuple (Tuple(..))
1719
import Data.URI (AbsoluteURI(..), Authority(..), Fragment(..), HierarchicalPart(..), Host(..), Port(..), Query(..), RelativePart(..), RelativeRef(..), Scheme(..), URI(..), UserInfo(..))
1820
import Data.URI.AbsoluteURI as AbsoluteURI
1921
import Data.URI.Authority as Authority
22+
import Data.URI.Common as Common
2023
import Data.URI.Host as Host
2124
import Data.URI.Host.Gen as Host.Gen
2225
import Data.URI.Port as Port
@@ -74,6 +77,22 @@ testParseQueryParses uri query =
7477
("parses: \"" <> uri <> "\"")
7578
(equal (Right query) (runParser Query.parser uri))
7679

80+
testMatch1FromMatches :: forall a. Either String Regex -> Int -> String -> Maybe String -> TestSuite a
81+
testMatch1FromMatches rx' n str expected = case rx' of
82+
Left error -> test "faulty regex given" (assert error false)
83+
Right rx ->
84+
test
85+
("matches: " <> show rx <> " at " <> show n <> " in " <> show str)
86+
(equal expected $ Common.match1From rx n str)
87+
88+
testMatch1FromMisses :: forall a. Either String Regex -> Int -> String -> TestSuite a
89+
testMatch1FromMisses rx' n str = case rx' of
90+
Left error -> test "faulty regex given" (assert error false)
91+
Right rx ->
92+
test
93+
("does not match: " <> show rx <> " at " <> show n <> " in " <> show str)
94+
(equal Nothing $ Common.match1From rx n str)
95+
7796
main :: forall eff. Eff (console :: CONSOLE, testOutput :: TESTOUTPUT, avar :: AVAR, exception :: EXCEPTION, random :: RANDOM | eff) Unit
7897
main = runTest $ suite "Data.URI" do
7998

@@ -443,6 +462,17 @@ main = runTest $ suite "Data.URI" do
443462
"key1=&key2="
444463
(Query (Tuple "key1" (Just "") : Tuple "key2" (Just "") : Nil))
445464

465+
suite "Common.match1From" do
466+
testMatch1FromMisses (regex "key1" noFlags) 0 ""
467+
testMatch1FromMisses (regex "key1" noFlags) 1 "key1"
468+
testMatch1FromMisses (regex "key1" noFlags) 1 "key1=&key1="
469+
testMatch1FromMisses (regex "key1" global) 1 "key1=&key1="
470+
testMatch1FromMisses (regex "key1|key2" noFlags) 1 "key1=&key2="
471+
472+
testMatch1FromMatches (regex "key1" noFlags) 0 "key1" (Just "key1")
473+
testMatch1FromMatches (regex "key1" noFlags) 6 "key1=&key1=" (Just "key1")
474+
testMatch1FromMatches (regex "key1|key2" noFlags) 6 "key1=&key2=" (Just "key2")
475+
446476
forAll :: forall eff prop. QC.Testable prop => QCG.Gen prop -> Test (console :: CONSOLE, random :: RANDOM, exception :: EXCEPTION | eff)
447477
forAll = quickCheck
448478

0 commit comments

Comments
 (0)