Let's try mozilla's fixedCharAt function

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