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

View File

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