on 704da02 help sign in
elk/libraries/String.elm
311 lines 8.3 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 sub, left, right, dropLeft, dropRight
14# Check for Substrings
15@docs contains, startsWith, endsWith, indexes, indices
17# Conversions
18@docs 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.String
32import Maybe (Maybe)
34{-| Check if a string is empty `(isEmpty "" == True)` -}
35isEmpty : String -> Bool
36isEmpty = Native.String.isEmpty
38{-| Add a character to the beginning of a string -}
39cons : Char -> String -> String
40cons = Native.String.cons
42{-| Split a non-empty string into its head and tail. This lets you
43pattern match on strings exactly as you would with lists.
45 uncons "abc" == Just ('a',"bc")
46 uncons "" == Nothing
47-}
48uncons : String -> Maybe (Char, String)
49uncons = Native.String.uncons
51{-| Append two strings. You can also use [the `(++)` operator](/library/List.elm#++)
52to do this.
54 append "butter" "fly" == "butterfly"
55-}
56append : String -> String -> String
57append = Native.String.append
59{-| Concatenate many strings into one.
61 concat ["never","the","less"] == "nevertheless"
62-}
63concat : [String] -> String
64concat = Native.String.concat
66{-| Get the length of a string `(length "innumerable" == 11)` -}
67length : String -> Int
68length = Native.String.length
70{-| Transform every character in a string
72 map (\c -> if c == '/' then '.' else c) "a/b/c" == "a.b.c"
73-}
74map : (Char -> Char) -> String -> String
75map = Native.String.map
77{-| Keep only the characters that satisfy the predicate:
79 filter isDigit "R2-D2" == "22"
80-}
81filter : (Char -> Bool) -> String -> String
82filter = Native.String.filter
84{-| Reverse a string. `(reverse "stressed" == "desserts")` -}
85reverse : String -> String
86reverse = Native.String.reverse
88{-| Reduce a string from the left:
90 foldl cons "" "time" == "emit"
91-}
92foldl : (Char -> b -> b) -> b -> String -> b
93foldl = Native.String.foldl
95{-| Reduce a string from the right:
97 foldr cons "" "time" == "time"
98-}
99foldr : (Char -> b -> b) -> b -> String -> b
100foldr = Native.String.foldr
102{-| Split a string using a given separator.
104 split "," "cat,dog,cow" == ["cat","dog","cow"]
105 split "/" "home/evan/Desktop/" == ["home","evan","Desktop"]
106-}
107split : String -> String -> [String]
108split = Native.String.split
110{-| Put many strings together with a given separator.
112 join " " ["cat","dog","cow"] == "cat dog cow"
113 join "/" ["home","evan","Desktop"] == "home/evan/Desktop"
114-}
115join : String -> [String] -> String
116join = Native.String.join
118{-| Repeat a string N times `(repeat 3 "ha" == "hahaha")` -}
119repeat : Int -> String -> String
120repeat = Native.String.repeat
122{-| Take a substring given a start and end index. Negative indexes
123are taken starting from the *end* of the list.
125 sub 7 9 "snakes on a plane!" == "on"
126 sub 0 6 "snakes on a plane!" == "snakes"
127 sub 0 -7 "snakes on a plane!" == "snakes on a"
128 sub -6 -1 "snakes on a plane!" == "plane"
129-}
130sub : Int -> Int -> String -> String
131sub = Native.String.sub
133{-| Take N characters from the left side of a string. -}
134left : Int -> String -> String
135left = Native.String.left
137{-| Take N characters from the right side of a string. -}
138right : Int -> String -> String
139right = Native.String.right
141{-| Drop N characters from the left side of a string. -}
142dropLeft : Int -> String -> String
143dropLeft = Native.String.dropLeft
145{-| Drop N characters from the right side of a string. -}
146dropRight : Int -> String -> String
147dropRight = Native.String.dropRight
149{-| Pad a string on both sides until it has a given length.
151 pad 5 ' ' "1" == " 1 "
152 pad 5 ' ' "11" == " 11 "
153 pad 5 ' ' "121" == " 121 "
154-}
155pad : Int -> Char -> String -> String
156pad = Native.String.pad
158{-| Pad a string on the left until it has a given length.
160 pad 5 '.' "1" == "....1"
161 pad 5 '.' "11" == "...11"
162 pad 5 '.' "121" == "..121"
163-}
164padLeft : Int -> Char -> String -> String
165padLeft = Native.String.padLeft
167{-| Pad a string on the right until it has a given length.
169 pad 5 '.' "1" == "1...."
170 pad 5 '.' "11" == "11..."
171 pad 5 '.' "121" == "121.."
172-}
173padRight : Int -> Char -> String -> String
174padRight = Native.String.padRight
176{-| Get rid of whitespace on both sides of a string.
178 trim " hats \n" == "hats"
179-}
180trim : String -> String
181trim = Native.String.trim
183{-| Get rid of whitespace on the left of a string.
185 trimLeft " hats \n" == "hats \n"
186-}
187trimLeft : String -> String
188trimLeft = Native.String.trimLeft
190{-| Get rid of whitespace on the left of a string.
192 trimRight " hats \n" == " hats"
193-}
194trimRight : String -> String
195trimRight = Native.String.trimRight
197{-| Break a string into words, splitting on chunks of whitespace.
199 words "How are \t you? \n Good?" == ["How","are","you?","Good?"]
200-}
201words : String -> [String]
202words = Native.String.words
204{-| Break a string into lines, splitting on newlines.
206 lines "How are you?\nGood? == ["How are you?", "Good?"]
207-}
208lines : String -> [String]
209lines = Native.String.lines
211{-| Convert a string to all upper case. Useful for case insensitive comparisons
212and VIRTUAL YELLING.
213-}
214toUpper : String -> String
215toUpper = Native.String.toUpper
217{-| Convert a string to all lower case. Useful for case insensitive comparisons. -}
218toLower : String -> String
219toLower = Native.String.toLower
221{-| Check to see if *any* characters satisfy a predicate.
223 any isDigit "90210" == True
224 any isDigit "R2-D2" == True
225 any isDigit "heart" == False
226-}
227any : (Char -> Bool) -> String -> Bool
228any = Native.String.any
230{-| Check to see if *all* characters satisfy a predicate.
232 all isDigit "90210" == True
233 all isDigit "R2-D2" == False
234 any isDigit "heart" == False
235-}
236all : (Char -> Bool) -> String -> Bool
237all = Native.String.all
239{-| See if a string contains another one.
241 contains "the" "theory" == True
242 contains "hat" "theory" == False
243 contains "THE" "theory" == False
244-}
245contains : String -> String -> Bool
246contains = Native.String.contains
248{-| See if a string starts with another one.
250 startsWith "the" "theory" == True
251 startsWith "ory" "theory" == False
252-}
253startsWith : String -> String -> Bool
254startsWith = Native.String.startsWith
256{-| See if a string ends with another one.
258 endsWith "the" "theory" == False
259 endsWith "ory" "theory" == True
260-}
261endsWith : String -> String -> Bool
262endsWith = Native.String.endsWith
264{-| Get all of the indexes for a substring.
266 indexes "i" "Mississippi" == [1,4,7,10]
267 indexes "ss" "Mississippi" == [2,5]
268 indexes "needle" "haystack" == []
269-}
270indexes : String -> String -> [Int]
271indexes = Native.String.indexes
273{-| Alias for `indexes` -}
274indices : String -> String -> [Int]
275indices = Native.String.indexes
277{-| Try to convert a string into an int, failing on improperly formatted strings.
279 toInt "123" == Just 123
280 toInt "-42" == Just -42
281 toInt "3.1" == Nothing
282 toInt "31a" == Nothing
283-}
284toInt : String -> Maybe Int
285toInt = Native.String.toInt
287{-| Try to convert a string into a float, failing on improperly formatted strings.
289 toFloat "123" == Just 123.0
290 toFloat "-42" == Just -42.0
291 toFloat "3.1" == Just 3.1
292 toFloat "31a" == Nothing
293-}
294toFloat : String -> Maybe Float
295toFloat = Native.String.toFloat
297{-| Convert a string to a list of characters.
299 toList "abc" == ['a','b','c']
300-}
301toList : String -> [Char]
302toList = Native.String.toList
304{-| Convert a list of characters into a String. Can be useful if you
305want to create a string primarly by consing, perhaps for decoding
306something.
308 fromList ['a','b','c'] == "abc"
309-}
310fromList : [Char] -> String
311fromList = Native.String.fromList