summaryrefslogtreecommitdiff
path: root/Utility/Misc.hs
diff options
context:
space:
mode:
authorGravatar Joey Hess <joey@kitenet.net>2013-04-08 23:56:37 -0400
committerGravatar Joey Hess <joey@kitenet.net>2013-04-08 23:56:37 -0400
commit67b8e8d55eefde1978fa92ae49020d161a9f544b (patch)
treedf7a4f44afdd896bec663be71ca50882cd06a8bf /Utility/Misc.hs
parentc79f8456dd2a74b028c66d3953062140d428b0ed (diff)
implement massReplace
This looks at the string one char at a time, which is hardly efficient.. but more than good enough for expanding variables in relatively short command lines.
Diffstat (limited to 'Utility/Misc.hs')
-rw-r--r--Utility/Misc.hs18
1 files changed, 18 insertions, 0 deletions
diff --git a/Utility/Misc.hs b/Utility/Misc.hs
index c04409563..1bb6de79f 100644
--- a/Utility/Misc.hs
+++ b/Utility/Misc.hs
@@ -11,6 +11,7 @@ import System.IO
import Control.Monad
import Foreign
import Data.Char
+import Data.List
import Control.Applicative
import System.Posix.Process (getAnyProcessStatus)
@@ -70,6 +71,23 @@ segmentDelim p l = map reverse $ go [] [] l
| p i = go [] ([i]:c:r) is
| otherwise = go (i:c) r is
+{- Replaces multiple values in a string.
+ -
+ - Takes care to skip over just-replaced values, so that they are not
+ - mangled. For example, massReplace [("foo", "new foo")] does not
+ - replace the "new foo" with "new new foo".
+ -}
+massReplace :: [(String, String)] -> String -> String
+massReplace vs = go [] vs
+ where
+
+ go acc _ [] = concat $ reverse acc
+ go acc [] (c:cs) = go ([c]:acc) vs cs
+ go acc ((val, replacement):rest) s
+ | val `isPrefixOf` s =
+ go (replacement:acc) vs (drop (length val) s)
+ | otherwise = go acc rest s
+
{- Given two orderings, returns the second if the first is EQ and returns
- the first otherwise.
-