1{-# LANGUAGE FlexibleContexts #-}
2--------------------------------------------------------------------------------
3-- | Miscellaneous string manipulation functions.
4module Hakyll.Core.Util.String
12--------------------------------------------------------------------------------
13import Data.Char (isSpace)
14import Data.List (isPrefixOf)
15import Data.Maybe (listToMaybe)
16import Text.Regex.TDFA ((=~~))
19--------------------------------------------------------------------------------
20-- | Trim a string (drop spaces, tabs and newlines at both sides).
21trim :: String -> String
22trim = reverse . trim' . reverse . trim'
24 trim' = dropWhile isSpace
27--------------------------------------------------------------------------------
28-- | A simple (but inefficient) regex replace funcion
29replaceAll :: String -- ^ Pattern
30 -> (String -> String) -- ^ Replacement (called on capture)
31 -> String -- ^ Source string
33replaceAll pattern f source = replaceAll' source
35 replaceAll' src = case listToMaybe (src =~~ pattern) of
38 let (before, tmp) = splitAt o src
39 (capture, after) = splitAt l tmp
40 in before ++ f capture ++ replaceAll' after
43--------------------------------------------------------------------------------
44-- | A simple regex split function. The resulting list will contain no empty
46splitAll :: String -- ^ Pattern
47 -> String -- ^ String to split
48 -> [String] -- ^ Result
49splitAll pattern = filter (not . null) . splitAll'
51 splitAll' src = case listToMaybe (src =~~ pattern) of
54 let (before, tmp) = splitAt o src
55 in before : splitAll' (drop l tmp)
59--------------------------------------------------------------------------------
60-- | Find the first instance of needle (must be non-empty) in haystack. We
61-- return the prefix of haystack before needle is matched.
65-- > needlePrefix "cd" "abcde" = "ab"
67-- > needlePrefix "ab" "abc" = ""
69-- > needlePrefix "ab" "xxab" = "xx"
71-- > needlePrefix "a" "xx" = "xx"
72needlePrefix :: String -> String -> Maybe String
73needlePrefix needle haystack = go [] haystack
77 | needle `isPrefixOf` xss = Just $ reverse acc
78 | 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