string: add strsplit
This commit is contained in:
parent
03eb0b0048
commit
14a681c53e
5 changed files with 45 additions and 3 deletions
2
Makefile
2
Makefile
|
|
@ -16,7 +16,7 @@ define DEFAULT_VAR =
|
||||||
endef
|
endef
|
||||||
|
|
||||||
# User controllable CFLAGS.
|
# User controllable CFLAGS.
|
||||||
CFLAGS ?= -g -O0 -pipe -Wall -Wextra
|
CFLAGS ?= -g -O2 -pipe -Wall -Wextra
|
||||||
|
|
||||||
# User controllable linker flags. We set none by default.
|
# User controllable linker flags. We set none by default.
|
||||||
LDFLAGS ?=
|
LDFLAGS ?=
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,6 @@
|
||||||
#define LOG_INFO "[LOG] "
|
#define LOG_INFO "[LOG] "
|
||||||
|
|
||||||
int printf(const char *restrict fmt, ...);
|
int printf(const char *restrict fmt, ...);
|
||||||
int snprintf(char *str, char *restrict fmt, size_t size, ...);
|
int snprintf(char *str, const char *restrict fmt, size_t size, ...);
|
||||||
|
|
||||||
#endif /* _SDTIO_H_ */
|
#endif /* _SDTIO_H_ */
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ void *memcpy(void *dest, const void *src, size_t num);
|
||||||
int memcmp(const void *aa, const void *bb, size_t num);
|
int memcmp(const void *aa, const void *bb, size_t num);
|
||||||
void *memset(void *str, int c, size_t n);
|
void *memset(void *str, int c, size_t n);
|
||||||
char *strcpy(char *dest, const char *src);
|
char *strcpy(char *dest, const char *src);
|
||||||
|
char **strsplit(const char *str, char delim, int *num);
|
||||||
size_t strlen(const char *c);
|
size_t strlen(const char *c);
|
||||||
int atoi(const char *a);
|
int atoi(const char *a);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ static int printf_write(const char *restrict s)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* this is seriously the approach i'm going with for now */
|
/* this is seriously the approach i'm going with for now */
|
||||||
int snprintf(char *str, char *restrict fmt, size_t size, ...)
|
int snprintf(char *str, const char *restrict fmt, size_t size, ...)
|
||||||
{
|
{
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
char c;
|
char c;
|
||||||
|
|
|
||||||
41
lib/string.c
41
lib/string.c
|
|
@ -1,5 +1,6 @@
|
||||||
/* SPDX-License-Identifier: GPL-2.0-only */
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <kernel/slab.h>
|
||||||
|
|
||||||
void *memcpy(void *dest, const void *src, size_t num)
|
void *memcpy(void *dest, const void *src, size_t num)
|
||||||
{
|
{
|
||||||
|
|
@ -74,3 +75,43 @@ int atoi(const char *a)
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char *strchr(const char *s, int c)
|
||||||
|
{
|
||||||
|
while (*s) {
|
||||||
|
if (*s == c)
|
||||||
|
return (char *)s;
|
||||||
|
s++;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
char **strsplit(const char *s, char delim, int *n)
|
||||||
|
{
|
||||||
|
int num = 0;
|
||||||
|
for (int i = 0; s[i]; i++) {
|
||||||
|
if (s[i] == delim)
|
||||||
|
num++;
|
||||||
|
}
|
||||||
|
|
||||||
|
char **ret = kmalloc((num + 2) * sizeof(char *), ALLOC_KERN);
|
||||||
|
int idx = 0;
|
||||||
|
int last = 0;
|
||||||
|
for (int i = 0; s[i]; i++) {
|
||||||
|
if (s[i] == delim) {
|
||||||
|
ret[idx] = kmalloc(i - last + 1, ALLOC_KERN);
|
||||||
|
memcpy(ret[idx], s + last, i - last);
|
||||||
|
ret[idx][i - last] = 0;
|
||||||
|
idx++;
|
||||||
|
last = i + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ret[idx] = kmalloc(strlen(s) - last + 1, ALLOC_KERN);
|
||||||
|
memcpy(ret[idx], s + last, strlen(s) - last);
|
||||||
|
ret[idx][strlen(s) - last] = 0;
|
||||||
|
idx++;
|
||||||
|
ret[idx] = NULL;
|
||||||
|
if (n)
|
||||||
|
*n = idx;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue