From 915fa6a08f31f44cf4289f785001e7e7b209ee6e Mon Sep 17 00:00:00 2001 From: Nicolas Pierron Date: Tue, 6 Oct 2009 09:21:52 +0000 Subject: [PATCH] introduce the stringAsChars ans replaceChars functions. svn path=/nixpkgs/trunk/; revision=17670 --- pkgs/lib/strings.nix | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/pkgs/lib/strings.nix b/pkgs/lib/strings.nix index dc1dbfd8de19..64abb514fb21 100644 --- a/pkgs/lib/strings.nix +++ b/pkgs/lib/strings.nix @@ -70,15 +70,36 @@ rec { else [(substring 0 1 s)] ++ stringToCharacters (substring 1 (builtins.sub l 1) s); + # Manipulate a string charcater by character and replace them by strings + # before concatenating the results. + stringAsChars = f: s: + concatStrings ( + map f (stringToCharacters s) + ); + # same as vim escape function. # Each character contained in list is prefixed by "\" escape = list : string : - lib.concatStrings (map (c: if lib.elem c list then "\\${c}" else c) (stringToCharacters string)); + stringAsChars (c: if lib.elem c list then "\\${c}" else c) string; # still ugly slow. But more correct now # [] for zsh escapeShellArg = lib.escape (stringToCharacters "\\ ';$`()|<>\t*[]"); + # replace characters by their substitutes. This function is equivalent to + # the `tr' command except that one character can be replace by multiple + # ones. e.g., + # replaceChars ["<" ">"] ["<" ">"] "" returns "<foo>". + replaceChars = del: new: s: + let + subst = c: + (lib.fold + (sub: res: if sub.fst == c then sub else res) + {fst = c; snd = c;} (lib.zipLists del new) + ).snd; + in + stringAsChars subst s; + # Compares strings not requiring context equality # Obviously, a workaround but works on all Nix versions eqStrings = a: b: (a+(substring 0 0 b)) == ((substring 0 0 a)+b);