os-k/src/kaleid/common/string.c

101 lines
2.1 KiB
C

//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
// Authors: spectral` //
// NeoX //
// //
// Desc: String-related functions //
//----------------------------------------------------------------------------//
#include <kaleid.h>
//
// Returns str's length
//
size_t strlen(const char *str)
{
const char *base = str;
while (*str++);
return (str - base - 1);
}
//
// Copy the string src into dest
//
char *strcpy(char *dest, const char *src)
{
char *base = dest;
while ((*dest++ = *src++));
return base;
}
//
// strcpy() but always writes n bytes
//
char *strncpy(char *dest, const char *src, size_t n)
{
size_t it;
for (it = 0; it < n && src[it]; it++) {
dest[it] = src[it];
}
while (it < n) dest[it++] = 0;
return dest;
}
//
// strncpy() but safer - always null-terminate,
// returns boolean indicating whether copy was complete
// XXX find a better name
// XXX actually implement this
//
size_t xstrcnpy(char *dest, const char *src, size_t n)
{
size_t it;
for (it = 0; it < n && src[it]; it++) {
dest[it] = src[it];
}
while (it < n) dest[it++] = 0;
return TRUE;
}
//
// Reverses the string src, putting the result into dest
//
char *strrev(char *dest, const char *src)
{
char *orig = dest;
size_t n = strlen(src);
dest[n--] = '\0';
while ((*dest++ = src[n--]));
return orig;
}
//
// Reverses a string, modifying it
// Returns a point to said string
//
char *reverse(char *str)
{
char ch, *orig = str;
size_t n = strlen(str);
char *temp = str + n - 1;
while (temp > str) {
ch = *temp;
*temp-- = *str;
*str++ = ch;
}
return orig;
}