Skip to content

Commit aaf199d

Browse files
committed
Added mempcpy and prototypes for additional <string.h> functions
1 parent a9056ac commit aaf199d

File tree

5 files changed

+101
-26
lines changed

5 files changed

+101
-26
lines changed

src/libc/include/string.h

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,41 @@ int memcmp(const void *s1, const void *s2, size_t n)
2020
void *memchr(const void *s, int c, size_t n)
2121
__attribute__((nonnull(1)));
2222

23-
void* memccpy(void *__restrict dest, const void *__restrict src, int c, size_t n) __NOEXCEPT
24-
__attribute__((nonnull(1, 2)));
23+
void *memrchr(const void *s, int c, size_t n)
24+
__NOEXCEPT __attribute__((nonnull(1))) __attribute((__pure__));
25+
26+
void *memmem(const void *haystack, size_t haystack_len, const void *needle, size_t needle_len)
27+
__NOEXCEPT __attribute__((nonnull(1, 3))) __attribute((__pure__));
28+
29+
void *memccpy(void *__restrict dest, const void *__restrict src, int c, size_t n)
30+
__NOEXCEPT __attribute__((nonnull(1, 2)));
31+
32+
void *mempcpy(void *__restrict dest, const void *__restrict src, size_t n)
33+
__NOEXCEPT __attribute__((nonnull(1, 2)));
2534

2635
char *strcpy(char *__restrict dest, const char *__restrict src)
2736
__attribute__((nonnull(1, 2)));
2837

2938
char *strncpy(char *__restrict dest, const char *__restrict src, size_t n)
3039
__attribute__((nonnull(1, 2)));
3140

32-
char *stpcpy(char *__restrict dest, const char *__restrict src) __NOEXCEPT
33-
__attribute__((nonnull(1, 2)));
41+
char *stpcpy(char *__restrict dest, const char *__restrict src)
42+
__NOEXCEPT __attribute__((nonnull(1, 2)));
3443

35-
char *stpncpy(char *__restrict dest, const char *__restrict src, size_t n) __NOEXCEPT
36-
__attribute__((nonnull(1, 2)));
44+
char *stpncpy(char *__restrict dest, const char *__restrict src, size_t n)
45+
__NOEXCEPT __attribute__((nonnull(1, 2)));
3746

38-
char *strlcpy(char *__restrict dest, const char *__restrict src, size_t n) __NOEXCEPT
39-
__attribute__((nonnull(1, 2)));
47+
char *strlcpy(char *__restrict dest, const char *__restrict src, size_t n)
48+
__NOEXCEPT __attribute__((nonnull(1, 2)));
4049

4150
char *strcat(char *__restrict dest, const char *__restrict src)
4251
__attribute__((nonnull(1, 2)));
4352

4453
char *strncat(char *__restrict dest, const char *__restrict src, size_t n)
4554
__attribute__((nonnull(1, 2)));
4655

47-
char *strlcat(char *__restrict dest, const char *__restrict src, size_t n) __NOEXCEPT
48-
__attribute__((nonnull(1, 2)));
56+
char *strlcat(char *__restrict dest, const char *__restrict src, size_t n)
57+
__NOEXCEPT __attribute__((nonnull(1, 2)));
4958

5059
char *strchr(const char *s, int c)
5160
__attribute__((nonnull(1)));
@@ -59,6 +68,9 @@ char *strpbrk(const char *s, const char *accept)
5968
char *strstr(const char *haystack, const char *needle)
6069
__attribute__((nonnull(1, 2)));
6170

71+
char *strcasestr(const char *haystack, const char *needle)
72+
__NOEXCEPT __attribute__((nonnull(1, 2))) __attribute__((__pure__));
73+
6274
char *strtok(char *__restrict s, const char *__restrict delim)
6375
__attribute__((nonnull(2)));
6476

@@ -86,11 +98,11 @@ int strcmp(const char *s1, const char *s2)
8698
int strncmp(const char *s1, const char *s2, size_t n)
8799
__attribute__((nonnull(1, 2)));
88100

89-
int strcasecmp(const char *s1, const char *s2) __NOEXCEPT
90-
__attribute__((nonnull(1, 2))) __attribute__((__pure__));
101+
int strcasecmp(const char *s1, const char *s2)
102+
__NOEXCEPT __attribute__((nonnull(1, 2))) __attribute__((__pure__));
91103

92-
int strncasecmp(const char *s1, const char *s2, size_t n) __NOEXCEPT
93-
__attribute__((nonnull(1, 2))) __attribute__((__pure__));
104+
int strncasecmp(const char *s1, const char *s2, size_t n)
105+
__NOEXCEPT __attribute__((nonnull(1, 2))) __attribute__((__pure__));
94106

95107
char* strerror(int errnum);
96108

src/libc/mempcpy.src

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
assume adl=1
2+
3+
section .text
4+
5+
public _mempcpy
6+
7+
_mempcpy:
8+
ld iy, 0
9+
add iy, sp
10+
ld bc, (iy + 9) ; Load count
11+
sbc hl, hl
12+
sbc hl, bc
13+
ld hl, (iy + 3) ; Load destination
14+
ret z ; zero bytes to copy
15+
ld de, (iy + 6) ; Load source
16+
ex de, hl
17+
ldir
18+
ret

src/libcxx/include/cstring

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ using ::memmove;
1414
using ::memset;
1515
using ::memcmp;
1616
using ::memchr;
17+
using ::memrchr;
18+
using ::memmem;
1719
using ::memccpy;
20+
using ::mempcpy;
1821
using ::strcpy;
1922
using ::strncpy;
2023
using ::stpcpy;
@@ -27,6 +30,7 @@ using ::strchr;
2730
using ::strrchr;
2831
using ::strpbrk;
2932
using ::strstr;
33+
using ::strcasestr;
3034
using ::strtok;
3135
using ::strdup;
3236
using ::strndup;

test/standalone/asprintf_fprintf/makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ DESCRIPTION = "CE C Toolchain Demo"
88
COMPRESSED = NO
99
ARCHIVED = NO
1010

11-
CFLAGS = -Wall -Wextra -Oz
12-
CXXFLAGS = -Wall -Wextra -Oz
11+
CFLAGS = -Wall -Wextra -Wformat=2 -Wshadow -Oz
12+
CXXFLAGS = -Wall -Wextra -Wformat=2 -Wshadow -Oz
1313

1414
PREFER_OS_LIBC = NO
1515

test/standalone/asprintf_fprintf/src/main.c

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,22 +130,28 @@ static char const * const fprintf_test =
130130

131131
static char const * const file_name = "FPRINTST";
132132

133-
static void get_diff_char(const char* buf, const char* test) {
133+
static void get_diff_char(const char* data, const char* test) {
134134
size_t char_pos = 0;
135135
char prev = '\0';
136-
while (*buf != '\0' && *test != '\0') {
137-
if (*buf != *test) {
138-
printf("prev %02X: %02X != %02X at %zu\n", prev, *buf, *test, char_pos);
139-
break;
136+
while (*data != '\0' && *test != '\0') {
137+
if (*data != *test) {
138+
printf("prev %02X: %02X != %02X at %zu\n", prev, *data, *test, char_pos);
139+
return;
140140
}
141-
prev = *buf;
142-
buf++;
141+
prev = *data;
142+
data++;
143143
test++;
144144
char_pos++;
145145
}
146146
}
147147

148148
int memccpy_tests(void) {
149+
// test zero byte case
150+
void* ptr = memccpy((void*)0xC0FFEE, (void*)0x123456, 123, 0);
151+
if (ptr != NULL) {
152+
printf("%p != NULL\n", ptr);
153+
return __LINE__;
154+
}
149155
file = fopen(file_name, "wb");
150156

151157
// Check if the file was opened successfully
@@ -209,8 +215,7 @@ int memccpy_tests(void) {
209215
return __LINE__;
210216
}
211217

212-
int alloc_size = (int)ftell(file);
213-
buf = (char*)calloc(alloc_size + 2, sizeof(char));
218+
buf = (char*)calloc(file_size + 1, sizeof(char));
214219
if (buf == NULL) {
215220
perror("calloc failure");
216221
return __LINE__;
@@ -235,6 +240,38 @@ int memccpy_tests(void) {
235240
return 0;
236241
}
237242

243+
int mempcpy_test(void) {
244+
// test zero byte case
245+
void* ptr = mempcpy((void*)0xC0FFEE, (void*)0x123456, 0);
246+
if (ptr != (void*)0xC0FFEE) {
247+
printf("%p != %p\n", ptr, (void*)0xC0FFEE);
248+
return __LINE__;
249+
}
250+
char data[192 + 1];
251+
memset(&data[ 0], 0x12, 64);
252+
memset(&data[ 64], 0x12, 64);
253+
memset(&data[128], 0x56, 64);
254+
char append[64 + 1];
255+
memset(append, 0x34, 64);
256+
char* res = mempcpy(&data[64], append, 64);
257+
if (res != &data[128]) {
258+
printf("%p != %p\n", res, data);
259+
return __LINE__;
260+
}
261+
262+
char truth[192 + 1];
263+
memset(&truth[ 0], 0x12, 64);
264+
memset(&truth[ 64], 0x34, 64);
265+
memset(&truth[128], 0x56, 64);
266+
267+
int cmp = memcmp(data, truth, 192);
268+
if (cmp != 0) {
269+
printf("cmp: %d\n", cmp);
270+
return __LINE__;
271+
}
272+
return 0;
273+
}
274+
238275
int run_tests(void) {
239276
int ret = 0;
240277
/* ti_asprintf */
@@ -247,7 +284,7 @@ int run_tests(void) {
247284
free(buf); buf = NULL;
248285
if (ret != 0) { return ret; }
249286

250-
/* nano_fprintf */
287+
/* nano_fprintf memccpy */
251288
ret = memccpy_tests();
252289
free(buf); buf = NULL;
253290
fclose(file);
@@ -256,6 +293,10 @@ int run_tests(void) {
256293
}
257294
if (ret != 0) { return ret; }
258295

296+
/* mempcpy */
297+
ret = mempcpy_test();
298+
if (ret != 0) { return ret; }
299+
259300
return 0;
260301
}
261302

0 commit comments

Comments
 (0)