summaryrefslogtreecommitdiffstats
path: root/lib/default.nix
blob: 26653d96d59d572d4fe957df8b5ff530b954d76b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
{ lib, ... }:

with builtins;

let
  inherit (lib) stringAsChars;
in

{


  # "7.4.335" -> "74"
  majmin = with lib; x : concatStrings (take 2 (splitString "." x));


  concat = xs :
    if xs == []
      then ""
      else head xs + concat (tail xs)
    ;

  flip = f : x : y : f y x;

  # isSuffixOf :: String -> String -> Bool
  isSuffixOf =
    s : xs :
      let
        sn = stringLength s;
        xsn = stringLength xs;
      in
        xsn >= sn && substring (xsn - sn) sn xs == s ;

  removeSuffix =
    s : xs : substring 0 (stringLength xs - stringLength s) xs;

  # setMap :: (String -> a -> b) -> Set String a -> [b]
  #setMap = f: xs: map (k : f k (getAttr k xs)) (attrNames xs);

  # setToList :: Set k a -> [a]
  #setToList = setMap (_: v: v);

  shell-escape =
    let
      isSafeChar = c: match "[-./0-9_a-zA-Z]" c != null;
    in
    stringAsChars (c:
      if isSafeChar c then c
      else if c == "\n" then "'\n'"
      else "\\${c}");

}