Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/third_party/closure/goog/string/string.js
4503 views
1
/**
2
* @license
3
* Copyright The Closure Library Authors.
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
7
/**
8
* @fileoverview Utilities for string manipulation.
9
*/
10
11
12
/**
13
* Namespace for string utilities
14
*/
15
goog.provide('goog.string');
16
goog.provide('goog.string.Unicode');
17
18
goog.require('goog.dom.safe');
19
goog.require('goog.html.uncheckedconversions');
20
goog.require('goog.string.Const');
21
goog.require('goog.string.internal');
22
23
goog.require('goog.utils');
24
25
26
/**
27
* @define {boolean} Enables HTML escaping of lowercase letter "e" which helps
28
* with detection of double-escaping as this letter is frequently used.
29
*/
30
goog.string.DETECT_DOUBLE_ESCAPING =
31
goog.define('goog.string.DETECT_DOUBLE_ESCAPING', false);
32
33
34
/**
35
* @define {boolean} Whether to force non-dom html unescaping.
36
*/
37
goog.string.FORCE_NON_DOM_HTML_UNESCAPING =
38
goog.define('goog.string.FORCE_NON_DOM_HTML_UNESCAPING', false);
39
40
41
/**
42
* Common Unicode string characters.
43
* @enum {string}
44
*/
45
goog.string.Unicode = {
46
NBSP: '\xa0',
47
ZERO_WIDTH_SPACE: '\u200b' // This is equivalent to <wbr>.
48
};
49
50
51
/**
52
* Fast prefix-checker.
53
* @param {string} str The string to check.
54
* @param {string} prefix A string to look for at the start of `str`.
55
* @return {boolean} True if `str` begins with `prefix`.
56
*/
57
goog.string.startsWith = goog.string.internal.startsWith;
58
59
60
/**
61
* Fast suffix-checker.
62
* @param {string} str The string to check.
63
* @param {string} suffix A string to look for at the end of `str`.
64
* @return {boolean} True if `str` ends with `suffix`.
65
*/
66
goog.string.endsWith = goog.string.internal.endsWith;
67
68
69
/**
70
* Case-insensitive prefix-checker.
71
* @param {string} str The string to check.
72
* @param {string} prefix A string to look for at the end of `str`.
73
* @return {boolean} True if `str` begins with `prefix` (ignoring
74
* case).
75
*/
76
goog.string.caseInsensitiveStartsWith =
77
goog.string.internal.caseInsensitiveStartsWith;
78
79
80
/**
81
* Case-insensitive suffix-checker.
82
* @param {string} str The string to check.
83
* @param {string} suffix A string to look for at the end of `str`.
84
* @return {boolean} True if `str` ends with `suffix` (ignoring
85
* case).
86
*/
87
goog.string.caseInsensitiveEndsWith =
88
goog.string.internal.caseInsensitiveEndsWith;
89
90
91
/**
92
* Case-insensitive equality checker.
93
* @param {string} str1 First string to check.
94
* @param {string} str2 Second string to check.
95
* @return {boolean} True if `str1` and `str2` are the same string,
96
* ignoring case.
97
*/
98
goog.string.caseInsensitiveEquals = goog.string.internal.caseInsensitiveEquals;
99
100
101
/**
102
* Does simple python-style string substitution.
103
* subs("foo%s hot%s", "bar", "dog") becomes "foobar hotdog".
104
* @param {string} str The string containing the pattern.
105
* @param {...*} var_args The items to substitute into the pattern.
106
* @return {string} A copy of `str` in which each occurrence of
107
* {@code %s} has been replaced an argument from `var_args`.
108
*/
109
goog.string.subs = function(str, var_args) {
110
'use strict';
111
const splitParts = str.split('%s');
112
let returnString = '';
113
114
const subsArguments = Array.prototype.slice.call(arguments, 1);
115
while (subsArguments.length &&
116
// Replace up to the last split part. We are inserting in the
117
// positions between split parts.
118
splitParts.length > 1) {
119
returnString += splitParts.shift() + subsArguments.shift();
120
}
121
122
return returnString + splitParts.join('%s'); // Join unused '%s'
123
};
124
125
126
/**
127
* Converts multiple whitespace chars (spaces, non-breaking-spaces, new lines
128
* and tabs) to a single space, and strips leading and trailing whitespace.
129
* @param {string} str Input string.
130
* @return {string} A copy of `str` with collapsed whitespace.
131
*/
132
goog.string.collapseWhitespace = function(str) {
133
'use strict';
134
// Since IE doesn't include non-breaking-space (0xa0) in their \s character
135
// class (as required by section 7.2 of the ECMAScript spec), we explicitly
136
// include it in the regexp to enforce consistent cross-browser behavior.
137
return str.replace(/[\s\xa0]+/g, ' ').replace(/^\s+|\s+$/g, '');
138
};
139
140
141
/**
142
* Checks if a string is empty or contains only whitespaces.
143
* @param {string} str The string to check.
144
* @return {boolean} Whether `str` is empty or whitespace only.
145
*/
146
goog.string.isEmptyOrWhitespace = goog.string.internal.isEmptyOrWhitespace;
147
148
149
/**
150
* Checks if a string is empty.
151
* @param {string} str The string to check.
152
* @return {boolean} Whether `str` is empty.
153
*/
154
goog.string.isEmptyString = function(str) {
155
'use strict';
156
return str.length == 0;
157
};
158
159
160
/**
161
* Checks if a string is empty or contains only whitespaces.
162
*
163
* @param {string} str The string to check.
164
* @return {boolean} Whether `str` is empty or whitespace only.
165
* @deprecated Use goog.string.isEmptyOrWhitespace instead.
166
*/
167
goog.string.isEmpty = goog.string.isEmptyOrWhitespace;
168
169
170
/**
171
* Checks if a string is null, undefined, empty or contains only whitespaces.
172
* @param {*} str The string to check.
173
* @return {boolean} Whether `str` is null, undefined, empty, or
174
* whitespace only.
175
* @deprecated Use goog.string.isEmptyOrWhitespace(goog.string.makeSafe(str))
176
* instead.
177
*/
178
goog.string.isEmptyOrWhitespaceSafe = function(str) {
179
'use strict';
180
return goog.string.isEmptyOrWhitespace(goog.string.makeSafe(str));
181
};
182
183
184
/**
185
* Checks if a string is null, undefined, empty or contains only whitespaces.
186
*
187
* @param {*} str The string to check.
188
* @return {boolean} Whether `str` is null, undefined, empty, or
189
* whitespace only.
190
* @deprecated Use goog.string.isEmptyOrWhitespace instead.
191
*/
192
goog.string.isEmptySafe = goog.string.isEmptyOrWhitespaceSafe;
193
194
195
/**
196
* Checks if a string is all breaking whitespace.
197
* @param {string} str The string to check.
198
* @return {boolean} Whether the string is all breaking whitespace.
199
*/
200
goog.string.isBreakingWhitespace = function(str) {
201
'use strict';
202
return !/[^\t\n\r ]/.test(str);
203
};
204
205
206
/**
207
* Checks if a string contains all letters.
208
* @param {string} str string to check.
209
* @return {boolean} True if `str` consists entirely of letters.
210
*/
211
goog.string.isAlpha = function(str) {
212
'use strict';
213
return !/[^a-zA-Z]/.test(str);
214
};
215
216
217
/**
218
* Checks if a string contains only numbers.
219
* @param {*} str string to check. If not a string, it will be
220
* casted to one.
221
* @return {boolean} True if `str` is numeric.
222
*/
223
goog.string.isNumeric = function(str) {
224
'use strict';
225
return !/[^0-9]/.test(str);
226
};
227
228
229
/**
230
* Checks if a string contains only numbers or letters.
231
* @param {string} str string to check.
232
* @return {boolean} True if `str` is alphanumeric.
233
*/
234
goog.string.isAlphaNumeric = function(str) {
235
'use strict';
236
return !/[^a-zA-Z0-9]/.test(str);
237
};
238
239
240
/**
241
* Checks if a character is a space character.
242
* @param {string} ch Character to check.
243
* @return {boolean} True if `ch` is a space.
244
*/
245
goog.string.isSpace = function(ch) {
246
'use strict';
247
return ch == ' ';
248
};
249
250
251
/**
252
* Checks if a character is a valid unicode character.
253
* @param {string} ch Character to check.
254
* @return {boolean} True if `ch` is a valid unicode character.
255
*/
256
goog.string.isUnicodeChar = function(ch) {
257
'use strict';
258
return ch.length == 1 && ch >= ' ' && ch <= '~' ||
259
ch >= '\u0080' && ch <= '\uFFFD';
260
};
261
262
263
/**
264
* Takes a string and replaces newlines with a space. Multiple lines are
265
* replaced with a single space.
266
* @param {string} str The string from which to strip newlines.
267
* @return {string} A copy of `str` stripped of newlines.
268
*/
269
goog.string.stripNewlines = function(str) {
270
'use strict';
271
return str.replace(/(\r\n|\r|\n)+/g, ' ');
272
};
273
274
275
/**
276
* Replaces Windows and Mac new lines with unix style: \r or \r\n with \n.
277
* @param {string} str The string to in which to canonicalize newlines.
278
* @return {string} `str` A copy of {@code} with canonicalized newlines.
279
*/
280
goog.string.canonicalizeNewlines = function(str) {
281
'use strict';
282
return str.replace(/(\r\n|\r|\n)/g, '\n');
283
};
284
285
286
/**
287
* Normalizes whitespace in a string, replacing all whitespace chars with
288
* a space.
289
* @param {string} str The string in which to normalize whitespace.
290
* @return {string} A copy of `str` with all whitespace normalized.
291
*/
292
goog.string.normalizeWhitespace = function(str) {
293
'use strict';
294
return str.replace(/\xa0|\s/g, ' ');
295
};
296
297
298
/**
299
* Normalizes spaces in a string, replacing all consecutive spaces and tabs
300
* with a single space. Replaces non-breaking space with a space.
301
* @param {string} str The string in which to normalize spaces.
302
* @return {string} A copy of `str` with all consecutive spaces and tabs
303
* replaced with a single space.
304
*/
305
goog.string.normalizeSpaces = function(str) {
306
'use strict';
307
return str.replace(/\xa0|[ \t]+/g, ' ');
308
};
309
310
311
/**
312
* Removes the breaking spaces from the left and right of the string and
313
* collapses the sequences of breaking spaces in the middle into single spaces.
314
* The original and the result strings render the same way in HTML.
315
* @param {string} str A string in which to collapse spaces.
316
* @return {string} Copy of the string with normalized breaking spaces.
317
*/
318
goog.string.collapseBreakingSpaces = function(str) {
319
'use strict';
320
return str.replace(/[\t\r\n ]+/g, ' ')
321
.replace(/^[\t\r\n ]+|[\t\r\n ]+$/g, '');
322
};
323
324
325
/**
326
* Trims white spaces to the left and right of a string.
327
* @param {string} str The string to trim.
328
* @return {string} A trimmed copy of `str`.
329
*/
330
goog.string.trim = goog.string.internal.trim;
331
332
333
/**
334
* Trims whitespaces at the left end of a string.
335
* @param {string} str The string to left trim.
336
* @return {string} A trimmed copy of `str`.
337
*/
338
goog.string.trimLeft = function(str) {
339
'use strict';
340
// Since IE doesn't include non-breaking-space (0xa0) in their \s character
341
// class (as required by section 7.2 of the ECMAScript spec), we explicitly
342
// include it in the regexp to enforce consistent cross-browser behavior.
343
return str.replace(/^[\s\xa0]+/, '');
344
};
345
346
347
/**
348
* Trims whitespaces at the right end of a string.
349
* @param {string} str The string to right trim.
350
* @return {string} A trimmed copy of `str`.
351
*/
352
goog.string.trimRight = function(str) {
353
'use strict';
354
// Since IE doesn't include non-breaking-space (0xa0) in their \s character
355
// class (as required by section 7.2 of the ECMAScript spec), we explicitly
356
// include it in the regexp to enforce consistent cross-browser behavior.
357
return str.replace(/[\s\xa0]+$/, '');
358
};
359
360
361
/**
362
* A string comparator that ignores case.
363
* -1 = str1 less than str2
364
* 0 = str1 equals str2
365
* 1 = str1 greater than str2
366
*
367
* @param {string} str1 The string to compare.
368
* @param {string} str2 The string to compare `str1` to.
369
* @return {number} The comparator result, as described above.
370
*/
371
goog.string.caseInsensitiveCompare =
372
goog.string.internal.caseInsensitiveCompare;
373
374
375
/**
376
* Compares two strings interpreting their numeric substrings as numbers.
377
*
378
* @param {string} str1 First string.
379
* @param {string} str2 Second string.
380
* @param {!RegExp} tokenizerRegExp Splits a string into substrings of
381
* non-negative integers, non-numeric characters and optionally fractional
382
* numbers starting with a decimal point.
383
* @return {number} Negative if str1 < str2, 0 is str1 == str2, positive if
384
* str1 > str2.
385
* @private
386
*/
387
goog.string.numberAwareCompare_ = function(str1, str2, tokenizerRegExp) {
388
'use strict';
389
if (str1 == str2) {
390
return 0;
391
}
392
if (!str1) {
393
return -1;
394
}
395
if (!str2) {
396
return 1;
397
}
398
399
// Using match to split the entire string ahead of time turns out to be faster
400
// for most inputs than using RegExp.exec or iterating over each character.
401
const tokens1 = str1.toLowerCase().match(tokenizerRegExp);
402
const tokens2 = str2.toLowerCase().match(tokenizerRegExp);
403
404
const count = Math.min(tokens1.length, tokens2.length);
405
406
for (let i = 0; i < count; i++) {
407
const a = tokens1[i];
408
const b = tokens2[i];
409
410
// Compare pairs of tokens, returning if one token sorts before the other.
411
if (a != b) {
412
// Only if both tokens are integers is a special comparison required.
413
// Decimal numbers are sorted as strings (e.g., '.09' < '.1').
414
const num1 = parseInt(a, 10);
415
if (!isNaN(num1)) {
416
const num2 = parseInt(b, 10);
417
if (!isNaN(num2) && num1 - num2) {
418
return num1 - num2;
419
}
420
}
421
return a < b ? -1 : 1;
422
}
423
}
424
425
// If one string is a substring of the other, the shorter string sorts first.
426
if (tokens1.length != tokens2.length) {
427
return tokens1.length - tokens2.length;
428
}
429
430
// The two strings must be equivalent except for case (perfect equality is
431
// tested at the head of the function.) Revert to default ASCII string
432
// comparison to stabilize the sort.
433
return str1 < str2 ? -1 : 1;
434
};
435
436
437
/**
438
* String comparison function that handles non-negative integer numbers in a
439
* way humans might expect. Using this function, the string 'File 2.jpg' sorts
440
* before 'File 10.jpg', and 'Version 1.9' before 'Version 1.10'. The comparison
441
* is mostly case-insensitive, though strings that are identical except for case
442
* are sorted with the upper-case strings before lower-case.
443
*
444
* This comparison function is up to 50x slower than either the default or the
445
* case-insensitive compare. It should not be used in time-critical code, but
446
* should be fast enough to sort several hundred short strings (like filenames)
447
* with a reasonable delay.
448
*
449
* @param {string} str1 The string to compare in a numerically sensitive way.
450
* @param {string} str2 The string to compare `str1` to.
451
* @return {number} less than 0 if str1 < str2, 0 if str1 == str2, greater than
452
* 0 if str1 > str2.
453
*/
454
goog.string.intAwareCompare = function(str1, str2) {
455
'use strict';
456
return goog.string.numberAwareCompare_(str1, str2, /\d+|\D+/g);
457
};
458
459
460
/**
461
* String comparison function that handles non-negative integer and fractional
462
* numbers in a way humans might expect. Using this function, the string
463
* 'File 2.jpg' sorts before 'File 10.jpg', and '3.14' before '3.2'. Equivalent
464
* to {@link goog.string.intAwareCompare} apart from the way how it interprets
465
* dots.
466
*
467
* @param {string} str1 The string to compare in a numerically sensitive way.
468
* @param {string} str2 The string to compare `str1` to.
469
* @return {number} less than 0 if str1 < str2, 0 if str1 == str2, greater than
470
* 0 if str1 > str2.
471
*/
472
goog.string.floatAwareCompare = function(str1, str2) {
473
'use strict';
474
return goog.string.numberAwareCompare_(str1, str2, /\d+|\.\d+|\D+/g);
475
};
476
477
478
/**
479
* Alias for {@link goog.string.floatAwareCompare}.
480
*
481
* @param {string} str1
482
* @param {string} str2
483
* @return {number}
484
*/
485
goog.string.numerateCompare = goog.string.floatAwareCompare;
486
487
488
/**
489
* URL-encodes a string
490
* @param {*} str The string to url-encode.
491
* @return {string} An encoded copy of `str` that is safe for urls.
492
* Note that '#', ':', and other characters used to delimit portions
493
* of URLs *will* be encoded.
494
*/
495
goog.string.urlEncode = function(str) {
496
'use strict';
497
return encodeURIComponent(String(str));
498
};
499
500
501
/**
502
* URL-decodes the string. We need to specially handle '+'s because
503
* the javascript library doesn't convert them to spaces.
504
* @param {string} str The string to url decode.
505
* @return {string} The decoded `str`.
506
*/
507
goog.string.urlDecode = function(str) {
508
'use strict';
509
return decodeURIComponent(str.replace(/\+/g, ' '));
510
};
511
512
513
/**
514
* Converts \n to <br>s or <br />s.
515
* @param {string} str The string in which to convert newlines.
516
* @param {boolean=} opt_xml Whether to use XML compatible tags.
517
* @return {string} A copy of `str` with converted newlines.
518
*/
519
goog.string.newLineToBr = goog.string.internal.newLineToBr;
520
521
522
/**
523
* Escapes double quote '"' and single quote '\'' characters in addition to
524
* '&', '<', and '>' so that a string can be included in an HTML tag attribute
525
* value within double or single quotes.
526
*
527
* It should be noted that > doesn't need to be escaped for the HTML or XML to
528
* be valid, but it has been decided to escape it for consistency with other
529
* implementations.
530
*
531
* With goog.string.DETECT_DOUBLE_ESCAPING, this function escapes also the
532
* lowercase letter "e".
533
*
534
* NOTE(user):
535
* HtmlEscape is often called during the generation of large blocks of HTML.
536
* Using statics for the regular expressions and strings is an optimization
537
* that can more than half the amount of time IE spends in this function for
538
* large apps, since strings and regexes both contribute to GC allocations.
539
*
540
* Testing for the presence of a character before escaping increases the number
541
* of function calls, but actually provides a speed increase for the average
542
* case -- since the average case often doesn't require the escaping of all 4
543
* characters and indexOf() is much cheaper than replace().
544
* The worst case does suffer slightly from the additional calls, therefore the
545
* opt_isLikelyToContainHtmlChars option has been included for situations
546
* where all 4 HTML entities are very likely to be present and need escaping.
547
*
548
* Some benchmarks (times tended to fluctuate +-0.05ms):
549
* FireFox IE6
550
* (no chars / average (mix of cases) / all 4 chars)
551
* no checks 0.13 / 0.22 / 0.22 0.23 / 0.53 / 0.80
552
* indexOf 0.08 / 0.17 / 0.26 0.22 / 0.54 / 0.84
553
* indexOf + re test 0.07 / 0.17 / 0.28 0.19 / 0.50 / 0.85
554
*
555
* An additional advantage of checking if replace actually needs to be called
556
* is a reduction in the number of object allocations, so as the size of the
557
* application grows the difference between the various methods would increase.
558
*
559
* @param {string} str string to be escaped.
560
* @param {boolean=} opt_isLikelyToContainHtmlChars Don't perform a check to see
561
* if the character needs replacing - use this option if you expect each of
562
* the characters to appear often. Leave false if you expect few html
563
* characters to occur in your strings, such as if you are escaping HTML.
564
* @return {string} An escaped copy of `str`.
565
*/
566
goog.string.htmlEscape = function(str, opt_isLikelyToContainHtmlChars) {
567
'use strict';
568
str = goog.string.internal.htmlEscape(str, opt_isLikelyToContainHtmlChars);
569
if (goog.string.DETECT_DOUBLE_ESCAPING) {
570
str = str.replace(goog.string.E_RE_, '&#101;');
571
}
572
return str;
573
};
574
575
576
/**
577
* Regular expression that matches a lowercase letter "e", for use in escaping.
578
* @const {!RegExp}
579
* @private
580
*/
581
goog.string.E_RE_ = /e/g;
582
583
584
/**
585
* Unescapes an HTML string.
586
*
587
* @param {string} str The string to unescape.
588
* @return {string} An unescaped copy of `str`.
589
*/
590
goog.string.unescapeEntities = function(str) {
591
'use strict';
592
if (goog.string.contains(str, '&')) {
593
// We are careful not to use a DOM if we do not have one or we explicitly
594
// requested non-DOM html unescaping.
595
if (!goog.string.FORCE_NON_DOM_HTML_UNESCAPING &&
596
'document' in goog.global) {
597
return goog.string.unescapeEntitiesUsingDom_(str);
598
} else {
599
// Fall back on pure XML entities
600
return goog.string.unescapePureXmlEntities_(str);
601
}
602
}
603
return str;
604
};
605
606
607
/**
608
* Unescapes a HTML string using the provided document.
609
*
610
* @param {string} str The string to unescape.
611
* @param {!Document} document A document to use in escaping the string.
612
* @return {string} An unescaped copy of `str`.
613
*/
614
goog.string.unescapeEntitiesWithDocument = function(str, document) {
615
'use strict';
616
if (goog.string.contains(str, '&')) {
617
return goog.string.unescapeEntitiesUsingDom_(str, document);
618
}
619
return str;
620
};
621
622
623
/**
624
* Unescapes an HTML string using a DOM to resolve non-XML, non-numeric
625
* entities. This function is XSS-safe and whitespace-preserving.
626
* @private
627
* @param {string} str The string to unescape.
628
* @param {Document=} opt_document An optional document to use for creating
629
* elements. If this is not specified then the default window.document
630
* will be used.
631
* @return {string} The unescaped `str` string.
632
*/
633
goog.string.unescapeEntitiesUsingDom_ = function(str, opt_document) {
634
'use strict';
635
/** @type {!Object<string, string>} */
636
const seen = {'&amp;': '&', '&lt;': '<', '&gt;': '>', '&quot;': '"'};
637
/** @type {!Element} */
638
let div;
639
if (opt_document) {
640
div = opt_document.createElement('div');
641
} else {
642
div = goog.global.document.createElement('div');
643
}
644
// Match as many valid entity characters as possible. If the actual entity
645
// happens to be shorter, it will still work as innerHTML will return the
646
// trailing characters unchanged. Since the entity characters do not include
647
// open angle bracket, there is no chance of XSS from the innerHTML use.
648
// Since no whitespace is passed to innerHTML, whitespace is preserved.
649
return str.replace(goog.string.HTML_ENTITY_PATTERN_, function(s, entity) {
650
'use strict';
651
// Check for cached entity.
652
let value = seen[s];
653
if (value) {
654
return value;
655
}
656
// Check for numeric entity.
657
if (entity.charAt(0) == '#') {
658
// Prefix with 0 so that hex entities (e.g. &#x10) parse as hex numbers.
659
const n = Number('0' + entity.slice(1));
660
if (!isNaN(n)) {
661
value = String.fromCharCode(n);
662
}
663
}
664
// Fall back to innerHTML otherwise.
665
if (!value) {
666
// Append a non-entity character to avoid a bug in Webkit that parses
667
// an invalid entity at the end of innerHTML text as the empty string.
668
goog.dom.safe.setInnerHtml(
669
div,
670
goog.html.uncheckedconversions
671
.safeHtmlFromStringKnownToSatisfyTypeContract(
672
goog.string.Const.from('Single HTML entity.'), s + ' '));
673
// Then remove the trailing character from the result.
674
value = div.firstChild.nodeValue.slice(0, -1);
675
}
676
// Cache and return.
677
return seen[s] = value;
678
});
679
};
680
681
682
/**
683
* Unescapes XML entities.
684
* @private
685
* @param {string} str The string to unescape.
686
* @return {string} An unescaped copy of `str`.
687
*/
688
goog.string.unescapePureXmlEntities_ = function(str) {
689
'use strict';
690
return str.replace(/&([^;]+);/g, function(s, entity) {
691
'use strict';
692
switch (entity) {
693
case 'amp':
694
return '&';
695
case 'lt':
696
return '<';
697
case 'gt':
698
return '>';
699
case 'quot':
700
return '"';
701
default:
702
if (entity.charAt(0) == '#') {
703
// Prefix with 0 so that hex entities (e.g. &#x10) parse as hex.
704
const n = Number('0' + entity.slice(1));
705
if (!isNaN(n)) {
706
return String.fromCharCode(n);
707
}
708
}
709
// For invalid entities we just return the entity
710
return s;
711
}
712
});
713
};
714
715
716
/**
717
* Regular expression that matches an HTML entity.
718
* See also HTML5: Tokenization / Tokenizing character references.
719
* @private
720
* @type {!RegExp}
721
*/
722
goog.string.HTML_ENTITY_PATTERN_ = /&([^;\s<&]+);?/g;
723
724
725
/**
726
* Do escaping of whitespace to preserve spatial formatting. We use character
727
* entity #160 to make it safer for xml.
728
* @param {string} str The string in which to escape whitespace.
729
* @param {boolean=} opt_xml Whether to use XML compatible tags.
730
* @return {string} An escaped copy of `str`.
731
*/
732
goog.string.whitespaceEscape = function(str, opt_xml) {
733
'use strict';
734
// This doesn't use goog.string.preserveSpaces for backwards compatibility.
735
return goog.string.newLineToBr(str.replace(/ /g, ' &#160;'), opt_xml);
736
};
737
738
739
/**
740
* Preserve spaces that would be otherwise collapsed in HTML by replacing them
741
* with non-breaking space Unicode characters.
742
* @param {string} str The string in which to preserve whitespace.
743
* @return {string} A copy of `str` with preserved whitespace.
744
*/
745
goog.string.preserveSpaces = function(str) {
746
'use strict';
747
return str.replace(/(^|[\n ]) /g, '$1' + goog.string.Unicode.NBSP);
748
};
749
750
751
/**
752
* Strip quote characters around a string. The second argument is a string of
753
* characters to treat as quotes. This can be a single character or a string of
754
* multiple character and in that case each of those are treated as possible
755
* quote characters. For example:
756
*
757
* <pre>
758
* goog.string.stripQuotes('"abc"', '"`') --> 'abc'
759
* goog.string.stripQuotes('`abc`', '"`') --> 'abc'
760
* </pre>
761
*
762
* @param {string} str The string to strip.
763
* @param {string} quoteChars The quote characters to strip.
764
* @return {string} A copy of `str` without the quotes.
765
*/
766
goog.string.stripQuotes = function(str, quoteChars) {
767
'use strict';
768
const length = quoteChars.length;
769
for (let i = 0; i < length; i++) {
770
const quoteChar = length == 1 ? quoteChars : quoteChars.charAt(i);
771
if (str.charAt(0) == quoteChar && str.charAt(str.length - 1) == quoteChar) {
772
return str.substring(1, str.length - 1);
773
}
774
}
775
return str;
776
};
777
778
779
/**
780
* Truncates a string to a certain length and adds '...' if necessary. The
781
* length also accounts for the ellipsis, so a maximum length of 10 and a string
782
* 'Hello World!' produces 'Hello W...'.
783
* @param {string} str The string to truncate.
784
* @param {number} chars Max number of characters.
785
* @param {boolean=} opt_protectEscapedCharacters Whether to protect escaped
786
* characters from being cut off in the middle.
787
* @return {string} The truncated `str` string.
788
*/
789
goog.string.truncate = function(str, chars, opt_protectEscapedCharacters) {
790
'use strict';
791
if (opt_protectEscapedCharacters) {
792
str = goog.string.unescapeEntities(str);
793
}
794
795
if (str.length > chars) {
796
str = str.substring(0, chars - 3) + '...';
797
}
798
799
if (opt_protectEscapedCharacters) {
800
str = goog.string.htmlEscape(str);
801
}
802
803
return str;
804
};
805
806
807
/**
808
* Truncate a string in the middle, adding "..." if necessary,
809
* and favoring the beginning of the string.
810
* @param {string} str The string to truncate the middle of.
811
* @param {number} chars Max number of characters.
812
* @param {boolean=} opt_protectEscapedCharacters Whether to protect escaped
813
* characters from being cutoff in the middle.
814
* @param {number=} opt_trailingChars Optional number of trailing characters to
815
* leave at the end of the string, instead of truncating as close to the
816
* middle as possible.
817
* @return {string} A truncated copy of `str`.
818
*/
819
goog.string.truncateMiddle = function(
820
str, chars, opt_protectEscapedCharacters, opt_trailingChars) {
821
'use strict';
822
if (opt_protectEscapedCharacters) {
823
str = goog.string.unescapeEntities(str);
824
}
825
826
if (opt_trailingChars && str.length > chars) {
827
if (opt_trailingChars > chars) {
828
opt_trailingChars = chars;
829
}
830
const endPoint = str.length - opt_trailingChars;
831
const startPoint = chars - opt_trailingChars;
832
str = str.substring(0, startPoint) + '...' + str.substring(endPoint);
833
} else if (str.length > chars) {
834
// Favor the beginning of the string:
835
let half = Math.floor(chars / 2);
836
const endPos = str.length - half;
837
half += chars % 2;
838
str = str.substring(0, half) + '...' + str.substring(endPos);
839
}
840
841
if (opt_protectEscapedCharacters) {
842
str = goog.string.htmlEscape(str);
843
}
844
845
return str;
846
};
847
848
849
/**
850
* Special chars that need to be escaped for goog.string.quote.
851
* @private {!Object<string, string>}
852
*/
853
goog.string.specialEscapeChars_ = {
854
'\0': '\\0',
855
'\b': '\\b',
856
'\f': '\\f',
857
'\n': '\\n',
858
'\r': '\\r',
859
'\t': '\\t',
860
'\x0B': '\\x0B', // '\v' is not supported in JScript
861
'"': '\\"',
862
'\\': '\\\\',
863
// To support the use case of embedding quoted strings inside of script
864
// tags, we have to make sure HTML comments and opening/closing script tags do
865
// not appear in the resulting string. The specific strings that must be
866
// escaped are documented at:
867
// https://html.spec.whatwg.org/multipage/scripting.html#restrictions-for-contents-of-script-elements
868
'<': '\\u003C' // NOTE: JSON.parse crashes on '\\x3c'.
869
};
870
871
872
/**
873
* Character mappings used internally for goog.string.escapeChar.
874
* @private {!Object<string, string>}
875
*/
876
goog.string.jsEscapeCache_ = {
877
'\'': '\\\''
878
};
879
880
881
/**
882
* Encloses a string in double quotes and escapes characters so that the
883
* string is a valid JS string. The resulting string is safe to embed in
884
* `<script>` tags as "<" is escaped.
885
* @param {string} s The string to quote.
886
* @return {string} A copy of `s` surrounded by double quotes.
887
*/
888
goog.string.quote = function(s) {
889
'use strict';
890
s = String(s);
891
const sb = ['"'];
892
for (let i = 0; i < s.length; i++) {
893
const ch = s.charAt(i);
894
const cc = ch.charCodeAt(0);
895
sb[i + 1] = goog.string.specialEscapeChars_[ch] ||
896
((cc > 31 && cc < 127) ? ch : goog.string.escapeChar(ch));
897
}
898
sb.push('"');
899
return sb.join('');
900
};
901
902
903
/**
904
* Takes a string and returns the escaped string for that input string.
905
* @param {string} str The string to escape.
906
* @return {string} An escaped string representing `str`.
907
*/
908
goog.string.escapeString = function(str) {
909
'use strict';
910
const sb = [];
911
for (let i = 0; i < str.length; i++) {
912
sb[i] = goog.string.escapeChar(str.charAt(i));
913
}
914
return sb.join('');
915
};
916
917
918
/**
919
* Takes a character and returns the escaped string for that character. For
920
* example escapeChar(String.fromCharCode(15)) -> "\\x0E".
921
* @param {string} c The character to escape.
922
* @return {string} An escaped string representing `c`.
923
*/
924
goog.string.escapeChar = function(c) {
925
'use strict';
926
if (c in goog.string.jsEscapeCache_) {
927
return goog.string.jsEscapeCache_[c];
928
}
929
930
if (c in goog.string.specialEscapeChars_) {
931
return goog.string.jsEscapeCache_[c] = goog.string.specialEscapeChars_[c];
932
}
933
934
let rv = c;
935
const cc = c.charCodeAt(0);
936
if (cc > 31 && cc < 127) {
937
rv = c;
938
} else {
939
// tab is 9 but handled above
940
if (cc < 256) {
941
rv = '\\x';
942
if (cc < 16 || cc > 256) {
943
rv += '0';
944
}
945
} else {
946
rv = '\\u';
947
if (cc < 4096) { // \u1000
948
rv += '0';
949
}
950
}
951
rv += cc.toString(16).toUpperCase();
952
}
953
954
return goog.string.jsEscapeCache_[c] = rv;
955
};
956
957
958
/**
959
* Determines whether a string contains a substring.
960
* @param {string} str The string to search.
961
* @param {string} subString The substring to search for.
962
* @return {boolean} Whether `str` contains `subString`.
963
*/
964
goog.string.contains = goog.string.internal.contains;
965
966
967
/**
968
* Determines whether a string contains a substring, ignoring case.
969
* @param {string} str The string to search.
970
* @param {string} subString The substring to search for.
971
* @return {boolean} Whether `str` contains `subString`.
972
*/
973
goog.string.caseInsensitiveContains =
974
goog.string.internal.caseInsensitiveContains;
975
976
977
/**
978
* Returns the non-overlapping occurrences of ss in s.
979
* If either s or ss evalutes to false, then returns zero.
980
* @param {string} s The string to look in.
981
* @param {string} ss The string to look for.
982
* @return {number} Number of occurrences of ss in s.
983
*/
984
goog.string.countOf = function(s, ss) {
985
'use strict';
986
return s && ss ? s.split(ss).length - 1 : 0;
987
};
988
989
990
/**
991
* Removes a substring of a specified length at a specific
992
* index in a string.
993
* @param {string} s The base string from which to remove.
994
* @param {number} index The index at which to remove the substring.
995
* @param {number} stringLength The length of the substring to remove.
996
* @return {string} A copy of `s` with the substring removed or the full
997
* string if nothing is removed or the input is invalid.
998
*/
999
goog.string.removeAt = function(s, index, stringLength) {
1000
'use strict';
1001
let resultStr = s;
1002
// If the index is greater or equal to 0 then remove substring
1003
if (index >= 0 && index < s.length && stringLength > 0) {
1004
resultStr = s.slice(0, index) + s.slice(index + stringLength);
1005
}
1006
return resultStr;
1007
};
1008
1009
1010
/**
1011
* Removes the first occurrence of a substring from a string.
1012
* @param {string} str The base string from which to remove.
1013
* @param {string} substr The string to remove.
1014
* @return {string} A copy of `str` with `substr` removed or the
1015
* full string if nothing is removed.
1016
*/
1017
goog.string.remove = function(str, substr) {
1018
'use strict';
1019
return str.replace(substr, '');
1020
};
1021
1022
1023
/**
1024
* Removes all occurrences of a substring from a string.
1025
* @param {string} s The base string from which to remove.
1026
* @param {string} ss The string to remove.
1027
* @return {string} A copy of `s` with `ss` removed or the full
1028
* string if nothing is removed.
1029
*/
1030
goog.string.removeAll = function(s, ss) {
1031
'use strict';
1032
const re = new RegExp(goog.string.regExpEscape(ss), 'g');
1033
return s.replace(re, '');
1034
};
1035
1036
1037
/**
1038
* Replaces all occurrences of a substring of a string with a new substring.
1039
* @param {string} s The base string from which to remove.
1040
* @param {string} ss The string to replace.
1041
* @param {string} replacement The replacement string.
1042
* @return {string} A copy of `s` with `ss` replaced by
1043
* `replacement` or the original string if nothing is replaced.
1044
*/
1045
goog.string.replaceAll = function(s, ss, replacement) {
1046
'use strict';
1047
const re = new RegExp(goog.string.regExpEscape(ss), 'g');
1048
return s.replace(re, replacement.replace(/\$/g, '$$$$'));
1049
};
1050
1051
1052
/**
1053
* Escapes characters in the string that are not safe to use in a RegExp.
1054
* @param {*} s The string to escape. If not a string, it will be casted
1055
* to one.
1056
* @return {string} A RegExp safe, escaped copy of `s`.
1057
*/
1058
goog.string.regExpEscape = function(s) {
1059
'use strict';
1060
return String(s)
1061
.replace(/([-()\[\]{}+?*.$\^|,:#<!\\])/g, '\\$1')
1062
.replace(/\x08/g, '\\x08');
1063
};
1064
1065
1066
/**
1067
* Repeats a string n times.
1068
* @param {string} string The string to repeat.
1069
* @param {number} length The number of times to repeat.
1070
* @return {string} A string containing `length` repetitions of
1071
* `string`.
1072
*/
1073
goog.string.repeat = (String.prototype.repeat) ? function(string, length) {
1074
'use strict';
1075
// The native method is over 100 times faster than the alternative.
1076
return string.repeat(length);
1077
} : function(string, length) {
1078
'use strict';
1079
return new Array(length + 1).join(string);
1080
};
1081
1082
1083
/**
1084
* Pads number to given length and optionally rounds it to a given precision.
1085
* For example:
1086
* <pre>padNumber(1.25, 2, 3) -> '01.250'
1087
* padNumber(1.25, 2) -> '01.25'
1088
* padNumber(1.25, 2, 1) -> '01.3'
1089
* padNumber(1.25, 0) -> '1.25'</pre>
1090
*
1091
* @param {number} num The number to pad.
1092
* @param {number} length The desired length.
1093
* @param {number=} opt_precision The desired precision.
1094
* @return {string} `num` as a string with the given options.
1095
*/
1096
goog.string.padNumber = function(num, length, opt_precision) {
1097
'use strict';
1098
if (!Number.isFinite(num)) return String(num);
1099
let s =
1100
(opt_precision !== undefined) ? num.toFixed(opt_precision) : String(num);
1101
let index = s.indexOf('.');
1102
if (index === -1) {
1103
index = s.length;
1104
}
1105
const sign = s[0] === '-' ? '-' : '';
1106
if (sign) {
1107
s = s.substring(1);
1108
}
1109
return sign + goog.string.repeat('0', Math.max(0, length - index)) + s;
1110
};
1111
1112
1113
/**
1114
* Returns a string representation of the given object, with
1115
* null and undefined being returned as the empty string.
1116
*
1117
* @param {*} obj The object to convert.
1118
* @return {string} A string representation of the `obj`.
1119
*/
1120
goog.string.makeSafe = function(obj) {
1121
'use strict';
1122
return obj == null ? '' : String(obj);
1123
};
1124
1125
/**
1126
* Returns a string with at least 64-bits of randomness.
1127
*
1128
* Doesn't trust JavaScript's random function entirely. Uses a combination of
1129
* random and current timestamp, and then encodes the string in base-36 to
1130
* make it shorter.
1131
*
1132
* @return {string} A random string, e.g. sn1s7vb4gcic.
1133
*/
1134
goog.string.getRandomString = function() {
1135
'use strict';
1136
const x = 2147483648;
1137
return Math.floor(Math.random() * x).toString(36) +
1138
Math.abs(Math.floor(Math.random() * x) ^ Date.now()).toString(36);
1139
};
1140
1141
1142
/**
1143
* Compares two version numbers.
1144
*
1145
* @param {string|number} version1 Version of first item.
1146
* @param {string|number} version2 Version of second item.
1147
*
1148
* @return {number} 1 if `version1` is higher.
1149
* 0 if arguments are equal.
1150
* -1 if `version2` is higher.
1151
*/
1152
goog.string.compareVersions = goog.string.internal.compareVersions;
1153
1154
1155
/**
1156
* String hash function similar to java.lang.String.hashCode().
1157
* The hash code for a string is computed as
1158
* s[0] * 31 ^ (n - 1) + s[1] * 31 ^ (n - 2) + ... + s[n - 1],
1159
* where s[i] is the ith character of the string and n is the length of
1160
* the string. We mod the result to make it between 0 (inclusive) and 2^32
1161
* (exclusive).
1162
* @param {string} str A string.
1163
* @return {number} Hash value for `str`, between 0 (inclusive) and 2^32
1164
* (exclusive). The empty string returns 0.
1165
*/
1166
goog.string.hashCode = function(str) {
1167
'use strict';
1168
let result = 0;
1169
for (let i = 0; i < str.length; ++i) {
1170
// Normalize to 4 byte range, 0 ... 2^32.
1171
result = (31 * result + str.charCodeAt(i)) >>> 0;
1172
}
1173
return result;
1174
};
1175
1176
1177
/**
1178
* The most recent unique ID. |0 is equivalent to Math.floor in this case.
1179
* @type {number}
1180
* @private
1181
*/
1182
goog.string.uniqueStringCounter_ = Math.random() * 0x80000000 | 0;
1183
1184
1185
/**
1186
* Generates and returns a string which is unique in the current document.
1187
* This is useful, for example, to create unique IDs for DOM elements.
1188
* @return {string} A unique id.
1189
*/
1190
goog.string.createUniqueString = function() {
1191
'use strict';
1192
return 'goog_' + goog.string.uniqueStringCounter_++;
1193
};
1194
1195
1196
/**
1197
* Converts the supplied string to a number, which may be Infinity or NaN.
1198
* This function strips whitespace: (toNumber(' 123') === 123)
1199
* This function accepts scientific notation: (toNumber('1e1') === 10)
1200
*
1201
* This is better than JavaScript's built-in conversions because, sadly:
1202
* (Number(' ') === 0) and (parseFloat('123a') === 123)
1203
*
1204
* @param {string} str The string to convert.
1205
* @return {number} The number the supplied string represents, or NaN.
1206
*/
1207
goog.string.toNumber = function(str) {
1208
'use strict';
1209
const num = Number(str);
1210
if (num == 0 && goog.string.isEmptyOrWhitespace(str)) {
1211
return NaN;
1212
}
1213
return num;
1214
};
1215
1216
1217
/**
1218
* Returns whether the given string is lower camel case (e.g. "isFooBar").
1219
*
1220
* Note that this assumes the string is entirely letters.
1221
* @see http://en.wikipedia.org/wiki/CamelCase#Variations_and_synonyms
1222
*
1223
* @param {string} str String to test.
1224
* @return {boolean} Whether the string is lower camel case.
1225
*/
1226
goog.string.isLowerCamelCase = function(str) {
1227
'use strict';
1228
return /^[a-z]+([A-Z][a-z]*)*$/.test(str);
1229
};
1230
1231
1232
/**
1233
* Returns whether the given string is upper camel case (e.g. "FooBarBaz").
1234
*
1235
* Note that this assumes the string is entirely letters.
1236
* @see http://en.wikipedia.org/wiki/CamelCase#Variations_and_synonyms
1237
*
1238
* @param {string} str String to test.
1239
* @return {boolean} Whether the string is upper camel case.
1240
*/
1241
goog.string.isUpperCamelCase = function(str) {
1242
'use strict';
1243
return /^([A-Z][a-z]*)+$/.test(str);
1244
};
1245
1246
1247
/**
1248
* Converts a string from selector-case to camelCase (e.g. from
1249
* "multi-part-string" to "multiPartString"), useful for converting
1250
* CSS selectors and HTML dataset keys to their equivalent JS properties.
1251
* @param {string} str The string in selector-case form.
1252
* @return {string} The string in camelCase form.
1253
*/
1254
goog.string.toCamelCase = function(str) {
1255
'use strict';
1256
return String(str).replace(/\-([a-z])/g, function(all, match) {
1257
'use strict';
1258
return match.toUpperCase();
1259
});
1260
};
1261
1262
1263
/**
1264
* Converts a string from camelCase to selector-case (e.g. from
1265
* "multiPartString" to "multi-part-string"), useful for converting JS
1266
* style and dataset properties to equivalent CSS selectors and HTML keys.
1267
* @param {string} str The string in camelCase form.
1268
* @return {string} The string in selector-case form.
1269
*/
1270
goog.string.toSelectorCase = function(str) {
1271
'use strict';
1272
return String(str).replace(/([A-Z])/g, '-$1').toLowerCase();
1273
};
1274
1275
1276
/**
1277
* Converts a string into TitleCase. First character of the string is always
1278
* capitalized in addition to the first letter of every subsequent word.
1279
* Words are delimited by one or more whitespaces by default. Custom delimiters
1280
* can optionally be specified to replace the default, which doesn't preserve
1281
* whitespace delimiters and instead must be explicitly included if needed.
1282
*
1283
* Default delimiter => " ":
1284
* goog.string.toTitleCase('oneTwoThree') => 'OneTwoThree'
1285
* goog.string.toTitleCase('one two three') => 'One Two Three'
1286
* goog.string.toTitleCase(' one two ') => ' One Two '
1287
* goog.string.toTitleCase('one_two_three') => 'One_two_three'
1288
* goog.string.toTitleCase('one-two-three') => 'One-two-three'
1289
*
1290
* Custom delimiter => "_-.":
1291
* goog.string.toTitleCase('oneTwoThree', '_-.') => 'OneTwoThree'
1292
* goog.string.toTitleCase('one two three', '_-.') => 'One two three'
1293
* goog.string.toTitleCase(' one two ', '_-.') => ' one two '
1294
* goog.string.toTitleCase('one_two_three', '_-.') => 'One_Two_Three'
1295
* goog.string.toTitleCase('one-two-three', '_-.') => 'One-Two-Three'
1296
* goog.string.toTitleCase('one...two...three', '_-.') => 'One...Two...Three'
1297
* goog.string.toTitleCase('one. two. three', '_-.') => 'One. two. three'
1298
* goog.string.toTitleCase('one-two.three', '_-.') => 'One-Two.Three'
1299
*
1300
* @param {string} str String value in camelCase form.
1301
* @param {string=} opt_delimiters Custom delimiter character set used to
1302
* distinguish words in the string value. Each character represents a
1303
* single delimiter. When provided, default whitespace delimiter is
1304
* overridden and must be explicitly included if needed.
1305
* @return {string} String value in TitleCase form.
1306
*/
1307
goog.string.toTitleCase = function(str, opt_delimiters) {
1308
'use strict';
1309
let delimiters = (typeof opt_delimiters === 'string') ?
1310
goog.string.regExpEscape(opt_delimiters) :
1311
'\\s';
1312
1313
// For IE8, we need to prevent using an empty character set. Otherwise,
1314
// incorrect matching will occur.
1315
delimiters = delimiters ? '|[' + delimiters + ']+' : '';
1316
1317
const regexp = new RegExp('(^' + delimiters + ')([a-z])', 'g');
1318
return str.replace(regexp, function(all, p1, p2) {
1319
'use strict';
1320
return p1 + p2.toUpperCase();
1321
});
1322
};
1323
1324
1325
/**
1326
* Capitalizes a string, i.e. converts the first letter to uppercase
1327
* and all other letters to lowercase, e.g.:
1328
*
1329
* goog.string.capitalize('one') => 'One'
1330
* goog.string.capitalize('ONE') => 'One'
1331
* goog.string.capitalize('one two') => 'One two'
1332
*
1333
* Note that this function does not trim initial whitespace.
1334
*
1335
* @param {string} str String value to capitalize.
1336
* @return {string} String value with first letter in uppercase.
1337
*/
1338
goog.string.capitalize = function(str) {
1339
'use strict';
1340
return String(str.charAt(0)).toUpperCase() +
1341
String(str.slice(1)).toLowerCase();
1342
};
1343
1344
1345
/**
1346
* Parse a string in decimal or hexidecimal ('0xFFFF') form.
1347
*
1348
* To parse a particular radix, please use parseInt(string, radix) directly. See
1349
* https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/parseInt
1350
*
1351
* This is a wrapper for the built-in parseInt function that will only parse
1352
* numbers as base 10 or base 16. Some JS implementations assume strings
1353
* starting with "0" are intended to be octal. ES3 allowed but discouraged
1354
* this behavior. ES5 forbids it. This function emulates the ES5 behavior.
1355
*
1356
* For more information, see Mozilla JS Reference: http://goo.gl/8RiFj
1357
*
1358
* @param {string|number|null|undefined} value The value to be parsed.
1359
* @return {number} The number, parsed. If the string failed to parse, this
1360
* will be NaN.
1361
*/
1362
goog.string.parseInt = function(value) {
1363
'use strict';
1364
// Force finite numbers to strings.
1365
if (isFinite(value)) {
1366
value = String(value);
1367
}
1368
1369
if (typeof value === 'string') {
1370
// If the string starts with '0x' or '-0x', parse as hex.
1371
return /^\s*-?0x/i.test(value) ? parseInt(value, 16) : parseInt(value, 10);
1372
}
1373
1374
return NaN;
1375
};
1376
1377
1378
/**
1379
* Splits a string on a separator a limited number of times.
1380
*
1381
* This implementation is more similar to Python or Java, where the limit
1382
* parameter specifies the maximum number of splits rather than truncating
1383
* the number of results.
1384
*
1385
* See http://docs.python.org/2/library/stdtypes.html#str.split
1386
* See JavaDoc: http://goo.gl/F2AsY
1387
* See Mozilla reference: http://goo.gl/dZdZs
1388
*
1389
* @param {string} str String to split.
1390
* @param {string} separator The separator.
1391
* @param {number} limit The limit to the number of splits. The resulting array
1392
* will have a maximum length of limit+1. Negative numbers are the same
1393
* as zero.
1394
* @return {!Array<string>} The string, split.
1395
*/
1396
goog.string.splitLimit = function(str, separator, limit) {
1397
'use strict';
1398
const parts = str.split(separator);
1399
const returnVal = [];
1400
1401
// Only continue doing this while we haven't hit the limit and we have
1402
// parts left.
1403
while (limit > 0 && parts.length) {
1404
returnVal.push(parts.shift());
1405
limit--;
1406
}
1407
1408
// If there are remaining parts, append them to the end.
1409
if (parts.length) {
1410
returnVal.push(parts.join(separator));
1411
}
1412
1413
return returnVal;
1414
};
1415
1416
1417
/**
1418
* Finds the characters to the right of the last instance of any separator
1419
*
1420
* This function is similar to goog.string.path.baseName, except it can take a
1421
* list of characters to split the string on. It will return the rightmost
1422
* grouping of characters to the right of any separator as a left-to-right
1423
* oriented string.
1424
*
1425
* @see goog.string.path.baseName
1426
* @param {string} str The string
1427
* @param {string|!Array<string>} separators A list of separator characters
1428
* @return {string} The last part of the string with respect to the separators
1429
*/
1430
goog.string.lastComponent = function(str, separators) {
1431
'use strict';
1432
if (!separators) {
1433
return str;
1434
} else if (typeof separators == 'string') {
1435
separators = [separators];
1436
}
1437
1438
let lastSeparatorIndex = -1;
1439
for (let i = 0; i < separators.length; i++) {
1440
if (separators[i] == '') {
1441
continue;
1442
}
1443
const currentSeparatorIndex = str.lastIndexOf(separators[i]);
1444
if (currentSeparatorIndex > lastSeparatorIndex) {
1445
lastSeparatorIndex = currentSeparatorIndex;
1446
}
1447
}
1448
if (lastSeparatorIndex == -1) {
1449
return str;
1450
}
1451
return str.slice(lastSeparatorIndex + 1);
1452
};
1453
1454
1455
/**
1456
* Computes the Levenshtein edit distance between two strings.
1457
* @param {string} a
1458
* @param {string} b
1459
* @return {number} The edit distance between the two strings.
1460
*/
1461
goog.string.editDistance = function(a, b) {
1462
'use strict';
1463
const v0 = [];
1464
const v1 = [];
1465
1466
if (a == b) {
1467
return 0;
1468
}
1469
1470
if (!a.length || !b.length) {
1471
return Math.max(a.length, b.length);
1472
}
1473
1474
for (let i = 0; i < b.length + 1; i++) {
1475
v0[i] = i;
1476
}
1477
1478
for (let i = 0; i < a.length; i++) {
1479
v1[0] = i + 1;
1480
1481
for (let j = 0; j < b.length; j++) {
1482
const cost = Number(a[i] != b[j]);
1483
// Cost for the substring is the minimum of adding one character, removing
1484
// one character, or a swap.
1485
v1[j + 1] = Math.min(v1[j] + 1, v0[j + 1] + 1, v0[j] + cost);
1486
}
1487
1488
for (let j = 0; j < v0.length; j++) {
1489
v0[j] = v1[j];
1490
}
1491
}
1492
1493
return v1[b.length];
1494
};
1495
1496