Optimize trim.

This commit is contained in:
John-David Dalton
2013-12-31 20:23:25 -06:00
parent 78f8a03da5
commit 5965591934
5 changed files with 165 additions and 117 deletions

View File

@@ -71,9 +71,6 @@
/** Used to detect hexadecimal string values */
var reHexPrefix = /^0[xX]/;
/** Used to match leading whitespace in strings */
var reLeadingWhitespace = RegExp('^[' + whitespace + ']+');
/** Used to ensure capturing order of template delimiters */
var reNoMatch = /($^)/;
@@ -443,6 +440,23 @@
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.
*
@@ -1935,8 +1949,8 @@
return '';
}
string = String(string);
while (whitespace.indexOf(string.charAt(++start)) > -1) { }
while (whitespace.indexOf(string.charAt(--end)) > -1) { }
while (++start < end && isWhitespaceAt(string, start)) { }
while (end-- && isWhitespaceAt(string, end)) { }
return string.slice(start, end + 1);
};
}