Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds isdigit() and isletter() to string.bas #917

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/lib/arch/zx48k/stdlib/string.bas
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions tests/functional/arch/zx48k/string_isdigit_isletter.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <string.bas>


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 )