fix(uart) sending utf-8 text, some part of text was ignored

This commit is contained in:
kerms 2024-07-17 09:45:10 +08:00
parent 70cf85aba5
commit dc861ab4ef
1 changed files with 46 additions and 11 deletions

View File

@ -54,43 +54,77 @@ function unescapeString(str: string): Uint8Array {
i++; i++;
switch (str[i]) { switch (str[i]) {
case 'n': case 'n':
resultArray.push('\n'.charCodeAt(0)); resultArray.push(0x0A); // LF
break; break;
case 'r': case 'r':
resultArray.push('\r'.charCodeAt(0)); resultArray.push(0x0D); // CR
break; break;
case 't': case 't':
resultArray.push('\t'.charCodeAt(0)); resultArray.push(0x09); // Tab
break; break;
case 'b': case 'b':
resultArray.push('\b'.charCodeAt(0)); resultArray.push(0x08); // Backspace
break; break;
case 'v': case 'v':
resultArray.push('\v'.charCodeAt(0)); resultArray.push(0x0B); // Vertical Tab
break; break;
case '0': case '0':
resultArray.push('\0'.charCodeAt(0)); resultArray.push(0x00); // Null
break; break;
case 'x': case 'x':
if (i + 2 < str.length) { if (i + 2 < str.length) {
const hex = str.slice(i + 1, i + 3); resultArray.push(parseInt(str.substr(i + 1, 2), 16));
resultArray.push(parseInt(hex, 16));
i += 2; i += 2;
} }
break; break;
case 'u': case 'u':
if (i + 4 < str.length) { if (i + 4 < str.length) {
const hex = str.slice(i + 1, i + 5); const codePoint = parseInt(str.substr(i + 1, 4), 16);
resultArray.push(parseInt(hex, 16)); if (codePoint < 0x80) {
resultArray.push(codePoint);
} else if (codePoint < 0x800) {
resultArray.push(192 | (codePoint >> 6));
resultArray.push(128 | (codePoint & 63));
} else if (codePoint < 0x10000) {
resultArray.push(224 | (codePoint >> 12));
resultArray.push(128 | ((codePoint >> 6) & 63));
resultArray.push(128 | (codePoint & 63));
} else {
resultArray.push(240 | (codePoint >> 18));
resultArray.push(128 | ((codePoint >> 12) & 63));
resultArray.push(128 | ((codePoint >> 6) & 63));
resultArray.push(128 | (codePoint & 63));
}
i += 4; i += 4;
} }
break; break;
default: default:
// This handles any escaped character that is not a special character
resultArray.push(str[i].charCodeAt(0)); resultArray.push(str[i].charCodeAt(0));
break; break;
} }
} else { } else {
resultArray.push(str[i].charCodeAt(0)); // This section now properly converts a direct character to UTF-8 when it's beyond the ASCII range
const code = str.codePointAt(i);
if (code == undefined) {
continue
}
if (code < 0x80) {
resultArray.push(code);
} else if (code < 0x800) {
resultArray.push(192 | (code >> 6));
resultArray.push(128 | (code & 63));
} else if (code < 0x10000) {
resultArray.push(224 | (code >> 12));
resultArray.push(128 | ((code >> 6) & 63));
resultArray.push(128 | (code & 63));
} else {
resultArray.push(240 | (code >> 18));
resultArray.push(128 | ((code >> 12) & 63));
resultArray.push(128 | ((code >> 6) & 63));
resultArray.push(128 | (code & 63));
i++; // Move past the second part of the surrogate pair
}
} }
i++; i++;
} }
@ -529,6 +563,7 @@ export const useDataViewerStore = defineStore('text-viewer', () => {
}, 200); }, 200);
function addString(item: string, isRX: boolean = false, doSend: boolean = false, type: number = 0) { function addString(item: string, isRX: boolean = false, doSend: boolean = false, type: number = 0) {
console.log(item);
const unescaped = unescapeString(item); const unescaped = unescapeString(item);
return addItem(unescaped, isRX, doSend, type); return addItem(unescaped, isRX, doSend, type);
} }