Better axl_strchr + handle finding '\0'
This commit is contained in:
parent
0d7cfcc8d6
commit
eece39c2ed
1 changed files with 11 additions and 14 deletions
25
axl_string.c
25
axl_string.c
|
|
@ -82,12 +82,12 @@ i8* axl_strncat(i8* dst, const i8* src, u32 n) //n actually means "not more than
|
|||
|
||||
i32 axl_strcmp(const i8* s1, const i8* s2)
|
||||
{
|
||||
if (!s1 || !s2)
|
||||
if(!s1 || !s2)
|
||||
{
|
||||
return (s1 == s2) ? 0 : (!s1 ? -1 : 1);
|
||||
}
|
||||
|
||||
for (;; s1++, s2++)
|
||||
for(;; s1++, s2++)
|
||||
{
|
||||
u8 c1 = *(const u8*)s1;
|
||||
u8 c2 = *(const u8*)s2;
|
||||
|
|
@ -103,17 +103,17 @@ i32 axl_strcmp(const i8* s1, const i8* s2)
|
|||
|
||||
i32 axl_strncmp(const i8* s1, const i8* s2, u32 n)
|
||||
{
|
||||
if (!s1 || !s2)
|
||||
if(!s1 || !s2)
|
||||
{
|
||||
return (s1 == s2) ? 0 : (!s1 ? -1 : 1);
|
||||
}
|
||||
|
||||
for (u32 i = 0; i < n; s1++, s2++, i++)
|
||||
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')
|
||||
if(c1 != c2 || c1 == '\0' || c2 == '\0')
|
||||
{
|
||||
return c1 - c2;
|
||||
}
|
||||
|
|
@ -124,21 +124,18 @@ i32 axl_strncmp(const i8* s1, const i8* s2, u32 n)
|
|||
|
||||
const i8* axl_strchr(const i8* str, i8 c)
|
||||
{
|
||||
if(!str)
|
||||
if(!str)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
u32 i = 0;
|
||||
|
||||
while(str[i] != '\0')
|
||||
|
||||
for(; *str != '\0'; ++str)
|
||||
{
|
||||
if(str[i] == c)
|
||||
if(*str == c)
|
||||
{
|
||||
return str + i;
|
||||
return str;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
return (c == '\0') ? str : NULL;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue