Reimplement foldString without regex
Lookbehind in regex is not supported in Safari browsers. This implementation should be more efficient so why not.
This commit is contained in:
1 parent
478a0af781
commit
2ca0caa258
1 file changed
+24
-9
+24
-9
@@ -29,21 +29,36 @@ function stringifyKey(str: string): string {
|
|||||||
function foldString(str: string): string[] {
|
function foldString(str: string): string[] {
|
||||||
if (str.length <= LINE_WIDTH) return [str];
|
if (str.length <= LINE_WIDTH) return [str];
|
||||||
if (str.startsWith(" ")) return [str];
|
if (str.startsWith(" ")) return [str];
|
||||||
const words = str.split(/(?<=[^ ]) (?=[^ ])/);
|
|
||||||
const lines: string[] = [];
|
const lines: string[] = [];
|
||||||
|
|
||||||
let idx = 0;
|
let idx = 0;
|
||||||
let len = 0;
|
let cand = 0;
|
||||||
for (const [i, word] of words.entries()) {
|
for (let i = 1; i < str.length - 1; ++i) {
|
||||||
len += word.length + 1;
|
if (str[i] !== " ") continue;
|
||||||
if (len >= LINE_WIDTH && len !== word.length) {
|
|
||||||
lines.push(words.slice(idx, i).join(" "));
|
if (str[i + 1] === " ") {
|
||||||
idx = i;
|
i += 2;
|
||||||
len = 0;
|
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;
|
return lines;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user