1-- | Miscellaneous string manipulation functions.
3module Hakyll.Core.Util.String
10import Data.Char (isSpace)
11import Data.List (isPrefixOf)
12import Data.Maybe (listToMaybe)
14import Text.Regex.TDFA ((=~~))
16-- | Trim a string (drop spaces, tabs and newlines at both sides).
18trim :: String -> String
19trim = reverse . trim' . reverse . trim'
21 trim' = dropWhile isSpace
23-- | A simple (but inefficient) regex replace funcion
25replaceAll :: String -- ^ Pattern
26 -> (String -> String) -- ^ Replacement (called on capture)
27 -> String -- ^ Source string
29replaceAll pattern f source = replaceAll' source
31 replaceAll' src = case listToMaybe (src =~~ pattern) of
34 let (before, tmp) = splitAt o src
35 (capture, after) = splitAt l tmp
36 in before ++ f capture ++ replaceAll' after
38-- | A simple regex split function. The resulting list will contain no empty
41splitAll :: String -- ^ Pattern
42 -> String -- ^ String to split
43 -> [String] -- ^ Result
44splitAll pattern = filter (not . null) . splitAll'
46 splitAll' src = case listToMaybe (src =~~ pattern) of
49 let (before, tmp) = splitAt o src
50 in before : splitAll' (drop l tmp)
53-- | Find the first instance of needle (must be non-empty) in
54-- haystack. We return the prefix of haystack before needle is
58-- needlePrefix "cd" "abcde" = "ab"
59-- needlePrefix "ab" "abc" = ""
60-- needlePrefix "ab" "xxab" = "xx"
61-- needlePrefix "a" "xx" = "xx"
63needlePrefix :: String -> String -> String
64needlePrefix needle haystack = go haystack
67 go xss@(x:xs) | needle `isPrefixOf` xss = []
68 | otherwise = x : go 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