Index: tools/telemetry/third_party/gsutilz/third_party/protorpc/experimental/javascript/closure/string/string.js |
diff --git a/third_party/google_input_tools/third_party/closure_library/closure/goog/string/string.js b/tools/telemetry/third_party/gsutilz/third_party/protorpc/experimental/javascript/closure/string/string.js |
similarity index 69% |
copy from third_party/google_input_tools/third_party/closure_library/closure/goog/string/string.js |
copy to tools/telemetry/third_party/gsutilz/third_party/protorpc/experimental/javascript/closure/string/string.js |
index 065a1a8409b4e453833be736d94409b00394c699..0725d09dec8bed300af7f5021b5381655e465074 100644 |
--- a/third_party/google_input_tools/third_party/closure_library/closure/goog/string/string.js |
+++ b/tools/telemetry/third_party/gsutilz/third_party/protorpc/experimental/javascript/closure/string/string.js |
@@ -14,7 +14,6 @@ |
/** |
* @fileoverview Utilities for string manipulation. |
- * @author arv@google.com (Erik Arvidsson) |
*/ |
@@ -26,19 +25,6 @@ goog.provide('goog.string.Unicode'); |
/** |
- * @define {boolean} Enables HTML escaping of lowercase letter "e" which helps |
- * with detection of double-escaping as this letter is frequently used. |
- */ |
-goog.define('goog.string.DETECT_DOUBLE_ESCAPING', false); |
- |
- |
-/** |
- * @define {boolean} Whether to force non-dom html unescaping. |
- */ |
-goog.define('goog.string.FORCE_NON_DOM_HTML_UNESCAPING', false); |
- |
- |
-/** |
* Common Unicode string characters. |
* @enum {string} |
*/ |
@@ -97,18 +83,6 @@ goog.string.caseInsensitiveEndsWith = function(str, suffix) { |
/** |
- * Case-insensitive equality checker. |
- * @param {string} str1 First string to check. |
- * @param {string} str2 Second string to check. |
- * @return {boolean} True if {@code str1} and {@code str2} are the same string, |
- * ignoring case. |
- */ |
-goog.string.caseInsensitiveEquals = function(str1, str2) { |
- return str1.toLowerCase() == str2.toLowerCase(); |
-}; |
- |
- |
-/** |
* Does simple python-style string substitution. |
* subs("foo%s hot%s", "bar", "dog") becomes "foobar hotdog". |
* @param {string} str The string containing the pattern. |
@@ -117,18 +91,18 @@ goog.string.caseInsensitiveEquals = function(str1, str2) { |
* {@code %s} has been replaced an argument from {@code var_args}. |
*/ |
goog.string.subs = function(str, var_args) { |
- var splitParts = str.split('%s'); |
- var returnString = ''; |
- |
- var subsArguments = Array.prototype.slice.call(arguments, 1); |
- while (subsArguments.length && |
- // Replace up to the last split part. We are inserting in the |
- // positions between split parts. |
- splitParts.length > 1) { |
- returnString += splitParts.shift() + subsArguments.shift(); |
+ // This appears to be slow, but testing shows it compares more or less |
+ // equivalent to the regex.exec method. |
+ for (var i = 1; i < arguments.length; i++) { |
+ // We cast to String in case an argument is a Function. Replacing $&, for |
+ // example, with $$$& stops the replace from subsituting the whole match |
+ // into the resultant string. $$$& in the first replace becomes $$& in the |
+ // second, which leaves $& in the resultant string. Also: |
+ // $$, $`, $', $n $nn |
+ var replacement = String(arguments[i]).replace(/\$/g, '$$$$'); |
+ str = str.replace(/\%s/, replacement); |
} |
- |
- return returnString + splitParts.join('%s'); // Join unused '%s' |
+ return str; |
}; |
@@ -149,9 +123,9 @@ goog.string.collapseWhitespace = function(str) { |
/** |
* Checks if a string is empty or contains only whitespaces. |
* @param {string} str The string to check. |
- * @return {boolean} Whether {@code str} is empty or whitespace only. |
+ * @return {boolean} True if {@code str} is empty or whitespace only. |
*/ |
-goog.string.isEmptyOrWhitespace = function(str) { |
+goog.string.isEmpty = function(str) { |
// testing length == 0 first is actually slower in all browsers (about the |
// same in Opera). |
// Since IE doesn't include non-breaking-space (0xa0) in their \s character |
@@ -162,54 +136,16 @@ goog.string.isEmptyOrWhitespace = function(str) { |
/** |
- * Checks if a string is empty. |
- * @param {string} str The string to check. |
- * @return {boolean} Whether {@code str} is empty. |
- */ |
-goog.string.isEmptyString = function(str) { |
- return str.length == 0; |
-}; |
- |
- |
-/** |
- * Checks if a string is empty or contains only whitespaces. |
- * |
- * TODO(user): Deprecate this when clients have been switched over to |
- * goog.string.isEmptyOrWhitespace. |
- * |
- * @param {string} str The string to check. |
- * @return {boolean} Whether {@code str} is empty or whitespace only. |
- */ |
-goog.string.isEmpty = goog.string.isEmptyOrWhitespace; |
- |
- |
-/** |
- * Checks if a string is null, undefined, empty or contains only whitespaces. |
+ * Checks if a string is null, empty or contains only whitespaces. |
* @param {*} str The string to check. |
- * @return {boolean} Whether {@code str} is null, undefined, empty, or |
- * whitespace only. |
- * @deprecated Use goog.string.isEmptyOrWhitespace(goog.string.makeSafe(str)) |
- * instead. |
+ * @return {boolean} True if{@code str} is null, empty, or whitespace only. |
*/ |
-goog.string.isEmptyOrWhitespaceSafe = function(str) { |
- return goog.string.isEmptyOrWhitespace(goog.string.makeSafe(str)); |
+goog.string.isEmptySafe = function(str) { |
+ return goog.string.isEmpty(goog.string.makeSafe(str)); |
}; |
/** |
- * Checks if a string is null, undefined, empty or contains only whitespaces. |
- * |
- * TODO(user): Deprecate this when clients have been switched over to |
- * goog.string.isEmptyOrWhitespaceSafe. |
- * |
- * @param {*} str The string to check. |
- * @return {boolean} Whether {@code str} is null, undefined, empty, or |
- * whitespace only. |
- */ |
-goog.string.isEmptySafe = goog.string.isEmptyOrWhitespaceSafe; |
- |
- |
-/** |
* Checks if a string is all breaking whitespace. |
* @param {string} str The string to check. |
* @return {boolean} Whether the string is all breaking whitespace. |
@@ -253,7 +189,7 @@ goog.string.isAlphaNumeric = function(str) { |
/** |
* Checks if a character is a space character. |
* @param {string} ch Character to check. |
- * @return {boolean} True if {@code ch} is a space. |
+ * @return {boolean} True if {code ch} is a space. |
*/ |
goog.string.isSpace = function(ch) { |
return ch == ' '; |
@@ -263,7 +199,7 @@ goog.string.isSpace = function(ch) { |
/** |
* Checks if a character is a valid unicode character. |
* @param {string} ch Character to check. |
- * @return {boolean} True if {@code ch} is a valid unicode character. |
+ * @return {boolean} True if {code ch} is a valid unicode character. |
*/ |
goog.string.isUnicodeChar = function(ch) { |
return ch.length == 1 && ch >= ' ' && ch <= '~' || |
@@ -333,17 +269,12 @@ goog.string.collapseBreakingSpaces = function(str) { |
* @param {string} str The string to trim. |
* @return {string} A trimmed copy of {@code str}. |
*/ |
-goog.string.trim = (goog.TRUSTED_SITE && String.prototype.trim) ? |
- function(str) { |
- return str.trim(); |
- } : |
- function(str) { |
- // Since IE doesn't include non-breaking-space (0xa0) in their \s |
- // character class (as required by section 7.2 of the ECMAScript spec), |
- // we explicitly include it in the regexp to enforce consistent |
- // cross-browser behavior. |
- return str.replace(/^[\s\xa0]+|[\s\xa0]+$/g, ''); |
- }; |
+goog.string.trim = function(str) { |
+ // Since IE doesn't include non-breaking-space (0xa0) in their \s character |
+ // class (as required by section 7.2 of the ECMAScript spec), we explicitly |
+ // include it in the regexp to enforce consistent cross-browser behavior. |
+ return str.replace(/^[\s\xa0]+|[\s\xa0]+$/g, ''); |
+}; |
/** |
@@ -472,6 +403,14 @@ goog.string.numerateCompare = function(str1, str2) { |
/** |
+ * Regular expression used for determining if a string needs to be encoded. |
+ * @type {RegExp} |
+ * @private |
+ */ |
+goog.string.encodeUriRegExp_ = /^[a-zA-Z0-9\-_.!~*'()]*$/; |
+ |
+ |
+/** |
* URL-encodes a string |
* @param {*} str The string to url-encode. |
* @return {string} An encoded copy of {@code str} that is safe for urls. |
@@ -479,7 +418,15 @@ goog.string.numerateCompare = function(str1, str2) { |
* of URLs *will* be encoded. |
*/ |
goog.string.urlEncode = function(str) { |
- return encodeURIComponent(String(str)); |
+ str = String(str); |
+ // Checking if the search matches before calling encodeURIComponent avoids an |
+ // extra allocation in IE6. This adds about 10us time in FF and a similiar |
+ // over head in IE6 for lower working set apps, but for large working set |
+ // apps like Gmail, it saves about 70us per call. |
+ if (!goog.string.encodeUriRegExp_.test(str)) { |
+ return encodeURIComponent(str); |
+ } |
+ return str; |
}; |
@@ -506,17 +453,13 @@ goog.string.newLineToBr = function(str, opt_xml) { |
/** |
- * Escapes double quote '"' and single quote '\'' characters in addition to |
- * '&', '<', and '>' so that a string can be included in an HTML tag attribute |
- * value within double or single quotes. |
+ * Escape double quote '"' characters in addition to '&', '<', and '>' so that a |
+ * string can be included in an HTML tag attribute value within double quotes. |
* |
* It should be noted that > doesn't need to be escaped for the HTML or XML to |
* be valid, but it has been decided to escape it for consistency with other |
* implementations. |
* |
- * With goog.string.DETECT_DOUBLE_ESCAPING, this function escapes also the |
- * lowercase letter "e". |
- * |
* NOTE(user): |
* HtmlEscape is often called during the generation of large blocks of HTML. |
* Using statics for the regular expressions and strings is an optimization |
@@ -552,43 +495,28 @@ goog.string.newLineToBr = function(str, opt_xml) { |
goog.string.htmlEscape = function(str, opt_isLikelyToContainHtmlChars) { |
if (opt_isLikelyToContainHtmlChars) { |
- str = str.replace(goog.string.AMP_RE_, '&') |
- .replace(goog.string.LT_RE_, '<') |
- .replace(goog.string.GT_RE_, '>') |
- .replace(goog.string.QUOT_RE_, '"') |
- .replace(goog.string.SINGLE_QUOTE_RE_, ''') |
- .replace(goog.string.NULL_RE_, '�'); |
- if (goog.string.DETECT_DOUBLE_ESCAPING) { |
- str = str.replace(goog.string.E_RE_, 'e'); |
- } |
- return str; |
+ return str.replace(goog.string.amperRe_, '&') |
+ .replace(goog.string.ltRe_, '<') |
+ .replace(goog.string.gtRe_, '>') |
+ .replace(goog.string.quotRe_, '"'); |
} else { |
// quick test helps in the case when there are no chars to replace, in |
// worst case this makes barely a difference to the time taken |
- if (!goog.string.ALL_RE_.test(str)) return str; |
+ if (!goog.string.allRe_.test(str)) return str; |
// str.indexOf is faster than regex.test in this case |
if (str.indexOf('&') != -1) { |
- str = str.replace(goog.string.AMP_RE_, '&'); |
+ str = str.replace(goog.string.amperRe_, '&'); |
} |
if (str.indexOf('<') != -1) { |
- str = str.replace(goog.string.LT_RE_, '<'); |
+ str = str.replace(goog.string.ltRe_, '<'); |
} |
if (str.indexOf('>') != -1) { |
- str = str.replace(goog.string.GT_RE_, '>'); |
+ str = str.replace(goog.string.gtRe_, '>'); |
} |
if (str.indexOf('"') != -1) { |
- str = str.replace(goog.string.QUOT_RE_, '"'); |
- } |
- if (str.indexOf('\'') != -1) { |
- str = str.replace(goog.string.SINGLE_QUOTE_RE_, '''); |
- } |
- if (str.indexOf('\x00') != -1) { |
- str = str.replace(goog.string.NULL_RE_, '�'); |
- } |
- if (goog.string.DETECT_DOUBLE_ESCAPING && str.indexOf('e') != -1) { |
- str = str.replace(goog.string.E_RE_, 'e'); |
+ str = str.replace(goog.string.quotRe_, '"'); |
} |
return str; |
} |
@@ -597,68 +525,42 @@ goog.string.htmlEscape = function(str, opt_isLikelyToContainHtmlChars) { |
/** |
* Regular expression that matches an ampersand, for use in escaping. |
- * @const {!RegExp} |
+ * @type {RegExp} |
* @private |
*/ |
-goog.string.AMP_RE_ = /&/g; |
+goog.string.amperRe_ = /&/g; |
/** |
* Regular expression that matches a less than sign, for use in escaping. |
- * @const {!RegExp} |
+ * @type {RegExp} |
* @private |
*/ |
-goog.string.LT_RE_ = /</g; |
+goog.string.ltRe_ = /</g; |
/** |
* Regular expression that matches a greater than sign, for use in escaping. |
- * @const {!RegExp} |
+ * @type {RegExp} |
* @private |
*/ |
-goog.string.GT_RE_ = />/g; |
+goog.string.gtRe_ = />/g; |
/** |
* Regular expression that matches a double quote, for use in escaping. |
- * @const {!RegExp} |
- * @private |
- */ |
-goog.string.QUOT_RE_ = /"/g; |
- |
- |
-/** |
- * Regular expression that matches a single quote, for use in escaping. |
- * @const {!RegExp} |
- * @private |
- */ |
-goog.string.SINGLE_QUOTE_RE_ = /'/g; |
- |
- |
-/** |
- * Regular expression that matches null character, for use in escaping. |
- * @const {!RegExp} |
- * @private |
- */ |
-goog.string.NULL_RE_ = /\x00/g; |
- |
- |
-/** |
- * Regular expression that matches a lowercase letter "e", for use in escaping. |
- * @const {!RegExp} |
+ * @type {RegExp} |
* @private |
*/ |
-goog.string.E_RE_ = /e/g; |
+goog.string.quotRe_ = /\"/g; |
/** |
* Regular expression that matches any character that needs to be escaped. |
- * @const {!RegExp} |
+ * @type {RegExp} |
* @private |
*/ |
-goog.string.ALL_RE_ = (goog.string.DETECT_DOUBLE_ESCAPING ? |
- /[\x00&<>"'e]/ : |
- /[\x00&<>"']/); |
+goog.string.allRe_ = /[&<>\"]/; |
/** |
@@ -669,10 +571,10 @@ goog.string.ALL_RE_ = (goog.string.DETECT_DOUBLE_ESCAPING ? |
*/ |
goog.string.unescapeEntities = function(str) { |
if (goog.string.contains(str, '&')) { |
- // We are careful not to use a DOM if we do not have one or we explicitly |
- // requested non-DOM html unescaping. |
- if (!goog.string.FORCE_NON_DOM_HTML_UNESCAPING && |
- 'document' in goog.global) { |
+ // We are careful not to use a DOM if we do not have one. We use the [] |
+ // notation so that the JSCompiler will not complain about these objects and |
+ // fields in the case where we have no DOM. |
+ if ('document' in goog.global) { |
return goog.string.unescapeEntitiesUsingDom_(str); |
} else { |
// Fall back on pure XML entities |
@@ -684,39 +586,15 @@ goog.string.unescapeEntities = function(str) { |
/** |
- * Unescapes a HTML string using the provided document. |
- * |
- * @param {string} str The string to unescape. |
- * @param {!Document} document A document to use in escaping the string. |
- * @return {string} An unescaped copy of {@code str}. |
- */ |
-goog.string.unescapeEntitiesWithDocument = function(str, document) { |
- if (goog.string.contains(str, '&')) { |
- return goog.string.unescapeEntitiesUsingDom_(str, document); |
- } |
- return str; |
-}; |
- |
- |
-/** |
* Unescapes an HTML string using a DOM to resolve non-XML, non-numeric |
* entities. This function is XSS-safe and whitespace-preserving. |
* @private |
* @param {string} str The string to unescape. |
- * @param {Document=} opt_document An optional document to use for creating |
- * elements. If this is not specified then the default window.document |
- * will be used. |
* @return {string} The unescaped {@code str} string. |
*/ |
-goog.string.unescapeEntitiesUsingDom_ = function(str, opt_document) { |
- /** @type {!Object<string, string>} */ |
+goog.string.unescapeEntitiesUsingDom_ = function(str) { |
var seen = {'&': '&', '<': '<', '>': '>', '"': '"'}; |
- var div; |
- if (opt_document) { |
- div = opt_document.createElement('div'); |
- } else { |
- div = goog.global.document.createElement('div'); |
- } |
+ var div = document.createElement('div'); |
// Match as many valid entity characters as possible. If the actual entity |
// happens to be shorter, it will still work as innerHTML will return the |
// trailing characters unchanged. Since the entity characters do not include |
@@ -799,23 +677,11 @@ goog.string.HTML_ENTITY_PATTERN_ = /&([^;\s<&]+);?/g; |
* @return {string} An escaped copy of {@code str}. |
*/ |
goog.string.whitespaceEscape = function(str, opt_xml) { |
- // This doesn't use goog.string.preserveSpaces for backwards compatibility. |
return goog.string.newLineToBr(str.replace(/ /g, '  '), opt_xml); |
}; |
/** |
- * Preserve spaces that would be otherwise collapsed in HTML by replacing them |
- * with non-breaking space Unicode characters. |
- * @param {string} str The string in which to preserve whitespace. |
- * @return {string} A copy of {@code str} with preserved whitespace. |
- */ |
-goog.string.preserveSpaces = function(str) { |
- return str.replace(/(^|[\n ]) /g, '$1' + goog.string.Unicode.NBSP); |
-}; |
- |
- |
-/** |
* Strip quote characters around a string. The second argument is a string of |
* characters to treat as quotes. This can be a single character or a string of |
* multiple character and in that case each of those are treated as possible |
@@ -912,7 +778,8 @@ goog.string.truncateMiddle = function(str, chars, |
/** |
* Special chars that need to be escaped for goog.string.quote. |
- * @private {!Object<string, string>} |
+ * @private |
+ * @type {Object} |
*/ |
goog.string.specialEscapeChars_ = { |
'\0': '\\0', |
@@ -929,7 +796,8 @@ goog.string.specialEscapeChars_ = { |
/** |
* Character mappings used internally for goog.string.escapeChar. |
- * @private {!Object<string, string>} |
+ * @private |
+ * @type {Object} |
*/ |
goog.string.jsEscapeCache_ = { |
'\'': '\\\'' |
@@ -1014,36 +882,31 @@ goog.string.escapeChar = function(c) { |
/** |
- * Determines whether a string contains a substring. |
- * @param {string} str The string to search. |
- * @param {string} subString The substring to search for. |
- * @return {boolean} Whether {@code str} contains {@code subString}. |
- */ |
-goog.string.contains = function(str, subString) { |
- return str.indexOf(subString) != -1; |
-}; |
- |
- |
-/** |
- * Determines whether a string contains a substring, ignoring case. |
- * @param {string} str The string to search. |
- * @param {string} subString The substring to search for. |
- * @return {boolean} Whether {@code str} contains {@code subString}. |
+ * Takes a string and creates a map (Object) in which the keys are the |
+ * characters in the string. The value for the key is set to true. You can |
+ * then use goog.object.map or goog.array.map to change the values. |
+ * @param {string} s The string to build the map from. |
+ * @return {Object} The map of characters used. |
*/ |
-goog.string.caseInsensitiveContains = function(str, subString) { |
- return goog.string.contains(str.toLowerCase(), subString.toLowerCase()); |
+// TODO(arv): It seems like we should have a generic goog.array.toMap. But do |
+// we want a dependency on goog.array in goog.string? |
+goog.string.toMap = function(s) { |
+ var rv = {}; |
+ for (var i = 0; i < s.length; i++) { |
+ rv[s.charAt(i)] = true; |
+ } |
+ return rv; |
}; |
/** |
- * Returns the non-overlapping occurrences of ss in s. |
- * If either s or ss evalutes to false, then returns zero. |
- * @param {string} s The string to look in. |
- * @param {string} ss The string to look for. |
- * @return {number} Number of occurrences of ss in s. |
+ * Checks whether a string contains a given character. |
+ * @param {string} s The string to test. |
+ * @param {string} ss The substring to test for. |
+ * @return {boolean} True if {@code s} contains {@code ss}. |
*/ |
-goog.string.countOf = function(s, ss) { |
- return s && ss ? s.split(ss).length - 1 : 0; |
+goog.string.contains = function(s, ss) { |
+ return s.indexOf(ss) != -1; |
}; |
@@ -1314,7 +1177,7 @@ goog.string.createUniqueString = function() { |
/** |
- * Converts the supplied string to a number, which may be Infinity or NaN. |
+ * Converts the supplied string to a number, which may be Ininity or NaN. |
* This function strips whitespace: (toNumber(' 123') === 123) |
* This function accepts scientific notation: (toNumber('1e1') === 10) |
* |
@@ -1326,7 +1189,7 @@ goog.string.createUniqueString = function() { |
*/ |
goog.string.toNumber = function(str) { |
var num = Number(str); |
- if (num == 0 && goog.string.isEmptyOrWhitespace(str)) { |
+ if (num == 0 && goog.string.isEmpty(str)) { |
return NaN; |
} |
return num; |
@@ -1334,31 +1197,11 @@ goog.string.toNumber = function(str) { |
/** |
- * Returns whether the given string is lower camel case (e.g. "isFooBar"). |
- * |
- * Note that this assumes the string is entirely letters. |
- * @see http://en.wikipedia.org/wiki/CamelCase#Variations_and_synonyms |
- * |
- * @param {string} str String to test. |
- * @return {boolean} Whether the string is lower camel case. |
- */ |
-goog.string.isLowerCamelCase = function(str) { |
- return /^[a-z]+([A-Z][a-z]*)*$/.test(str); |
-}; |
- |
- |
-/** |
- * Returns whether the given string is upper camel case (e.g. "FooBarBaz"). |
- * |
- * Note that this assumes the string is entirely letters. |
- * @see http://en.wikipedia.org/wiki/CamelCase#Variations_and_synonyms |
- * |
- * @param {string} str String to test. |
- * @return {boolean} Whether the string is upper camel case. |
+ * A memoized cache for goog.string.toCamelCase. |
+ * @type {Object.<string>} |
+ * @private |
*/ |
-goog.string.isUpperCamelCase = function(str) { |
- return /^([A-Z][a-z]*)+$/.test(str); |
-}; |
+goog.string.toCamelCaseCache_ = {}; |
/** |
@@ -1369,13 +1212,23 @@ goog.string.isUpperCamelCase = function(str) { |
* @return {string} The string in camelCase form. |
*/ |
goog.string.toCamelCase = function(str) { |
- return String(str).replace(/\-([a-z])/g, function(all, match) { |
- return match.toUpperCase(); |
- }); |
+ return goog.string.toCamelCaseCache_[str] || |
+ (goog.string.toCamelCaseCache_[str] = |
+ String(str).replace(/\-([a-z])/g, function(all, match) { |
+ return match.toUpperCase(); |
+ })); |
}; |
/** |
+ * A memoized cache for goog.string.toSelectorCase. |
+ * @type {Object.<string>} |
+ * @private |
+ */ |
+goog.string.toSelectorCaseCache_ = {}; |
+ |
+ |
+/** |
* Converts a string from camelCase to selector-case (e.g. from |
* "multiPartString" to "multi-part-string"), useful for converting JS |
* style and dataset properties to equivalent CSS selectors and HTML keys. |
@@ -1383,183 +1236,7 @@ goog.string.toCamelCase = function(str) { |
* @return {string} The string in selector-case form. |
*/ |
goog.string.toSelectorCase = function(str) { |
- return String(str).replace(/([A-Z])/g, '-$1').toLowerCase(); |
-}; |
- |
- |
-/** |
- * Converts a string into TitleCase. First character of the string is always |
- * capitalized in addition to the first letter of every subsequent word. |
- * Words are delimited by one or more whitespaces by default. Custom delimiters |
- * can optionally be specified to replace the default, which doesn't preserve |
- * whitespace delimiters and instead must be explicitly included if needed. |
- * |
- * Default delimiter => " ": |
- * goog.string.toTitleCase('oneTwoThree') => 'OneTwoThree' |
- * goog.string.toTitleCase('one two three') => 'One Two Three' |
- * goog.string.toTitleCase(' one two ') => ' One Two ' |
- * goog.string.toTitleCase('one_two_three') => 'One_two_three' |
- * goog.string.toTitleCase('one-two-three') => 'One-two-three' |
- * |
- * Custom delimiter => "_-.": |
- * goog.string.toTitleCase('oneTwoThree', '_-.') => 'OneTwoThree' |
- * goog.string.toTitleCase('one two three', '_-.') => 'One two three' |
- * goog.string.toTitleCase(' one two ', '_-.') => ' one two ' |
- * goog.string.toTitleCase('one_two_three', '_-.') => 'One_Two_Three' |
- * goog.string.toTitleCase('one-two-three', '_-.') => 'One-Two-Three' |
- * goog.string.toTitleCase('one...two...three', '_-.') => 'One...Two...Three' |
- * goog.string.toTitleCase('one. two. three', '_-.') => 'One. two. three' |
- * goog.string.toTitleCase('one-two.three', '_-.') => 'One-Two.Three' |
- * |
- * @param {string} str String value in camelCase form. |
- * @param {string=} opt_delimiters Custom delimiter character set used to |
- * distinguish words in the string value. Each character represents a |
- * single delimiter. When provided, default whitespace delimiter is |
- * overridden and must be explicitly included if needed. |
- * @return {string} String value in TitleCase form. |
- */ |
-goog.string.toTitleCase = function(str, opt_delimiters) { |
- var delimiters = goog.isString(opt_delimiters) ? |
- goog.string.regExpEscape(opt_delimiters) : '\\s'; |
- |
- // For IE8, we need to prevent using an empty character set. Otherwise, |
- // incorrect matching will occur. |
- delimiters = delimiters ? '|[' + delimiters + ']+' : ''; |
- |
- var regexp = new RegExp('(^' + delimiters + ')([a-z])', 'g'); |
- return str.replace(regexp, function(all, p1, p2) { |
- return p1 + p2.toUpperCase(); |
- }); |
-}; |
- |
- |
-/** |
- * Capitalizes a string, i.e. converts the first letter to uppercase |
- * and all other letters to lowercase, e.g.: |
- * |
- * goog.string.capitalize('one') => 'One' |
- * goog.string.capitalize('ONE') => 'One' |
- * goog.string.capitalize('one two') => 'One two' |
- * |
- * Note that this function does not trim initial whitespace. |
- * |
- * @param {string} str String value to capitalize. |
- * @return {string} String value with first letter in uppercase. |
- */ |
-goog.string.capitalize = function(str) { |
- return String(str.charAt(0)).toUpperCase() + |
- String(str.substr(1)).toLowerCase(); |
-}; |
- |
- |
-/** |
- * Parse a string in decimal or hexidecimal ('0xFFFF') form. |
- * |
- * To parse a particular radix, please use parseInt(string, radix) directly. See |
- * https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/parseInt |
- * |
- * This is a wrapper for the built-in parseInt function that will only parse |
- * numbers as base 10 or base 16. Some JS implementations assume strings |
- * starting with "0" are intended to be octal. ES3 allowed but discouraged |
- * this behavior. ES5 forbids it. This function emulates the ES5 behavior. |
- * |
- * For more information, see Mozilla JS Reference: http://goo.gl/8RiFj |
- * |
- * @param {string|number|null|undefined} value The value to be parsed. |
- * @return {number} The number, parsed. If the string failed to parse, this |
- * will be NaN. |
- */ |
-goog.string.parseInt = function(value) { |
- // Force finite numbers to strings. |
- if (isFinite(value)) { |
- value = String(value); |
- } |
- |
- if (goog.isString(value)) { |
- // If the string starts with '0x' or '-0x', parse as hex. |
- return /^\s*-?0x/i.test(value) ? |
- parseInt(value, 16) : parseInt(value, 10); |
- } |
- |
- return NaN; |
-}; |
- |
- |
-/** |
- * Splits a string on a separator a limited number of times. |
- * |
- * This implementation is more similar to Python or Java, where the limit |
- * parameter specifies the maximum number of splits rather than truncating |
- * the number of results. |
- * |
- * See http://docs.python.org/2/library/stdtypes.html#str.split |
- * See JavaDoc: http://goo.gl/F2AsY |
- * See Mozilla reference: http://goo.gl/dZdZs |
- * |
- * @param {string} str String to split. |
- * @param {string} separator The separator. |
- * @param {number} limit The limit to the number of splits. The resulting array |
- * will have a maximum length of limit+1. Negative numbers are the same |
- * as zero. |
- * @return {!Array<string>} The string, split. |
- */ |
- |
-goog.string.splitLimit = function(str, separator, limit) { |
- var parts = str.split(separator); |
- var returnVal = []; |
- |
- // Only continue doing this while we haven't hit the limit and we have |
- // parts left. |
- while (limit > 0 && parts.length) { |
- returnVal.push(parts.shift()); |
- limit--; |
- } |
- |
- // If there are remaining parts, append them to the end. |
- if (parts.length) { |
- returnVal.push(parts.join(separator)); |
- } |
- |
- return returnVal; |
-}; |
- |
- |
-/** |
- * Computes the Levenshtein edit distance between two strings. |
- * @param {string} a |
- * @param {string} b |
- * @return {number} The edit distance between the two strings. |
- */ |
-goog.string.editDistance = function(a, b) { |
- var v0 = []; |
- var v1 = []; |
- |
- if (a == b) { |
- return 0; |
- } |
- |
- if (!a.length || !b.length) { |
- return Math.max(a.length, b.length); |
- } |
- |
- for (var i = 0; i < b.length + 1; i++) { |
- v0[i] = i; |
- } |
- |
- for (var i = 0; i < a.length; i++) { |
- v1[0] = i + 1; |
- |
- for (var j = 0; j < b.length; j++) { |
- var cost = a[i] != b[j]; |
- // Cost for the substring is the minimum of adding one character, removing |
- // one character, or a swap. |
- v1[j + 1] = Math.min(v1[j] + 1, v0[j + 1] + 1, v0[j] + cost); |
- } |
- |
- for (var j = 0; j < v0.length; j++) { |
- v0[j] = v1[j]; |
- } |
- } |
- |
- return v1[b.length]; |
+ return goog.string.toSelectorCaseCache_[str] || |
+ (goog.string.toSelectorCaseCache_[str] = |
+ String(str).replace(/([A-Z])/g, '-$1').toLowerCase()); |
}; |