strings: fix several edge cases in the string library

fixes #252
This commit is contained in:
Irmen de Jong committed 2026-11-14 22:59:32 +01:00
1 parent de17e79536
commit b9fae18200
6 files changed
+138 -69

No files matched your search

+7
View File
@@ -244,6 +244,13 @@ strncpy .proc
; returns the length of the string that was copied in Y.
sta P8ZP_SCRATCH_W2
sty P8ZP_SCRATCH_W2+1
cpx #0
bne _nonzero
ldy #0
lda #0
sta (P8ZP_SCRATCH_W1),y
rts
_nonzero
ldy #$ff
- iny
lda (P8ZP_SCRATCH_W2),y
@@ -18,17 +18,17 @@ strings {
while numparts < maxparts {
skipwhitespace()
if s[index]!=0 {
parts^^ = s+index
numparts++
parts++
skipchars()
if s[index]==0
break
if numparts < maxparts
s[index] = 0
index++
}
if s[index]==0
break
parts^^ = s+index
numparts++
parts++
skipchars()
if s[index]==0
break
if numparts < maxparts
s[index] = 0
index++
}
if numparts>0 {
parts--
@@ -56,14 +56,14 @@ strings {
sub rstrip(str s) {
; -- gets rid of whitespace and other non-visible characters at the end of the string
if s[0]==0
return
cx16.r0L = length(s)
do {
cx16.r0L--
cx16.r1L = s[cx16.r0L]
} until cx16.r0L==0 or isprint(cx16.r1L) and not isspace(cx16.r1L)
s[cx16.r0L+1] = 0
ubyte n = length(s)
while n>0 {
ubyte c = s[n-1]
if isprint(c) and not isspace(c)
break
n--
}
s[n] = 0
}
sub lstripped(str s) -> str {
@@ -86,14 +86,13 @@ strings {
sub rtrim(str s) {
; -- gets rid of whitespace characters at the end of the string
if s[0]==0
return
cx16.r0L = length(s)
do {
cx16.r0L--
cx16.r1L = s[cx16.r0L]
} until cx16.r0L==0 or not isspace(cx16.r1L)
s[cx16.r0L+1] = 0
ubyte n = length(s)
while n>0 {
if not isspace(s[n-1])
break
n--
}
s[n] = 0
}
sub ltrimmed(str s) -> str {
@@ -134,21 +133,39 @@ strings {
else
last_token_source = source
if last_token_source^^ == 0
^^ubyte scan = last_token_source
^^ubyte dptr
; skip leading delimiter characters
while scan^^ != 0 {
dptr = delimiters
while dptr^^ != 0 {
if scan^^ == dptr^^
break
dptr += 1
}
if dptr^^ == 0
break
scan += 1
}
last_token_source = scan
if scan^^ == 0
return 0
while last_token_source^^ != 0 {
^^ubyte dptr = delimiters
source = scan
while scan^^ != 0 {
dptr = delimiters
while dptr^^ != 0 {
if last_token_source^^ == dptr^^ {
last_token_source^^ = 0
last_token_source += 1
if scan^^ == dptr^^ {
scan^^ = 0
last_token_source = scan + 1
return source
}
dptr += 1
}
last_token_source += 1
scan += 1
}
last_token_source = scan
return source
}
^^ubyte last_token_source
+7 -4
View File
@@ -268,12 +268,11 @@ _found tya
jsr length
sty P8ZP_SCRATCH_B1
cpx P8ZP_SCRATCH_B1
beq _max_too_small
bmi _max_too_small
bcc _max_too_small
txa
sec
sbc P8ZP_SCRATCH_B1
sta P8ZP_SCRATCH_B1
tax
tya
clc
adc cx16.r0
@@ -283,13 +282,13 @@ _found tya
sta P8ZP_SCRATCH_W1+1
lda cx16.r1
ldy cx16.r1+1
ldx P8ZP_SCRATCH_B1
jsr prog8_lib.strncpy
tya
clc
adc P8ZP_SCRATCH_B1
rts
_max_too_small
lda P8ZP_SCRATCH_B1
rts
}}
}
@@ -846,6 +845,10 @@ _yes sec
; or if needle doesn't occur in haystack it returns Carry clear and 255 (an invalid index.)
cx16.r2L = length(haystack)
cx16.r3L = length(needle)
if cx16.r3L == 0 {
sys.set_carry()
return 0
}
if cx16.r3L <= cx16.r2L {
cx16.r2L = cx16.r2L-cx16.r3L+1
cx16.r3 = haystack
+32 -3
View File
@@ -21,6 +21,8 @@ strings {
; Also, you have to make sure yourself that length is smaller or equal to the length of the source string.
; Modifies in-place, doesn’t return a value (so can’t be used in an expression).
target[slen] = 0
if slen==0
return
ubyte ix
for ix in 0 to slen-1 {
target[ix] = source[ix]
@@ -32,6 +34,10 @@ strings {
; It is assumed the target string buffer is large enough to contain the result.
; Also, you have to make sure yourself that length is smaller or equal to the length of the source string.
; Modifies in-place, doesn’t return a value (so can’t be used in an expression).
if slen==0 {
target[0] = 0
return
}
ubyte offset = length(source)-slen
ubyte ix
for ix in 0 to slen-1 {
@@ -46,6 +52,10 @@ strings {
; It is assumed the target string buffer is large enough to contain the result.
; Also, you have to make sure yourself that start and length are within bounds of the strings.
; Modifies in-place, doesn’t return a value (so can’t be used in an expression).
if slen==0 {
target[0] = 0
return
}
ubyte ix
for ix in 0 to slen-1 {
target[ix] = source[ix+start]
@@ -56,8 +66,13 @@ strings {
sub find(str st, ubyte character) -> ubyte, bool {
; Locates the first position of the given character in the string,
; returns index in A, and boolean if found or not. (when false A will also be 255, an invalid index).
ubyte n = length(st)
if n==0 {
sys.clear_carry()
return 255, false
}
ubyte ix
for ix in 0 to length(st)-1 {
for ix in 0 to n-1 {
if st[ix]==character {
sys.set_carry()
return ix, true
@@ -71,8 +86,13 @@ strings {
; Locates the position of the first End Of Line character in the string.
; This is a convenience function that looks for both a CR or LF (byte 13 or byte 10) as being a possible Line Ending.
; returns index in A, and boolean if found or not. (when false A will also be 255, an invalid index).
ubyte n = length(st)
if n==0 {
sys.clear_carry()
return 255, false
}
ubyte ix
for ix in 0 to length(st)-1 {
for ix in 0 to n-1 {
if st[ix] in "\x0a\x0d" {
sys.set_carry()
return ix, true
@@ -85,8 +105,13 @@ strings {
sub rfind(str stringptr, ubyte character) -> ubyte, bool {
; Locates the first position of the given character in the string, starting from the right.
; returns index in A, and boolean if found or not. (when false A will also be 255, an invalid index).
ubyte n = length(stringptr)
if n==0 {
sys.clear_carry()
return 255, false
}
ubyte ix
for ix in length(stringptr)-1 downto 0 {
for ix in n-1 downto 0 {
if stringptr[ix]==character {
sys.set_carry()
return ix, true
@@ -282,6 +307,10 @@ strings {
; or if needle doesn't occur in haystack it returns Carry clear and 255 (an invalid index.)
ubyte haystack_len = length(haystack)
ubyte needle_len = length(needle)
if needle_len == 0 {
sys.set_carry()
return 0
}
if needle_len <= haystack_len {
ubyte idx
haystack_len = haystack_len - needle_len + 1
+28 -28
View File
@@ -146,14 +146,14 @@ class TestCompilerOnExamplesC64: FunSpec({
val onlyC64 = cartesianProduct(
listOf(
ExampleSizes("balloonflight", c64SizeOptimized=1738, c64SizeUnoptimized=3606),
ExampleSizes("banking", c64SizeOptimized=680, c64SizeUnoptimized=1356),
ExampleSizes("banking", c64SizeOptimized=680, c64SizeUnoptimized=1313),
ExampleSizes("bdmusic-irq", c64SizeOptimized=1001, c64SizeUnoptimized=1019),
ExampleSizes("bdmusic", c64SizeOptimized=938, c64SizeUnoptimized=1011),
ExampleSizes("vera64-bdmusic", c64SizeOptimized=2375, c64SizeUnoptimized=2487),
ExampleSizes("charset", c64SizeOptimized=835, c64SizeUnoptimized=860),
ExampleSizes("cube3d-sprites", c64SizeOptimized=2846, c64SizeUnoptimized=4075),
ExampleSizes("fileselector", c64SizeOptimized=3523, c64SizeUnoptimized=4510),
ExampleSizes("library/main", c64SizeOptimized=321, c64SizeUnoptimized=1147),
ExampleSizes("fileselector", c64SizeOptimized=3523, c64SizeUnoptimized=4577),
ExampleSizes("library/main", c64SizeOptimized=321, c64SizeUnoptimized=1102),
ExampleSizes("multiplexer", c64SizeOptimized=1987, c64SizeUnoptimized=3333),
ExampleSizes("petswirl", c64SizeOptimized=1121, c64SizeUnoptimized=2435),
ExampleSizes("plasma", c64SizeOptimized=1532, c64SizeUnoptimized=3085),
@@ -196,24 +196,24 @@ class TestCompilerOnExamplesCx16: FunSpec({
listOf(
ExampleSizes("vtui/testvtui", cx16SizeOptimized=3041, cx16SizeUnoptimized=3041),
ExampleSizes("pcmaudio/play-adpcm", cx16SizeOptimized=37994, cx16SizeUnoptimized=38450),
ExampleSizes("pcmaudio/stream-wav", cx16SizeOptimized=8026, cx16SizeUnoptimized=10081),
ExampleSizes("pcmaudio/stream-simple-aflow", cx16SizeOptimized=1842, cx16SizeUnoptimized=3612),
ExampleSizes("pcmaudio/stream-simple-poll", cx16SizeOptimized=1556, cx16SizeUnoptimized=3322),
ExampleSizes("pcmaudio/vumeter", cx16SizeOptimized=3862, cx16SizeUnoptimized=6221),
ExampleSizes("sprites/dragon", cx16SizeOptimized=1785, cx16SizeUnoptimized=5746),
ExampleSizes("pcmaudio/stream-wav", cx16SizeOptimized=8026, cx16SizeUnoptimized=10099),
ExampleSizes("pcmaudio/stream-simple-aflow", cx16SizeOptimized=1842, cx16SizeUnoptimized=3596),
ExampleSizes("pcmaudio/stream-simple-poll", cx16SizeOptimized=1556, cx16SizeUnoptimized=3307),
ExampleSizes("pcmaudio/vumeter", cx16SizeOptimized=3862, cx16SizeUnoptimized=6213),
ExampleSizes("sprites/dragon", cx16SizeOptimized=1785, cx16SizeUnoptimized=5734),
ExampleSizes("sprites/dragons", cx16SizeOptimized=1503, cx16SizeUnoptimized=5361),
ExampleSizes("zsmkit_v1/demo1", cx16SizeOptimized=7472, cx16SizeUnoptimized=7910),
ExampleSizes("zsmkit_v1/demo2", cx16SizeOptimized=708, cx16SizeUnoptimized=2516),
ExampleSizes("zsmkit_v1/demo2", cx16SizeOptimized=708, cx16SizeUnoptimized=2508),
// zsmkit_v1/zsmkit_high and zsmkit_v1/zsmkit_low don't compile (empty source files)
ExampleSizes("zsmkit_v2/demo", cx16SizeOptimized=790, cx16SizeUnoptimized=2868),
ExampleSizes("zsmkit_v2/demo", cx16SizeOptimized=790, cx16SizeUnoptimized=2891),
// zsmkit_v2/zsmkit doesn't compile (empty source file)
ExampleSizes("banking/program", cx16SizeOptimized=665, cx16SizeUnoptimized=2510),
ExampleSizes("dynamicbanking/program", cx16SizeOptimized=1211, cx16SizeUnoptimized=3089),
ExampleSizes("fileselector/standalone", cx16SizeOptimized=4110, cx16SizeUnoptimized=6054),
ExampleSizes("fileselector/standalone", cx16SizeOptimized=4110, cx16SizeUnoptimized=6064),
ExampleSizes("fileselector/main", cx16SizeOptimized=480, cx16SizeUnoptimized=2314),
// fileselector/fselector and fileselector/namesorting don't compile (empty source files)
ExampleSizes("pointers/fountain-cx16", cx16SizeOptimized=1269, cx16SizeUnoptimized=3354),
ExampleSizes("amiga", cx16SizeOptimized=7619, cx16SizeUnoptimized=11610),
ExampleSizes("amiga", cx16SizeOptimized=7619, cx16SizeUnoptimized=11603),
ExampleSizes("audioroutines", cx16SizeOptimized=948, cx16SizeUnoptimized=1244),
ExampleSizes("automatons", cx16SizeOptimized=1037, cx16SizeUnoptimized=3094),
ExampleSizes("balloonflight", cx16SizeOptimized=4139, cx16SizeUnoptimized=6941),
@@ -227,9 +227,9 @@ class TestCompilerOnExamplesCx16: FunSpec({
ExampleSizes("cobramk3-verafx", cx16SizeOptimized=5156, cx16SizeUnoptimized=6853),
ExampleSizes("colorbars", cx16SizeOptimized=1606, cx16SizeUnoptimized=3648),
ExampleSizes("cxlogo", cx16SizeOptimized=599, cx16SizeUnoptimized=2639),
ExampleSizes("diskspeed", cx16SizeOptimized=6111, cx16SizeUnoptimized=9829),
ExampleSizes("diskspeed", cx16SizeOptimized=6111, cx16SizeUnoptimized=9843),
ExampleSizes("fileseek", cx16SizeOptimized=2462, cx16SizeUnoptimized=4291),
ExampleSizes("floatparse", cx16SizeOptimized=8303, cx16SizeUnoptimized=9171),
ExampleSizes("floatparse", cx16SizeOptimized=8303, cx16SizeUnoptimized=9158),
ExampleSizes("interpolation", cx16SizeOptimized=3606, cx16SizeUnoptimized=6247),
ExampleSizes("kefrenbars", cx16SizeOptimized=2273, cx16SizeUnoptimized=5764),
ExampleSizes("keyboardhandler", cx16SizeOptimized=412, cx16SizeUnoptimized=717),
@@ -240,10 +240,10 @@ class TestCompilerOnExamplesCx16: FunSpec({
ExampleSizes("multi-irq-old", cx16SizeOptimized=962, cx16SizeUnoptimized=1418),
ExampleSizes("plasma", cx16SizeOptimized=1579, cx16SizeUnoptimized=3424),
ExampleSizes("rasterbars", cx16SizeOptimized=1200, cx16SizeUnoptimized=3112),
ExampleSizes("serialdownload", cx16SizeOptimized=2887, cx16SizeUnoptimized=4034),
ExampleSizes("showbmx", cx16SizeOptimized=2756, cx16SizeUnoptimized=5106),
ExampleSizes("serialdownload", cx16SizeOptimized=2887, cx16SizeUnoptimized=4026),
ExampleSizes("showbmx", cx16SizeOptimized=2756, cx16SizeUnoptimized=5126),
ExampleSizes("snow", cx16SizeOptimized=3099, cx16SizeUnoptimized=6998),
ExampleSizes("sortingbench", cx16SizeOptimized=2928, cx16SizeUnoptimized=5601),
ExampleSizes("sortingbench", cx16SizeOptimized=2928, cx16SizeUnoptimized=5586),
ExampleSizes("spotlight", cx16SizeOptimized=908, cx16SizeUnoptimized=3116),
ExampleSizes("starszoom", cx16SizeOptimized=2498, cx16SizeUnoptimized=6061),
ExampleSizes("test_gfx_hires", cx16SizeOptimized=5017, cx16SizeUnoptimized=8875),
@@ -282,7 +282,7 @@ class TestCompilerOnExamplesBothC64andCx16: FunSpec({
ExampleSizes("cube3d", c64SizeOptimized=2538, c64SizeUnoptimized=3820, cx16SizeOptimized=2477, cx16SizeUnoptimized=4031),
ExampleSizes("cube3d-float", c64SizeOptimized=2390, c64SizeUnoptimized=2441, cx16SizeOptimized=2283, cx16SizeUnoptimized=2606),
ExampleSizes("cube3d-gfx", c64SizeOptimized=3648, c64SizeUnoptimized=4966, cx16SizeOptimized=2318, cx16SizeUnoptimized=3852),
ExampleSizes("dirlist", c64SizeOptimized=1515, c64SizeUnoptimized=2291, cx16SizeOptimized=1525, cx16SizeUnoptimized=3270),
ExampleSizes("dirlist", c64SizeOptimized=1515, c64SizeUnoptimized=2254, cx16SizeOptimized=1525, cx16SizeUnoptimized=3270),
ExampleSizes("fibonacci", c64SizeOptimized=582, c64SizeUnoptimized=601, cx16SizeOptimized=634, cx16SizeUnoptimized=933),
ExampleSizes("fractal-tree", c64SizeOptimized=2778, c64SizeUnoptimized=2821, cx16SizeOptimized=1168, cx16SizeUnoptimized=1462),
ExampleSizes("maze", c64SizeOptimized=2994, c64SizeUnoptimized=4931, cx16SizeOptimized=2793, cx16SizeUnoptimized=4899),
@@ -296,12 +296,12 @@ class TestCompilerOnExamplesBothC64andCx16: FunSpec({
ExampleSizes("swirl", c64SizeOptimized=1010, c64SizeUnoptimized=2273, cx16SizeOptimized=1039, cx16SizeUnoptimized=2584),
ExampleSizes("swirl-float", c64SizeOptimized=664, c64SizeUnoptimized=690, cx16SizeOptimized=528, cx16SizeUnoptimized=838),
ExampleSizes("tehtriz", c64SizeOptimized=4300, c64SizeUnoptimized=6328, cx16SizeOptimized=6154, cx16SizeUnoptimized=8495),
ExampleSizes("textelite", c64SizeOptimized=11174, c64SizeUnoptimized=13831, cx16SizeOptimized=10710, cx16SizeUnoptimized=14444),
ExampleSizes("pointers/animalgame", c64SizeOptimized=1965, c64SizeUnoptimized=2521, cx16SizeOptimized=1991, cx16SizeUnoptimized=2801),
ExampleSizes("textelite", c64SizeOptimized=11174, c64SizeUnoptimized=13902, cx16SizeOptimized=10710, cx16SizeUnoptimized=14490),
ExampleSizes("pointers/animalgame", c64SizeOptimized=1965, c64SizeUnoptimized=2505, cx16SizeOptimized=1991, cx16SizeUnoptimized=2786),
ExampleSizes("pointers/binarytree", c64SizeOptimized=2672, c64SizeUnoptimized=2735, cx16SizeOptimized=2175, cx16SizeUnoptimized=2487),
ExampleSizes("pointers/hashtable", c64SizeOptimized=3296, c64SizeUnoptimized=4119, cx16SizeOptimized=2822, cx16SizeUnoptimized=3695),
ExampleSizes("pointers/hashtable", c64SizeOptimized=3296, c64SizeUnoptimized=4085, cx16SizeOptimized=2822, cx16SizeUnoptimized=3713),
ExampleSizes("pointers/sortedlist", c64SizeOptimized=1393, c64SizeUnoptimized=3179, cx16SizeOptimized=1324, cx16SizeUnoptimized=3389),
ExampleSizes("pointers/sorting", c64SizeOptimized=1653, c64SizeUnoptimized=2387, cx16SizeOptimized=1679, cx16SizeUnoptimized=2536),
ExampleSizes("pointers/sorting", c64SizeOptimized=1653, c64SizeUnoptimized=2458, cx16SizeOptimized=1679, cx16SizeUnoptimized=2525),
),
listOf(false, true),
listOf(C64Target(), Cx16Target())
@@ -334,16 +334,16 @@ class TestCompilerOnExamplesVirtual: FunSpec({
val onlyVirtual = cartesianProduct(
listOf(
ExampleSizes("bouncegfx", virtualInstrCountOptimized=197, virtualInstrCountUnoptimized=821, virtualRegCountOptimized=69, virtualRegCountUnoptimized=354),
ExampleSizes("bsieve", virtualInstrCountOptimized=262, virtualInstrCountUnoptimized=1900, virtualRegCountOptimized=74, virtualRegCountUnoptimized=800),
ExampleSizes("bsieve", virtualInstrCountOptimized=262, virtualInstrCountUnoptimized=1981, virtualRegCountOptimized=74, virtualRegCountUnoptimized=828),
ExampleSizes("fountain", virtualInstrCountOptimized=194, virtualInstrCountUnoptimized=800, virtualRegCountOptimized=80, virtualRegCountUnoptimized=347),
ExampleSizes("pixelshader", virtualInstrCountOptimized=84, virtualInstrCountUnoptimized=210, virtualRegCountOptimized=17, virtualRegCountUnoptimized=60),
ExampleSizes("sincos", virtualInstrCountOptimized=300, virtualInstrCountUnoptimized=907, virtualRegCountOptimized=115, virtualRegCountUnoptimized=386),
ExampleSizes("pointers/animalgame", virtualInstrCountOptimized=437, virtualInstrCountUnoptimized=2076, virtualRegCountOptimized=158, virtualRegCountUnoptimized=882),
ExampleSizes("pointers/binarytree", virtualInstrCountOptimized=655, virtualInstrCountUnoptimized=2239, virtualRegCountOptimized=264, virtualRegCountUnoptimized=968),
ExampleSizes("pointers/hashtable", virtualInstrCountOptimized=665, virtualInstrCountUnoptimized=2241, virtualRegCountOptimized=279, virtualRegCountUnoptimized=987),
ExampleSizes("pointers/sortedlist", virtualInstrCountOptimized=400, virtualInstrCountUnoptimized=2476, virtualRegCountOptimized=133, virtualRegCountUnoptimized=1061),
ExampleSizes("pointers/animalgame", virtualInstrCountOptimized=437, virtualInstrCountUnoptimized=2153, virtualRegCountOptimized=158, virtualRegCountUnoptimized=913),
ExampleSizes("pointers/binarytree", virtualInstrCountOptimized=655, virtualInstrCountUnoptimized=2320, virtualRegCountOptimized=264, virtualRegCountUnoptimized=997),
ExampleSizes("pointers/hashtable", virtualInstrCountOptimized=665, virtualInstrCountUnoptimized=2318, virtualRegCountOptimized=279, virtualRegCountUnoptimized=1017),
ExampleSizes("pointers/sortedlist", virtualInstrCountOptimized=400, virtualInstrCountUnoptimized=2553, virtualRegCountOptimized=133, virtualRegCountUnoptimized=1091),
ExampleSizes("pointers/fountain-virtual", virtualInstrCountOptimized=216, virtualInstrCountUnoptimized=819, virtualRegCountOptimized=88, virtualRegCountUnoptimized=348),
ExampleSizes("pointers/sorting", virtualInstrCountOptimized=452, virtualInstrCountUnoptimized=2163, virtualRegCountOptimized=141, virtualRegCountUnoptimized=915)
ExampleSizes("pointers/sorting", virtualInstrCountOptimized=452, virtualInstrCountUnoptimized=2240, virtualRegCountOptimized=141, virtualRegCountUnoptimized=944)
),
listOf(false, true)
)
+13
View File
@@ -1855,6 +1855,8 @@ manipulation
``nappend (string, suffix, maxlength) -> ubyte length``
Appends the suffix string to the other string, up to the given maximum length of the combined string.
It is assumed the target string buffer is large enough to contain the result (which includes a terminating 0 byte),
so it must be at least ``maxlength+1`` bytes long.
Returns the length of the combined string.
``ncopy (from, to, maxlength) -> ubyte length``
@@ -1917,6 +1919,17 @@ searching
in a string than using an `in` containment check - because this find routine
properly stops at the first 0-byte string terminator it encounters in case the string was modified.
``find_eol (string) -> ubyte index, bool found``
Locates the first End Of Line character in the string, and a boolean (in Carry flag)
to say if it was found at all. If no line ending is found, index 255 (and false) is returned.
This is a convenience function that treats both CR (13) and LF (10) as a possible line ending.
``findstr (haystack, needle) -> ubyte index``
Searches for the ``needle`` string in the ``haystack`` string.
If found, returns the index in haystack where the needle first occurs, with the Carry flag set.
If the needle doesn't occur, returns 255 (an invalid index) with the Carry flag clear.
An empty needle is considered to be found at index 0.
``pattern_match (string, pattern) -> bool`` (not on Virtual target)
Returns true if the string matches the pattern, false if not.
The matching is CASE-SENSITIVE. If you want to match case-insensitively, use pattern_match_nocase().