fix: fromDecimal Auto delimiter now correctly handles multiple numbers

Fixes #2217

Summary:
The fromDecimal function now correctly handles the 'Auto' delimiter mode,
similar to fromHex. Previously, when delim='Auto' (the default), it would
only parse the first number, ignoring subsequent numbers regardless of
the separator used.

Changes:
- src/core/lib/Decimal.mjs:
  * When delim='Auto', use regex /[^\d-]+/ to split on any non-digit,
    non-minus character (automatically detecting delimiters)
  * Filter out empty strings from split result
  * Matches the behavior of fromHex's Auto mode

- tests/operations/tests/FromDecimal.mjs:
  * Added test cases for Auto delimiter with space, comma, and mixed separators
  * Ensures Auto mode correctly parses multiple numbers with various delimiters

Before:
  Input: "72 101 108 108 111" (Auto delimiter)
  Output: Only parsed 72 (first number)

After:
  Input: "72 101 108 108 111" (Auto delimiter)
  Output: Correctly parsed all numbers → "Hello"
This commit is contained in:
aicontentcreate2023-star 2026-03-05 07:31:27 +08:00
parent cd7dafdf53
commit 9966d00988
2 changed files with 45 additions and 5 deletions

View File

@ -24,12 +24,19 @@ import Utils from "../Utils.mjs";
* fromDecimal("10:20:30", "Colon"); * fromDecimal("10:20:30", "Colon");
*/ */
export function fromDecimal(data, delim="Auto") { export function fromDecimal(data, delim="Auto") {
let byteStr;
if (delim === "Auto") {
// Auto mode: split on any non-digit, non-minus character (similar to fromHex)
byteStr = data.split(/[^\d-]+/);
} else {
delim = Utils.charRep(delim); delim = Utils.charRep(delim);
const output = []; byteStr = data.split(delim);
let byteStr = data.split(delim); }
if (byteStr[byteStr.length-1] === "")
byteStr = byteStr.slice(0, byteStr.length-1);
// Remove empty strings from the array
byteStr = byteStr.filter(str => str !== "");
const output = [];
for (let i = 0; i < byteStr.length; i++) { for (let i = 0; i < byteStr.length; i++) {
output[i] = parseInt(byteStr[i], 10); output[i] = parseInt(byteStr[i], 10);
} }

View File

@ -30,4 +30,37 @@ TestRegister.addTests([
}, },
], ],
}, },
{
name: "From Decimal with Auto delimiter (space)",
input: "72 101 108 108 111",
expectedOutput: "Hello",
recipeConfig: [
{
op: "From Decimal",
args: ["Auto", false]
},
],
},
{
name: "From Decimal with Auto delimiter (comma)",
input: "72,101,108,108,111",
expectedOutput: "Hello",
recipeConfig: [
{
op: "From Decimal",
args: ["Auto", false]
},
],
},
{
name: "From Decimal with Auto delimiter (mixed)",
input: "72, 101 : 108; 108\t111",
expectedOutput: "Hello",
recipeConfig: [
{
op: "From Decimal",
args: ["Auto", false]
},
],
},
]); ]);