on ccce6cc help sign in
elk/libraries/String.elm
339 lines 8.7 kB history raw
1module String where
2{-| A built-in representation for efficient string manipulation. String literals
3are enclosed in `"double quotes"`. Strings are *not* lists of characters.
5# Basics
6@docs isEmpty, length, reverse, repeat
8# Building and Splitting
9@docs cons, uncons, append, concat, split, join, words, lines
11# Get Substrings
12@docs slice, left, right, dropLeft, dropRight
14# Check for Substrings
15@docs contains, startsWith, endsWith, indexes, indices
17# Conversions
18@docs show, toInt, toFloat, toList, fromList
20# Formatting
21Cosmetic operations such as padding with extra characters or trimming whitespace.
23@docs toUpper, toLower,
24 pad, padLeft, padRight,
25 trim, trimLeft, trimRight
27# Higher-Order Functions
28@docs map, filter, foldl, foldr, any, all
29-}
31import Native.Show
32import Native.String
33import Maybe (Maybe)
35{-| Check if a string is empty.
37 isEmpty "" == True
38 isEmpty "the world" == False
39-}
40isEmpty : String -> Bool
41isEmpty = Native.String.isEmpty
43{-| Add a character to the beginning of a string -}
44cons : Char -> String -> String
45cons = Native.String.cons
47{-| Split a non-empty string into its head and tail. This lets you
48pattern match on strings exactly as you would with lists.
50 uncons "abc" == Just ('a',"bc")
51 uncons "" == Nothing
52-}
53uncons : String -> Maybe (Char, String)
54uncons = Native.String.uncons
56{-| Append two strings. You can also use [the `(++)` operator](/library/List.elm#++)
57to do this.
59 append "butter" "fly" == "butterfly"
60-}
61append : String -> String -> String
62append = Native.String.append
64{-| Concatenate many strings into one.
66 concat ["never","the","less"] == "nevertheless"
67-}
68concat : [String] -> String
69concat = Native.String.concat
71{-| Get the length of a string.
73 length "innumerable" == 11
75-}
76length : String -> Int
77length = Native.String.length
79{-| Transform every character in a string
81 map (\c -> if c == '/' then '.' else c) "a/b/c" == "a.b.c"
82-}
83map : (Char -> Char) -> String -> String
84map = Native.String.map
86{-| Keep only the characters that satisfy the predicate:
88 filter isDigit "R2-D2" == "22"
89-}
90filter : (Char -> Bool) -> String -> String
91filter = Native.String.filter
93{-| Reverse a string.
95 reverse "stressed" == "desserts"
96-}
97reverse : String -> String
98reverse = Native.String.reverse
100{-| Reduce a string from the left:
102 foldl cons "" "time" == "emit"
103-}
104foldl : (Char -> b -> b) -> b -> String -> b
105foldl = Native.String.foldl
107{-| Reduce a string from the right:
109 foldr cons "" "time" == "time"
110-}
111foldr : (Char -> b -> b) -> b -> String -> b
112foldr = Native.String.foldr
114{-| Split a string using a given separator.
116 split "," "cat,dog,cow" == ["cat","dog","cow"]
117 split "/" "home/evan/Desktop/" == ["home","evan","Desktop", ""]
119Use `Regex.split` if you need something more flexible.
120-}
121split : String -> String -> [String]
122split = Native.String.split
124{-| Put many strings together with a given separator.
126 join "a" ["H","w","ii","n"] == "Hawaiian"
127 join " " ["cat","dog","cow"] == "cat dog cow"
128 join "/" ["home","evan","Desktop"] == "home/evan/Desktop"
129-}
130join : String -> [String] -> String
131join = Native.String.join
133{-| Repeat a string N times.
135 repeat 3 "ha" == "hahaha"
136-}
137repeat : Int -> String -> String
138repeat = Native.String.repeat
140{-| Take a substring given a start and end index. Negative indexes
141are taken starting from the *end* of the list.
143 slice 7 9 "snakes on a plane!" == "on"
144 slice 0 6 "snakes on a plane!" == "snakes"
145 slice 0 -7 "snakes on a plane!" == "snakes on a"
146 slice -6 -1 "snakes on a plane!" == "plane"
147-}
148slice : Int -> Int -> String -> String
149slice = Native.String.slice
151{-| Take N characters from the left side of a string. -}
152left : Int -> String -> String
153left = Native.String.left
155{-| Take N characters from the right side of a string. -}
156right : Int -> String -> String
157right = Native.String.right
159{-| Drop N characters from the left side of a string. -}
160dropLeft : Int -> String -> String
161dropLeft = Native.String.dropLeft
163{-| Drop N characters from the right side of a string. -}
164dropRight : Int -> String -> String
165dropRight = Native.String.dropRight
167{-| Pad a string on both sides until it has a given length.
169 pad 5 ' ' "1" == " 1 "
170 pad 5 ' ' "11" == " 11 "
171 pad 5 ' ' "121" == " 121 "
172-}
173pad : Int -> Char -> String -> String
174pad = Native.String.pad
176{-| Pad a string on the left until it has a given length.
178 padLeft 5 '.' "1" == "....1"
179 padLeft 5 '.' "11" == "...11"
180 padLeft 5 '.' "121" == "..121"
181-}
182padLeft : Int -> Char -> String -> String
183padLeft = Native.String.padLeft
185{-| Pad a string on the right until it has a given length.
187 padRight 5 '.' "1" == "1...."
188 padRight 5 '.' "11" == "11..."
189 padRight 5 '.' "121" == "121.."
190-}
191padRight : Int -> Char -> String -> String
192padRight = Native.String.padRight
194{-| Get rid of whitespace on both sides of a string.
196 trim " hats \n" == "hats"
197-}
198trim : String -> String
199trim = Native.String.trim
201{-| Get rid of whitespace on the left of a string.
203 trimLeft " hats \n" == "hats \n"
204-}
205trimLeft : String -> String
206trimLeft = Native.String.trimLeft
208{-| Get rid of whitespace on the right of a string.
210 trimRight " hats \n" == " hats"
211-}
212trimRight : String -> String
213trimRight = Native.String.trimRight
215{-| Break a string into words, splitting on chunks of whitespace.
217 words "How are \t you? \n Good?" == ["How","are","you?","Good?"]
218-}
219words : String -> [String]
220words = Native.String.words
222{-| Break a string into lines, splitting on newlines.
224 lines "How are you?\nGood?" == ["How are you?", "Good?"]
225-}
226lines : String -> [String]
227lines = Native.String.lines
229{-| Convert a string to all upper case. Useful for case insensitive comparisons
230and VIRTUAL YELLING.
231-}
232toUpper : String -> String
233toUpper = Native.String.toUpper
235{-| Convert a string to all lower case. Useful for case insensitive comparisons. -}
236toLower : String -> String
237toLower = Native.String.toLower
239{-| Check to see if *any* characters satisfy a predicate.
241 any isDigit "90210" == True
242 any isDigit "R2-D2" == True
243 any isDigit "heart" == False
244-}
245any : (Char -> Bool) -> String -> Bool
246any = Native.String.any
248{-| Check to see if *all* characters satisfy a predicate.
250 all isDigit "90210" == True
251 all isDigit "R2-D2" == False
252 all isDigit "heart" == False
253-}
254all : (Char -> Bool) -> String -> Bool
255all = Native.String.all
257{-| See if a string contains another one.
259 contains "the" "theory" == True
260 contains "hat" "theory" == False
261 contains "THE" "theory" == False
263Use `Regex.contains` if you need something more flexible.
264-}
265contains : String -> String -> Bool
266contains = Native.String.contains
268{-| See if a string starts with another one.
270 startsWith "the" "theory" == True
271 startsWith "ory" "theory" == False
272-}
273startsWith : String -> String -> Bool
274startsWith = Native.String.startsWith
276{-| See if a string ends with another one.
278 endsWith "the" "theory" == False
279 endsWith "ory" "theory" == True
280-}
281endsWith : String -> String -> Bool
282endsWith = Native.String.endsWith
284{-| Get all of the indexes for a substring.
286 indexes "i" "Mississippi" == [1,4,7,10]
287 indexes "ss" "Mississippi" == [2,5]
288 indexes "needle" "haystack" == []
289-}
290indexes : String -> String -> [Int]
291indexes = Native.String.indexes
293{-| Alias for `indexes` -}
294indices : String -> String -> [Int]
295indices = Native.String.indexes
297{-| Turn any kind of value into a string.
299 show 42 == "42"
300 show [1,2] == "[1,2]"
301-}
302show : a -> String
303show = Native.Show.show
305{-| Try to convert a string into an int, failing on improperly formatted strings.
307 toInt "123" == Just 123
308 toInt "-42" == Just -42
309 toInt "3.1" == Nothing
310 toInt "31a" == Nothing
311-}
312toInt : String -> Maybe Int
313toInt = Native.String.toInt
315{-| Try to convert a string into a float, failing on improperly formatted strings.
317 toFloat "123" == Just 123.0
318 toFloat "-42" == Just -42.0
319 toFloat "3.1" == Just 3.1
320 toFloat "31a" == Nothing
321-}
322toFloat : String -> Maybe Float
323toFloat = Native.String.toFloat
325{-| Convert a string to a list of characters.
327 toList "abc" == ['a','b','c']
328-}
329toList : String -> [Char]
330toList = Native.String.toList
332{-| Convert a list of characters into a String. Can be useful if you
333want to create a string primarly by consing, perhaps for decoding
334something.
336 fromList ['a','b','c'] == "abc"
337-}
338fromList : [Char] -> String
339fromList = Native.String.fromList