diff --git a/compiler/res/prog8lib/string.p8 b/compiler/res/prog8lib/string.p8 index 402e2426b..48d6f5b22 100644 --- a/compiler/res/prog8lib/string.p8 +++ b/compiler/res/prog8lib/string.p8 @@ -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 }} } diff --git a/docs/source/libraries.rst b/docs/source/libraries.rst index 826885168..588eb7c70 100644 --- a/docs/source/libraries.rst +++ b/docs/source/libraries.rst @@ -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. diff --git a/docs/source/todo.rst b/docs/source/todo.rst index e2e8b067f..88b80b61b 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -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? ... diff --git a/examples/test.p8 b/examples/test.p8 index f0f3b9a20..13ff923ef 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -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? } }