Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8)

Unified Diff: third_party/google_input_tools/third_party/closure_library/closure/goog/string/string.js

Issue 1257313003: Update Google Input Tools (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Free up grd resources. Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/google_input_tools/third_party/closure_library/closure/goog/string/string.js
diff --git a/third_party/google_input_tools/third_party/closure_library/closure/goog/string/string.js b/third_party/google_input_tools/third_party/closure_library/closure/goog/string/string.js
index 90ee58cedc6d78d58a4a244ebcec416386d85d95..065a1a8409b4e453833be736d94409b00394c699 100644
--- a/third_party/google_input_tools/third_party/closure_library/closure/goog/string/string.js
+++ b/third_party/google_input_tools/third_party/closure_library/closure/goog/string/string.js
@@ -14,6 +14,7 @@
/**
* @fileoverview Utilities for string manipulation.
+ * @author arv@google.com (Erik Arvidsson)
*/
@@ -32,6 +33,12 @@ 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}
*/
@@ -142,9 +149,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} True if {@code str} is empty or whitespace only.
+ * @return {boolean} Whether {@code str} is empty or whitespace only.
*/
-goog.string.isEmpty = function(str) {
+goog.string.isEmptyOrWhitespace = 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
@@ -155,17 +162,54 @@ goog.string.isEmpty = 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.
* @param {*} str The string to check.
- * @return {boolean} True if{@code str} is null, undefined, empty, or
+ * @return {boolean} Whether {@code str} is null, undefined, empty, or
* whitespace only.
+ * @deprecated Use goog.string.isEmptyOrWhitespace(goog.string.makeSafe(str))
+ * instead.
*/
-goog.string.isEmptySafe = function(str) {
- return goog.string.isEmpty(goog.string.makeSafe(str));
+goog.string.isEmptyOrWhitespaceSafe = function(str) {
+ return goog.string.isEmptyOrWhitespace(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.
@@ -209,7 +253,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 == ' ';
@@ -219,7 +263,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 <= '~' ||
@@ -289,12 +333,17 @@ goog.string.collapseBreakingSpaces = function(str) {
* @param {string} str The string to trim.
* @return {string} A trimmed copy of {@code str}.
*/
-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, '');
-};
+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, '');
+ };
/**
@@ -620,10 +669,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. 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) {
+ // 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) {
return goog.string.unescapeEntitiesUsingDom_(str);
} else {
// Fall back on pure XML entities
@@ -660,6 +709,7 @@ goog.string.unescapeEntitiesWithDocument = function(str, document) {
* @return {string} The unescaped {@code str} string.
*/
goog.string.unescapeEntitiesUsingDom_ = function(str, opt_document) {
+ /** @type {!Object<string, string>} */
var seen = {'&amp;': '&', '&lt;': '<', '&gt;': '>', '&quot;': '"'};
var div;
if (opt_document) {
@@ -862,8 +912,7 @@ goog.string.truncateMiddle = function(str, chars,
/**
* Special chars that need to be escaped for goog.string.quote.
- * @private
- * @type {Object}
+ * @private {!Object<string, string>}
*/
goog.string.specialEscapeChars_ = {
'\0': '\\0',
@@ -880,8 +929,7 @@ goog.string.specialEscapeChars_ = {
/**
* Character mappings used internally for goog.string.escapeChar.
- * @private
- * @type {Object}
+ * @private {!Object<string, string>}
*/
goog.string.jsEscapeCache_ = {
'\'': '\\\''
@@ -966,24 +1014,6 @@ goog.string.escapeChar = function(c) {
/**
- * 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.
- */
-// 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;
-};
-
-
-/**
* Determines whether a string contains a substring.
* @param {string} str The string to search.
* @param {string} subString The substring to search for.
@@ -1296,7 +1326,7 @@ goog.string.createUniqueString = function() {
*/
goog.string.toNumber = function(str) {
var num = Number(str);
- if (num == 0 && goog.string.isEmpty(str)) {
+ if (num == 0 && goog.string.isEmptyOrWhitespace(str)) {
return NaN;
}
return num;
@@ -1404,6 +1434,25 @@ goog.string.toTitleCase = function(str, opt_delimiters) {
/**
+ * 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
@@ -1452,7 +1501,7 @@ goog.string.parseInt = function(value) {
* @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.
+ * @return {!Array<string>} The string, split.
*/
goog.string.splitLimit = function(str, separator, limit) {
@@ -1474,3 +1523,43 @@ goog.string.splitLimit = function(str, separator, limit) {
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];
+};

Powered by Google App Engine
This is Rietveld 408576698