|
153 | 153 | var reRegExpChar = /[\\^$.*+?()[\]{}|]/g,
|
154 | 154 | reHasRegExpChar = RegExp(reRegExpChar.source);
|
155 | 155 |
|
156 |
| - /** Used to match leading and trailing whitespace. */ |
157 |
| - var reTrim = /^\s+|\s+$/g, |
158 |
| - reTrimStart = /^\s+/, |
159 |
| - reTrimEnd = /\s+$/; |
| 156 | + /** Used to match leading whitespace. */ |
| 157 | + var reTrimStart = /^\s+/; |
| 158 | + |
| 159 | + /** Used to match a single whitespace character. */ |
| 160 | + var reWhitespace = /\s/; |
160 | 161 |
|
161 | 162 | /** Used to match wrap detail comments. */
|
162 | 163 | var reWrapComment = /\{(?:\n\/\* \[wrapped with .+\] \*\/)?\n?/,
|
|
1006 | 1007 | });
|
1007 | 1008 | }
|
1008 | 1009 |
|
| 1010 | + /** |
| 1011 | + * The base implementation of `_.trim`. |
| 1012 | + * |
| 1013 | + * @private |
| 1014 | + * @param {string} string The string to trim. |
| 1015 | + * @returns {string} Returns the trimmed string. |
| 1016 | + */ |
| 1017 | + function baseTrim(string) { |
| 1018 | + return string |
| 1019 | + ? string.slice(0, trimmedEndIndex(string) + 1).replace(reTrimStart, '') |
| 1020 | + : string; |
| 1021 | + } |
| 1022 | + |
1009 | 1023 | /**
|
1010 | 1024 | * The base implementation of `_.unary` without support for storing metadata.
|
1011 | 1025 | *
|
|
1339 | 1353 | : asciiToArray(string);
|
1340 | 1354 | }
|
1341 | 1355 |
|
| 1356 | + /** |
| 1357 | + * Used by `_.trim` and `_.trimEnd` to get the index of the last non-whitespace |
| 1358 | + * character of `string`. |
| 1359 | + * |
| 1360 | + * @private |
| 1361 | + * @param {string} string The string to inspect. |
| 1362 | + * @returns {number} Returns the index of the last non-whitespace character. |
| 1363 | + */ |
| 1364 | + function trimmedEndIndex(string) { |
| 1365 | + var index = string.length; |
| 1366 | + |
| 1367 | + while (index-- && reWhitespace.test(string.charAt(index))) {} |
| 1368 | + return index; |
| 1369 | + } |
| 1370 | + |
1342 | 1371 | /**
|
1343 | 1372 | * Used by `_.unescape` to convert HTML entities to characters.
|
1344 | 1373 | *
|
|
12507 | 12536 | if (typeof value != 'string') {
|
12508 | 12537 | return value === 0 ? value : +value;
|
12509 | 12538 | }
|
12510 |
| - value = value.replace(reTrim, ''); |
| 12539 | + value = baseTrim(value); |
12511 | 12540 | var isBinary = reIsBinary.test(value);
|
12512 | 12541 | return (isBinary || reIsOctal.test(value))
|
12513 | 12542 | ? freeParseInt(value.slice(2), isBinary ? 2 : 8)
|
|
14998 | 15027 | function trim(string, chars, guard) {
|
14999 | 15028 | string = toString(string);
|
15000 | 15029 | if (string && (guard || chars === undefined)) {
|
15001 |
| - return string.replace(reTrim, ''); |
| 15030 | + return baseTrim(string); |
15002 | 15031 | }
|
15003 | 15032 | if (!string || !(chars = baseToString(chars))) {
|
15004 | 15033 | return string;
|
|
15033 | 15062 | function trimEnd(string, chars, guard) {
|
15034 | 15063 | string = toString(string);
|
15035 | 15064 | if (string && (guard || chars === undefined)) {
|
15036 |
| - return string.replace(reTrimEnd, ''); |
| 15065 | + return string.slice(0, trimmedEndIndex(string) + 1); |
15037 | 15066 | }
|
15038 | 15067 | if (!string || !(chars = baseToString(chars))) {
|
15039 | 15068 | return string;
|
|
0 commit comments