added strings.find_eol()

This commit is contained in:
Irmen de Jong
2025-06-03 18:08:30 +02:00
parent b5e51ab937
commit d067fa4b73
3 changed files with 77 additions and 46 deletions

View File

@@ -151,6 +151,33 @@ _found tya
}}
}
asmsub find_eol(uword string @AY) -> ubyte @A, bool @Pc {
; 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 Carry set if found + index in A, or Carry clear if not found (and A will be 255, an invalid index).
%asm {{
; need to copy the the cx16 virtual registers to zeropage to make this run on C64...
sta P8ZP_SCRATCH_W1
sty P8ZP_SCRATCH_W1+1
ldy #0
- lda (P8ZP_SCRATCH_W1),y
beq _notfound
cmp #13
beq _found
cmp #10
beq _found
iny
bne -
_notfound lda #255
clc
rts
_found tya
sec
rts
}}
}
asmsub rfind(uword string @AY, ubyte character @X) -> ubyte @A, bool @Pc {
; Locates the first position of the given character in the string, starting from the right.
; returns Carry set if found + index in A, or Carry clear if not found (and A will be 255, an invalid index).

View File

@@ -53,34 +53,47 @@ strings {
target[ix]=0
}
sub find(str st, ubyte character) -> ubyte {
sub find(str st, ubyte character) -> ubyte, bool {
; Locates the first position of the given character in the string,
; returns Carry set if found + index in A, or Carry clear if not found (and A will be 255, an invalid index).
; NOTE: because this isn't an asmsub, there's only a SINGLE return value here. On the c64/cx16 targets etc there are 2 return values.
; returns index in A, and boolean if found or not. (when false A will also be 255, an invalid index).
ubyte ix
for ix in 0 to length(st)-1 {
if st[ix]==character {
sys.set_carry()
return ix
return ix, true
}
}
sys.clear_carry()
return 255
return 255, false
}
sub rfind(uword stringptr, ubyte character) -> ubyte {
sub find_eol(str st) -> ubyte, bool {
; 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 ix
for ix in 0 to length(st)-1 {
if st[ix] in "\x0a\x0d" {
sys.set_carry()
return ix, true
}
}
sys.clear_carry()
return 255, false
}
sub rfind(uword stringptr, ubyte character) -> ubyte, bool {
; Locates the first position of the given character in the string, starting from the right.
; returns Carry set if found + index in A, or Carry clear if not found (and A will be 255, an invalid index).
; NOTE: because this isn't an asmsub, there's only a SINGLE return value here. On the c64/cx16 targets etc there are 2 return values.
; returns index in A, and boolean if found or not. (when false A will also be 255, an invalid index).
ubyte ix
for ix in length(stringptr)-1 downto 0 {
if stringptr[ix]==character {
sys.set_carry()
return ix
return ix, true
}
}
sys.clear_carry()
return 255
return 255, false
}
sub contains(str st, ubyte character) -> bool {

View File

@@ -1,48 +1,39 @@
%import textio
%import strings
%zeropage basicsafe
%encoding petscii
main {
sub start() {
bool @shared bb, bb2
str textiso = iso:"first\x0asecond\x0athird\x0a"
str textpetscii = petscii:"first\nsecond\nthird\n"
bb=true
bb2=false
txt.print(" iso: ")
dump(textiso)
txt.nl()
cx16.r0L, void = strings.find(textiso, '\n')
txt.print_ub(cx16.r0L)
txt.spc()
cx16.r0L, void = strings.find_eol(textiso)
txt.print_ub(cx16.r0L)
txt.nl()
if bb and test() and testasm()
txt.print("yes1 ")
else
txt.print("error1 ")
if bb2 or test2() or testasm2()
txt.print("error2 ")
else
txt.print("yes2 ")
txt.print("petscii: ")
dump(textpetscii)
txt.nl()
cx16.r0L, void = strings.find(textpetscii, '\n')
txt.print_ub(cx16.r0L)
txt.spc()
cx16.r0L, void = strings.find_eol(textpetscii)
txt.print_ub(cx16.r0L)
txt.nl()
}
sub test() -> bool {
cx16.r0++
return true
sub dump(uword ptr) {
while @(ptr)!=0 {
txt.print_ubhex(@(ptr), false)
txt.spc()
ptr++
}
sub test2() -> bool {
cx16.r0++
return false
}
asmsub testasm() -> bool @A {
%asm {{
lda #1
ldy #0
rts
}}
}
asmsub testasm2() -> bool @A {
%asm {{
lda #0
ldy #1
rts
}}
}
}