mirror of
https://github.com/irmen/prog8.git
synced 2025-09-27 07:17:01 +00:00
added strings.find_eol()
This commit is contained in:
@@ -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 {
|
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.
|
; 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).
|
; returns Carry set if found + index in A, or Carry clear if not found (and A will be 255, an invalid index).
|
||||||
|
@@ -53,34 +53,47 @@ strings {
|
|||||||
target[ix]=0
|
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,
|
; 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).
|
; returns index in A, and boolean if found or not. (when false A will also 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.
|
|
||||||
ubyte ix
|
ubyte ix
|
||||||
for ix in 0 to length(st)-1 {
|
for ix in 0 to length(st)-1 {
|
||||||
if st[ix]==character {
|
if st[ix]==character {
|
||||||
sys.set_carry()
|
sys.set_carry()
|
||||||
return ix
|
return ix, true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sys.clear_carry()
|
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.
|
; 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).
|
; returns index in A, and boolean if found or not. (when false A will also 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.
|
|
||||||
ubyte ix
|
ubyte ix
|
||||||
for ix in length(stringptr)-1 downto 0 {
|
for ix in length(stringptr)-1 downto 0 {
|
||||||
if stringptr[ix]==character {
|
if stringptr[ix]==character {
|
||||||
sys.set_carry()
|
sys.set_carry()
|
||||||
return ix
|
return ix, true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sys.clear_carry()
|
sys.clear_carry()
|
||||||
return 255
|
return 255, false
|
||||||
}
|
}
|
||||||
|
|
||||||
sub contains(str st, ubyte character) -> bool {
|
sub contains(str st, ubyte character) -> bool {
|
||||||
|
@@ -1,48 +1,39 @@
|
|||||||
%import textio
|
%import textio
|
||||||
|
%import strings
|
||||||
%zeropage basicsafe
|
%zeropage basicsafe
|
||||||
|
%encoding petscii
|
||||||
|
|
||||||
main {
|
main {
|
||||||
sub start() {
|
sub start() {
|
||||||
bool @shared bb, bb2
|
str textiso = iso:"first\x0asecond\x0athird\x0a"
|
||||||
|
str textpetscii = petscii:"first\nsecond\nthird\n"
|
||||||
|
|
||||||
bb=true
|
txt.print(" iso: ")
|
||||||
bb2=false
|
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("petscii: ")
|
||||||
txt.print("yes1 ")
|
dump(textpetscii)
|
||||||
else
|
txt.nl()
|
||||||
txt.print("error1 ")
|
cx16.r0L, void = strings.find(textpetscii, '\n')
|
||||||
|
txt.print_ub(cx16.r0L)
|
||||||
if bb2 or test2() or testasm2()
|
txt.spc()
|
||||||
txt.print("error2 ")
|
cx16.r0L, void = strings.find_eol(textpetscii)
|
||||||
else
|
txt.print_ub(cx16.r0L)
|
||||||
txt.print("yes2 ")
|
|
||||||
txt.nl()
|
txt.nl()
|
||||||
}
|
}
|
||||||
|
|
||||||
sub test() -> bool {
|
sub dump(uword ptr) {
|
||||||
cx16.r0++
|
while @(ptr)!=0 {
|
||||||
return true
|
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
|
|
||||||
}}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user