game/axl_string.c

141 lines
2.1 KiB
C

#include "axl_string.h"
u32 axl_strlen(const i8* s)
{
if(s == NULL)
{
return 0;
}
u32 len = 0;
while(s[len] != '\0')
{
len++;
}
return len;
}
i8* axl_strcpy(i8* dst, const i8* src)
{
if(dst == NULL || src == NULL)
{
return dst;
}
i8* start = dst;
while((*(dst++) = *(src++)) != '\0');
return start;
}
i8* axl_strncpy(i8* dst, const i8* src, u32 n)
{
if(dst == NULL || src == NULL)
{
return dst;
}
u32 i = 0;
for(; i < n && src[i] != '\0'; i++)
{
dst[i] = src[i];
}
for(; i < n; i++)
{
dst[i] = '\0';
}
return dst;
}
i8* axl_strcat(i8* dst, const i8* src)
{
if(dst == NULL || src == NULL)
{
return dst;
}
axl_strcpy(dst + axl_strlen(dst), src);
return dst;
}
i8* axl_strncat(i8* dst, const i8* src, u32 n) //n actually means "not more than"
{
if(!dst || !src)
{
return dst;
}
i8* p = dst + axl_strlen(dst);
while (n-- && (*p++ = *src++)); //quick note: '\0' == 0
*p = '\0'; //in case we copied exactly n without '\0'
return dst;
}
i32 axl_strcmp(const i8* s1, const i8* s2)
{
if(!s1 || !s2)
{
return (s1 == s2) ? 0 : (!s1 ? -1 : 1);
}
for(;; s1++, s2++)
{
u8 c1 = *(const u8*)s1;
u8 c2 = *(const u8*)s2;
if (c1 != c2 || c1 == '\0' || c2 == '\0')
{
return c1 - c2;
}
}
return 0;
}
i32 axl_strncmp(const i8* s1, const i8* s2, u32 n)
{
if(!s1 || !s2)
{
return (s1 == s2) ? 0 : (!s1 ? -1 : 1);
}
for(u32 i = 0; i < n; s1++, s2++, i++)
{
u8 c1 = *(const u8*)s1;
u8 c2 = *(const u8*)s2;
if(c1 != c2 || c1 == '\0' || c2 == '\0')
{
return c1 - c2;
}
}
return 0;
}
const i8* axl_strchr(const i8* str, i8 c)
{
if(!str)
{
return NULL;
}
for(; *str != '\0'; ++str)
{
if(*str == c)
{
return str;
}
}
return (c == '\0') ? str : NULL;
}