Let's try mozilla's fixedCharAt function

master
Caleb Fontenot 2023-04-30 22:56:54 +07:00
parent e0b3279cc1
commit 025ee853b0
1 changed files with 21 additions and 30 deletions

@ -217,25 +217,16 @@ function charIterate(input) {
let temp = input.split('');
for (let i = 0; i < stringLength; ++i) {
let compareString = temp.join("");
//console.log(surrogatePairs.exec(compareString), compareString);
let stringEval = undefined, stringEvalIndex = undefined;
try {
while ((stringEval = surrogatePairs.exec(compareString)) !== null) {
stringEvalIndex = stringEval.index;
//console.log("emoji is at index", stringEvalIndex);
}
} catch (e) {
console.log(e);
console.log("Handling exception...");
while ((stringEval = surrogatePairs.exec(compareString)) !== null) {
stringEvalIndex = stringEval.index;
//console.log("emoji is at index", stringEvalIndex);
}
if (stringEvalIndex == i) {
stringLength--;
temp.splice(i, 1);
//console.log("Decreasing stringLength variable to", stringLength);
}
}
if (charIterateState == undefined) {
console.log("undefined! setting to zero");
@ -299,7 +290,7 @@ function design5(input) {
outputLine += fixedCharAt(input, charIterate(input)) + space + input + space + spaceOffset + fixedCharAt(input, charIterate(input));
}
//console.log(i, j, outputLine);
console.log(i, j, outputLine);
if (lineHasInput) {
outputString += outputLine + "\n";
}
@ -312,33 +303,33 @@ function design5(input) {
function fixedCharAt(str, idx) {
str = String(str);
//const surrogatePairs = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g;
while (surrogatePairs.exec(str) !== null) {
const lastIdx = surrogatePairs.lastIndex;
if (lastIdx - 2 < idx) {
idx++;
} else {
break;
}
const lastIdx = surrogatePairs.lastIndex;
if (lastIdx - 2 < idx) {
idx++;
} else {
break;
}
}
if (idx >= str.length || idx < 0) {
return "";
return "";
}
let ret = str.charAt(idx);
if (
/[\uD800-\uDBFF]/.test(ret) &&
/[\uDC00-\uDFFF]/.test(str.charAt(idx + 1))
/[\uD800-\uDBFF]/.test(ret) &&
/[\uDC00-\uDFFF]/.test(str.charAt(idx + 1))
) {
// Go one further, since one of the "characters" is part of a surrogate pair
ret += str.charAt(idx + 1);
// Go one further, since one of the "characters" is part of a surrogate pair
ret += str.charAt(idx + 1);
}
return ret;
}
}
//console.log(design4("super long test string super long test string"));
//"super long test string super long test string"