1--------------------------------------------------------------------------------
2-- | Miscellaneous string manipulation functions.
3module Hakyll.Core.Util.String
11--------------------------------------------------------------------------------
12import Data.Char (isSpace)
13import Data.List (isPrefixOf)
14import Data.Maybe (listToMaybe)
15import Text.Regex.TDFA ((=~~))
18--------------------------------------------------------------------------------
19-- | Trim a string (drop spaces, tabs and newlines at both sides).
20trim :: String -> String
21trim = reverse . trim' . reverse . trim'
23 trim' = dropWhile isSpace
26--------------------------------------------------------------------------------
27-- | A simple (but inefficient) regex replace funcion
28replaceAll :: String -- ^ Pattern
29 -> (String -> String) -- ^ Replacement (called on capture)
30 -> String -- ^ Source string
32replaceAll pattern f source = replaceAll' source
34 replaceAll' src = case listToMaybe (src =~~ pattern) of
37 let (before, tmp) = splitAt o src
38 (capture, after) = splitAt l tmp
39 in before ++ f capture ++ replaceAll' after
42--------------------------------------------------------------------------------
43-- | A simple regex split function. The resulting list will contain no empty
45splitAll :: String -- ^ Pattern
46 -> String -- ^ String to split
47 -> [String] -- ^ Result
48splitAll pattern = filter (not . null) . splitAll'
50 splitAll' src = case listToMaybe (src =~~ pattern) of
53 let (before, tmp) = splitAt o src
54 in before : splitAll' (drop l tmp)
58--------------------------------------------------------------------------------
59-- | Find the first instance of needle (must be non-empty) in haystack. We
60-- return the prefix of haystack before needle is matched.
64-- > needlePrefix "cd" "abcde" = "ab"
66-- > needlePrefix "ab" "abc" = ""
68-- > needlePrefix "ab" "xxab" = "xx"
70-- > needlePrefix "a" "xx" = "xx"
71needlePrefix :: String -> String -> Maybe String
72needlePrefix needle haystack = go [] haystack
76 | needle `isPrefixOf` xss = Just $ reverse acc
77 | otherwise = go (x : acc) xs
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list