mirror of
https://github.com/cc65/cc65.git
synced 2024-06-15 17:30:06 +00:00
Implement strcasestr as part of strstr
This commit is contained in:
parent
82165c1a77
commit
b5d259bafb
|
@ -1,36 +0,0 @@
|
||||||
/*
|
|
||||||
** strcasestr.c
|
|
||||||
**
|
|
||||||
** Colin Leroy-Mira, 2024
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*****************************************************************************/
|
|
||||||
/* Code */
|
|
||||||
/*****************************************************************************/
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
char* __fastcall__ strcasestr(const char *str, const char *substr) {
|
|
||||||
size_t len_a = strlen(str);
|
|
||||||
size_t len_b = strlen(substr);
|
|
||||||
const char *end_str;
|
|
||||||
|
|
||||||
if (len_a < len_b)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
len_a -= len_b;
|
|
||||||
|
|
||||||
for (end_str = str + len_a + 1; str < end_str; str++) {
|
|
||||||
if (!strncasecmp(str, substr, len_b))
|
|
||||||
return (char *)str;
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
|
|
@ -4,11 +4,18 @@
|
||||||
; char* strstr (const char* haystack, const char* needle);
|
; char* strstr (const char* haystack, const char* needle);
|
||||||
;
|
;
|
||||||
|
|
||||||
.export _strstr
|
.export _strstr, _strcasestr
|
||||||
.import popptr1
|
.import popptr1, _tolower
|
||||||
.importzp ptr1, ptr2, ptr3, ptr4, tmp1
|
.importzp ptr1, ptr2, ptr3, ptr4, tmp1, tmp2, tmp3
|
||||||
|
|
||||||
_strstr:
|
_strstr:
|
||||||
|
ldy #$01
|
||||||
|
bne :+
|
||||||
|
_strcasestr:
|
||||||
|
ldy #$00
|
||||||
|
:
|
||||||
|
sty tmp2 ; Set case sensitivity
|
||||||
|
|
||||||
sta ptr2 ; Save needle
|
sta ptr2 ; Save needle
|
||||||
stx ptr2+1
|
stx ptr2+1
|
||||||
sta ptr4 ; Setup temp copy for later
|
sta ptr4 ; Setup temp copy for later
|
||||||
|
@ -24,10 +31,19 @@ _strstr:
|
||||||
; 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).
|
||||||
|
|
||||||
sta tmp1 ; Save start of needle
|
ldx tmp2 ; Lowercase if needed
|
||||||
|
bne :+
|
||||||
|
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
|
||||||
cmp tmp1 ; Start of needle found?
|
|
||||||
|
ldx tmp2 ; Lowercase if needed
|
||||||
|
bne :+
|
||||||
|
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
|
||||||
|
@ -43,7 +59,7 @@ _strstr:
|
||||||
bcc @L3
|
bcc @L3
|
||||||
inc ptr1+1
|
inc ptr1+1
|
||||||
|
|
||||||
; ptr1 points to the start of needle now. Setup temporary pointers for the
|
; ptr1 points to the start of needle in haystack now. Setup temporary pointers for the
|
||||||
; search. The low byte of ptr4 is already set.
|
; search. The low byte of ptr4 is already set.
|
||||||
|
|
||||||
@L3: sta ptr3
|
@L3: sta ptr3
|
||||||
|
@ -57,7 +73,19 @@ _strstr:
|
||||||
|
|
||||||
@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)
|
||||||
cmp (ptr3),y ; Compare with haystack
|
|
||||||
|
ldx tmp2 ; Lowercase if needed
|
||||||
|
bne :+
|
||||||
|
jsr _tolower
|
||||||
|
: sta tmp3
|
||||||
|
|
||||||
|
lda (ptr3),y ; Compare with haystack
|
||||||
|
|
||||||
|
ldx tmp2 ; Lowercase if needed
|
||||||
|
bne :+
|
||||||
|
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
|
||||||
|
@ -82,14 +110,3 @@ _strstr:
|
||||||
lda #$00 ; return NULL
|
lda #$00 ; return NULL
|
||||||
tax
|
tax
|
||||||
rts
|
rts
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -17,12 +17,12 @@
|
||||||
_tolower:
|
_tolower:
|
||||||
cpx #$00 ; out of range?
|
cpx #$00 ; out of range?
|
||||||
bne @L2 ; if so, return the argument unchanged
|
bne @L2 ; if so, return the argument unchanged
|
||||||
tay ; 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?
|
||||||
beq @L1 ; jump if no
|
beq @L1 ; jump if no
|
||||||
tya ; restore char
|
pla ; restore char
|
||||||
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: tya ; restore char
|
@L1: pla ; restore char
|
||||||
@L2: rts
|
@L2: rts
|
||||||
|
|
Loading…
Reference in New Issue
Block a user