CeDOS - common/string.c

common/string.c (view raw)

#include <stdint.h>

#include "string.h"

void *memcpy (void *destination, const void *source, size_t num) {
    if (destination >= source) {
        for (uint32_t i = 0; i < num; i++) {
            ((uint8_t*)destination)[num - 1 - i] = ((uint8_t*)source)[num - 1 - i];
        }
    } else {
        for (uint32_t i = 0; i < num; i++) {
            ((uint8_t*)destination)[i] = ((uint8_t*)source)[i];
        }
    }
    return destination;
}

char *strcpy(char *destination, const char *source) {
    int i = 0;
    while (source[i]) {
        destination[i] = source[i];
        i++;
    }
    destination[i] = 0;
    return destination;
}

void *memset (void *ptr, int value, size_t num) {
    for (uint32_t i = 0; i < num; i++) {
        ((uint8_t*)ptr)[i] = (uint8_t)value;
    }
    return ptr;
}

unsigned int strlen (const char *str) {
    int i = 0;
    while (str[i]) { i++; }
    return i;
}

int strcmp(const char *str1, const char *str2) {
    int i = 0;

    while (1) {
        if (str1[i] != str2[i]) {
            return str1[i] - str2[i];
        }

        if (str1[i] == 0) {
            return 0;
        }

        i++;
    }
}

const char * strchr ( const char * str, int character ) {
    int i = 0;

    while (1) {
        if (str[i] == 0) {
            return NULL;
        }

        if (str[i] == character) {
            return &(str[i]);
        }

        i++;
    }
}

/**
 * Creates a copy of the string @p src into buffer @p dest with whitespace
 * stripped from the left side. Truncates the string if the buffer is too small.
 *
 * @param src Source string to be copied
 * @param dest Destination buffer to copy the stripped string into
 * @param bufflen Length of buffer dest
 *
 * @return Length of the resulting string
 */
size_t strlstrip(char *dest, const char *src, size_t bufflen) {
	/* skip whitespace at the start */
	while (WHITESPACE(*src)) {
		src++;
	}

	/* copy until string stops */
	size_t i = 0;
	for (; src[i] != 0 && i < bufflen; i++) {
		dest[i] = src[i];
	}

	dest[i] = 0;
	return i;
}

/**
 * Creates a copy of the string @p src into buffer @p dest with whitespace
 * stripped from the right side.
 *
 * @param src Source string to be copied
 * @param dest Destination buffer to copy the stripped string into
 * @param srclen Length of the original string
 *
 * @return Length of the resulting string
 */
size_t strrstrip(char *dest, const char *src, size_t srclen) {
	/* skip whitespace at the end */
	while (srclen > 0 && WHITESPACE(src[srclen - 1])) {
		srclen--;
	}

	/* copy until string stops */
	size_t i = 0;
	for (; src[i] != 0 && i < srclen; i++) {
		dest[i] = src[i];
	}

	dest[i] = 0;
	return i;
}

/**
 * Compares two strings in a case-sensitive manner.
 *
 * @param str1 String to be compared
 * @param str2 String to be compared
 *
 * @return 0 if both strings are equal,
 *   <0 if upper(str1) < upper(str2),
 *   >0 if upper(str1) > upper(str2)
 */
int sstrcmp(const char *str1, const char *str2) {
    int i = 0;

    while (1) {
        if (TO_UPPER(str1[i]) != TO_UPPER(str2[i])) {
            return TO_UPPER(str1[i]) - TO_UPPER(str2[i]);
        }

        if (str1[i] == 0) {
            return 0;
        }

        i++;
    }
}