From 2ca0caa258ffca3389a140835df50acf9afca138 Mon Sep 17 00:00:00 2001 From: Zaid Abdulla Date: Fri, 26 Feb 2021 13:22:58 -0800 Subject: [PATCH] Reimplement foldString without regex Lookbehind in regex is not supported in Safari browsers. This implementation should be more efficient so why not. --- lib/common/yaml.ts | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/lib/common/yaml.ts b/lib/common/yaml.ts index 77a3ce9..2bb0e09 100644 --- a/lib/common/yaml.ts +++ b/lib/common/yaml.ts @@ -29,21 +29,36 @@ function stringifyKey(str: string): string { function foldString(str: string): string[] { if (str.length <= LINE_WIDTH) return [str]; if (str.startsWith(" ")) return [str]; - const words = str.split(/(?<=[^ ]) (?=[^ ])/); const lines: string[] = []; let idx = 0; - let len = 0; - for (const [i, word] of words.entries()) { - len += word.length + 1; - if (len >= LINE_WIDTH && len !== word.length) { - lines.push(words.slice(idx, i).join(" ")); - idx = i; - len = 0; + let cand = 0; + for (let i = 1; i < str.length - 1; ++i) { + if (str[i] !== " ") continue; + + if (str[i + 1] === " ") { + i += 2; + while (str[i] === " ") ++i; + continue; } + + if (i <= idx + LINE_WIDTH) { + cand = i; + continue; + } + + const c = cand > idx ? cand : i; + lines.push(str.slice(idx, c)); + idx = c + 1; + cand = i; } - if (idx < words.length) lines.push(words.slice(idx).join(" ")); + if (cand > idx && str.length > idx + LINE_WIDTH) { + lines.push(str.slice(idx, cand)); + idx = cand + 1; + } + + lines.push(str.slice(idx)); return lines; }