-
Notifications
You must be signed in to change notification settings - Fork 133
/
strings.dart
298 lines (275 loc) · 9.29 KB
/
strings.dart
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Copyright 2013 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
library quiver.strings;
/**
* Returns [true] if [s] is either null, empty or is solely made of whitespace
* characters (as defined by [String.trim]).
*/
bool isBlank(String s) => s == null || s.trim().isEmpty;
/**
* Returns a string with characters from the given [s] in reverse order.
*/
String flip(String s) {
if (s == null || s == '') return s;
StringBuffer sb = new StringBuffer();
var runes = s.runes;
for (int i = runes.length - 1; i >= 0; i--) {
sb.writeCharCode(runes.elementAt(i));
}
return sb.toString();
}
/**
* If [s] is [null] returns empty string, otherwise returns [s] as is.
*/
String nullToEmpty(String s) => s == null ? '' : s;
/**
* If [s] is an empty string returns [null], otherwise returns [s] as is.
*/
String emptyToNull(String s) => s == '' ? null : s;
/**
* Concatenates [s] to itself a given number of [times]. Empty and null
* strings will always result in empty and null strings respectively no matter
* how many [times] they are [repeat]ed.
*
* If [times] is negative, returns the [flip]ped string repeated given number
* of [times].
*/
String repeat(String s, int times) {
if (s == null || s == '') return s;
if (times < 0) {
return repeat(flip(s), -times);
}
StringBuffer sink = new StringBuffer();
_repeat(sink, s, times);
return sink.toString();
}
/**
* Loops over [s] and returns traversed characters. Takes arbitrary [from] and
* [to] indices. Works as a substitute for [String.substring], except it never
* throws [RangeError]. Supports negative indices. Think of an index as a
* coordinate in an infinite in both directions vector filled with repeating
* string [s], whose 0-th coordinate coincides with the 0-th character in [s].
* Then [loop] returns the sub-vector defined by the interval ([from], [to]).
* [from] is inclusive. [to] is exclusive.
*
* This method throws exceptions on [null] and empty strings.
*
* If [to] is omitted or is [null] the traversing ends at the end of the loop.
*
* If [to] < [from], traverses [s] in the opposite direction.
*
* For example:
*
* loop('Hello, World!', 7) == 'World!'
* loop('ab', 0, 6) == 'ababab'
* loop('test.txt', -3) == 'txt'
* loop('ldwor', -3, 2) == 'world'
*/
String loop(String s, int from, [int to]) {
if (s == null || s == '') {
throw new ArgumentError('Input string cannot be null or empty');
}
if (to != null && to < from) {
return loop(flip(s), -from, -to);
}
int len = s.length;
int leftFrag = from >= 0 ? from ~/ len : ((from - len) ~/ len);
if (to == null) {
to = (leftFrag + 1) * len;
}
int rightFrag = to - 1 >= 0 ? to ~/ len : ((to - len) ~/ len);
int fragOffset = rightFrag - leftFrag - 1;
if (fragOffset == -1) {
return s.substring(from - leftFrag * len, to - rightFrag * len);
}
StringBuffer sink = new StringBuffer(s.substring(from - leftFrag * len));
_repeat(sink, s, fragOffset);
sink.write(s.substring(0, to - rightFrag * len));
return sink.toString();
}
void _repeat(StringBuffer sink, String s, int times) {
for (int i = 0; i < times; i++) {
sink.write(s);
}
}
/**
* Returns a [String] of length [width] padded on the left with characters from
* [fill] if `input.length` is less than [width]. [fill] is repeated if
* neccessary to pad.
*
* Returns [input] if `input.length` is equal to or greater than width. [input]
* can be `null` and is treated as an empty string.
*/
@deprecated
String padLeft(String input, int width, String fill) {
if (fill == null || fill.length == 0) {
throw new ArgumentError('fill cannot be null or empty');
}
if (input == null || input.length == 0) return loop(fill, 0, width);
if (input.length >= width) return input;
return loop(fill, 0, width - input.length) + input;
}
/**
* Returns a [String] of length [width] padded on the right with characters from
* [fill] if `input.length` is less than [width]. Characters are selected from
* [fill] starting at the end, so that the last character in [fill] is the last
* character in the result. [fill] is repeated if neccessary to pad.
*
* Returns [input] if `input.length` is equal to or greater than width. [input]
* can be `null` and is treated as an empty string.
*/
@deprecated
String padRight(String input, int width, String fill) {
if (fill == null || fill.length == 0) {
throw new ArgumentError('fill cannot be null or empty');
}
if (input == null || input.length == 0) {
return loop(fill, -width, 0);
}
if (input.length >= width) return input;
return input + loop(fill, input.length - width, 0);
}
/**
* Removes leading whitespace from a string.
*
* Whitespace is defined to be the same as [String.trim].
*/
@deprecated
String trimLeft(String input) {
int i = 0;
for (var rune in input.runes) {
if (isWhitespace(rune)) {
i++;
} else {
break;
}
}
return input.substring(i);
}
/**
* Removes trailing whitespace from a string.
*
* Whitespace is defined to be the same as [String.trim].
*/
@deprecated
String trimRight(String input) {
int i = 0;
int lastNonWhitespace = -1;
for (var rune in input.runes) {
i++;
if (!isWhitespace(rune)) {
lastNonWhitespace = i;
}
}
if (lastNonWhitespace == -1) return '';
return input.substring(0, lastNonWhitespace);
}
/**
* Returns `true` if [rune] represents a whitespace character.
*
* The definition of whitespace matches that used in [String.trim] which is
* based on Unicode 6.2. This maybe be a different set of characters than the
* environment's [RegExp] definition for whitespace, which is given by the
* ECMAScript standard: http://ecma-international.org/ecma-262/5.1/#sec-15.10
*/
bool isWhitespace(int rune) =>
((rune >= 0x0009 && rune <= 0x000D) || rune == 0x0020 || rune == 0x0085 ||
rune == 0x00A0 || rune == 0x1680 || rune == 0x180E ||
(rune >= 0x2000 && rune <= 0x200A) || rune == 0x2028 || rune == 0x2029 ||
rune == 0x202F || rune == 0x205F || rune == 0x3000 || rune == 0xFEFF);
/**
* Returns a [String] of length [width] padded with the same number of
* characters on the left and right from [fill]. On the right, characters are
* selected from [fill] starting at the end so that the last character in [fill]
* is the last character in the result. [fill] is repeated if neccessary to pad.
*
* Returns [input] if `input.length` is equal to or greater than width. [input]
* can be `null` and is treated as an empty string.
*
* If there are an odd number of characters to pad, then the right will be
* padded with one more than the left.
*/
String center(String input, int width, String fill) {
if (fill == null || fill.length == 0) {
throw new ArgumentError('fill cannot be null or empty');
}
if (input == null) input = '';
var leftWidth = input.length + (width - input.length) ~/ 2;
return padRight(
padLeft(input, leftWidth, fill),
width,
fill);
}
/**
* Returns `true` if [a] and [b] are equal after being converted to lower case,
* or are both null.
*/
bool equalsIgnoreCase(String a, String b) =>
(a == null && b == null) ||
(a != null && b != null && a.toLowerCase() == b.toLowerCase());
/**
* Compares [a] and [b] after converting to lower case.
*
* Both [a] and [b] must not be null.
*/
int compareIgnoreCase(String a, String b) =>
a.toLowerCase().compareTo(b.toLowerCase());
/**
* Returns a copy of the [input] where the first (a-z) letter of each word is
* capitalized.
*/
String capitalize(String input) =>
input.replaceAllMapped(new RegExp(r'(^| )(\w)'), (Match m) => m.group(0).toUpperCase());
/**
* Returns a new string where [replacement] is inserted at the [index] of
* the [original] string.
*
* If [overwrite] is given, only the specified number of characters will be
* overwritten. (Default: length of [replacement])
*
* If the [replacement] is longer than [original], the returned string will
* be longer than [original].
*/
String replace(String original, int index, String replacement, {int overwrite}) {
StringBuffer str = new StringBuffer();
if (index > 0) {
str.write(original.substring(0, index));
}
if (overwrite == null) {
overwrite = replacement.length;
}
str.write(replacement);
if (overwrite > 0) {
if (overwrite < original.length) {
str.write(original.substring(index + overwrite));
}
} else {
str.write(original.substring(index));
}
return str.toString();
}
/**
* Removes a specified [length] of the [original] string starting from the
* given [index].
*/
String remove(String original, int index, int length) {
return replace(original, index, '', overwrite: length);
}
/**
* Inserts the value of [insertion] at the given [index] character of
* the [original] string.
*/
String insert(String original, int index, String insertion) {
return replace(original, index, insertion, overwrite: 0);
}