/*++

Copyright (c) 2024, Quinn Stephens.
Provided under the BSD 3-Clause license.

Module Name:

   str.c

Abstract:

   Provides string manipulation routines.

--*/

#include <string.h>

size_t
strlen (
    const char *str
    )

{
    const char *ptr;

    ptr = str;
    while (*ptr++);

    return ptr - str - sizeof(char);
}

size_t
strnlen (
    const char *str,
    size_t maxlen
    )

{
    size_t len;

    len = 0;
    while (len < maxlen && str[len++]);

    return len - sizeof(char);
}

int
strcmp (
    const char* s1,
    const char* s2
    )

{
    while (*s1 == *s2) {
        if (*s1 == '\0') {
            return 0;
        }

        s1++;
        s2++;
    }

    return *(unsigned char *)s1 - *(unsigned char*)s2;
}

int
strncmp (
    const char* s1,
    const char* s2,
    size_t n
    )

{
    while (n > 0) {
        if (*s1 != *s2) {
            return *(unsigned char *)s1 - *(unsigned char*)s2;
        }

        if (*s1 == '\0') {
            return 0;
        }

        n--;
        s1++;
        s2++;
    }

    return 0;
}

char *
strchr (
    const char *s,
    int c
    )

{
    while (*s != (char)c) {
        if (!*s) {
            return NULL;
        }

        s++;
    }

    return (char *)s;
}

char *
strstr (
    const char *haystack,
    const char *needle
    )

{
    const char *ptr = haystack;

    if (!*needle) {
        return (char *)haystack;
    }

    while ((ptr = strchr(ptr, *needle)) != NULL) {
        if (strcmp(ptr, needle) == 0) {
            return (char *)ptr;
        }
    }

    return NULL;
}