git.iko.soy/typeable/pandoc
on master help sign in
T.P.Shared: cleanup.
9e728b4John MacFarlane
src/Text/Pandoc/Shared.hs+26−11
@@ -151,21 +151,22 @@ splitBy :: (a -> Bool) -> [a] -> [[a]]
151splitBy _ [] = []
151splitBy _ [] = []
152splitBy isSep lst =
152splitBy isSep lst =
153 let (first, rest) = break isSep lst
153 let (first, rest) = break isSep lst
154− rest' = dropWhile isSep rest
155− in first:splitBy isSep rest'
154+ in first:splitBy isSep (dropWhile isSep rest)
156
155
156+-- | Split text by groups of one or more separator.
157splitTextBy :: (Char -> Bool) -> T.Text -> [T.Text]
157splitTextBy :: (Char -> Bool) -> T.Text -> [T.Text]
158splitTextBy isSep t
158splitTextBy isSep t
159 | T.null t = []
159 | T.null t = []
160 | otherwise = let (first, rest) = T.break isSep t
160 | otherwise = let (first, rest) = T.break isSep t
161− rest' = T.dropWhile isSep rest
162− in first : splitTextBy isSep rest'
161+ in first : splitTextBy isSep (T.dropWhile isSep rest)
163
162
163+{-# DEPRECATED splitByIndices "This function is slated for removal" #-}
164splitByIndices :: [Int] -> [a] -> [[a]]
164splitByIndices :: [Int] -> [a] -> [[a]]
165splitByIndices [] lst = [lst]
165splitByIndices [] lst = [lst]
166splitByIndices (x:xs) lst = first:splitByIndices (map (\y -> y - x) xs) rest
166splitByIndices (x:xs) lst = first:splitByIndices (map (\y -> y - x) xs) rest
167 where (first, rest) = splitAt x lst
167 where (first, rest) = splitAt x lst
168
168
169+{-# DEPRECATED splitStringByIndices "This function is slated for removal" #-}
169-- | Split string into chunks divided at specified indices.
170-- | Split string into chunks divided at specified indices.
170splitStringByIndices :: [Int] -> [Char] -> [[Char]]
171splitStringByIndices :: [Int] -> [Char] -> [[Char]]
171splitStringByIndices [] lst = [lst]
172splitStringByIndices [] lst = [lst]
@@ -173,15 +174,22 @@ splitStringByIndices (x:xs) lst =
173 let (first, rest) = splitAt' x lst in
174 let (first, rest) = splitAt' x lst in
174 first : splitStringByIndices (map (\y -> y - x) xs) rest
175 first : splitStringByIndices (map (\y -> y - x) xs) rest
175
176
176−splitTextByIndices :: [Int] -> T.Text -> [T.Text]
177+-- DEPRECATED: can be removed when splitStringByIndices is
177−splitTextByIndices ns = fmap T.pack . splitStringByIndices ns . T.unpack
178−
179splitAt' :: Int -> [Char] -> ([Char],[Char])
178splitAt' :: Int -> [Char] -> ([Char],[Char])
180splitAt' _ [] = ([],[])
179splitAt' _ [] = ([],[])
181splitAt' n xs | n <= 0 = ([],xs)
180splitAt' n xs | n <= 0 = ([],xs)
182splitAt' n (x:xs) = (x:ys,zs)
181splitAt' n (x:xs) = (x:ys,zs)
183 where (ys,zs) = splitAt' (n - charWidth x) xs
182 where (ys,zs) = splitAt' (n - charWidth x) xs
184
183
184+splitTextByIndices :: [Int] -> T.Text -> [T.Text]
185+splitTextByIndices ns = splitTextByRelIndices (zipWith (-) ns (0:ns))
186+ where
187+ splitTextByRelIndices [] t = [t]
188+ splitTextByRelIndices (x:xs) t =
189+ let (first, rest) = T.splitAt x t
190+ in first : splitTextByRelIndices xs rest
191+
192+{-# DEPRECATED substitute "This function is slated for removal" #-}
185-- | Replace each occurrence of one sublist in a list with another.
193-- | Replace each occurrence of one sublist in a list with another.
186substitute :: (Eq a) => [a] -> [a] -> [a] -> [a]
194substitute :: (Eq a) => [a] -> [a] -> [a] -> [a]
187substitute _ _ [] = []
195substitute _ _ [] = []
@@ -254,17 +262,24 @@ notElemText c = T.all (/= c)
254stripTrailingNewlines :: T.Text -> T.Text
262stripTrailingNewlines :: T.Text -> T.Text
255stripTrailingNewlines = T.dropWhileEnd (== '\n')
263stripTrailingNewlines = T.dropWhileEnd (== '\n')
256
264
265+isWS :: Char -> Bool
266+isWS ' ' = True
267+isWS '\r' = True
268+isWS '\n' = True
269+isWS '\t' = True
270+isWS _ = False
271+
257-- | Remove leading and trailing space (including newlines) from string.
272-- | Remove leading and trailing space (including newlines) from string.
258trim :: T.Text -> T.Text
273trim :: T.Text -> T.Text
259−trim = T.dropAround (`elemText` " \r\n\t")
274+trim = T.dropAround isWS
260
275
261-- | Remove leading space (including newlines) from string.
276-- | Remove leading space (including newlines) from string.
262triml :: T.Text -> T.Text
277triml :: T.Text -> T.Text
263−triml = T.dropWhile (`elemText` " \r\n\t")
278+triml = T.dropWhile isWS
264
279
265-- | Remove trailing space (including newlines) from string.
280-- | Remove trailing space (including newlines) from string.
266trimr :: T.Text -> T.Text
281trimr :: T.Text -> T.Text
267−trimr = T.dropWhileEnd (`elemText` " \r\n\t")
282+trimr = T.dropWhileEnd isWS
268
283
269-- | Trim leading space and trailing space unless after \.
284-- | Trim leading space and trailing space unless after \.
270trimMath :: T.Text -> T.Text
285trimMath :: T.Text -> T.Text
@@ -275,7 +290,7 @@ trimMath = triml . T.reverse . stripBeginSpace . T.reverse -- no Text.spanEnd
275 | Just ('\\', _) <- T.uncons suff = T.cons (T.last pref) suff
290 | Just ('\\', _) <- T.uncons suff = T.cons (T.last pref) suff
276 | otherwise = suff
291 | otherwise = suff
277 where
292 where
278− (pref, suff) = T.span (`elemText` " \t\n\r") t
293+ (pref, suff) = T.span isWS t
279
294
280-- | Strip leading and trailing characters from string
295-- | Strip leading and trailing characters from string
281stripFirstAndLast :: T.Text -> T.Text
296stripFirstAndLast :: T.Text -> T.Text