Add error handling for Unicode characters beyond Latin-1 and test for Latin-1 accepted

This commit is contained in:
Philip Le Riche 2026-03-07 19:16:40 +00:00
parent 19368b2911
commit 759febdf53
2 changed files with 29 additions and 1 deletions

View File

@ -5,6 +5,7 @@
*/ */
import Operation from "../Operation.mjs"; import Operation from "../Operation.mjs";
import OperationError from "../errors/OperationError.mjs";
/* ---------- helper functions ---------- */ /* ---------- helper functions ---------- */
@ -17,6 +18,11 @@ function textToBigInt(text) {
let result = 0n; let result = 0n;
for (let i = 0; i < text.length; i++) { for (let i = 0; i < text.length; i++) {
const charCode = BigInt(text.charCodeAt(i)); const charCode = BigInt(text.charCodeAt(i));
if (charCode > 255n) {
throw new OperationError(
`Character at position ${i} exceeds Latin-1 range (0-255).\n` +
"Only ASCII and Latin-1 characters are supported.");
}
result = (result << 8n) | charCode; result = (result << 8n) | charCode;
} }
return result; return result;
@ -58,7 +64,10 @@ class TextIntegerConverter extends Operation {
"<b>Input format detection:</b><br>" + "<b>Input format detection:</b><br>" +
"Decimal: digits 0-9 only<br>" + "Decimal: digits 0-9 only<br>" +
"Hexadecimal: 0x... prefix<br>" + "Hexadecimal: 0x... prefix<br>" +
"Quoted or unquoted text: treated as string<br><br>" ; "Quoted or unquoted text: treated as string<br><br>" +
"<b>Character limitations:</b><br>" +
"Text input may only contain ASCII and Latin-1 characters (code point < 256).<br>" +
"Multi-byte Unicode characters will generate an error.<br><br>." ;
this.infoURL = "https://wikipedia.org/wiki/Endianness"; this.infoURL = "https://wikipedia.org/wiki/Endianness";
this.inputType = "string"; this.inputType = "string";
this.outputType = "string"; this.outputType = "string";

View File

@ -94,6 +94,25 @@ TestRegister.addTests([
}, },
], ],
}, },
{
name: "Text-Integer Conversion implicit round trip string-string Latin-1",
input: "U+00FF",
expectedOutput: "U+00FF", // U+00FF (Latin small letter y with diaeresis)
recipeConfig: [
{
op: "Unescape Unicode Characters",
args: ["U+"],
},
{
op: "Text-Integer Conversion",
args: ["String"],
},
{
op: "Escape Unicode Characters",
args: ["U+", false, 4, true],
},
],
},
{ {
name: "Text-Integer Conversion unquoted text to decimal", name: "Text-Integer Conversion unquoted text to decimal",
input: "Hi", input: "Hi",