#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>

size_t memset (void* dst, uint8_t b, size_t n) {
  uint8_t* dst1 = dst;
  size_t i;
  for (i = 0; i < n; i++)
    dst1[i] = b;
  return i;
}

size_t memcpy (void* dst, const void* src, size_t n) {
  uint8_t* dst1 = dst;
  const uint8_t* src1 = src;
  size_t i;
  for (i = 0; i < n; i++)
    dst1[i] = src1[i];
  return i;
}

// SOURCE: https://stackoverflow.com/a/48967408
void strncpy (char* dst, const char* src, size_t n) {
  size_t i = 0;
  while (i++ != n && (*dst++ = *src++))
    ;
}

size_t strlen (const char* str) {
  const char* s;
  for (s = str; *s; ++s)
    ;
  return (s - str);
}

int memcmp (const void* s1, const void* s2, size_t n) {
  unsigned char* p = (unsigned char*)s1;
  unsigned char* q = (unsigned char*)s2;

  while (n--) {
    if (*p != *q) {
      return (int)*p - (int)*q;
    }
    p++, q++;
  }
  return 0;
}

void strtokenize (const char* str, char delim, void* ctx, strtokenize_cb_func_t cb) {
  const char* start = str;

  while (*start) {
    while (*start && *start == delim)
      start++;

    if (!*start)
      break;

    const char* end = start;
    while (*end && *end != delim)
      end++;

    if (!cb (ctx, start, (size_t)(end - start)))
      break;

    start = end;
  }
}

void str_split_lines (const char* str, size_t total_len, void* ctx, strtokenize_cb_func_t cb) {
  if (str == NULL)
    return;

  const char* start = str;
  const char* end;
  size_t remaining = total_len;

  while (remaining > 0) {
    end = memchr (start, '\n', remaining);

    if (end != NULL) {
      size_t line_len = (size_t)(end - start);

      if (!cb (ctx, start, line_len))
        return;

      start = end + 1;
      remaining = total_len - (size_t)(start - str);

      if (remaining == 0)
        cb (ctx, start, 0);
    } else {
      cb (ctx, start, remaining);
      break;
    }
  }
}

/* https://svnweb.freebsd.org/base/stable/7/lib/libc/string/memchr.c?view=markup */
void* memchr (const void* s, unsigned char c, size_t n) {
  if (n != 0) {
    const unsigned char* p = s;
    do {
      if (*p++ == c)
        return ((void*)(p - 1));
    } while (--n != 0);
  }

  return NULL;
}

/* https://stackoverflow.com/a/34873406 */
int strcmp (const char* s1, const char* s2) {
  while (*s1 && (*s1 == *s2)) {
    s1++;
    s2++;
  }
  return *(const unsigned char*)s1 - *(const unsigned char*)s2;
}

/* https://stackoverflow.com/a/2490637 */
char* strcat (char* dest, const char* src) {
  char* rdest = dest;

  while (*dest)
    dest++;

  while ((*dest++ = *src++))
    ;

  return rdest;
}

int strncmp (const char* s1, const char* s2, size_t n) {
  while (n && *s1 && (*s1 == *s2)) {
    ++s1;
    ++s2;
    --n;
  }
  if (n == 0) {
    return 0;
  } else {
    return (*(unsigned char*)s1 - *(unsigned char*)s2);
  }
}

/* https://stackoverflow.com/questions/49131175/recreate-the-strstr-function */
char* strstr (const char* str, const char* substring) {
  const char* a;
  const char* b;

  b = substring;

  if (*b == 0) {
    return (char*)str;
  }

  for (; *str != 0; str += 1) {
    if (*str != *b) {
      continue;
    }

    a = str;
    while (1) {
      if (*b == 0) {
        return (char*)str;
      }
      if (*a++ != *b++) {
        break;
      }
    }
    b = substring;
  }

  return NULL;
}

/* https://github.com/gcc-mirror/gcc/blob/master/libiberty/strchr.c */
char* strchr (register const char* s, int c) {
  do {
    if (*s == c) {
      return (char*)s;
    }
  } while (*s++);
  return (0);
}

/* SOURCE: https://aticleworld.com/memmove-function-implementation-in-c/ */
void* memmove (void* dest, const void* src, unsigned int n) {
  char* pcSource = (char*)src;
  char* pcDstn = (char*)dest;
  // return if pcDstn and pcSource is NULL
  if ((pcSource == NULL) || (pcDstn == NULL)) {
    return NULL;
  }
  // overlap buffer
  if ((pcSource < pcDstn) && (pcDstn < pcSource + n)) {
    for (pcDstn += n, pcSource += n; n--;) {
      *--pcDstn = *--pcSource;
    }
  } else {
    while (n--) {
      *pcDstn++ = *pcSource++;
    }
  }
  return dest;
}

char* strncat (char* dest, const char* src, size_t n) {
  char* ptr = dest;
  while (*ptr != '\0')
    ptr++;

  while (n > 0 && *src != '\0') {
    *ptr++ = *src++;
    n--;
  }

  *ptr = '\0';
  return dest;
}

/* https://stackoverflow.com/questions/14476627/strcpy-implementation-in-c */
char* strcpy (char* strDest, const char* strSrc) {
  char* temp = strDest;
  while ((*strDest++ = *strSrc++))
    ;
  return temp;
}

int isalnum (int c) { return isalpha (c) || isdigit (c); }

int isalpha (int c) { return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'); }

int iscntrl (int c) { return (c >= 0 && c <= 32) || (c == 127); }

int isdigit (int c) { return (c >= '0' && c <= '9'); }

int isgraph (int c) { return (c > 32 && c <= 126); }

int islower (int c) { return (c >= 'A' && c <= 'z'); }

int isprint (int c) { return (c >= 32 && c <= 126); }

int ispunct (int c) { return isgraph (c) && !isalnum (c); }

int isspace (int c) {
  return (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v');
}

int isupper (int c) { return (c >= 'A' && c <= 'Z'); }

int isxdigit (int c) { return isdigit (c) || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'); }

int isascii (int c) { return (c >= 0 && c <= 127); }

int isblank (int c) { return (c == ' ' || c == '\t'); }

int tolower (int chr) { return (chr >= 'A' && chr <= 'Z') ? (chr + 32) : (chr); }

int toupper (int chr) { return (chr >= 'a' && chr <= 'z') ? (chr - 32) : (chr); }
