diff options
author | John Ericson <git@JohnEricson.me> | 2022-10-28 23:22:18 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-28 23:22:18 +0100 |
commit | 13f2a6f38db44385ae1c7d3d01170149de328abb (patch) | |
tree | 9184949feb826f00b7a5bc4175dda4667055e44c /doc/manual/utils.nix | |
parent | 12461e246b02371c6b6981b4e65985e9397474e1 (diff) | |
parent | b7e8a3bf4cbb2448db860f65ea13ef2c64b6883b (diff) |
Merge branch 'master' into indexed-store-path-outputs
Diffstat (limited to 'doc/manual/utils.nix')
-rw-r--r-- | doc/manual/utils.nix | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/doc/manual/utils.nix b/doc/manual/utils.nix index d4b18472f..d0643ef46 100644 --- a/doc/manual/utils.nix +++ b/doc/manual/utils.nix @@ -5,6 +5,32 @@ rec { concatStrings = concatStringsSep ""; + replaceStringsRec = from: to: string: + # recursively replace occurrences of `from` with `to` within `string` + # example: + # replaceStringRec "--" "-" "hello-----world" + # => "hello-world" + let + replaced = replaceStrings [ from ] [ to ] string; + in + if replaced == string then string else replaceStringsRec from to replaced; + + squash = replaceStringsRec "\n\n\n" "\n\n"; + + trim = string: + # trim trailing spaces and squash non-leading spaces + let + trimLine = line: + let + # separate leading spaces from the rest + parts = split "(^ *)" line; + spaces = head (elemAt parts 1); + rest = elemAt parts 2; + # drop trailing spaces + body = head (split " *$" rest); + in spaces + replaceStringsRec " " " " body; + in concatStringsSep "\n" (map trimLine (splitLines string)); + # FIXME: O(n^2) unique = foldl' (acc: e: if elem e acc then acc else acc ++ [ e ]) []; |