git.iko.soy/typeable/pandoc
on master help sign in
T.P.Shared: cleanup.

Cleanup up some functions and added deprecation pragmas to funtions no longer used in the code base.

commit9e728b40f36d48c687372ad447670186ed415337 authorJohn MacFarlane <jgm@berkeley.edu> dateWed 17 Feb 2021, 17:21 (6 years ago) parent743f721
1 file changed+26−11
src/Text/Pandoc/Shared.hs+26−11
src/Text/Pandoc/Shared.hs+26−11
@@ -151,21 +151,22 @@ splitBy :: (a -> Bool) -> [a] -> [[a]]
151151splitBy _ [] = []
152152splitBy isSep lst =
153153 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)
156155
156+-- | Split text by groups of one or more separator.
157157splitTextBy :: (Char -> Bool) -> T.Text -> [T.Text]
158158splitTextBy isSep t
159159 | T.null t = []
160160 | 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)
163162
163+{-# DEPRECATED splitByIndices "This function is slated for removal" #-}
164164splitByIndices :: [Int] -> [a] -> [[a]]
165165splitByIndices [] lst = [lst]
166166splitByIndices (x:xs) lst = first:splitByIndices (map (\y -> y - x) xs) rest
167167 where (first, rest) = splitAt x lst
168168
169+{-# DEPRECATED splitStringByIndices "This function is slated for removal" #-}
169170-- | Split string into chunks divided at specified indices.
170171splitStringByIndices :: [Int] -> [Char] -> [[Char]]
171172splitStringByIndices [] lst = [lst]
@@ -173,15 +174,22 @@ splitStringByIndices (x:xs) lst =
173174 let (first, rest) = splitAt' x lst in
174175 first : splitStringByIndices (map (\y -> y - x) xs) rest
175176
176−splitTextByIndices :: [Int] -> T.Text -> [T.Text]
177−splitTextByIndices ns = fmap T.pack . splitStringByIndices ns . T.unpack
178−
177+-- DEPRECATED: can be removed when splitStringByIndices is
179178splitAt' :: Int -> [Char] -> ([Char],[Char])
180179splitAt' _ [] = ([],[])
181180splitAt' n xs | n <= 0 = ([],xs)
182181splitAt' n (x:xs) = (x:ys,zs)
183182 where (ys,zs) = splitAt' (n - charWidth x) xs
184183
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" #-}
185193-- | Replace each occurrence of one sublist in a list with another.
186194substitute :: (Eq a) => [a] -> [a] -> [a] -> [a]
187195substitute _ _ [] = []
@@ -254,17 +262,24 @@ notElemText c = T.all (/= c)
254262stripTrailingNewlines :: T.Text -> T.Text
255263stripTrailingNewlines = T.dropWhileEnd (== '\n')
256264
265+isWS :: Char -> Bool
266+isWS ' ' = True
267+isWS '\r' = True
268+isWS '\n' = True
269+isWS '\t' = True
270+isWS _ = False
271+
257272-- | Remove leading and trailing space (including newlines) from string.
258273trim :: T.Text -> T.Text
259−trim = T.dropAround (`elemText` " \r\n\t")
274+trim = T.dropAround isWS
260275
261276-- | Remove leading space (including newlines) from string.
262277triml :: T.Text -> T.Text
263−triml = T.dropWhile (`elemText` " \r\n\t")
278+triml = T.dropWhile isWS
264279
265280-- | Remove trailing space (including newlines) from string.
266281trimr :: T.Text -> T.Text
267−trimr = T.dropWhileEnd (`elemText` " \r\n\t")
282+trimr = T.dropWhileEnd isWS
268283
269284-- | Trim leading space and trailing space unless after \.
270285trimMath :: T.Text -> T.Text
@@ -275,7 +290,7 @@ trimMath = triml . T.reverse . stripBeginSpace . T.reverse -- no Text.spanEnd
275290 | Just ('\\', _) <- T.uncons suff = T.cons (T.last pref) suff
276291 | otherwise = suff
277292 where
278− (pref, suff) = T.span (`elemText` " \t\n\r") t
293+ (pref, suff) = T.span isWS t
279294
280295-- | Strip leading and trailing characters from string
281296stripFirstAndLast :: T.Text -> T.Text