Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3c82ebb

Browse files
committed
Deprecated: improve .trim for strings containing lots of whitespaces
The old implementation took O(N^2) time to trim the string when multipleadjacent spaces were present.For instance, consider the string "A B" (10 spaces between A and B).Then old regexp /[\s]+$/ would take 10 steps until it realizesthe regexp does not match at position 1.Then it would try to match the regexp at position 2, and it would take 9 steps.Then 8 steps for position 3, ... so it would take 10*10/2 steps untilit figures out the regexp does not match.The new approach is to require "non-whitespace" char before the whitespace run,so it spends just one step for each space in the string.
1 parent0f6c3d9 commit3c82ebb

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

‎src/deprecated.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@ define( [
1515

1616
// Support: Android <=4.0 only
1717
// Make sure we trim BOM and NBSP
18-
varrtrim=/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;
18+
// Require that the "whitespace run" starts from a non-whitespace
19+
// to avoid O(N^2) behavior when the engine would try matching "\s$" at each space position.
20+
// It is important that non-whitespace char is mandatory for regexp performance reasons,
21+
// however, it does not break correctness since whitespace-only string will be trimmed by ltrim above.
22+
varltrim=/^[\s\uFEFF\xA0]+/,
23+
rtrim=/([^\s\uFEFF\xA0])[\s\uFEFF\xA0]+$/,
24+
useDefaultTrim=false;
1925

2026
// Bind a function to a context, optionally partially applying any
2127
// arguments.
@@ -79,9 +85,16 @@ jQuery.isNumeric = function( obj ) {
7985
!isNaN(obj-parseFloat(obj));
8086
};
8187

88+
if(String.prototype.trim){
89+
useDefaultTrim="_"===" \uFEFF\xA0_\uFEFF\xA0 ".trim();
90+
}
91+
8292
jQuery.trim=function(text){
83-
returntext==null ?
84-
"" :
85-
(text+"").replace(rtrim,"");
93+
if(text==null){
94+
return"";
95+
}
96+
returnuseDefaultTrim ?
97+
(text+"").trim() :
98+
(text+"").replace(ltrim,"").replace(rtrim,"$1");
8699
};
87100
});

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp