Path: blob/trunk/third_party/closure/goog/string/string.js
4503 views
/**1* @license2* Copyright The Closure Library Authors.3* SPDX-License-Identifier: Apache-2.04*/56/**7* @fileoverview Utilities for string manipulation.8*/91011/**12* Namespace for string utilities13*/14goog.provide('goog.string');15goog.provide('goog.string.Unicode');1617goog.require('goog.dom.safe');18goog.require('goog.html.uncheckedconversions');19goog.require('goog.string.Const');20goog.require('goog.string.internal');2122goog.require('goog.utils');232425/**26* @define {boolean} Enables HTML escaping of lowercase letter "e" which helps27* with detection of double-escaping as this letter is frequently used.28*/29goog.string.DETECT_DOUBLE_ESCAPING =30goog.define('goog.string.DETECT_DOUBLE_ESCAPING', false);313233/**34* @define {boolean} Whether to force non-dom html unescaping.35*/36goog.string.FORCE_NON_DOM_HTML_UNESCAPING =37goog.define('goog.string.FORCE_NON_DOM_HTML_UNESCAPING', false);383940/**41* Common Unicode string characters.42* @enum {string}43*/44goog.string.Unicode = {45NBSP: '\xa0',46ZERO_WIDTH_SPACE: '\u200b' // This is equivalent to <wbr>.47};484950/**51* Fast prefix-checker.52* @param {string} str The string to check.53* @param {string} prefix A string to look for at the start of `str`.54* @return {boolean} True if `str` begins with `prefix`.55*/56goog.string.startsWith = goog.string.internal.startsWith;575859/**60* Fast suffix-checker.61* @param {string} str The string to check.62* @param {string} suffix A string to look for at the end of `str`.63* @return {boolean} True if `str` ends with `suffix`.64*/65goog.string.endsWith = goog.string.internal.endsWith;666768/**69* Case-insensitive prefix-checker.70* @param {string} str The string to check.71* @param {string} prefix A string to look for at the end of `str`.72* @return {boolean} True if `str` begins with `prefix` (ignoring73* case).74*/75goog.string.caseInsensitiveStartsWith =76goog.string.internal.caseInsensitiveStartsWith;777879/**80* Case-insensitive suffix-checker.81* @param {string} str The string to check.82* @param {string} suffix A string to look for at the end of `str`.83* @return {boolean} True if `str` ends with `suffix` (ignoring84* case).85*/86goog.string.caseInsensitiveEndsWith =87goog.string.internal.caseInsensitiveEndsWith;888990/**91* Case-insensitive equality checker.92* @param {string} str1 First string to check.93* @param {string} str2 Second string to check.94* @return {boolean} True if `str1` and `str2` are the same string,95* ignoring case.96*/97goog.string.caseInsensitiveEquals = goog.string.internal.caseInsensitiveEquals;9899100/**101* Does simple python-style string substitution.102* subs("foo%s hot%s", "bar", "dog") becomes "foobar hotdog".103* @param {string} str The string containing the pattern.104* @param {...*} var_args The items to substitute into the pattern.105* @return {string} A copy of `str` in which each occurrence of106* {@code %s} has been replaced an argument from `var_args`.107*/108goog.string.subs = function(str, var_args) {109'use strict';110const splitParts = str.split('%s');111let returnString = '';112113const subsArguments = Array.prototype.slice.call(arguments, 1);114while (subsArguments.length &&115// Replace up to the last split part. We are inserting in the116// positions between split parts.117splitParts.length > 1) {118returnString += splitParts.shift() + subsArguments.shift();119}120121return returnString + splitParts.join('%s'); // Join unused '%s'122};123124125/**126* Converts multiple whitespace chars (spaces, non-breaking-spaces, new lines127* and tabs) to a single space, and strips leading and trailing whitespace.128* @param {string} str Input string.129* @return {string} A copy of `str` with collapsed whitespace.130*/131goog.string.collapseWhitespace = function(str) {132'use strict';133// Since IE doesn't include non-breaking-space (0xa0) in their \s character134// class (as required by section 7.2 of the ECMAScript spec), we explicitly135// include it in the regexp to enforce consistent cross-browser behavior.136return str.replace(/[\s\xa0]+/g, ' ').replace(/^\s+|\s+$/g, '');137};138139140/**141* Checks if a string is empty or contains only whitespaces.142* @param {string} str The string to check.143* @return {boolean} Whether `str` is empty or whitespace only.144*/145goog.string.isEmptyOrWhitespace = goog.string.internal.isEmptyOrWhitespace;146147148/**149* Checks if a string is empty.150* @param {string} str The string to check.151* @return {boolean} Whether `str` is empty.152*/153goog.string.isEmptyString = function(str) {154'use strict';155return str.length == 0;156};157158159/**160* Checks if a string is empty or contains only whitespaces.161*162* @param {string} str The string to check.163* @return {boolean} Whether `str` is empty or whitespace only.164* @deprecated Use goog.string.isEmptyOrWhitespace instead.165*/166goog.string.isEmpty = goog.string.isEmptyOrWhitespace;167168169/**170* Checks if a string is null, undefined, empty or contains only whitespaces.171* @param {*} str The string to check.172* @return {boolean} Whether `str` is null, undefined, empty, or173* whitespace only.174* @deprecated Use goog.string.isEmptyOrWhitespace(goog.string.makeSafe(str))175* instead.176*/177goog.string.isEmptyOrWhitespaceSafe = function(str) {178'use strict';179return goog.string.isEmptyOrWhitespace(goog.string.makeSafe(str));180};181182183/**184* Checks if a string is null, undefined, empty or contains only whitespaces.185*186* @param {*} str The string to check.187* @return {boolean} Whether `str` is null, undefined, empty, or188* whitespace only.189* @deprecated Use goog.string.isEmptyOrWhitespace instead.190*/191goog.string.isEmptySafe = goog.string.isEmptyOrWhitespaceSafe;192193194/**195* Checks if a string is all breaking whitespace.196* @param {string} str The string to check.197* @return {boolean} Whether the string is all breaking whitespace.198*/199goog.string.isBreakingWhitespace = function(str) {200'use strict';201return !/[^\t\n\r ]/.test(str);202};203204205/**206* Checks if a string contains all letters.207* @param {string} str string to check.208* @return {boolean} True if `str` consists entirely of letters.209*/210goog.string.isAlpha = function(str) {211'use strict';212return !/[^a-zA-Z]/.test(str);213};214215216/**217* Checks if a string contains only numbers.218* @param {*} str string to check. If not a string, it will be219* casted to one.220* @return {boolean} True if `str` is numeric.221*/222goog.string.isNumeric = function(str) {223'use strict';224return !/[^0-9]/.test(str);225};226227228/**229* Checks if a string contains only numbers or letters.230* @param {string} str string to check.231* @return {boolean} True if `str` is alphanumeric.232*/233goog.string.isAlphaNumeric = function(str) {234'use strict';235return !/[^a-zA-Z0-9]/.test(str);236};237238239/**240* Checks if a character is a space character.241* @param {string} ch Character to check.242* @return {boolean} True if `ch` is a space.243*/244goog.string.isSpace = function(ch) {245'use strict';246return ch == ' ';247};248249250/**251* Checks if a character is a valid unicode character.252* @param {string} ch Character to check.253* @return {boolean} True if `ch` is a valid unicode character.254*/255goog.string.isUnicodeChar = function(ch) {256'use strict';257return ch.length == 1 && ch >= ' ' && ch <= '~' ||258ch >= '\u0080' && ch <= '\uFFFD';259};260261262/**263* Takes a string and replaces newlines with a space. Multiple lines are264* replaced with a single space.265* @param {string} str The string from which to strip newlines.266* @return {string} A copy of `str` stripped of newlines.267*/268goog.string.stripNewlines = function(str) {269'use strict';270return str.replace(/(\r\n|\r|\n)+/g, ' ');271};272273274/**275* Replaces Windows and Mac new lines with unix style: \r or \r\n with \n.276* @param {string} str The string to in which to canonicalize newlines.277* @return {string} `str` A copy of {@code} with canonicalized newlines.278*/279goog.string.canonicalizeNewlines = function(str) {280'use strict';281return str.replace(/(\r\n|\r|\n)/g, '\n');282};283284285/**286* Normalizes whitespace in a string, replacing all whitespace chars with287* a space.288* @param {string} str The string in which to normalize whitespace.289* @return {string} A copy of `str` with all whitespace normalized.290*/291goog.string.normalizeWhitespace = function(str) {292'use strict';293return str.replace(/\xa0|\s/g, ' ');294};295296297/**298* Normalizes spaces in a string, replacing all consecutive spaces and tabs299* with a single space. Replaces non-breaking space with a space.300* @param {string} str The string in which to normalize spaces.301* @return {string} A copy of `str` with all consecutive spaces and tabs302* replaced with a single space.303*/304goog.string.normalizeSpaces = function(str) {305'use strict';306return str.replace(/\xa0|[ \t]+/g, ' ');307};308309310/**311* Removes the breaking spaces from the left and right of the string and312* collapses the sequences of breaking spaces in the middle into single spaces.313* The original and the result strings render the same way in HTML.314* @param {string} str A string in which to collapse spaces.315* @return {string} Copy of the string with normalized breaking spaces.316*/317goog.string.collapseBreakingSpaces = function(str) {318'use strict';319return str.replace(/[\t\r\n ]+/g, ' ')320.replace(/^[\t\r\n ]+|[\t\r\n ]+$/g, '');321};322323324/**325* Trims white spaces to the left and right of a string.326* @param {string} str The string to trim.327* @return {string} A trimmed copy of `str`.328*/329goog.string.trim = goog.string.internal.trim;330331332/**333* Trims whitespaces at the left end of a string.334* @param {string} str The string to left trim.335* @return {string} A trimmed copy of `str`.336*/337goog.string.trimLeft = function(str) {338'use strict';339// Since IE doesn't include non-breaking-space (0xa0) in their \s character340// class (as required by section 7.2 of the ECMAScript spec), we explicitly341// include it in the regexp to enforce consistent cross-browser behavior.342return str.replace(/^[\s\xa0]+/, '');343};344345346/**347* Trims whitespaces at the right end of a string.348* @param {string} str The string to right trim.349* @return {string} A trimmed copy of `str`.350*/351goog.string.trimRight = function(str) {352'use strict';353// Since IE doesn't include non-breaking-space (0xa0) in their \s character354// class (as required by section 7.2 of the ECMAScript spec), we explicitly355// include it in the regexp to enforce consistent cross-browser behavior.356return str.replace(/[\s\xa0]+$/, '');357};358359360/**361* A string comparator that ignores case.362* -1 = str1 less than str2363* 0 = str1 equals str2364* 1 = str1 greater than str2365*366* @param {string} str1 The string to compare.367* @param {string} str2 The string to compare `str1` to.368* @return {number} The comparator result, as described above.369*/370goog.string.caseInsensitiveCompare =371goog.string.internal.caseInsensitiveCompare;372373374/**375* Compares two strings interpreting their numeric substrings as numbers.376*377* @param {string} str1 First string.378* @param {string} str2 Second string.379* @param {!RegExp} tokenizerRegExp Splits a string into substrings of380* non-negative integers, non-numeric characters and optionally fractional381* numbers starting with a decimal point.382* @return {number} Negative if str1 < str2, 0 is str1 == str2, positive if383* str1 > str2.384* @private385*/386goog.string.numberAwareCompare_ = function(str1, str2, tokenizerRegExp) {387'use strict';388if (str1 == str2) {389return 0;390}391if (!str1) {392return -1;393}394if (!str2) {395return 1;396}397398// Using match to split the entire string ahead of time turns out to be faster399// for most inputs than using RegExp.exec or iterating over each character.400const tokens1 = str1.toLowerCase().match(tokenizerRegExp);401const tokens2 = str2.toLowerCase().match(tokenizerRegExp);402403const count = Math.min(tokens1.length, tokens2.length);404405for (let i = 0; i < count; i++) {406const a = tokens1[i];407const b = tokens2[i];408409// Compare pairs of tokens, returning if one token sorts before the other.410if (a != b) {411// Only if both tokens are integers is a special comparison required.412// Decimal numbers are sorted as strings (e.g., '.09' < '.1').413const num1 = parseInt(a, 10);414if (!isNaN(num1)) {415const num2 = parseInt(b, 10);416if (!isNaN(num2) && num1 - num2) {417return num1 - num2;418}419}420return a < b ? -1 : 1;421}422}423424// If one string is a substring of the other, the shorter string sorts first.425if (tokens1.length != tokens2.length) {426return tokens1.length - tokens2.length;427}428429// The two strings must be equivalent except for case (perfect equality is430// tested at the head of the function.) Revert to default ASCII string431// comparison to stabilize the sort.432return str1 < str2 ? -1 : 1;433};434435436/**437* String comparison function that handles non-negative integer numbers in a438* way humans might expect. Using this function, the string 'File 2.jpg' sorts439* before 'File 10.jpg', and 'Version 1.9' before 'Version 1.10'. The comparison440* is mostly case-insensitive, though strings that are identical except for case441* are sorted with the upper-case strings before lower-case.442*443* This comparison function is up to 50x slower than either the default or the444* case-insensitive compare. It should not be used in time-critical code, but445* should be fast enough to sort several hundred short strings (like filenames)446* with a reasonable delay.447*448* @param {string} str1 The string to compare in a numerically sensitive way.449* @param {string} str2 The string to compare `str1` to.450* @return {number} less than 0 if str1 < str2, 0 if str1 == str2, greater than451* 0 if str1 > str2.452*/453goog.string.intAwareCompare = function(str1, str2) {454'use strict';455return goog.string.numberAwareCompare_(str1, str2, /\d+|\D+/g);456};457458459/**460* String comparison function that handles non-negative integer and fractional461* numbers in a way humans might expect. Using this function, the string462* 'File 2.jpg' sorts before 'File 10.jpg', and '3.14' before '3.2'. Equivalent463* to {@link goog.string.intAwareCompare} apart from the way how it interprets464* dots.465*466* @param {string} str1 The string to compare in a numerically sensitive way.467* @param {string} str2 The string to compare `str1` to.468* @return {number} less than 0 if str1 < str2, 0 if str1 == str2, greater than469* 0 if str1 > str2.470*/471goog.string.floatAwareCompare = function(str1, str2) {472'use strict';473return goog.string.numberAwareCompare_(str1, str2, /\d+|\.\d+|\D+/g);474};475476477/**478* Alias for {@link goog.string.floatAwareCompare}.479*480* @param {string} str1481* @param {string} str2482* @return {number}483*/484goog.string.numerateCompare = goog.string.floatAwareCompare;485486487/**488* URL-encodes a string489* @param {*} str The string to url-encode.490* @return {string} An encoded copy of `str` that is safe for urls.491* Note that '#', ':', and other characters used to delimit portions492* of URLs *will* be encoded.493*/494goog.string.urlEncode = function(str) {495'use strict';496return encodeURIComponent(String(str));497};498499500/**501* URL-decodes the string. We need to specially handle '+'s because502* the javascript library doesn't convert them to spaces.503* @param {string} str The string to url decode.504* @return {string} The decoded `str`.505*/506goog.string.urlDecode = function(str) {507'use strict';508return decodeURIComponent(str.replace(/\+/g, ' '));509};510511512/**513* Converts \n to <br>s or <br />s.514* @param {string} str The string in which to convert newlines.515* @param {boolean=} opt_xml Whether to use XML compatible tags.516* @return {string} A copy of `str` with converted newlines.517*/518goog.string.newLineToBr = goog.string.internal.newLineToBr;519520521/**522* Escapes double quote '"' and single quote '\'' characters in addition to523* '&', '<', and '>' so that a string can be included in an HTML tag attribute524* value within double or single quotes.525*526* It should be noted that > doesn't need to be escaped for the HTML or XML to527* be valid, but it has been decided to escape it for consistency with other528* implementations.529*530* With goog.string.DETECT_DOUBLE_ESCAPING, this function escapes also the531* lowercase letter "e".532*533* NOTE(user):534* HtmlEscape is often called during the generation of large blocks of HTML.535* Using statics for the regular expressions and strings is an optimization536* that can more than half the amount of time IE spends in this function for537* large apps, since strings and regexes both contribute to GC allocations.538*539* Testing for the presence of a character before escaping increases the number540* of function calls, but actually provides a speed increase for the average541* case -- since the average case often doesn't require the escaping of all 4542* characters and indexOf() is much cheaper than replace().543* The worst case does suffer slightly from the additional calls, therefore the544* opt_isLikelyToContainHtmlChars option has been included for situations545* where all 4 HTML entities are very likely to be present and need escaping.546*547* Some benchmarks (times tended to fluctuate +-0.05ms):548* FireFox IE6549* (no chars / average (mix of cases) / all 4 chars)550* no checks 0.13 / 0.22 / 0.22 0.23 / 0.53 / 0.80551* indexOf 0.08 / 0.17 / 0.26 0.22 / 0.54 / 0.84552* indexOf + re test 0.07 / 0.17 / 0.28 0.19 / 0.50 / 0.85553*554* An additional advantage of checking if replace actually needs to be called555* is a reduction in the number of object allocations, so as the size of the556* application grows the difference between the various methods would increase.557*558* @param {string} str string to be escaped.559* @param {boolean=} opt_isLikelyToContainHtmlChars Don't perform a check to see560* if the character needs replacing - use this option if you expect each of561* the characters to appear often. Leave false if you expect few html562* characters to occur in your strings, such as if you are escaping HTML.563* @return {string} An escaped copy of `str`.564*/565goog.string.htmlEscape = function(str, opt_isLikelyToContainHtmlChars) {566'use strict';567str = goog.string.internal.htmlEscape(str, opt_isLikelyToContainHtmlChars);568if (goog.string.DETECT_DOUBLE_ESCAPING) {569str = str.replace(goog.string.E_RE_, 'e');570}571return str;572};573574575/**576* Regular expression that matches a lowercase letter "e", for use in escaping.577* @const {!RegExp}578* @private579*/580goog.string.E_RE_ = /e/g;581582583/**584* Unescapes an HTML string.585*586* @param {string} str The string to unescape.587* @return {string} An unescaped copy of `str`.588*/589goog.string.unescapeEntities = function(str) {590'use strict';591if (goog.string.contains(str, '&')) {592// We are careful not to use a DOM if we do not have one or we explicitly593// requested non-DOM html unescaping.594if (!goog.string.FORCE_NON_DOM_HTML_UNESCAPING &&595'document' in goog.global) {596return goog.string.unescapeEntitiesUsingDom_(str);597} else {598// Fall back on pure XML entities599return goog.string.unescapePureXmlEntities_(str);600}601}602return str;603};604605606/**607* Unescapes a HTML string using the provided document.608*609* @param {string} str The string to unescape.610* @param {!Document} document A document to use in escaping the string.611* @return {string} An unescaped copy of `str`.612*/613goog.string.unescapeEntitiesWithDocument = function(str, document) {614'use strict';615if (goog.string.contains(str, '&')) {616return goog.string.unescapeEntitiesUsingDom_(str, document);617}618return str;619};620621622/**623* Unescapes an HTML string using a DOM to resolve non-XML, non-numeric624* entities. This function is XSS-safe and whitespace-preserving.625* @private626* @param {string} str The string to unescape.627* @param {Document=} opt_document An optional document to use for creating628* elements. If this is not specified then the default window.document629* will be used.630* @return {string} The unescaped `str` string.631*/632goog.string.unescapeEntitiesUsingDom_ = function(str, opt_document) {633'use strict';634/** @type {!Object<string, string>} */635const seen = {'&': '&', '<': '<', '>': '>', '"': '"'};636/** @type {!Element} */637let div;638if (opt_document) {639div = opt_document.createElement('div');640} else {641div = goog.global.document.createElement('div');642}643// Match as many valid entity characters as possible. If the actual entity644// happens to be shorter, it will still work as innerHTML will return the645// trailing characters unchanged. Since the entity characters do not include646// open angle bracket, there is no chance of XSS from the innerHTML use.647// Since no whitespace is passed to innerHTML, whitespace is preserved.648return str.replace(goog.string.HTML_ENTITY_PATTERN_, function(s, entity) {649'use strict';650// Check for cached entity.651let value = seen[s];652if (value) {653return value;654}655// Check for numeric entity.656if (entity.charAt(0) == '#') {657// Prefix with 0 so that hex entities (e.g. ) parse as hex numbers.658const n = Number('0' + entity.slice(1));659if (!isNaN(n)) {660value = String.fromCharCode(n);661}662}663// Fall back to innerHTML otherwise.664if (!value) {665// Append a non-entity character to avoid a bug in Webkit that parses666// an invalid entity at the end of innerHTML text as the empty string.667goog.dom.safe.setInnerHtml(668div,669goog.html.uncheckedconversions670.safeHtmlFromStringKnownToSatisfyTypeContract(671goog.string.Const.from('Single HTML entity.'), s + ' '));672// Then remove the trailing character from the result.673value = div.firstChild.nodeValue.slice(0, -1);674}675// Cache and return.676return seen[s] = value;677});678};679680681/**682* Unescapes XML entities.683* @private684* @param {string} str The string to unescape.685* @return {string} An unescaped copy of `str`.686*/687goog.string.unescapePureXmlEntities_ = function(str) {688'use strict';689return str.replace(/&([^;]+);/g, function(s, entity) {690'use strict';691switch (entity) {692case 'amp':693return '&';694case 'lt':695return '<';696case 'gt':697return '>';698case 'quot':699return '"';700default:701if (entity.charAt(0) == '#') {702// Prefix with 0 so that hex entities (e.g. ) parse as hex.703const n = Number('0' + entity.slice(1));704if (!isNaN(n)) {705return String.fromCharCode(n);706}707}708// For invalid entities we just return the entity709return s;710}711});712};713714715/**716* Regular expression that matches an HTML entity.717* See also HTML5: Tokenization / Tokenizing character references.718* @private719* @type {!RegExp}720*/721goog.string.HTML_ENTITY_PATTERN_ = /&([^;\s<&]+);?/g;722723724/**725* Do escaping of whitespace to preserve spatial formatting. We use character726* entity #160 to make it safer for xml.727* @param {string} str The string in which to escape whitespace.728* @param {boolean=} opt_xml Whether to use XML compatible tags.729* @return {string} An escaped copy of `str`.730*/731goog.string.whitespaceEscape = function(str, opt_xml) {732'use strict';733// This doesn't use goog.string.preserveSpaces for backwards compatibility.734return goog.string.newLineToBr(str.replace(/ /g, '  '), opt_xml);735};736737738/**739* Preserve spaces that would be otherwise collapsed in HTML by replacing them740* with non-breaking space Unicode characters.741* @param {string} str The string in which to preserve whitespace.742* @return {string} A copy of `str` with preserved whitespace.743*/744goog.string.preserveSpaces = function(str) {745'use strict';746return str.replace(/(^|[\n ]) /g, '$1' + goog.string.Unicode.NBSP);747};748749750/**751* Strip quote characters around a string. The second argument is a string of752* characters to treat as quotes. This can be a single character or a string of753* multiple character and in that case each of those are treated as possible754* quote characters. For example:755*756* <pre>757* goog.string.stripQuotes('"abc"', '"`') --> 'abc'758* goog.string.stripQuotes('`abc`', '"`') --> 'abc'759* </pre>760*761* @param {string} str The string to strip.762* @param {string} quoteChars The quote characters to strip.763* @return {string} A copy of `str` without the quotes.764*/765goog.string.stripQuotes = function(str, quoteChars) {766'use strict';767const length = quoteChars.length;768for (let i = 0; i < length; i++) {769const quoteChar = length == 1 ? quoteChars : quoteChars.charAt(i);770if (str.charAt(0) == quoteChar && str.charAt(str.length - 1) == quoteChar) {771return str.substring(1, str.length - 1);772}773}774return str;775};776777778/**779* Truncates a string to a certain length and adds '...' if necessary. The780* length also accounts for the ellipsis, so a maximum length of 10 and a string781* 'Hello World!' produces 'Hello W...'.782* @param {string} str The string to truncate.783* @param {number} chars Max number of characters.784* @param {boolean=} opt_protectEscapedCharacters Whether to protect escaped785* characters from being cut off in the middle.786* @return {string} The truncated `str` string.787*/788goog.string.truncate = function(str, chars, opt_protectEscapedCharacters) {789'use strict';790if (opt_protectEscapedCharacters) {791str = goog.string.unescapeEntities(str);792}793794if (str.length > chars) {795str = str.substring(0, chars - 3) + '...';796}797798if (opt_protectEscapedCharacters) {799str = goog.string.htmlEscape(str);800}801802return str;803};804805806/**807* Truncate a string in the middle, adding "..." if necessary,808* and favoring the beginning of the string.809* @param {string} str The string to truncate the middle of.810* @param {number} chars Max number of characters.811* @param {boolean=} opt_protectEscapedCharacters Whether to protect escaped812* characters from being cutoff in the middle.813* @param {number=} opt_trailingChars Optional number of trailing characters to814* leave at the end of the string, instead of truncating as close to the815* middle as possible.816* @return {string} A truncated copy of `str`.817*/818goog.string.truncateMiddle = function(819str, chars, opt_protectEscapedCharacters, opt_trailingChars) {820'use strict';821if (opt_protectEscapedCharacters) {822str = goog.string.unescapeEntities(str);823}824825if (opt_trailingChars && str.length > chars) {826if (opt_trailingChars > chars) {827opt_trailingChars = chars;828}829const endPoint = str.length - opt_trailingChars;830const startPoint = chars - opt_trailingChars;831str = str.substring(0, startPoint) + '...' + str.substring(endPoint);832} else if (str.length > chars) {833// Favor the beginning of the string:834let half = Math.floor(chars / 2);835const endPos = str.length - half;836half += chars % 2;837str = str.substring(0, half) + '...' + str.substring(endPos);838}839840if (opt_protectEscapedCharacters) {841str = goog.string.htmlEscape(str);842}843844return str;845};846847848/**849* Special chars that need to be escaped for goog.string.quote.850* @private {!Object<string, string>}851*/852goog.string.specialEscapeChars_ = {853'\0': '\\0',854'\b': '\\b',855'\f': '\\f',856'\n': '\\n',857'\r': '\\r',858'\t': '\\t',859'\x0B': '\\x0B', // '\v' is not supported in JScript860'"': '\\"',861'\\': '\\\\',862// To support the use case of embedding quoted strings inside of script863// tags, we have to make sure HTML comments and opening/closing script tags do864// not appear in the resulting string. The specific strings that must be865// escaped are documented at:866// https://html.spec.whatwg.org/multipage/scripting.html#restrictions-for-contents-of-script-elements867'<': '\\u003C' // NOTE: JSON.parse crashes on '\\x3c'.868};869870871/**872* Character mappings used internally for goog.string.escapeChar.873* @private {!Object<string, string>}874*/875goog.string.jsEscapeCache_ = {876'\'': '\\\''877};878879880/**881* Encloses a string in double quotes and escapes characters so that the882* string is a valid JS string. The resulting string is safe to embed in883* `<script>` tags as "<" is escaped.884* @param {string} s The string to quote.885* @return {string} A copy of `s` surrounded by double quotes.886*/887goog.string.quote = function(s) {888'use strict';889s = String(s);890const sb = ['"'];891for (let i = 0; i < s.length; i++) {892const ch = s.charAt(i);893const cc = ch.charCodeAt(0);894sb[i + 1] = goog.string.specialEscapeChars_[ch] ||895((cc > 31 && cc < 127) ? ch : goog.string.escapeChar(ch));896}897sb.push('"');898return sb.join('');899};900901902/**903* Takes a string and returns the escaped string for that input string.904* @param {string} str The string to escape.905* @return {string} An escaped string representing `str`.906*/907goog.string.escapeString = function(str) {908'use strict';909const sb = [];910for (let i = 0; i < str.length; i++) {911sb[i] = goog.string.escapeChar(str.charAt(i));912}913return sb.join('');914};915916917/**918* Takes a character and returns the escaped string for that character. For919* example escapeChar(String.fromCharCode(15)) -> "\\x0E".920* @param {string} c The character to escape.921* @return {string} An escaped string representing `c`.922*/923goog.string.escapeChar = function(c) {924'use strict';925if (c in goog.string.jsEscapeCache_) {926return goog.string.jsEscapeCache_[c];927}928929if (c in goog.string.specialEscapeChars_) {930return goog.string.jsEscapeCache_[c] = goog.string.specialEscapeChars_[c];931}932933let rv = c;934const cc = c.charCodeAt(0);935if (cc > 31 && cc < 127) {936rv = c;937} else {938// tab is 9 but handled above939if (cc < 256) {940rv = '\\x';941if (cc < 16 || cc > 256) {942rv += '0';943}944} else {945rv = '\\u';946if (cc < 4096) { // \u1000947rv += '0';948}949}950rv += cc.toString(16).toUpperCase();951}952953return goog.string.jsEscapeCache_[c] = rv;954};955956957/**958* Determines whether a string contains a substring.959* @param {string} str The string to search.960* @param {string} subString The substring to search for.961* @return {boolean} Whether `str` contains `subString`.962*/963goog.string.contains = goog.string.internal.contains;964965966/**967* Determines whether a string contains a substring, ignoring case.968* @param {string} str The string to search.969* @param {string} subString The substring to search for.970* @return {boolean} Whether `str` contains `subString`.971*/972goog.string.caseInsensitiveContains =973goog.string.internal.caseInsensitiveContains;974975976/**977* Returns the non-overlapping occurrences of ss in s.978* If either s or ss evalutes to false, then returns zero.979* @param {string} s The string to look in.980* @param {string} ss The string to look for.981* @return {number} Number of occurrences of ss in s.982*/983goog.string.countOf = function(s, ss) {984'use strict';985return s && ss ? s.split(ss).length - 1 : 0;986};987988989/**990* Removes a substring of a specified length at a specific991* index in a string.992* @param {string} s The base string from which to remove.993* @param {number} index The index at which to remove the substring.994* @param {number} stringLength The length of the substring to remove.995* @return {string} A copy of `s` with the substring removed or the full996* string if nothing is removed or the input is invalid.997*/998goog.string.removeAt = function(s, index, stringLength) {999'use strict';1000let resultStr = s;1001// If the index is greater or equal to 0 then remove substring1002if (index >= 0 && index < s.length && stringLength > 0) {1003resultStr = s.slice(0, index) + s.slice(index + stringLength);1004}1005return resultStr;1006};100710081009/**1010* Removes the first occurrence of a substring from a string.1011* @param {string} str The base string from which to remove.1012* @param {string} substr The string to remove.1013* @return {string} A copy of `str` with `substr` removed or the1014* full string if nothing is removed.1015*/1016goog.string.remove = function(str, substr) {1017'use strict';1018return str.replace(substr, '');1019};102010211022/**1023* Removes all occurrences of a substring from a string.1024* @param {string} s The base string from which to remove.1025* @param {string} ss The string to remove.1026* @return {string} A copy of `s` with `ss` removed or the full1027* string if nothing is removed.1028*/1029goog.string.removeAll = function(s, ss) {1030'use strict';1031const re = new RegExp(goog.string.regExpEscape(ss), 'g');1032return s.replace(re, '');1033};103410351036/**1037* Replaces all occurrences of a substring of a string with a new substring.1038* @param {string} s The base string from which to remove.1039* @param {string} ss The string to replace.1040* @param {string} replacement The replacement string.1041* @return {string} A copy of `s` with `ss` replaced by1042* `replacement` or the original string if nothing is replaced.1043*/1044goog.string.replaceAll = function(s, ss, replacement) {1045'use strict';1046const re = new RegExp(goog.string.regExpEscape(ss), 'g');1047return s.replace(re, replacement.replace(/\$/g, '$$$$'));1048};104910501051/**1052* Escapes characters in the string that are not safe to use in a RegExp.1053* @param {*} s The string to escape. If not a string, it will be casted1054* to one.1055* @return {string} A RegExp safe, escaped copy of `s`.1056*/1057goog.string.regExpEscape = function(s) {1058'use strict';1059return String(s)1060.replace(/([-()\[\]{}+?*.$\^|,:#<!\\])/g, '\\$1')1061.replace(/\x08/g, '\\x08');1062};106310641065/**1066* Repeats a string n times.1067* @param {string} string The string to repeat.1068* @param {number} length The number of times to repeat.1069* @return {string} A string containing `length` repetitions of1070* `string`.1071*/1072goog.string.repeat = (String.prototype.repeat) ? function(string, length) {1073'use strict';1074// The native method is over 100 times faster than the alternative.1075return string.repeat(length);1076} : function(string, length) {1077'use strict';1078return new Array(length + 1).join(string);1079};108010811082/**1083* Pads number to given length and optionally rounds it to a given precision.1084* For example:1085* <pre>padNumber(1.25, 2, 3) -> '01.250'1086* padNumber(1.25, 2) -> '01.25'1087* padNumber(1.25, 2, 1) -> '01.3'1088* padNumber(1.25, 0) -> '1.25'</pre>1089*1090* @param {number} num The number to pad.1091* @param {number} length The desired length.1092* @param {number=} opt_precision The desired precision.1093* @return {string} `num` as a string with the given options.1094*/1095goog.string.padNumber = function(num, length, opt_precision) {1096'use strict';1097if (!Number.isFinite(num)) return String(num);1098let s =1099(opt_precision !== undefined) ? num.toFixed(opt_precision) : String(num);1100let index = s.indexOf('.');1101if (index === -1) {1102index = s.length;1103}1104const sign = s[0] === '-' ? '-' : '';1105if (sign) {1106s = s.substring(1);1107}1108return sign + goog.string.repeat('0', Math.max(0, length - index)) + s;1109};111011111112/**1113* Returns a string representation of the given object, with1114* null and undefined being returned as the empty string.1115*1116* @param {*} obj The object to convert.1117* @return {string} A string representation of the `obj`.1118*/1119goog.string.makeSafe = function(obj) {1120'use strict';1121return obj == null ? '' : String(obj);1122};11231124/**1125* Returns a string with at least 64-bits of randomness.1126*1127* Doesn't trust JavaScript's random function entirely. Uses a combination of1128* random and current timestamp, and then encodes the string in base-36 to1129* make it shorter.1130*1131* @return {string} A random string, e.g. sn1s7vb4gcic.1132*/1133goog.string.getRandomString = function() {1134'use strict';1135const x = 2147483648;1136return Math.floor(Math.random() * x).toString(36) +1137Math.abs(Math.floor(Math.random() * x) ^ Date.now()).toString(36);1138};113911401141/**1142* Compares two version numbers.1143*1144* @param {string|number} version1 Version of first item.1145* @param {string|number} version2 Version of second item.1146*1147* @return {number} 1 if `version1` is higher.1148* 0 if arguments are equal.1149* -1 if `version2` is higher.1150*/1151goog.string.compareVersions = goog.string.internal.compareVersions;115211531154/**1155* String hash function similar to java.lang.String.hashCode().1156* The hash code for a string is computed as1157* s[0] * 31 ^ (n - 1) + s[1] * 31 ^ (n - 2) + ... + s[n - 1],1158* where s[i] is the ith character of the string and n is the length of1159* the string. We mod the result to make it between 0 (inclusive) and 2^321160* (exclusive).1161* @param {string} str A string.1162* @return {number} Hash value for `str`, between 0 (inclusive) and 2^321163* (exclusive). The empty string returns 0.1164*/1165goog.string.hashCode = function(str) {1166'use strict';1167let result = 0;1168for (let i = 0; i < str.length; ++i) {1169// Normalize to 4 byte range, 0 ... 2^32.1170result = (31 * result + str.charCodeAt(i)) >>> 0;1171}1172return result;1173};117411751176/**1177* The most recent unique ID. |0 is equivalent to Math.floor in this case.1178* @type {number}1179* @private1180*/1181goog.string.uniqueStringCounter_ = Math.random() * 0x80000000 | 0;118211831184/**1185* Generates and returns a string which is unique in the current document.1186* This is useful, for example, to create unique IDs for DOM elements.1187* @return {string} A unique id.1188*/1189goog.string.createUniqueString = function() {1190'use strict';1191return 'goog_' + goog.string.uniqueStringCounter_++;1192};119311941195/**1196* Converts the supplied string to a number, which may be Infinity or NaN.1197* This function strips whitespace: (toNumber(' 123') === 123)1198* This function accepts scientific notation: (toNumber('1e1') === 10)1199*1200* This is better than JavaScript's built-in conversions because, sadly:1201* (Number(' ') === 0) and (parseFloat('123a') === 123)1202*1203* @param {string} str The string to convert.1204* @return {number} The number the supplied string represents, or NaN.1205*/1206goog.string.toNumber = function(str) {1207'use strict';1208const num = Number(str);1209if (num == 0 && goog.string.isEmptyOrWhitespace(str)) {1210return NaN;1211}1212return num;1213};121412151216/**1217* Returns whether the given string is lower camel case (e.g. "isFooBar").1218*1219* Note that this assumes the string is entirely letters.1220* @see http://en.wikipedia.org/wiki/CamelCase#Variations_and_synonyms1221*1222* @param {string} str String to test.1223* @return {boolean} Whether the string is lower camel case.1224*/1225goog.string.isLowerCamelCase = function(str) {1226'use strict';1227return /^[a-z]+([A-Z][a-z]*)*$/.test(str);1228};122912301231/**1232* Returns whether the given string is upper camel case (e.g. "FooBarBaz").1233*1234* Note that this assumes the string is entirely letters.1235* @see http://en.wikipedia.org/wiki/CamelCase#Variations_and_synonyms1236*1237* @param {string} str String to test.1238* @return {boolean} Whether the string is upper camel case.1239*/1240goog.string.isUpperCamelCase = function(str) {1241'use strict';1242return /^([A-Z][a-z]*)+$/.test(str);1243};124412451246/**1247* Converts a string from selector-case to camelCase (e.g. from1248* "multi-part-string" to "multiPartString"), useful for converting1249* CSS selectors and HTML dataset keys to their equivalent JS properties.1250* @param {string} str The string in selector-case form.1251* @return {string} The string in camelCase form.1252*/1253goog.string.toCamelCase = function(str) {1254'use strict';1255return String(str).replace(/\-([a-z])/g, function(all, match) {1256'use strict';1257return match.toUpperCase();1258});1259};126012611262/**1263* Converts a string from camelCase to selector-case (e.g. from1264* "multiPartString" to "multi-part-string"), useful for converting JS1265* style and dataset properties to equivalent CSS selectors and HTML keys.1266* @param {string} str The string in camelCase form.1267* @return {string} The string in selector-case form.1268*/1269goog.string.toSelectorCase = function(str) {1270'use strict';1271return String(str).replace(/([A-Z])/g, '-$1').toLowerCase();1272};127312741275/**1276* Converts a string into TitleCase. First character of the string is always1277* capitalized in addition to the first letter of every subsequent word.1278* Words are delimited by one or more whitespaces by default. Custom delimiters1279* can optionally be specified to replace the default, which doesn't preserve1280* whitespace delimiters and instead must be explicitly included if needed.1281*1282* Default delimiter => " ":1283* goog.string.toTitleCase('oneTwoThree') => 'OneTwoThree'1284* goog.string.toTitleCase('one two three') => 'One Two Three'1285* goog.string.toTitleCase(' one two ') => ' One Two '1286* goog.string.toTitleCase('one_two_three') => 'One_two_three'1287* goog.string.toTitleCase('one-two-three') => 'One-two-three'1288*1289* Custom delimiter => "_-.":1290* goog.string.toTitleCase('oneTwoThree', '_-.') => 'OneTwoThree'1291* goog.string.toTitleCase('one two three', '_-.') => 'One two three'1292* goog.string.toTitleCase(' one two ', '_-.') => ' one two '1293* goog.string.toTitleCase('one_two_three', '_-.') => 'One_Two_Three'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*1299* @param {string} str String value in camelCase form.1300* @param {string=} opt_delimiters Custom delimiter character set used to1301* distinguish words in the string value. Each character represents a1302* single delimiter. When provided, default whitespace delimiter is1303* overridden and must be explicitly included if needed.1304* @return {string} String value in TitleCase form.1305*/1306goog.string.toTitleCase = function(str, opt_delimiters) {1307'use strict';1308let delimiters = (typeof opt_delimiters === 'string') ?1309goog.string.regExpEscape(opt_delimiters) :1310'\\s';13111312// For IE8, we need to prevent using an empty character set. Otherwise,1313// incorrect matching will occur.1314delimiters = delimiters ? '|[' + delimiters + ']+' : '';13151316const regexp = new RegExp('(^' + delimiters + ')([a-z])', 'g');1317return str.replace(regexp, function(all, p1, p2) {1318'use strict';1319return p1 + p2.toUpperCase();1320});1321};132213231324/**1325* Capitalizes a string, i.e. converts the first letter to uppercase1326* and all other letters to lowercase, e.g.:1327*1328* goog.string.capitalize('one') => 'One'1329* goog.string.capitalize('ONE') => 'One'1330* goog.string.capitalize('one two') => 'One two'1331*1332* Note that this function does not trim initial whitespace.1333*1334* @param {string} str String value to capitalize.1335* @return {string} String value with first letter in uppercase.1336*/1337goog.string.capitalize = function(str) {1338'use strict';1339return String(str.charAt(0)).toUpperCase() +1340String(str.slice(1)).toLowerCase();1341};134213431344/**1345* Parse a string in decimal or hexidecimal ('0xFFFF') form.1346*1347* To parse a particular radix, please use parseInt(string, radix) directly. See1348* https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/parseInt1349*1350* This is a wrapper for the built-in parseInt function that will only parse1351* numbers as base 10 or base 16. Some JS implementations assume strings1352* starting with "0" are intended to be octal. ES3 allowed but discouraged1353* this behavior. ES5 forbids it. This function emulates the ES5 behavior.1354*1355* For more information, see Mozilla JS Reference: http://goo.gl/8RiFj1356*1357* @param {string|number|null|undefined} value The value to be parsed.1358* @return {number} The number, parsed. If the string failed to parse, this1359* will be NaN.1360*/1361goog.string.parseInt = function(value) {1362'use strict';1363// Force finite numbers to strings.1364if (isFinite(value)) {1365value = String(value);1366}13671368if (typeof value === 'string') {1369// If the string starts with '0x' or '-0x', parse as hex.1370return /^\s*-?0x/i.test(value) ? parseInt(value, 16) : parseInt(value, 10);1371}13721373return NaN;1374};137513761377/**1378* Splits a string on a separator a limited number of times.1379*1380* This implementation is more similar to Python or Java, where the limit1381* parameter specifies the maximum number of splits rather than truncating1382* the number of results.1383*1384* See http://docs.python.org/2/library/stdtypes.html#str.split1385* See JavaDoc: http://goo.gl/F2AsY1386* See Mozilla reference: http://goo.gl/dZdZs1387*1388* @param {string} str String to split.1389* @param {string} separator The separator.1390* @param {number} limit The limit to the number of splits. The resulting array1391* will have a maximum length of limit+1. Negative numbers are the same1392* as zero.1393* @return {!Array<string>} The string, split.1394*/1395goog.string.splitLimit = function(str, separator, limit) {1396'use strict';1397const parts = str.split(separator);1398const returnVal = [];13991400// Only continue doing this while we haven't hit the limit and we have1401// parts left.1402while (limit > 0 && parts.length) {1403returnVal.push(parts.shift());1404limit--;1405}14061407// If there are remaining parts, append them to the end.1408if (parts.length) {1409returnVal.push(parts.join(separator));1410}14111412return returnVal;1413};141414151416/**1417* Finds the characters to the right of the last instance of any separator1418*1419* This function is similar to goog.string.path.baseName, except it can take a1420* list of characters to split the string on. It will return the rightmost1421* grouping of characters to the right of any separator as a left-to-right1422* oriented string.1423*1424* @see goog.string.path.baseName1425* @param {string} str The string1426* @param {string|!Array<string>} separators A list of separator characters1427* @return {string} The last part of the string with respect to the separators1428*/1429goog.string.lastComponent = function(str, separators) {1430'use strict';1431if (!separators) {1432return str;1433} else if (typeof separators == 'string') {1434separators = [separators];1435}14361437let lastSeparatorIndex = -1;1438for (let i = 0; i < separators.length; i++) {1439if (separators[i] == '') {1440continue;1441}1442const currentSeparatorIndex = str.lastIndexOf(separators[i]);1443if (currentSeparatorIndex > lastSeparatorIndex) {1444lastSeparatorIndex = currentSeparatorIndex;1445}1446}1447if (lastSeparatorIndex == -1) {1448return str;1449}1450return str.slice(lastSeparatorIndex + 1);1451};145214531454/**1455* Computes the Levenshtein edit distance between two strings.1456* @param {string} a1457* @param {string} b1458* @return {number} The edit distance between the two strings.1459*/1460goog.string.editDistance = function(a, b) {1461'use strict';1462const v0 = [];1463const v1 = [];14641465if (a == b) {1466return 0;1467}14681469if (!a.length || !b.length) {1470return Math.max(a.length, b.length);1471}14721473for (let i = 0; i < b.length + 1; i++) {1474v0[i] = i;1475}14761477for (let i = 0; i < a.length; i++) {1478v1[0] = i + 1;14791480for (let j = 0; j < b.length; j++) {1481const cost = Number(a[i] != b[j]);1482// Cost for the substring is the minimum of adding one character, removing1483// one character, or a swap.1484v1[j + 1] = Math.min(v1[j] + 1, v0[j + 1] + 1, v0[j] + cost);1485}14861487for (let j = 0; j < v0.length; j++) {1488v0[j] = v1[j];1489}1490}14911492return v1[b.length];1493};149414951496