|
1 | 1 | module Data.URI.Common where
|
2 | 2 |
|
| 3 | +import Prelude |
3 | 4 | import Control.Alt ((<|>))
|
4 |
| -import Control.Monad (replicateM) |
| 5 | +import Data.Array (replicateM) |
| 6 | +import Data.List (List(..), fromList) |
5 | 7 | import Data.Function (Fn5(), runFn5)
|
6 | 8 | import Data.Maybe (Maybe(..))
|
7 |
| -import Data.String (joinWith, length) |
| 9 | +import qualified Data.String as S |
8 | 10 | import Data.URI.Types
|
9 | 11 | import Text.Parsing.StringParser (Parser(..), ParseError(..), unParser)
|
10 | 12 | import Text.Parsing.StringParser.String (string)
|
11 | 13 | import qualified Data.String.Regex as Rx
|
12 | 14 |
|
13 |
| -rep :: Number -> Parser String -> Parser String |
14 |
| -rep n p = joinWith "" <$> replicateM n p |
| 15 | +joinWith :: String -> List String -> String |
| 16 | +joinWith x y = S.joinWith x $ fromList y |
| 17 | + |
| 18 | +rep :: Int -> Parser String -> Parser String |
| 19 | +rep n p = S.joinWith "" <$> replicateM n p |
15 | 20 |
|
16 | 21 | rxPat :: String -> Parser String
|
17 | 22 | rxPat rx = anyMatch $ Rx.regex rx (Rx.noFlags { ignoreCase = true })
|
@@ -39,22 +44,10 @@ parseSubDelims = rxPat "[!$&'()*+;=]"
|
39 | 44 |
|
40 | 45 | anyMatch :: Rx.Regex -> Parser String
|
41 | 46 | anyMatch rx = Parser \{ str: str, pos: i } fc sc -> case match1From rx i str of
|
42 |
| - Just m -> sc m { str: str, pos: i + (length m) } |
| 47 | + Just m -> sc m { str: str, pos: i + (S.length m) } |
43 | 48 | Nothing -> fc i (ParseError $ "Expected " ++ show rx)
|
44 | 49 |
|
45 |
| -match1From :: Rx.Regex -> Number -> String -> Maybe String |
46 |
| -match1From rx i str = runFn5 match1FromImpl Just Nothing rx i str |
47 |
| - |
48 |
| -foreign import match1FromImpl |
49 |
| - """ |
50 |
| - function match1FromImpl(just, nothing, rx, i, str) { |
51 |
| - var rxStr = rx.toString(); |
52 |
| - var flagIndex = rxStr.lastIndexOf("/"); |
53 |
| - var pattern = rxStr.substring(1, flagIndex); |
54 |
| - var flags = rxStr.substring(flagIndex + 1); |
55 |
| - rx = new RegExp(pattern, flags.indexOf("g") === -1 ? flags + "g" : flags); |
56 |
| - rx.lastIndex = i; |
57 |
| - var result = rx.exec(str); |
58 |
| - return result && result.index === i ? just(result[0]) : nothing; |
59 |
| - } |
60 |
| - """ :: forall a. Fn5 (a -> Maybe a) (Maybe a) Rx.Regex Number String (Maybe String) |
| 50 | +match1From :: Rx.Regex -> Int -> String -> Maybe String |
| 51 | +match1From rx i str = runFn5 _match1From Just Nothing rx i str |
| 52 | + |
| 53 | +foreign import _match1From :: forall a. Fn5 (a -> Maybe a) (Maybe a) Rx.Regex Int String (Maybe String) |
0 commit comments