diff options
author | Vladimír Čunát <vcunat@gmail.com> | 2015-10-03 13:33:13 +0200 |
---|---|---|
committer | Vladimír Čunát <vcunat@gmail.com> | 2015-10-03 13:33:37 +0200 |
commit | 5227fb1dd53fcb5918b9342dff4868f4ad68427e (patch) | |
tree | d6cd521e3f67944031216a27f740f28f22b73b41 /lib/strings.nix | |
parent | d6dd3b8bd1eaeeb21dfdb5051cd4732c748ce5d7 (diff) | |
parent | 33373d939a19f465228ddede6d38ce9032b5916b (diff) | |
download | nixpkgs-5227fb1dd53fcb5918b9342dff4868f4ad68427e.tar nixpkgs-5227fb1dd53fcb5918b9342dff4868f4ad68427e.tar.gz nixpkgs-5227fb1dd53fcb5918b9342dff4868f4ad68427e.tar.bz2 nixpkgs-5227fb1dd53fcb5918b9342dff4868f4ad68427e.tar.lz nixpkgs-5227fb1dd53fcb5918b9342dff4868f4ad68427e.tar.xz nixpkgs-5227fb1dd53fcb5918b9342dff4868f4ad68427e.tar.zst nixpkgs-5227fb1dd53fcb5918b9342dff4868f4ad68427e.zip |
Merge commit staging+systemd into closure-size
Many non-conflict problems weren't (fully) resolved in this commit yet.
Diffstat (limited to 'lib/strings.nix')
-rw-r--r-- | lib/strings.nix | 104 |
1 files changed, 54 insertions, 50 deletions
diff --git a/lib/strings.nix b/lib/strings.nix index ee5a59bdaf8..372c8833c32 100644 --- a/lib/strings.nix +++ b/lib/strings.nix @@ -8,11 +8,15 @@ in rec { - inherit (builtins) stringLength substring head tail isString; + inherit (builtins) stringLength substring head tail isString replaceStrings; # Concatenate a list of strings. - concatStrings = lib.fold (x: y: x + y) ""; + concatStrings = + if builtins ? concatStringsSep then + builtins.concatStringsSep "" + else + lib.foldl' (x: y: x + y) ""; # Map a function over a list and concatenate the resulting strings. @@ -25,14 +29,13 @@ rec { intersperse = separator: list: if list == [] || length list == 1 then list - else [(head list) separator] - ++ (intersperse separator (tail list)); + else tail (lib.concatMap (x: [separator x]) list); # Concatenate a list of strings with a separator between each element, e.g. # concatStringsSep " " ["foo" "bar" "xyzzy"] == "foo bar xyzzy" - concatStringsSep = separator: list: - concatStrings (intersperse separator list); + concatStringsSep = builtins.concatStringsSep or (separator: list: + concatStrings (intersperse separator list)); concatMapStringsSep = sep: f: list: concatStringsSep sep (map f list); concatImapStringsSep = sep: f: list: concatStringsSep sep (lib.imap f list); @@ -63,13 +66,13 @@ rec { # Determine whether a string has given prefix/suffix. hasPrefix = pref: str: - eqStrings (substring 0 (stringLength pref) str) pref; + substring 0 (stringLength pref) str == pref; hasSuffix = suff: str: let lenStr = stringLength str; lenSuff = stringLength suff; in lenStr >= lenSuff && - eqStrings (substring (lenStr - lenSuff) lenStr str) suff; + substring (lenStr - lenSuff) lenStr str == suff; # Convert a string to a list of characters (i.e. singleton strings). @@ -78,63 +81,57 @@ rec { # will likely be horribly inefficient; Nix is not a general purpose # programming language. Complex string manipulations should, if # appropriate, be done in a derivation. - stringToCharacters = s: let l = stringLength s; in - if l == 0 - then [] - else map (p: substring p 1 s) (lib.range 0 (l - 1)); + stringToCharacters = s: + map (p: substring p 1 s) (lib.range 0 (stringLength s - 1)); - # Manipulate a string charcater by character and replace them by strings - # before concatenating the results. + # Manipulate a string charactter 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 : - stringAsChars (c: if lib.elem c list then "\\${c}" else c) string; + # Escape occurrence of the elements of ‘list’ in ‘string’ by + # prefixing it with a backslash. For example, ‘escape ["(" ")"] + # "(foo)"’ returns the string ‘\(foo\)’. + escape = list: replaceChars list (map (c: "\\${c}") list); - # still ugly slow. But more correct now - # [] for zsh + # Escape all characters that have special meaning in the Bourne shell. 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 ["<" ">"] ["<" ">"] "<foo>" returns "<foo>". - replaceChars = del: new: s: + # Obsolete - use replaceStrings instead. + replaceChars = builtins.replaceStrings or ( + del: new: s: let + substList = lib.zipLists del new; subst = c: - (lib.fold - (sub: res: if sub.fst == c then sub else res) - {fst = c; snd = c;} (lib.zipLists del new) - ).snd; + let found = lib.findFirst (sub: sub.fst == c) null substList; in + if found == null then + c + else + found.snd; in - stringAsChars subst s; + stringAsChars subst s); - # Case conversion utilities + # Case conversion utilities. lowerChars = stringToCharacters "abcdefghijklmnopqrstuvwxyz"; upperChars = stringToCharacters "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; toLower = replaceChars upperChars lowerChars; toUpper = replaceChars lowerChars upperChars; - # Appends string context from another string - addContextFrom = a: b: substring 0 0 a + b; - # Compares strings not requiring context equality - # Obviously, a workaround but works on all Nix versions - eqStrings = a: b: addContextFrom b a == addContextFrom a b; + # Appends string context from another string. + addContextFrom = a: b: substring 0 0 a + b; - # Cut a string with a separator and produces a list of strings which were - # separated by this separator. e.g., - # `splitString "." "foo.bar.baz"' returns ["foo" "bar" "baz"]. + # Cut a string with a separator and produces a list of strings which + # were separated by this separator; e.g., `splitString "." + # "foo.bar.baz"' returns ["foo" "bar" "baz"]. splitString = _sep: _s: let sep = addContextFrom _s _sep; @@ -177,7 +174,7 @@ rec { sufLen = stringLength suf; sLen = stringLength s; in - if sufLen <= sLen && eqStrings suf (substring (sLen - sufLen) sufLen s) then + if sufLen <= sLen && suf == substring (sLen - sufLen) sufLen s then substring 0 (sLen - sufLen) s else s; @@ -196,21 +193,22 @@ rec { # Extract name with version from URL. Ask for separator which is - # supposed to start extension - nameFromURL = url: sep: let - components = splitString "/" url; - filename = lib.last components; - name = builtins.head (splitString sep filename); - in - assert ! eqStrings name filename; - name; + # supposed to start extension. + nameFromURL = url: sep: + let + components = splitString "/" url; + filename = lib.last components; + name = builtins.head (splitString sep filename); + in assert name != filename; name; # Create an --{enable,disable}-<feat> string that can be passed to # standard GNU Autoconf scripts. enableFeature = enable: feat: "--${if enable then "enable" else "disable"}-${feat}"; - # Create a fixed width string with additional prefix to match required width + + # Create a fixed width string with additional prefix to match + # required width. fixedWidthString = width: filler: str: let strw = lib.stringLength str; @@ -219,6 +217,12 @@ rec { assert strw <= width; if strw == width then str else filler + fixedWidthString reqWidth filler str; - # Format a number adding leading zeroes up to fixed width + + # Format a number adding leading zeroes up to fixed width. fixedWidthNumber = width: n: fixedWidthString width "0" (toString n); + + + # Check whether a value is a store path. + isStorePath = x: builtins.substring 0 1 (toString x) == "/" && dirOf (builtins.toPath x) == builtins.storeDir; + } |