game/axl_string.c

124 lines
1.9 KiB
C
Raw Normal View History

2025-11-27 20:51:48 +03:00
#include "axl_string.h"
u32 axl_strlen(const i8* s)
{
2025-11-27 21:14:48 +03:00
if(s == NULL)
{
return 0;
}
2025-11-30 17:32:50 +03:00
2025-11-27 20:51:48 +03:00
u32 len = 0;
while(s[len] != '\0')
{
len++;
}
return len;
}
2025-11-27 20:58:48 +03:00
i8* axl_strcpy(i8* dst, const i8* src)
{
2025-11-27 21:14:48 +03:00
if(dst == NULL || src == NULL)
{
return dst;
}
2025-11-27 20:58:48 +03:00
i8* start = dst;
while((*(dst++) = *(src++)) != '\0');
return start;
}
2025-11-27 21:10:44 +03:00
i8* axl_strncpy(i8* dst, const i8* src, u32 n)
{
2025-11-27 21:14:48 +03:00
if(dst == NULL || src == NULL)
{
return dst;
}
2025-11-27 21:10:44 +03:00
u32 i = 0;
for(; i < n && src[i] != '\0'; i++)
{
dst[i] = src[i];
}
for(; i < n; i++)
{
dst[i] = '\0';
}
return dst;
}
2025-11-30 17:18:38 +03:00
i8* axl_strcat(i8* dst, const i8* src)
{
if(dst == NULL || src == NULL)
{
return dst;
}
2025-12-01 20:16:07 +03:00
axl_strcpy(dst + axl_strlen(dst), src);
return dst;
2025-11-30 17:18:38 +03:00
}
2025-11-30 17:32:50 +03:00
i8* axl_strncat(i8* dst, const i8* src, u32 n) //n actually means "not more than"
{
if(!dst || !src)
2025-11-30 17:32:50 +03:00
{
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;
}
2025-11-30 18:19:14 +03:00
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;
}
2025-11-30 18:35:17 +03:00
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++)
{
u8 c1 = *(const u8*)s1;
u8 c2 = *(const u8*)s2;
if (c1 != c2 || c1 == '\0' || c2 == '\0')
{
return c1 - c2;
}
}
return 0;
}