Skip to content

Commit 21f85f8

Browse files
committed
Don't use regex that can be slow
1 parent f3145e8 commit 21f85f8

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

lib/inlines.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -980,17 +980,38 @@ var parseInline = function(block) {
980980
// Parse string content in block into inline children,
981981
// using refmap to resolve references.
982982
var parseInlines = function(block) {
983-
// trim() removes non-ASCII whitespaces, vertical tab, form feed and so on.
983+
// String.protoype.trim() removes non-ASCII whitespaces, vertical tab, form feed and so on.
984984
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trim#return_value
985985
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#white_space
986986
// Removes only ASCII tab and space.
987-
this.subject = block._string_content.replace(/^[\t \r\n]+|[\t \r\n]+$/g, "")
987+
this.subject = trim(block._string_content)
988988
this.pos = 0;
989989
this.delimiters = null;
990990
this.brackets = null;
991991
while (this.parseInline(block)) {}
992992
block._string_content = null; // allow raw string to be garbage collected
993993
this.processEmphasis(null);
994+
995+
function trim(str) {
996+
var start = 0;
997+
for(; start < str.length; start++) {
998+
if (!isSpace(str.charCodeAt(start))) {
999+
break;
1000+
}
1001+
}
1002+
var end = str.length - 1;
1003+
for(; end >= start; end--) {
1004+
if (!isSpace(str.charCodeAt(end))) {
1005+
break;
1006+
}
1007+
}
1008+
return str.slice(start, end + 1);
1009+
1010+
function isSpace(c) {
1011+
// U+0020 = space, U+0009 = tab, U+000A = LF, U+000D = CR
1012+
return c === 0x20 || c === 9 || c === 0xa || c === 0xd;
1013+
}
1014+
}
9941015
};
9951016

9961017
// The InlineParser object.

0 commit comments

Comments
 (0)