kit

kit
git clone https://git.ryansepassi.com/git/kit.git
Log | Files | Refs | README

string.c (2725B)


      1 //===-- string.c - kit freestanding string primitives -------------------===//
      2 //
      3 // SPDX-License-Identifier: 0BSD
      4 //===----------------------------------------------------------------------===//
      5 
      6 #include <stddef.h>
      7 
      8 __attribute__((weak)) void* memchr(const void* s, int c, size_t n) {
      9   const unsigned char* p = (const unsigned char*)s;
     10   unsigned char v = (unsigned char)c;
     11   for (size_t i = 0; i < n; i++) {
     12     if (p[i] == v) return (void*)(p + i);
     13   }
     14   return NULL;
     15 }
     16 
     17 __attribute__((weak)) size_t strlen(const char* s) {
     18   size_t n = 0;
     19   while (s[n] != '\0') n++;
     20   return n;
     21 }
     22 
     23 __attribute__((weak)) size_t strnlen(const char* s, size_t maxlen) {
     24   size_t n = 0;
     25   while (n < maxlen && s[n] != '\0') n++;
     26   return n;
     27 }
     28 
     29 __attribute__((weak)) char* strcpy(char* dest, const char* src) {
     30   char* d = dest;
     31   while ((*d++ = *src++) != '\0') {
     32   }
     33   return dest;
     34 }
     35 
     36 __attribute__((weak)) char* strncpy(char* dest, const char* src, size_t n) {
     37   size_t i = 0;
     38   for (; i < n && src[i] != '\0'; i++) dest[i] = src[i];
     39   for (; i < n; i++) dest[i] = '\0';
     40   return dest;
     41 }
     42 
     43 __attribute__((weak)) char* strcat(char* dest, const char* src) {
     44   strcpy(dest + strlen(dest), src);
     45   return dest;
     46 }
     47 
     48 __attribute__((weak)) char* strncat(char* dest, const char* src, size_t n) {
     49   char* d = dest + strlen(dest);
     50   size_t i = 0;
     51   while (i < n && src[i] != '\0') {
     52     d[i] = src[i];
     53     i++;
     54   }
     55   d[i] = '\0';
     56   return dest;
     57 }
     58 
     59 __attribute__((weak)) int strcmp(const char* s1, const char* s2) {
     60   const unsigned char* a = (const unsigned char*)s1;
     61   const unsigned char* b = (const unsigned char*)s2;
     62   while (*a != '\0' && *a == *b) {
     63     a++;
     64     b++;
     65   }
     66   return (int)*a - (int)*b;
     67 }
     68 
     69 __attribute__((weak)) int strncmp(const char* s1, const char* s2, size_t n) {
     70   const unsigned char* a = (const unsigned char*)s1;
     71   const unsigned char* b = (const unsigned char*)s2;
     72   for (size_t i = 0; i < n; i++) {
     73     if (a[i] != b[i] || a[i] == '\0') return (int)a[i] - (int)b[i];
     74   }
     75   return 0;
     76 }
     77 
     78 __attribute__((weak)) char* strchr(const char* s, int c) {
     79   char v = (char)c;
     80   for (;;) {
     81     if (*s == v) return (char*)s;
     82     if (*s == '\0') return NULL;
     83     s++;
     84   }
     85 }
     86 
     87 __attribute__((weak)) char* strrchr(const char* s, int c) {
     88   char v = (char)c;
     89   const char* last = NULL;
     90   for (;;) {
     91     if (*s == v) last = s;
     92     if (*s == '\0') return (char*)last;
     93     s++;
     94   }
     95 }
     96 
     97 __attribute__((weak)) char* strstr(const char* haystack, const char* needle) {
     98   if (*needle == '\0') return (char*)haystack;
     99   for (; *haystack != '\0'; haystack++) {
    100     const char* h = haystack;
    101     const char* n = needle;
    102     while (*n != '\0' && *h == *n) {
    103       h++;
    104       n++;
    105     }
    106     if (*n == '\0') return (char*)haystack;
    107   }
    108   return NULL;
    109 }