From 52e0674c54d4b53b2132e750106cc9acb266ed39 Mon Sep 17 00:00:00 2001 From: _ov4 Date: Tue, 20 Oct 2026 11:04:39 +0000 Subject: feat(library): add experimental string IS_NUM function Co-Authored-By: Claude Opus 5.5 --- pseudocode-lib/README.md | 2 ++ pseudocode-lib/_exp_string.cc | 9 +++++++++ pseudocode-lib/_exp_string.cps | 4 ++++ tests/library/exp-string/expected.txt | 3 +++ tests/library/exp-string/notes.md | 2 ++ tests/library/exp-string/program.cps | 13 +++++++++++++ 6 files changed, 33 insertions(+) diff --git a/pseudocode-lib/README.md b/pseudocode-lib/README.md index 90fab0e..9182b14 100644 --- a/pseudocode-lib/README.md +++ b/pseudocode-lib/README.md @@ -32,3 +32,5 @@ The library provides `&`, `_EXP_STRING_NUM_TO_STR` (INTEGER/REAL), `_EXP_STRING_ `_EXP_STRING_ASC(S : _EXP_STRING)` returns the unsigned code of the first byte as INTEGER, or 0 for an empty string. Its `(Ch : CHAR)` overload returns the unsigned byte code of Ch. For ASCII characters this is the ASCII code. `_EXP_STRING_CHR(Code : INTEGER)` returns a new `_EXP_STRING` containing the low eight bits of Code as one byte. Unlike built-in STRING, `_EXP_STRING` preserves NUL: `_EXP_STRING_CHR(0)` has length 1. + +`_EXP_STRING_IS_NUM(S : _EXP_STRING)` returns BOOLEAN indicating whether a nonempty string is fully consumed by `strtod`. Leading whitespace is accepted; trailing whitespace, other trailing text, and embedded NUL bytes are rejected. Invalid numeric text returns FALSE without throwing. diff --git a/pseudocode-lib/_exp_string.cc b/pseudocode-lib/_exp_string.cc index ae53ac1..4cfd650 100644 --- a/pseudocode-lib/_exp_string.cc +++ b/pseudocode-lib/_exp_string.cc @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -127,6 +128,14 @@ void _exp_string_impl_chr(void *Target, long long Code) { text(Target).assign(1, static_cast(static_cast(Code))); }); } +int _exp_string_impl_is_num(void *Handle) { + return checked([&]() -> int { + const auto &Source = text(Handle); + char *End = nullptr; + std::strtod(Source.c_str(), &End); + return !Source.empty() && End == Source.c_str() + Source.size(); + }); +} void _exp_string_impl_output(void *Handle) { checked([&] { const auto &Text = text(Handle); diff --git a/pseudocode-lib/_exp_string.cps b/pseudocode-lib/_exp_string.cps index 965a251..975a583 100644 --- a/pseudocode-lib/_exp_string.cps +++ b/pseudocode-lib/_exp_string.cps @@ -24,6 +24,7 @@ EXTERN "C" EXTERN_FUNCTION _exp_string_impl_ucase_char(C_CHAR) RETURNS C_CHAR EXTERN "C" EXTERN_FUNCTION _exp_string_impl_asc(_EXP_STRING_HANDLE) RETURNS _EXP_STRING_C_INTEGER EXTERN "C" EXTERN_FUNCTION _exp_string_impl_asc_char(C_CHAR) RETURNS _EXP_STRING_C_INTEGER EXTERN "C" EXTERN_PROCEDURE _exp_string_impl_chr(_EXP_STRING_HANDLE, _EXP_STRING_C_INTEGER) +EXTERN "C" EXTERN_FUNCTION _exp_string_impl_is_num(_EXP_STRING_HANDLE) RETURNS C_INT EXTERN "C" EXTERN_PROCEDURE _exp_string_impl_output(_EXP_STRING_HANDLE) CLASS _EXP_STRING @@ -117,3 +118,6 @@ FUNCTION _EXP_STRING_CHR(Code : INTEGER) RETURNS _EXP_STRING CALL _exp_string_impl_chr(Result._HANDLE, (_EXP_STRING_C_INTEGER)Code) RETURN Result ENDFUNCTION +FUNCTION _EXP_STRING_IS_NUM(S : _EXP_STRING) RETURNS BOOLEAN + RETURN (BOOLEAN)_exp_string_impl_is_num(S._HANDLE) +ENDFUNCTION diff --git a/tests/library/exp-string/expected.txt b/tests/library/exp-string/expected.txt index 0680cf2..030a4c2 100644 --- a/tests/library/exp-string/expected.txt +++ b/tests/library/exp-string/expected.txt @@ -39,3 +39,6 @@ AZ 65 1:0 65:255 +TRUE:FALSE:FALSE +TRUE:FALSE:FALSE +FALSE:FALSE diff --git a/tests/library/exp-string/notes.md b/tests/library/exp-string/notes.md index 7eddda1..e2a1bbc 100644 --- a/tests/library/exp-string/notes.md +++ b/tests/library/exp-string/notes.md @@ -19,3 +19,5 @@ UCASE converts AbC@19 to ABC@19 without modifying the source. Its CHAR overload ASC returns 65 for CHAR A and the first byte of ABCDEFGH, zero for empty strings or NUL, and 255 for a byte with all eight bits set. These byte-code functions are compiler/library extensions. CHR codes 65 and 90 concatenate to AZ; converting A to a code and back preserves 65. Code zero produces one retained NUL byte. Taking the low eight bits gives 65 for 321 and 255 for -1. + +IS_NUM accepts 12.5 and a leading space before 12.5. It rejects trailing junk, empty input, trailing space, whitespace alone, and a NUL byte before or after a valid number, without throwing. Numeric recognition follows the compiler's strtod-based extension. diff --git a/tests/library/exp-string/program.cps b/tests/library/exp-string/program.cps index e789e54..0adf86c 100644 --- a/tests/library/exp-string/program.cps +++ b/tests/library/exp-string/program.cps @@ -46,6 +46,7 @@ CALL CheckLCase() CALL CheckUCase() CALL CheckAsc() CALL CheckChr() +CALL CheckIsNum() PROCEDURE CheckLeft() DECLARE Letters : _EXP_STRING((C_STRING)"ABCDEFGH") @@ -123,3 +124,15 @@ PROCEDURE CheckChr() OUTPUT _EXP_STRING_LENGTH(_EXP_STRING_CHR(0)), ":", _EXP_STRING_ASC(_EXP_STRING_CHR(0)) OUTPUT _EXP_STRING_ASC(_EXP_STRING_CHR(321)), ":", _EXP_STRING_ASC(_EXP_STRING_CHR(-1)) ENDPROCEDURE + +PROCEDURE CheckIsNum() + DECLARE Number : _EXP_STRING((C_STRING)"12.5") + DECLARE Junk : _EXP_STRING((C_STRING)"12x") + DECLARE Empty : _EXP_STRING + DECLARE Leading : _EXP_STRING((C_STRING)" 12.5") + DECLARE Trailing : _EXP_STRING((C_STRING)"12.5 ") + DECLARE Whitespace : _EXP_STRING((C_STRING)" ") + OUTPUT _EXP_STRING_IS_NUM(Number), ":", _EXP_STRING_IS_NUM(Junk), ":", _EXP_STRING_IS_NUM(Empty) + OUTPUT _EXP_STRING_IS_NUM(Leading), ":", _EXP_STRING_IS_NUM(Trailing), ":", _EXP_STRING_IS_NUM(Whitespace) + OUTPUT _EXP_STRING_IS_NUM(Number & _EXP_STRING_CHR(0)), ":", _EXP_STRING_IS_NUM(_EXP_STRING_CHR(0) & Number) +ENDPROCEDURE -- cgit v1.3.1-4-g156e