From af4c70684f0798a2f51193bf02b4f6839a406d25 Mon Sep 17 00:00:00 2001 From: baltasar Date: Fri, 29 Nov 2024 21:16:13 +0100 Subject: [PATCH 1/2] Adds isdigit() and isletter() to string.bas --- src/lib/arch/zx48k/stdlib/string.bas | 35 +++++++++++++++++++ .../arch/zx48k/string_isdigit_isletter.bas | 24 +++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 tests/functional/arch/zx48k/string_isdigit_isletter.bas diff --git a/src/lib/arch/zx48k/stdlib/string.bas b/src/lib/arch/zx48k/stdlib/string.bas index 547c65bfd..a0340ddae 100644 --- a/src/lib/arch/zx48k/stdlib/string.bas +++ b/src/lib/arch/zx48k/stdlib/string.bas @@ -305,6 +305,41 @@ function trim(ByVal s$ as String, ByVal rep$ as String) as String return ltrim(rtrim(s$, rep$), rep$) end function + +' ---------------------------------------------------------------- +' function isdigit(ByVal s$) +' +' Returns not 0 if the first element of s$ is a digit, +' or 0 otherwise. +' For example: +' isdigit("a") returns 0, isdigit("2") returns 1 +' ---------------------------------------------------------------- +function isdigit(ByVal s$ as String) as ubyte + dim asc_key as ubyte = code( s$( 0 ) ) + + return asc_key >= code( "0" ) _ + and asc_key <= code( "9" ) +end function + + +' ---------------------------------------------------------------- +' function isletter(ByVal s$) +' +' Returns not 0 if the first element of s$ is a letter, +' or 0 otherwise. +' For example: +' isletter("0") returns 0, isletter("a") returns 1 +' ---------------------------------------------------------------- +function isletter(ByVal s$ as String) as ubyte + dim asc_key as ubyte = code( s$( 0 ) ) + + return ( asc_key >= code( "a" ) _ + and asc_key <= code( "z" ) ) _ + or ( asc_key >= code( "A" ) _ + and asc_key <= code( "Z" ) ) +end function + + #undef __MAX_LEN__ #pragma pop(string_base) diff --git a/tests/functional/arch/zx48k/string_isdigit_isletter.bas b/tests/functional/arch/zx48k/string_isdigit_isletter.bas new file mode 100644 index 000000000..028358c5f --- /dev/null +++ b/tests/functional/arch/zx48k/string_isdigit_isletter.bas @@ -0,0 +1,24 @@ +#include + + +sub show_isdigit_letter_for(byval s as string) + print bright 1; "str: "; inverse 1; s + chr$( 13 ) + print bright 1; "dig: "; + for i = 0 to len( s ) - 1 + print( isdigit( s( i ) ) ); + next + print( chr( 13 ) ) + print bright 1; "ltr: "; + for i = 0 to len( s ) - 1 + print( isletter( s( i ) ) ); + next + print( chr( 13 ) ) +end sub + + +dim a as string = "abc0123456789cba.-,[]()/=?:" +dim b as string = "0123abc456abc789.-,[]()/=?:" + +cls +show_isdigit_letter_for( a ) +show_isdigit_letter_for( b ) From 467dd96aa1569feee7c6e42c0b9fff2b0bcdbdac Mon Sep 17 00:00:00 2001 From: baltasar Date: Sun, 8 Dec 2024 13:38:09 +0100 Subject: [PATCH 2/2] Fix: isletter and isdigit mirrored on lib/zxnext --- src/lib/arch/zxnext/stdlib/string.bas | 35 +++++++++++++++++++ .../arch/zx48k/string_isdigit_isletter.bas | 24 ------------- 2 files changed, 35 insertions(+), 24 deletions(-) delete mode 100644 tests/functional/arch/zx48k/string_isdigit_isletter.bas diff --git a/src/lib/arch/zxnext/stdlib/string.bas b/src/lib/arch/zxnext/stdlib/string.bas index 0053e9485..dff90fed4 100644 --- a/src/lib/arch/zxnext/stdlib/string.bas +++ b/src/lib/arch/zxnext/stdlib/string.bas @@ -305,6 +305,41 @@ function trim(ByVal s$ as String, ByVal rep$ as String) as String return ltrim(rtrim(s$, rep$), rep$) end function + +' ---------------------------------------------------------------- +' function isdigit(ByVal s$) +' +' Returns not 0 if the first element of s$ is a digit, +' or 0 otherwise. +' For example: +' isdigit("a") returns 0, isdigit("2") returns 1 +' ---------------------------------------------------------------- +function isdigit(ByVal s$ as String) as ubyte + dim asc_key as ubyte = code( s$( 0 ) ) + + return asc_key >= code( "0" ) _ + and asc_key <= code( "9" ) +end function + + +' ---------------------------------------------------------------- +' function isletter(ByVal s$) +' +' Returns not 0 if the first element of s$ is a letter, +' or 0 otherwise. +' For example: +' isletter("0") returns 0, isletter("a") returns 1 +' ---------------------------------------------------------------- +function isletter(ByVal s$ as String) as ubyte + dim asc_key as ubyte = code( s$( 0 ) ) + + return ( asc_key >= code( "a" ) _ + and asc_key <= code( "z" ) ) _ + or ( asc_key >= code( "A" ) _ + and asc_key <= code( "Z" ) ) +end function + + #undef __MAX_LEN__ #pragma pop(string_base) diff --git a/tests/functional/arch/zx48k/string_isdigit_isletter.bas b/tests/functional/arch/zx48k/string_isdigit_isletter.bas deleted file mode 100644 index 028358c5f..000000000 --- a/tests/functional/arch/zx48k/string_isdigit_isletter.bas +++ /dev/null @@ -1,24 +0,0 @@ -#include - - -sub show_isdigit_letter_for(byval s as string) - print bright 1; "str: "; inverse 1; s + chr$( 13 ) - print bright 1; "dig: "; - for i = 0 to len( s ) - 1 - print( isdigit( s( i ) ) ); - next - print( chr( 13 ) ) - print bright 1; "ltr: "; - for i = 0 to len( s ) - 1 - print( isletter( s( i ) ) ); - next - print( chr( 13 ) ) -end sub - - -dim a as string = "abc0123456789cba.-,[]()/=?:" -dim b as string = "0123abc456abc789.-,[]()/=?:" - -cls -show_isdigit_letter_for( a ) -show_isdigit_letter_for( b )