Browse Source

fixed string.h related const convertions

master
Mathieu Serandour 6 months ago
parent
commit
31ef1f160f
  1. 35
      kernel/lib/string.c
  2. 16
      kernel/lib/string.h

35
kernel/lib/string.c

@ -1,5 +1,5 @@
#include "string.h"
#include "../memory/heap.h"
size_t strlen(const char* str) {
const char* ptr = str;
@ -89,7 +89,7 @@ int strncmp(const char* str1, const char* str2, size_t n) {
}
const char* strchr (const char *_s, int c) {
char* strchr (const char *_s, int c) {
const char* s=_s;
char curr;
@ -97,7 +97,7 @@ const char* strchr (const char *_s, int c) {
curr = *s;
if(curr == c)
return s;
return (const char *)s;
else if(curr == '\0')
break;
@ -108,7 +108,7 @@ const char* strchr (const char *_s, int c) {
}
const char* strrchr(const char *s, int c) {
char* strrchr(const char *s, int c) {
char curr;
const char* found = NULL;
@ -124,11 +124,11 @@ const char* strrchr(const char *s, int c) {
s++;
}
return found;
return (const char*)found;
}
const char *strstr(const char *haystack, const char *needle) {
char *strstr(const char *haystack, const char *needle) {
int i = 0;
char c, ci;
@ -138,7 +138,7 @@ const char *strstr(const char *haystack, const char *needle) {
ci = needle[i];
if(ci == '\0')
return haystack - i - 1;
return (char *)(haystack - i - 1);
c = *(haystack++);
@ -333,6 +333,27 @@ char* strtok(char* restrict str, const char* restrict delim) {
return strtok_r(str, delim, &saveptr);
}
char *strdup(const char *s) {
size_t len = strlen(s);
char* ret = malloc(len+1);
if(!ret)
return NULL;
memcpy(ret, s, len+1);
return ret;
}
char *strndup(const char *s, size_t n) {
size_t len = strnlen(s, n);
char* ret = malloc(len+1);
if(!ret)
return NULL;
memcpy(ret, s, len+1);
return ret;
}
int memsum(const void* ptr, size_t size) {
uint8_t sum = 0;

16
kernel/lib/string.h

@ -20,14 +20,22 @@ void * memmove (void *, const void *, size_t);
//void * memrchr (const void *, int, size_t) __ATTR_PURE__;
char *strdup(const char *s);
char *strndup(const char *s, size_t n);
size_t strlen(const char* str) __ATTR_PURE__;
size_t strlen (const char* str) __ATTR_PURE__;
size_t strnlen(const char* str, size_t n) __ATTR_PURE__;
int strcmp (const char* str1, const char* str2) __ATTR_PURE__;
int strncmp(const char* str1, const char* str2, size_t n) __ATTR_PURE__;
const char* strchr (const char *s, int c) __ATTR_PURE__;
const char* strrchr(const char *s, int c) __ATTR_PURE__;
const char* strstr(const char *haystack, const char *needle) __ATTR_PURE__;
char* strchr (const char *s, int c) __ATTR_PURE__;
char* strrchr(const char *s, int c) __ATTR_PURE__;
char* strstr (const char *haystack, const char *needle) __ATTR_PURE__;
char* strcpy (char* __restrict__ dst, const char* __restrict__ src);

Loading…
Cancel
Save