From 518d2612e6c225933f0eb7b91a7b828455eac594 Mon Sep 17 00:00:00 2001 From: Steven Janzou Date: Fri, 21 Mar 2025 02:07:33 -0600 Subject: [PATCH 1/2] Update isdigit, isalpha, and isalnum to handle Unicode characters --- src/stdlib.cpp | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/src/stdlib.cpp b/src/stdlib.cpp index 1e8fdc1..3852fe2 100644 --- a/src/stdlib.cpp +++ b/src/stdlib.cpp @@ -1862,19 +1862,49 @@ static void _ascii(lk::invoke_t &cxt) { static void _isalpha(lk::invoke_t &cxt) { LK_DOC("isalpha", "Returns true if the argument is an alphabetic character A-Z,a-z.", "(character):boolean"); lk_string s = cxt.arg(0).as_string(); - cxt.result().assign(::isalpha(s.length() > 0 ? (int) s[0] : 0) ? 1.0 : 0.0); + double x = 0; + try { + lk_char y = s[0]; + if (y<256) + x = ::isalpha(s.length() > 0 ? (int)y : 0) ? 1.0 : 0.0; + } + catch (const std::exception&) { + x = 0; + } + cxt.result().assign(x); + //cxt.result().assign(::isalpha(s.length() > 0 ? (int) s[0] : 0) ? 1.0 : 0.0); } static void _isdigit(lk::invoke_t &cxt) { LK_DOC("isdigit", "Returns true if the argument is a numeric digit 0-9.", "(character):boolean"); lk_string s = cxt.arg(0).as_string(); - cxt.result().assign(::isdigit(s.length() > 0 ? (int) s[0] : 0) ? 1.0 : 0.0); + double x = 0; + try { + lk_char y = s[0]; + if (y < 256) + x = ::isdigit(s.length() > 0 ? (int)y : 0) ? 1.0 : 0.0; + } + catch (const std::exception&) { + x = 0; + } + cxt.result().assign(x); +// cxt.result().assign(::isdigit(s.length() > 0 ? (int) s[0] : 0) ? 1.0 : 0.0); } static void _isalnum(lk::invoke_t &cxt) { LK_DOC("isalnum", "Returns true if the argument is an alphanumeric A-Z,a-z,0-9.", "(character):boolean"); lk_string s = cxt.arg(0).as_string(); - cxt.result().assign(::isalnum(s.length() > 0 ? (int) s[0] : 0) ? 1.0 : 0.0); + double x = 0; + try { + lk_char y = s[0]; + if (y < 256) + x = ::isalnum(s.length() > 0 ? (int)y : 0) ? 1.0 : 0.0; + } + catch (const std::exception&) { + x = 0; + } + cxt.result().assign(x); + // cxt.result().assign(::isalnum(s.length() > 0 ? (int) s[0] : 0) ? 1.0 : 0.0); } static void _char(lk::invoke_t &cxt) { From 447dafacbc9a4ec662364c9d6b48d02b15fbae55 Mon Sep 17 00:00:00 2001 From: Paul Gilman Date: Fri, 21 Mar 2025 11:16:13 -0700 Subject: [PATCH 2/2] Fix isdigit, isalpha, isalnum to handle null character --- src/stdlib.cpp | 50 ++++++++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/src/stdlib.cpp b/src/stdlib.cpp index 3852fe2..109bc20 100644 --- a/src/stdlib.cpp +++ b/src/stdlib.cpp @@ -1863,13 +1863,15 @@ static void _isalpha(lk::invoke_t &cxt) { LK_DOC("isalpha", "Returns true if the argument is an alphabetic character A-Z,a-z.", "(character):boolean"); lk_string s = cxt.arg(0).as_string(); double x = 0; - try { - lk_char y = s[0]; - if (y<256) - x = ::isalpha(s.length() > 0 ? (int)y : 0) ? 1.0 : 0.0; - } - catch (const std::exception&) { - x = 0; + if (!s.IsNull()) { + try { + lk_char y = s[0]; + if (y < 256 && !s.IsNull()) + x = ::isalpha(s.length() > 0 ? (int)y : 0) ? 1.0 : 0.0; + } + catch (const std::exception&) { + x = 0; + } } cxt.result().assign(x); //cxt.result().assign(::isalpha(s.length() > 0 ? (int) s[0] : 0) ? 1.0 : 0.0); @@ -1879,29 +1881,33 @@ static void _isdigit(lk::invoke_t &cxt) { LK_DOC("isdigit", "Returns true if the argument is a numeric digit 0-9.", "(character):boolean"); lk_string s = cxt.arg(0).as_string(); double x = 0; - try { - lk_char y = s[0]; - if (y < 256) - x = ::isdigit(s.length() > 0 ? (int)y : 0) ? 1.0 : 0.0; - } - catch (const std::exception&) { - x = 0; + if (!s.IsNull()) { + try { + lk_char y = s[0]; + if (y < 256) + x = ::isdigit(s.length() > 0 ? (int)y : 0) ? 1.0 : 0.0; + } + catch (const std::exception&) { + x = 0; + } } cxt.result().assign(x); // cxt.result().assign(::isdigit(s.length() > 0 ? (int) s[0] : 0) ? 1.0 : 0.0); } -static void _isalnum(lk::invoke_t &cxt) { +static void _isalnum(lk::invoke_t& cxt) { LK_DOC("isalnum", "Returns true if the argument is an alphanumeric A-Z,a-z,0-9.", "(character):boolean"); lk_string s = cxt.arg(0).as_string(); double x = 0; - try { - lk_char y = s[0]; - if (y < 256) - x = ::isalnum(s.length() > 0 ? (int)y : 0) ? 1.0 : 0.0; - } - catch (const std::exception&) { - x = 0; + if (!s.IsNull()) { + try { + lk_char y = s[0]; + if (y < 256) + x = ::isalnum(s.length() > 0 ? (int)y : 0) ? 1.0 : 0.0; + } + catch (const std::exception&) { + x = 0; + } } cxt.result().assign(x); // cxt.result().assign(::isalnum(s.length() > 0 ? (int) s[0] : 0) ? 1.0 : 0.0);