-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrings.nix
More file actions
201 lines (186 loc) · 5.73 KB
/
strings.nix
File metadata and controls
201 lines (186 loc) · 5.73 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
{ pkgs }: let
p = { inherit pkgs; };
inherit (pkgs.lib.lists) elemAt last foldl' length elem;
inherit (pkgs.lib.strings) concatStringsSep hasPrefix hasSuffix toInt splitString match optionalString substring stringLength replaceStrings stringToCharacters upperChars lowerChars stringAsChars;
inherit (pkgs.lib.attrsets) mapAttrsToList;
inherit (import ./operators.nix p) or_ eq add sub;
inherit (import ./numbers.nix p) floatishToInt;
inherit (import ./trivial.nix p) applyIf ifEl;
inherit (import ./lists p) findFirstIndex;
ws = [ " " "\t" "\n" ];
in rec {
_doc = ''
Helper functions related to strings.
'';
mkURL = {
description = ''
The opposite of splitURL. Create a URL string from its components.
'';
type = "{String, query :: {String}} -> String";
__functor = _: {
protocol ? "",
username ? "",
password ? "",
domain ? "",
port ? "",
path ? "",
query ? {}
}: let
password' = optionalString (password != "") ":${password}";
domain' = (optionalString (username != "" || password != "") "@") + domain;
port' = optionalString (port != "") (":${toString port}");
path' = (optionalString (path != "") "/") + path;
query' = (optionalString (query != {}) "?") + (concatStringsSep "&" (mapAttrsToList (n: v: "${n}=${v}") query));
in "${protocol}://${username}${password'}${domain'}${port'}${path'}${query'}";
};
getPortFromURL = {
description = ''
Get a port from a URL.
'';
type = "String -> Int";
examples = [
''getPortFromURL "https://example.com:1234" == 1234''
];
__functor = _: s: toInt (last (splitString ":" s));
};
concatLines = {
description = ''
Concatenate lines together separated by newlines.
'';
type = "[String] -> String";
examples = [
''concatLines [ "a" "b" "c" ] == "a\nb\nc"''
];
__functor = _: ss: concatStringsSep "\n" ss;
};
unlines = {
description = ''
Replace newlines with spaces.
'';
type = "String -> String";
examples = [
''unlines "a\nb\nc" == "a b c"''
];
__functor = _: ss: replaceStrings [ "\n" ] [ " " ] ss;
};
trimLeadChars = {
description = ''
Trim leading characters.
'';
type = "[String] -> String -> String";
examples = [
''trimLeadChars [ "x" ] "xabc" == "abc"''
''trimLeadChars [ "x" "y" ] "yxabc" == "abc"''
];
__functor = _: cs: s: if foldl' (a: x: a || hasPrefix x s) false cs then trimLeadChars cs (substring 1 (stringLength s) s) else s;
};
trimTrailChars = {
description = ''
Trim trailing characters.
'';
type = "[String] -> String -> String";
examples = [
''trimTrailChars [ "x" ] "abcx" == "abc"''
''trimTrailChars [ "x" "y" ] "abcyx" == "abc"''
];
__functor = _: cs: s: if foldl' (a: x: a || hasSuffix x s) false cs then trimTrailChars cs (substring 0 ((stringLength s) - 1) s) else s;
};
trimLead = {
description = ''
Trim leading whitespace.
'';
type = "String -> String";
examples = [
''trimLead " abc" == "abc"''
''trimLead " abc " == "abc "''
];
__functor = _: trimLeadChars ws;
};
trimTrail = {
description = ''
Trim trailing whitespace.
'';
type = "String -> String";
examples = [
''trimTrail "abc " == "abc"''
''trimTrail " abc " == " abc"''
];
__functor = _: trimTrailChars ws;
};
trim = {
description = ''
Trim leading and trailing whitespace.
'';
type = "String -> String";
examples = [
''trim " abc " == "abc"''
];
__functor = _: s: trimTrailChars ws (trimLeadChars ws s);
};
optionalSuffix = {
description = ''
Add a suffix to a string if it doesn't exist.
'';
type = "String -> String -> String";
examples = [
''optionalSuffix ".desktop" "app" == "app.desktop"''
''optionalSuffix ".desktop" "app.desktop" == "app.desktop"''
];
__functor = _: x: s: s + optionalString (!(hasSuffix x s)) x;
};
optionalPrefix = {
description = ''
Add a prefix to a string if it doesn't exist.
'';
type = "String -> String -> String";
examples = [
''optionalPrefix "app" ".desktop" == "app.desktop"''
''optionalPrefix "app" "app.desktop" == "app.desktop"''
];
__functor = _: x: s: (optionalString (!(hasPrefix x s)) x) + s;
};
isUpper = {
description = ''
Determine whether a string is uppercase.
'';
type = "String -> Bool";
examples = [
''isUpper "A" == true''
''isUpper "a" == false''
''isUpper "FOO" == true''
''isUpper "Foo" == false''
];
__functor = _: s: let
chars = stringToCharacters s;
in if length chars == 0 then false else foldl' (a: x: a && elem x upperChars) true chars;
};
isLower = {
description = ''
Determine whether a string is lowercase.
'';
type = "String -> Bool";
examples = [
''isLower "a" == true''
''isLower "A" == false''
''isLower "foo" == true''
''isLower "Foo" == false''
];
__functor = _: s: let
chars = stringToCharacters s;
in if length chars == 0 then false else foldl' (a: x: a && elem x lowerChars) true chars;
};
rot13 = {
description = ''
Rot13 a string.
'';
type = "String -> String";
examples = [
''rot13 "the quick brown fox jumps over the lazy dog" == "gur dhvpx oebja sbk whzcf bire gur ynml qbt"''
];
__functor = _: s: stringAsChars (x: applyIf (elem x (upperChars ++ lowerChars)) (let
charset = if isUpper x then upperChars else lowerChars;
cid = findFirstIndex (eq x) null charset;
rid = (ifEl (floatishToInt cid < 13) add sub) cid 13;
in x: elemAt charset (floatishToInt rid)) x) s;
};
}