1
0
mirror of https://github.com/cc65/cc65.git synced 2024-11-19 06:31:31 +00:00

Factorize to save 20 bytes

This commit is contained in:
Colin Leroy-Mira 2024-03-19 18:07:17 +01:00
parent b5d259bafb
commit 0c681b42ef
2 changed files with 21 additions and 31 deletions

View File

@ -5,16 +5,18 @@
;
.export _strstr, _strcasestr
.import popptr1, _tolower
.importzp ptr1, ptr2, ptr3, ptr4, tmp1, tmp2, tmp3
.import popptr1, return0, tolower_a
.importzp ptr1, ptr2, ptr3, ptr4, tmp1, tmp2
maybe_lower: ; Lowercase char in A if needed
jmp tolower_a ; patched on entry with either JMP or RTS
_strstr:
ldy #$01
ldy #$60 ; RTS
bne :+
_strcasestr:
ldy #$00
:
sty tmp2 ; Set case sensitivity
ldy #$4C ; JMP absolute
: sty maybe_lower
sta ptr2 ; Save needle
stx ptr2+1
@ -31,19 +33,13 @@ _strcasestr:
; Search for the beginning of the string (this is not an optimal search
; strategy [in fact, it's pretty dumb], but it's simple to implement).
ldx tmp2 ; Lowercase if needed
bne :+
jsr _tolower
: sta tmp1 ; Save start of needle
jsr maybe_lower ; Lowercase if needed
sta tmp1 ; Save start of needle
@L1: lda (ptr1),y ; Get next char from haystack
beq @NotFound ; Jump if end
ldx tmp2 ; Lowercase if needed
bne :+
jsr _tolower
: cmp tmp1 ; Start of needle found?
jsr maybe_lower ; Lowercase if needed
cmp tmp1 ; Start of needle found?
beq @L2 ; Jump if so
iny ; Next char
bne @L1
@ -74,18 +70,13 @@ _strcasestr:
@L4: lda (ptr4),y ; Get char from needle
beq @Found ; Jump if end of needle (-> found)
ldx tmp2 ; Lowercase if needed
bne :+
jsr _tolower
: sta tmp3
jsr maybe_lower ; Lowercase if needed
sta tmp2
lda (ptr3),y ; Compare with haystack
ldx tmp2 ; Lowercase if needed
bne :+
jsr _tolower
: cmp tmp3
jsr maybe_lower ; Lowercase if needed
cmp tmp2
bne @L5 ; Jump if not equal
iny ; Next char
bne @L4
@ -107,6 +98,4 @@ _strcasestr:
; We reached end of haystack without finding needle
@NotFound:
lda #$00 ; return NULL
tax
rts
jmp return0 ; return NULL

View File

@ -10,13 +10,14 @@
; int tolower (int c);
;
.export _tolower
.export _tolower, tolower_a
.include "ctype.inc"
.import ctypemaskdirect
_tolower:
cpx #$00 ; out of range?
bne @L2 ; if so, return the argument unchanged
bne out ; if so, return the argument unchanged
tolower_a:
pha ; save char
jsr ctypemaskdirect ; get character classification
and #CT_UPPER ; upper case char?
@ -25,4 +26,4 @@ _tolower:
adc #<('a'-'A') ; make lower case char (ctypemaskdirect ensures carry clear)
rts
@L1: pla ; restore char
@L2: rts
out: rts