From 33439eb3a7764d6d65dda73a8de30cce33d08e7e Mon Sep 17 00:00:00 2001 From: Tatsunori Uchino Date: Sat, 14 Dec 2024 00:27:06 +0900 Subject: [PATCH] Don't use regex that can be slow --- lib/inlines.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/inlines.js b/lib/inlines.js index 1f0cbb7c..7246cd41 100644 --- a/lib/inlines.js +++ b/lib/inlines.js @@ -984,13 +984,33 @@ var parseInlines = function(block) { // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trim#return_value // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#white_space // Removes only ASCII tab and space. - this.subject = block._string_content.replace(/^[\t \r\n]+|[\t \r\n]+$/g, "") + this.subject = trim(block._string_content) this.pos = 0; this.delimiters = null; this.brackets = null; while (this.parseInline(block)) {} block._string_content = null; // allow raw string to be garbage collected this.processEmphasis(null); + + function trim(str) { + let start = 0; + for(; start < str.length; start++) { + if (!isSpace(str[start])) { + break; + } + } + let end = str.length - 1; + for(; end >= start; end--) { + if (!isSpace(str[end])) { + break; + } + } + return str.slice(start, end + 1); + + function isSpace(c) { + return c === " " || c === "\t" || c === "\n" || c === "\r"; + } + } }; // The InlineParser object.