library API change: string.find now returns index of character + carry bit status (instead of substring address)

This commit is contained in:
Irmen de Jong 2022-01-24 21:37:04 +01:00
parent b7d06f2c0a
commit 46f9fab140
4 changed files with 44 additions and 23 deletions

View File

@ -130,9 +130,9 @@ _startloop dey
}}
}
asmsub find(uword string @R0, ubyte character @A) -> uword @AY {
asmsub find(uword string @R0, ubyte character @A) -> ubyte @A, ubyte @Pc {
; Locates the first position of the given character in the string,
; returns the string starting with this character or $0000 if the character is not found.
; returns Carry set if found + index in A, or Carry clear if not found.
%asm {{
; need to copy the the cx16 virtual registers to zeropage to make this run on C64...
sta P8ZP_SCRATCH_B1
@ -147,18 +147,11 @@ _startloop dey
beq _found
iny
bne -
_notfound lda #0
ldy #0
_notfound clc
rts
_found sty P8ZP_SCRATCH_B1
ldy P8ZP_SCRATCH_W1+1
lda P8ZP_SCRATCH_W1
clc
adc P8ZP_SCRATCH_B1
bcc +
iny
+ rts
_found tya
sec
rts
}}
}

View File

@ -174,9 +174,9 @@ Provides string manipulation routines.
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).
``find(string, char) -> uword address``
Locates the first position of the given character in the string, returns the string starting
with this character or $0000 if the character is not found.
``find(string, char) -> ubyte index + carry bit``
Locates the first position of the given character in the string, returns carry bit set if found
and the index in the string. Or carry bit clear if the character was not found.
``compare(string1, string2) -> ubyte result``
Returns -1, 0 or 1 depeding on wether string1 sorts before, equal or after string2.

View File

@ -3,7 +3,6 @@ TODO
For next release
^^^^^^^^^^^^^^^^
- string.find should return index of found character + carry set if found, carry clear if not found. (fix cx16assem, it uses current behavior. Also fix docs!)
- if char in "string" should fall back to string.find if string is longer than... 16?
...

View File

@ -4,11 +4,40 @@
main {
sub start() {
uword m1 = memory("mem1", 123, $100)
uword m2 = memory("mem2", 999, 2)
txt.print_uwhex(m1, true)
txt.nl()
txt.print_uwhex(m2, true)
txt.nl()
str s1 = "irmen@razorvine.net"
ubyte ff = string.find(s1, '@')
if_cs {
txt.print_uwhex(&s1+ff, true)
txt.spc()
txt.print(&s1+ff)
txt.nl()
}
ff = string.find(s1, 'i')
if_cs {
txt.print_uwhex(&s1+ff, true)
txt.spc()
txt.print(&s1+ff)
txt.nl()
}
ff = string.find(s1, 't')
if_cs {
txt.print_uwhex(&s1+ff, true)
txt.spc()
txt.print(&s1+ff)
txt.nl()
}
ff = string.find(s1, 'q')
if_cs {
txt.print_uwhex(&s1+ff, true)
txt.spc()
txt.print(&s1+ff)
txt.nl()
}
; txt.print_uwhex(s1+ff, true) ; TODO fix compiler crash on s1+ff. why no crash when using 1-argument functioncall?
}
}