Replace isWhitespaceAt with trimmedLeftIndex and trimmedRightIndex.

This commit is contained in:
John-David Dalton
2014-01-01 10:52:28 -06:00
parent 5965591934
commit 6e5846d206
6 changed files with 243 additions and 191 deletions

View File

@@ -39,7 +39,7 @@
'\n\r\u2028\u2029' +
// unicode category "Zs" space separators
'\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000'
'\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000'
);
/** Used to match empty string literals in compiled template source */
@@ -440,23 +440,6 @@
return typeof value.toString != 'function' && typeof (value + '') == 'string';
}
/**
* Checks the given string to determine if the character at the specified
* index is whitespace.
*
* @private
* @param {string} string The string to inspect.
* @param {number} index The character index.
* @returns {boolean} Returns `true` if specifed character is whitespace, else `false`.
*/
function isWhitespaceAt(string, index) {
var c = string.charCodeAt(index);
return (
(c <= 160 && (c >= 9 && c <= 13) || c == 32 || c == 160) || c == 5760 || c == 6158 ||
(c >= 8192 && (c <= 8202 || c == 8232 || c == 8233 || c == 8239 || c == 8287 || c == 12288 || c == 65279))
);
}
/**
* Releases the given array back to the array pool.
*
@@ -515,6 +498,46 @@
return result;
}
/**
* Gets the index of the first non-whitespace character of a given string.
*
* @private
* @param {string} string The string to inspect.
* @returns {number} Returns the index of the first non-whitespace character.
*/
function trimmedLeftIndex(string) {
var index = -1,
length = string.length;
while (++index < length) {
var c = string.charCodeAt(index);
if (!((c <= 160 && (c >= 9 && c <= 13) || c == 32 || c == 160) || c == 5760 || c == 6158 ||
(c >= 8192 && (c <= 8202 || c == 8232 || c == 8233 || c == 8239 || c == 8287 || c == 12288 || c == 65279)))) {
break;
}
}
return index;
}
/**
* Gets the index of the last non-whitespace character of a given string.
*
* @private
* @param {string} string The string to inspect.
* @returns {number} Returns the index of the last non-whitespace character.
*/
function trimmedRightIndex(string) {
var index = string.length;
while (index--) {
var c = string.charCodeAt(index);
if (!((c <= 160 && (c >= 9 && c <= 13) || c == 32 || c == 160) || c == 5760 || c == 6158 ||
(c >= 8192 && (c <= 8202 || c == 8232 || c == 8233 || c == 8239 || c == 8287 || c == 12288 || c == 65279)))) {
break;
}
}
return index;
}
/*--------------------------------------------------------------------------*/
/**
@@ -1942,16 +1965,10 @@
// fallback for environments without a proper `String#trim`
if (!nativeTrim || nativeTrim.call(whitespace)) {
trim = function(string) {
var start = -1,
end = string ? string.length : 0;
if (!end || string == null) {
return '';
}
string = String(string);
while (++start < end && isWhitespaceAt(string, start)) { }
while (end-- && isWhitespaceAt(string, end)) { }
return string.slice(start, end + 1);
string = string == null ? '' : String(string);
return string
? string.slice(trimmedLeftIndex(string), trimmedRightIndex(string) + 1)
: string;
};
}