From b5d259bafb1b3bcf4eb2c1ddc92d14b3c891fd49 Mon Sep 17 00:00:00 2001 From: Colin Leroy-Mira Date: Mon, 18 Mar 2024 19:52:04 +0100 Subject: [PATCH] Implement strcasestr as part of strstr --- libsrc/common/strcasestr.c | 36 -------------------------- libsrc/common/strstr.s | 53 +++++++++++++++++++++++++------------- libsrc/common/tolower.s | 6 ++--- 3 files changed, 38 insertions(+), 57 deletions(-) delete mode 100644 libsrc/common/strcasestr.c diff --git a/libsrc/common/strcasestr.c b/libsrc/common/strcasestr.c deleted file mode 100644 index 693b43a37..000000000 --- a/libsrc/common/strcasestr.c +++ /dev/null @@ -1,36 +0,0 @@ -/* -** strcasestr.c -** -** Colin Leroy-Mira, 2024 -*/ - - - -#include -#include -#include - - - -/*****************************************************************************/ -/* 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; -} diff --git a/libsrc/common/strstr.s b/libsrc/common/strstr.s index 84f633245..9cc9c0d33 100644 --- a/libsrc/common/strstr.s +++ b/libsrc/common/strstr.s @@ -4,11 +4,18 @@ ; char* strstr (const char* haystack, const char* needle); ; - .export _strstr - .import popptr1 - .importzp ptr1, ptr2, ptr3, ptr4, tmp1 + .export _strstr, _strcasestr + .import popptr1, _tolower + .importzp ptr1, ptr2, ptr3, ptr4, tmp1, tmp2, tmp3 _strstr: + ldy #$01 + bne :+ +_strcasestr: + ldy #$00 +: + sty tmp2 ; Set case sensitivity + sta ptr2 ; Save needle stx ptr2+1 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 ; 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 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 iny ; Next char bne @L1 @@ -43,7 +59,7 @@ _strstr: bcc @L3 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. @L3: sta ptr3 @@ -57,7 +73,19 @@ _strstr: @L4: lda (ptr4),y ; Get char from needle 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 iny ; Next char bne @L4 @@ -82,14 +110,3 @@ _strstr: lda #$00 ; return NULL tax rts - - - - - - - - - - - diff --git a/libsrc/common/tolower.s b/libsrc/common/tolower.s index 828be1cb1..22b030da3 100644 --- a/libsrc/common/tolower.s +++ b/libsrc/common/tolower.s @@ -17,12 +17,12 @@ _tolower: cpx #$00 ; out of range? bne @L2 ; if so, return the argument unchanged - tay ; save char + pha ; save char jsr ctypemaskdirect ; get character classification and #CT_UPPER ; upper case char? beq @L1 ; jump if no - tya ; restore char + pla ; restore char adc #<('a'-'A') ; make lower case char (ctypemaskdirect ensures carry clear) rts -@L1: tya ; restore char +@L1: pla ; restore char @L2: rts