22-- | A String represents a sequence of characters.
33-- | For details of the underlying implementation, see [String Reference at MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String).
44module Data.String
5- ( charAt
5+ ( Pattern (..)
6+ , Replacement (..)
7+ , charAt
68 , charCodeAt
79 , fromCharArray
810 , toChar
@@ -17,6 +19,7 @@ module Data.String
1719 , singleton
1820 , localeCompare
1921 , replace
22+ , replaceAll
2023 , take
2124 , takeWhile
2225 , drop
@@ -36,17 +39,39 @@ module Data.String
3639import Prelude
3740
3841import Data.Maybe (Maybe (..), isJust )
42+ import Data.Newtype (class Newtype )
3943import Data.String.Unsafe as U
4044
45+ -- | A newtype used in cases where there is a string to be matched.
46+ newtype Pattern = Pattern String
47+
48+ derive instance eqPattern :: Eq Pattern
49+ derive instance ordPattern :: Ord Pattern
50+ derive instance newtypePattern :: Newtype Pattern _
51+
52+ instance showPattern :: Show Pattern where
53+ show (Pattern s) = " (Pattern " <> s <> " )"
54+
55+ -- | A newtype used in cases to specify a replacement for a pattern.
56+ newtype Replacement = Replacement String
57+
58+ derive instance eqReplacement :: Eq Replacement
59+ derive instance ordReplacement :: Ord Replacement
60+ derive instance newtypeReplacement :: Newtype Replacement _
61+
62+ instance showReplacement :: Show Replacement where
63+ show (Replacement s) = " (Replacement " <> s <> " )"
64+
4165-- | Returns the character at the given index, if the index is within bounds.
4266charAt :: Int -> String -> Maybe Char
4367charAt = _charAt Just Nothing
4468
45- foreign import _charAt :: (forall a . a -> Maybe a )
46- -> (forall a . Maybe a )
47- -> Int
48- -> String
49- -> Maybe Char
69+ foreign import _charAt
70+ :: (forall a . a -> Maybe a )
71+ -> (forall a . Maybe a )
72+ -> Int
73+ -> String
74+ -> Maybe Char
5075
5176-- | Returns a string of length `1` containing the given character.
5277foreign import singleton :: Char -> String
@@ -56,19 +81,21 @@ foreign import singleton :: Char -> String
5681charCodeAt :: Int -> String -> Maybe Int
5782charCodeAt = _charCodeAt Just Nothing
5883
59- foreign import _charCodeAt :: (forall a . a -> Maybe a )
60- -> (forall a . Maybe a )
61- -> Int
62- -> String
63- -> Maybe Int
84+ foreign import _charCodeAt
85+ :: (forall a . a -> Maybe a )
86+ -> (forall a . Maybe a )
87+ -> Int
88+ -> String
89+ -> Maybe Int
6490
6591toChar :: String -> Maybe Char
6692toChar = _toChar Just Nothing
6793
68- foreign import _toChar :: (forall a . a -> Maybe a )
69- -> (forall a . Maybe a )
70- -> String
71- -> Maybe Char
94+ foreign import _toChar
95+ :: (forall a . a -> Maybe a )
96+ -> (forall a . Maybe a )
97+ -> String
98+ -> Maybe Char
7299
73100-- | Returns `true` if the given string is empty.
74101null :: String -> Boolean
@@ -81,7 +108,7 @@ uncons "" = Nothing
81108uncons s = Just { head: U .charAt zero s, tail: drop one s }
82109
83110-- | Returns the longest prefix (possibly empty) of characters that satisfy
84- -- | the predicate:
111+ -- | the predicate.
85112takeWhile :: (Char -> Boolean ) -> String -> String
86113takeWhile p s = take (count p s) s
87114
@@ -91,80 +118,82 @@ dropWhile p s = drop (count p s) s
91118
92119-- | If the string starts with the given prefix, return the portion of the
93120-- | string left after removing it, as a Just value. Otherwise, return Nothing.
94- -- | * `stripPrefix "http:" "http://purescript.org" == Just "//purescript.org"`
95- -- | * `stripPrefix "http:" "https://purescript.org" == Nothing`
96- stripPrefix :: String -> String -> Maybe String
97- stripPrefix prefix str =
121+ -- | * `stripPrefix (Pattern "http:") "http://purescript.org" == Just "//purescript.org"`
122+ -- | * `stripPrefix (Pattern "http:") "https://purescript.org" == Nothing`
123+ stripPrefix :: Pattern -> String -> Maybe String
124+ stripPrefix prefix@( Pattern prefixS) str =
98125 case indexOf prefix str of
99- Just 0 -> Just $ drop (length prefix ) str
100- _ -> Nothing
126+ Just 0 -> Just $ drop (length prefixS ) str
127+ _ -> Nothing
101128
102129-- | If the string ends with the given suffix, return the portion of the
103130-- | string left after removing it, as a Just value. Otherwise, return Nothing.
104- -- | * `stripSuffix ".exe" "psc.exe" == Just "psc"`
105- -- | * `stripSuffix ".exe" "psc" == Nothing`
106- stripSuffix :: String -> String -> Maybe String
107- stripSuffix suffix str =
131+ -- | * `stripSuffix (Pattern ".exe") "psc.exe" == Just "psc"`
132+ -- | * `stripSuffix (Pattern ".exe") "psc" == Nothing`
133+ stripSuffix :: Pattern -> String -> Maybe String
134+ stripSuffix suffix@( Pattern suffixS) str =
108135 case lastIndexOf suffix str of
109- Just x | x == length str - length suffix ->
110- Just $ take x str
111- _ ->
112- Nothing
136+ Just x | x == length str - length suffixS -> Just $ take x str
137+ _ -> Nothing
113138
114139-- | Converts an array of characters into a string.
115140foreign import fromCharArray :: Array Char -> String
116141
117142-- | Checks whether the first string exists in the second string.
118- contains :: String -> String -> Boolean
119- contains x s = isJust ( indexOf x s)
143+ contains :: Pattern -> String -> Boolean
144+ contains pat = isJust <<< indexOf pat
120145
121146-- | Returns the index of the first occurrence of the first string in the
122147-- | second string. Returns `Nothing` if there is no match.
123- indexOf :: String -> String -> Maybe Int
148+ indexOf :: Pattern -> String -> Maybe Int
124149indexOf = _indexOf Just Nothing
125150
126- foreign import _indexOf :: (forall a . a -> Maybe a )
127- -> (forall a . Maybe a )
128- -> String
129- -> String
130- -> Maybe Int
151+ foreign import _indexOf
152+ :: (forall a . a -> Maybe a )
153+ -> (forall a . Maybe a )
154+ -> Pattern
155+ -> String
156+ -> Maybe Int
131157
132158-- | Returns the index of the first occurrence of the first string in the
133159-- | second string, starting at the given index. Returns `Nothing` if there is
134160-- | no match.
135- indexOf' :: String -> Int -> String -> Maybe Int
161+ indexOf' :: Pattern -> Int -> String -> Maybe Int
136162indexOf' = _indexOf' Just Nothing
137163
138- foreign import _indexOf' :: (forall a . a -> Maybe a )
139- -> (forall a . Maybe a )
140- -> String
141- -> Int
142- -> String
143- -> Maybe Int
164+ foreign import _indexOf'
165+ :: (forall a . a -> Maybe a )
166+ -> (forall a . Maybe a )
167+ -> Pattern
168+ -> Int
169+ -> String
170+ -> Maybe Int
144171
145172-- | Returns the index of the last occurrence of the first string in the
146173-- | second string. Returns `Nothing` if there is no match.
147- lastIndexOf :: String -> String -> Maybe Int
174+ lastIndexOf :: Pattern -> String -> Maybe Int
148175lastIndexOf = _lastIndexOf Just Nothing
149176
150- foreign import _lastIndexOf :: (forall a . a -> Maybe a )
151- -> (forall a . Maybe a )
152- -> String
153- -> String
154- -> Maybe Int
177+ foreign import _lastIndexOf
178+ :: (forall a . a -> Maybe a )
179+ -> (forall a . Maybe a )
180+ -> Pattern
181+ -> String
182+ -> Maybe Int
155183
156184-- | Returns the index of the last occurrence of the first string in the
157185-- | second string, starting at the given index. Returns `Nothing` if there is
158186-- | no match.
159- lastIndexOf' :: String -> Int -> String -> Maybe Int
187+ lastIndexOf' :: Pattern -> Int -> String -> Maybe Int
160188lastIndexOf' = _lastIndexOf' Just Nothing
161189
162- foreign import _lastIndexOf' :: (forall a . a -> Maybe a )
163- -> (forall a . Maybe a )
164- -> String
165- -> Int
166- -> String
167- -> Maybe Int
190+ foreign import _lastIndexOf'
191+ :: (forall a . a -> Maybe a )
192+ -> (forall a . Maybe a )
193+ -> Pattern
194+ -> Int
195+ -> String
196+ -> Maybe Int
168197
169198-- | Returns the number of characters the string is composed of.
170199foreign import length :: String -> Int
@@ -173,15 +202,19 @@ foreign import length :: String -> Int
173202localeCompare :: String -> String -> Ordering
174203localeCompare = _localeCompare LT EQ GT
175204
176- foreign import _localeCompare :: Ordering
177- -> Ordering
178- -> Ordering
179- -> String
180- -> String
181- -> Ordering
205+ foreign import _localeCompare
206+ :: Ordering
207+ -> Ordering
208+ -> Ordering
209+ -> String
210+ -> String
211+ -> Ordering
182212
183213-- | Replaces the first occurence of the first argument with the second argument.
184- foreign import replace :: String -> String -> String -> String
214+ foreign import replace :: Pattern -> Replacement -> String -> String
215+
216+ -- | Replaces all occurences of the first argument with the second argument.
217+ foreign import replaceAll :: Pattern -> Replacement -> String -> String
185218
186219-- | Returns the first `n` characters of the string.
187220foreign import take :: Int -> String -> String
@@ -196,7 +229,7 @@ foreign import count :: (Char -> Boolean) -> String -> Int
196229-- | Returns the substrings of the second string separated along occurences
197230-- | of the first string.
198231-- | * `split " " "hello world" == ["hello", "world"]`
199- foreign import split :: String -> String -> Array String
232+ foreign import split :: Pattern -> String -> Array String
200233
201234-- | Returns the substrings of split at the given index, if the index is within bounds.
202235splitAt :: Int -> String -> Maybe (Array String )
0 commit comments