Skip to content

Commit af4c706

Browse files
committed
Adds isdigit() and isletter() to string.bas
1 parent 1ad9f1c commit af4c706

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

src/lib/arch/zx48k/stdlib/string.bas

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,41 @@ function trim(ByVal s$ as String, ByVal rep$ as String) as String
305305
return ltrim(rtrim(s$, rep$), rep$)
306306
end function
307307

308+
309+
' ----------------------------------------------------------------
310+
' function isdigit(ByVal s$)
311+
'
312+
' Returns not 0 if the first element of s$ is a digit,
313+
' or 0 otherwise.
314+
' For example:
315+
' isdigit("a") returns 0, isdigit("2") returns 1
316+
' ----------------------------------------------------------------
317+
function isdigit(ByVal s$ as String) as ubyte
318+
dim asc_key as ubyte = code( s$( 0 ) )
319+
320+
return asc_key >= code( "0" ) _
321+
and asc_key <= code( "9" )
322+
end function
323+
324+
325+
' ----------------------------------------------------------------
326+
' function isletter(ByVal s$)
327+
'
328+
' Returns not 0 if the first element of s$ is a letter,
329+
' or 0 otherwise.
330+
' For example:
331+
' isletter("0") returns 0, isletter("a") returns 1
332+
' ----------------------------------------------------------------
333+
function isletter(ByVal s$ as String) as ubyte
334+
dim asc_key as ubyte = code( s$( 0 ) )
335+
336+
return ( asc_key >= code( "a" ) _
337+
and asc_key <= code( "z" ) ) _
338+
or ( asc_key >= code( "A" ) _
339+
and asc_key <= code( "Z" ) )
340+
end function
341+
342+
308343
#undef __MAX_LEN__
309344

310345
#pragma pop(string_base)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <string.bas>
2+
3+
4+
sub show_isdigit_letter_for(byval s as string)
5+
print bright 1; "str: "; inverse 1; s + chr$( 13 )
6+
print bright 1; "dig: ";
7+
for i = 0 to len( s ) - 1
8+
print( isdigit( s( i ) ) );
9+
next
10+
print( chr( 13 ) )
11+
print bright 1; "ltr: ";
12+
for i = 0 to len( s ) - 1
13+
print( isletter( s( i ) ) );
14+
next
15+
print( chr( 13 ) )
16+
end sub
17+
18+
19+
dim a as string = "abc0123456789cba.-,[]()/=?:"
20+
dim b as string = "0123abc456abc789.-,[]()/=?:"
21+
22+
cls
23+
show_isdigit_letter_for( a )
24+
show_isdigit_letter_for( b )

0 commit comments

Comments
 (0)