From ca6027abcd276d2c46698e36d6703baccad3458c Mon Sep 17 00:00:00 2001 From: Kwarde Date: Mon, 28 Jul 2025 17:29:03 +0200 Subject: [PATCH] strfind: assure re-check is done on mismatch after initial match + add tests --- src/string.asm | 11 +++++++++++ src/tests.asm | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/src/string.asm b/src/string.asm index 50266b5..87df0a5 100644 --- a/src/string.asm +++ b/src/string.asm @@ -192,6 +192,17 @@ strfind: .noMatch: ; not gonna figure out here if there was an existing match already. just XOR r10b,r10b and load original pointer to what[] to rsi + ; EDIT: actually yes I am. strfind("gidgiddy", "giddy", 0); [gid]giddy<>[gid]dy is a match but then gid[g]iddy<>gid[d]y is not while it should check gid[g]iddy<>[g]iddy + test r10b, r10b + jz .continueLoop + mov rsi, r11 + mov r9b, byte [rsi] + cmp byte [rdi], r9b + jne .itReallyWasNoMatchAfterAll + mov r8, rcx + inc rsi + jmp .continueLoop + .itReallyWasNoMatchAfterAll: xor r10b, r10b mov rsi, r11 diff --git a/src/tests.asm b/src/tests.asm index 7414275..5a0dd4b 100644 --- a/src/tests.asm +++ b/src/tests.asm @@ -83,6 +83,7 @@ extern strcpy extern strcat extern strclr extern strcmp +extern strfind ;convert.asm extern atoi extern itoa @@ -132,6 +133,7 @@ section .rodata TEST_strcat equ 1 TEST_strclr equ 1 TEST_strcmp equ 1 + TEST_strfind equ 1 ;convert.asm TEST_atoi equ 1 TEST_itoa equ 1 @@ -315,6 +317,15 @@ section .rodata addTest(strcmp5, "strcmp(strBuff1, str1)") ;14 addTest(strcmp6_1, "strclr(strBuff1, 64)") addTest(strcmp6_2, "strcmp(str1, strBuff1)") ;0 + ;strfind() + addTestHeader(_strfind, "strfind") + strfind_str db "I am not a gidgiddy goat or a gigiddyd goat but a giddy goat!",EOS + strfind_what db "giddy",EOS + strfind_strs db "\tstrfind_str: %s\n\tstrfind_what: %s\n",EOS + addTest(strfind1, "strfind(strfind_str, strfind_what, 0)") ;15 + addTest(strfind2, "strfind(strfind_str, strfind_what, 15)") ;33 + addTest(strfind3, "strfind(strfind_str, strfind_what, 33)") ;51 + addTest(strfind4, "strfind(strfind_str, strfind_what, 51)") ;0 ;atoi() addTestHeader(_atoi, "atoi") addTest(atoi1, "atoi(''079168045'')") ;79168045 @@ -1144,6 +1155,48 @@ _start: assert_eq(0) %endif +;--- strfind() +%if TEST_strfind + printTestHeader(_strfind) + + lea rdi, [rel strfind_strs] + lea rsi, [rel strfind_str] + lea rdx, [rel strfind_what] + call printf + + ; TEST 1: strfind(strfind_str, strfind_what, 0) => 15 + printTest(strfind1) + lea rdi, [rel strfind_str] + lea rsi, [rel strfind_what] + xor rdx, rdx + call strfind + assert_eq(15) + + ; TEST 2: strfind(strfind_str, strfind_what, 15) => 33 + printTest(strfind2) + lea rdi, [rel strfind_str] + lea rsi, [rel strfind_what] + mov rdx, 15 + call strfind + assert_eq(33) + + ; TEST 3: strfind(strfind_str, strfind_what, 33) => 51 + printTest(strfind3) + lea rdi, [rel strfind_str] + lea rsi, [rel strfind_what] + mov rdx, 33 + call strfind + assert_eq(51) + + ; TEST 4: strfind(strfind_str, strfind_what, 51) + printTest(strfind4) + lea rdi, [rel strfind_str] + lea rsi, [rel strfind_what] + mov rdx, 51 + call strfind + assert_eq(0) +%endif + ;--- atoi() %if TEST_atoi printTestHeader(_atoi)