added strings.isxdigit()

fixed size bugs in several virtual string to number conv routines
This commit is contained in:
Irmen de Jong
2026-04-03 23:33:00 +02:00
parent 42d3340fe4
commit 7658a0ab12
9 changed files with 106 additions and 54 deletions
+19
View File
@@ -726,6 +726,25 @@ fail clc ; yes, no match found, return with c=0
}}
}
asmsub isxdigit(ubyte petsciichar @A) -> bool @Pc {
; hex digit: 0-9, a-f, A-F. Normalize to lowercase first, then check.
%asm {{
jsr lowerchar
cmp #'0'
bcc _no
cmp #'9'+1
bcc _yes
cmp #'a'
bcc _no
cmp #'f'+1
bcc _yes
_no clc
rts
_yes sec
rts
}}
}
asmsub isupper(ubyte petsciichar @A) -> bool @Pc {
; shifted petscii has 2 ranges that contain the upper case letters... 97-122 and 193-218
%asm {{
+44 -24
View File
@@ -1,5 +1,7 @@
; Number conversions routines.
%import strings
conv {
; ----- number conversions to decimal strings ----
@@ -267,14 +269,15 @@ sub hex2uword(str string) -> uword {
if @(string)=='$'
string++
repeat {
char = @(string)
if char==0
char = strings.lowerchar(@(string))
if strings.isxdigit(char) {
result <<= 4
if char>='0' and char<='9'
result |= char-'0'
else
result |= char-'a'+10
} else
return result
result <<= 4
if char>='0' and char<='9'
result |= char-'0'
else
result |= char-'a'+10
string++
}
}
@@ -287,14 +290,15 @@ sub hex2long(str string) -> long {
if @(string)=='$'
string++
repeat {
char = @(string)
if char==0
char = strings.lowerchar(@(string))
if strings.isxdigit(char) {
result <<= 4
if char>='0' and char<='9'
result |= char-'0'
else
result |= char-'a'+10
} else
return result
result <<= 4
if char>='0' and char<='9'
result |= char-'0'
else
result |= char-'a'+10
string++
}
}
@@ -308,7 +312,7 @@ sub str2long(str string) -> long {
alias digit_char = cx16.r0L
repeat {
digit_char = @(string)
if digit_char in '0' to '9' {
if strings.isdigit(digit_char) {
result = (result<<1) + (result<<3) ; multiply by 10
result += digit_char - '0' ; add digit
} else {
@@ -332,11 +336,13 @@ sub bin2uword(str string) -> uword {
string++
repeat {
char = @(string)
if char==0
return result
result <<= 1
if char=='1'
if char=='0' {
result <<= 1
} else if char=='1' {
result <<= 1
result |= 1
} else
return result
string++
}
}
@@ -344,12 +350,26 @@ sub bin2uword(str string) -> uword {
sub any2uword(str string) -> uword, ubyte {
; -- convert any number string (any prefix allowed) to uword.
; returns the parsed word value, and the number of processed characters (including the prefix symbol)
ubyte length
while string[length]!=0 length++
ubyte count
when string[0] {
'$' -> return hex2uword(string), length
'%' -> return bin2uword(string), length
else -> return str2uword(string), length
'$' -> {
count = 1
while strings.isxdigit(string[count])
count++
return hex2uword(string), count
}
'%' -> {
count = 1
while string[count]=='0' or string[count]=='1'
count++
return bin2uword(string), count
}
else -> {
count = 0
while strings.isdigit(string[count])
count++
return str2uword(string), count
}
}
}
+5
View File
@@ -237,6 +237,11 @@ strings {
return character>='0' and character<='9'
}
sub isxdigit(ubyte character) -> bool {
character = lowerchar(character)
return isdigit(character) or character>='a' and character<='f'
}
sub isupper(ubyte character) -> bool {
return character>='A' and character<='Z'
}
@@ -365,12 +365,13 @@ strings {
find_eol (str string @AY) -> ubyte @A, bool @Pc
findstr (str haystack, str needle) -> ubyte
hash (str string @AY) -> ubyte @A
isdigit (ubyte petsciichar @A) -> bool @Pc
isletter (ubyte petsciichar @A) -> bool @Pc
islower (ubyte petsciichar @A) -> bool @Pc
isprint (ubyte petsciichar @A) -> bool @Pc
isspace (ubyte petsciichar @A) -> bool @Pc
isupper (ubyte petsciichar @A) -> bool @Pc
isdigit (ubyte petsciichar @A) -> bool @Pc
isletter (ubyte petsciichar @A) -> bool @Pc
islower (ubyte petsciichar @A) -> bool @Pc
isprint (ubyte petsciichar @A) -> bool @Pc
isspace (ubyte petsciichar @A) -> bool @Pc
isupper (ubyte petsciichar @A) -> bool @Pc
isxdigit (ubyte petsciichar @A) -> bool @Pc
left (str source @AX, ubyte length @Y, str target @R1) clobbers (A,Y)
length (str string @AY) clobbers (A) -> ubyte @Y
lower (str st @AY) -> ubyte @Y
@@ -495,12 +495,13 @@ strings {
find_eol (str string @AY) -> ubyte @A, bool @Pc
findstr (str haystack, str needle) -> ubyte
hash (str string @AY) -> ubyte @A
isdigit (ubyte petsciichar @A) -> bool @Pc
isletter (ubyte petsciichar @A) -> bool @Pc
islower (ubyte petsciichar @A) -> bool @Pc
isprint (ubyte petsciichar @A) -> bool @Pc
isspace (ubyte petsciichar @A) -> bool @Pc
isupper (ubyte petsciichar @A) -> bool @Pc
isdigit (ubyte petsciichar @A) -> bool @Pc
isletter (ubyte petsciichar @A) -> bool @Pc
islower (ubyte petsciichar @A) -> bool @Pc
isprint (ubyte petsciichar @A) -> bool @Pc
isspace (ubyte petsciichar @A) -> bool @Pc
isupper (ubyte petsciichar @A) -> bool @Pc
isxdigit (ubyte petsciichar @A) -> bool @Pc
left (str source @AX, ubyte length @Y, str target @R1) clobbers (A,Y)
length (str string @AY) clobbers (A) -> ubyte @Y
lower (str st @AY) -> ubyte @Y
@@ -859,12 +859,13 @@ strings {
find_eol (str string @AY) -> ubyte @A, bool @Pc
findstr (str haystack, str needle) -> ubyte
hash (str string @AY) -> ubyte @A
isdigit (ubyte petsciichar @A) -> bool @Pc
isletter (ubyte petsciichar @A) -> bool @Pc
islower (ubyte petsciichar @A) -> bool @Pc
isprint (ubyte petsciichar @A) -> bool @Pc
isspace (ubyte petsciichar @A) -> bool @Pc
isupper (ubyte petsciichar @A) -> bool @Pc
isdigit (ubyte petsciichar @A) -> bool @Pc
isletter (ubyte petsciichar @A) -> bool @Pc
islower (ubyte petsciichar @A) -> bool @Pc
isprint (ubyte petsciichar @A) -> bool @Pc
isspace (ubyte petsciichar @A) -> bool @Pc
isupper (ubyte petsciichar @A) -> bool @Pc
isxdigit (ubyte petsciichar @A) -> bool @Pc
left (str source @AX, ubyte length @Y, str target @R1) clobbers (A,Y)
length (str string @AY) clobbers (A) -> ubyte @Y
lower (str st @AY) -> ubyte @Y
@@ -473,12 +473,13 @@ strings {
find_eol (str string @AY) -> ubyte @A, bool @Pc
findstr (str haystack, str needle) -> ubyte
hash (str string @AY) -> ubyte @A
isdigit (ubyte petsciichar @A) -> bool @Pc
isletter (ubyte petsciichar @A) -> bool @Pc
islower (ubyte petsciichar @A) -> bool @Pc
isprint (ubyte petsciichar @A) -> bool @Pc
isspace (ubyte petsciichar @A) -> bool @Pc
isupper (ubyte petsciichar @A) -> bool @Pc
isdigit (ubyte petsciichar @A) -> bool @Pc
isletter (ubyte petsciichar @A) -> bool @Pc
islower (ubyte petsciichar @A) -> bool @Pc
isprint (ubyte petsciichar @A) -> bool @Pc
isspace (ubyte petsciichar @A) -> bool @Pc
isupper (ubyte petsciichar @A) -> bool @Pc
isxdigit (ubyte petsciichar @A) -> bool @Pc
left (str source @AX, ubyte length @Y, str target @R1) clobbers (A,Y)
length (str string @AY) clobbers (A) -> ubyte @Y
lower (str st @AY) -> ubyte @Y
@@ -377,12 +377,13 @@ strings {
find_eol (str st) -> ubyte, bool
findstr (str haystack, str needle) -> ubyte
hash (str st) -> ubyte
isdigit (ubyte character) -> bool
isletter (ubyte character) -> bool
islower (ubyte character) -> bool
isprint (ubyte character) -> bool
isspace (ubyte character) -> bool
isupper (ubyte character) -> bool
isdigit (ubyte character) -> bool
isletter (ubyte character) -> bool
islower (ubyte character) -> bool
isprint (ubyte character) -> bool
isspace (ubyte character) -> bool
isupper (ubyte character) -> bool
isxdigit (ubyte character) -> bool
left (str source, ubyte slen, str target)
length (str st) -> ubyte
lower (str st) -> ubyte
+3
View File
@@ -1176,6 +1176,9 @@ conversion and classification
``isdigit (char)``
Returns boolean if the character is a numerical digit 0-9
``isxdigit (char)``
Returns boolean if the character is a hexadecimal digit 0-9, a-f, or A-F.
``islower (char)``, ``isupper (char)``, ``isletter (char)``
Returns true if the character is a shifted-PETSCII lowercase letter, uppercase letter, or any letter, respectively.