diff options
author | Guillaume Maudoux <guillaume.maudoux@tweag.io> | 2022-10-16 20:39:19 +0200 |
---|---|---|
committer | Guillaume Maudoux <guillaume.maudoux@tweag.io> | 2022-10-16 20:39:19 +0200 |
commit | 3f9f6ae12712b366c038f21de99c8ede6c805be9 (patch) | |
tree | f2510a23941efd48c102580fc945107c5cb32e12 /doc/manual/utils.nix | |
parent | 96f2dd99d39da61706895b05ed864558679fac79 (diff) | |
parent | 3093bd3a855b8fa1f572fd5a33c1971adf5e3e08 (diff) |
Merge remote-tracking branch 'origin/master' into coerce-string
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 ]) []; |