Added further optimizations and unit tests.
This commit is contained in:
@@ -1,54 +1,54 @@
|
||||
;
|
||||
; Ullrich von Bassewitz, 11.06.1998
|
||||
; Christian Krueger: 05-Aug-2013, optimization
|
||||
;
|
||||
; size_t strcspn (const char* s1, const char* s2);
|
||||
;
|
||||
|
||||
.export _strcspn
|
||||
.import popax
|
||||
.importzp ptr1, ptr2, tmp1, tmp2, tmp3
|
||||
.import popax, _strlen
|
||||
.importzp ptr1, ptr2, tmp1, tmp2
|
||||
|
||||
_strcspn:
|
||||
sta ptr2 ; Save s2
|
||||
stx ptr2+1
|
||||
jsr popax ; Get s1
|
||||
sta ptr1
|
||||
stx ptr1+1
|
||||
ldx #0 ; low counter byte
|
||||
stx tmp1 ; high counter byte
|
||||
ldy #$00
|
||||
jsr _strlen ; get length in a/x and transfer s2 to ptr1
|
||||
; Note: It does not make sense to
|
||||
; have more than 255 test chars, so
|
||||
; we don't support a high byte here! (ptr1+1 is
|
||||
; also unchanged in strlen then (important!))
|
||||
; -> the original implementation also
|
||||
; ignored this case
|
||||
|
||||
L1: lda (ptr1),y ; get next char from s1
|
||||
beq L6 ; jump if done
|
||||
sta tmp2 ; save char
|
||||
sta tmp1 ; tmp1 = strlen of test chars
|
||||
jsr popax ; get and save s1
|
||||
sta ptr2 ; to ptr2
|
||||
stx ptr2+1
|
||||
ldx #0 ; low counter byte
|
||||
stx tmp2 ; high counter byte
|
||||
|
||||
loadChar:
|
||||
ldy #0
|
||||
lda (ptr2),y ; get next char from s1
|
||||
beq leave ; handly byte of s1
|
||||
advance:
|
||||
inc ptr2 ; advance string position to test
|
||||
bne check
|
||||
inc ptr2+1
|
||||
dey ; correct next iny (faster/shorter than bne...)
|
||||
|
||||
checkNext:
|
||||
iny
|
||||
bne L2
|
||||
inc ptr1+1
|
||||
L2: sty tmp3 ; save index into s1
|
||||
check: cpy tmp1 ; compare with length of test character string
|
||||
beq endOfTestChars
|
||||
cmp (ptr1),y ; found matching char?
|
||||
bne checkNext
|
||||
|
||||
ldy #0 ; get index into s2
|
||||
L3: lda (ptr2),y ;
|
||||
beq L4 ; jump if done
|
||||
cmp tmp2
|
||||
beq L6
|
||||
iny
|
||||
bne L3
|
||||
|
||||
; The character was not found in s2. Increment the counter and start over
|
||||
|
||||
L4: ldy tmp3 ; reload index
|
||||
inx
|
||||
bne L1
|
||||
inc tmp1
|
||||
bne L1
|
||||
|
||||
; The character was found, or we reached the end of s1. Return count of
|
||||
; characters
|
||||
|
||||
L6: txa ; get low counter byte
|
||||
ldx tmp1 ; get high counter byte
|
||||
leave: txa ; restore position of finding
|
||||
ldx tmp2 ; and return
|
||||
rts
|
||||
|
||||
|
||||
|
||||
endOfTestChars:
|
||||
inx
|
||||
bne loadChar
|
||||
inc tmp2
|
||||
bne loadChar ; like bra...
|
||||
|
||||
|
||||
Reference in New Issue
Block a user