Skip to content

Commit a802ef4

Browse files
ZERICO2005adriweb
authored andcommitted
add strchrnul
1 parent 8c1312a commit a802ef4

File tree

5 files changed

+48
-2
lines changed

5 files changed

+48
-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

src/libcxx/include/cstring

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ using ::strncat;
2929
using ::strlcat;
3030
using ::strchr;
3131
using ::strrchr;
32+
using ::strchrnul;
3233
using ::strpbrk;
3334
using ::strstr;
3435
using ::strcasestr;

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
@@ -796,6 +800,17 @@ int memmem_test(void) {
796800
return 0;
797801
}
798802

803+
int strchrnul_test(void) {
804+
C(T_strchrnul(SINK, '\0') == SINK);
805+
C(T_strchrnul(SINK, '\xff') == SINK);
806+
const size_t test_3_len = strlen(test_3);
807+
C(T_strchrnul(test_3, '\0') == test_3 + test_3_len);
808+
for (size_t i = 0; i < test_3_len; i++) {
809+
C(T_strchrnul(test_3, test_3[i]) == strchr(test_3, test_3[i]));
810+
}
811+
return 0;
812+
}
813+
799814
int run_tests(void) {
800815
int ret = 0;
801816
/* boot_asprintf */
@@ -825,6 +840,7 @@ int run_tests(void) {
825840
TEST(strlcat_test());
826841
TEST(stpncpy_test());
827842
TEST(memmem_test());
843+
TEST(strchrnul_test());
828844

829845
return 0;
830846
}

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)