Skip to content

Commit ec2b776

Browse files
committed
add strchrnul
1 parent 97cddd9 commit ec2b776

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

src/libc/include/string.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ char *strchr(const char *s, int c)
6565
char *strrchr(const char *s, int c)
6666
__attribute__((nonnull(1)));
6767

68+
char *strchrnul(const char *s, int c)
69+
__NOEXCEPT __attribute__((nonnull(1))) __attribute__((__pure__));
70+
6871
char *strpbrk(const char *s, const char *accept)
6972
__attribute__((nonnull(1, 2)));
7073

src/libc/strchrnul.src

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
assume adl=1
2+
3+
section .text
4+
5+
public _strchrnul
6+
7+
_strchrnul:
8+
pop bc, hl, de
9+
push de, hl, bc
10+
xor a, a
11+
ld bc, 0
12+
push hl
13+
cpir
14+
sbc hl, hl
15+
sbc hl, bc
16+
ex (sp), hl
17+
pop bc
18+
ld a, e
19+
; HL = str
20+
; BC = strlen(str) + 1
21+
; A = ch
22+
cpir
23+
dec hl
24+
; points to the '\0' if ch was not found
25+
ret

test/standalone/asprintf_fprintf/src/main.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ int T_strcmp(const char *s1, const char *s2)
7474
int T_strncmp(const char *s1, const char *s2, size_t n)
7575
__attribute__((nonnull(1, 2)));
7676

77+
char *T_strchrnul(const char *s, int c)
78+
__attribute__((nonnull(1)));
79+
7780
void T_bzero(void* s, size_t n);
7881

7982
#else
@@ -92,6 +95,7 @@ void T_bzero(void* s, size_t n);
9295
#define T_strlen strlen
9396
#define T_strcmp strcmp
9497
#define T_strncmp strncmp
98+
#define T_strchrnul strchrnul
9599
#define T_bzero bzero
96100

97101
#endif
@@ -778,6 +782,17 @@ int memmem_test(void) {
778782
return 0;
779783
}
780784

785+
int strchrnul_test(void) {
786+
C(T_strchrnul(SINK, '\0') == SINK);
787+
C(T_strchrnul(SINK, '\xff') == SINK);
788+
const size_t test_3_len = strlen(test_3);
789+
C(T_strchrnul(test_3, '\0') == test_3 + test_3_len);
790+
for (size_t i = 0; i < test_3_len; i++) {
791+
C(T_strchrnul(test_3, test_3[i]) == strchr(test_3, test_3[i]));
792+
}
793+
return 0;
794+
}
795+
781796
int run_tests(void) {
782797
int ret = 0;
783798
/* boot_asprintf */
@@ -807,6 +822,7 @@ int run_tests(void) {
807822
TEST(strlcat_test());
808823
TEST(stpncpy_test());
809824
TEST(memmem_test());
825+
TEST(strchrnul_test());
810826

811827
return 0;
812828
}

test/standalone/asprintf_fprintf/src/rename.asm

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
section .text
44

55
public _T_memset, _T_memcpy, _T_memmove, _T_memcmp, _T_memccpy, _T_mempcpy, _T_memrchr, _T_memmem
6-
public _T_strlen, _T_strcmp, _T_strncmp, _T_stpcpy, _T_stpncpy, _T_strlcat
6+
public _T_strlen, _T_strcmp, _T_strncmp, _T_stpcpy, _T_stpncpy, _T_strlcat, _T_strchrnul
77
public _T_bzero
88

99
_T_memset := _memset
@@ -21,6 +21,7 @@ _T_strncmp := _strncmp
2121
_T_stpcpy := _stpcpy
2222
_T_stpncpy := _stpncpy
2323
_T_strlcat := _strlcat
24+
_T_strchrnul := _strchrnul
2425

2526
_T_bzero := _bzero
2627

@@ -31,5 +32,5 @@ _NULL_ptr:
3132
db $00, $00, $00
3233

3334
extern _memset, _memcpy, _memmove, _memcmp, _memccpy, _mempcpy, _memrchr, _memmem
34-
extern _strlen, _strcmp, _strncmp, _stpcpy, _stpncpy, _strlcat
35+
extern _strlen, _strcmp, _strncmp, _stpcpy, _stpncpy, _strlcat, _strchrnul
3536
extern _bzero

0 commit comments

Comments
 (0)