luxos/Kernel/library/stdlib/str_ops.c

86 lines
1.4 KiB
C
Raw Permalink Normal View History

/*
* str_ops.c
*
* Created on: Aug 27, 2011
* Author: Tiberiu
*/
#include <types.h>
#include <ctype.h>
uint32 strlen (string s)
{
string end = s;
while (*end != '\0')
end++;
return (uint32)(end - s);
}
int32 strcmp (string a, string b)
{
unsigned char c1, c2;
while (*a != '\0' && *b != '\0' && *a == *b) {
a++; b++;
}
c1 = (*(unsigned char*) a);
c2 = (*(unsigned char*) b);
return ((c1 < c2) ? -1 : (c1 > c2));
}
int32 strncmp (string a, string b, uint32 n)
{
unsigned char uc1, uc2;
if (!n) return 0;
while (n-- > 0 && *a == *b) {
if (n == 0 || (*a == *b && *a == '\0')) return 0;
a++; b++;
}
uc1 = (* (unsigned char*)a);
uc2 = (* (unsigned char*)b);
return ((uc1 < uc2) ? -1 : (uc1 > uc2));
}
int32 strcasecmp (string a, string b)
{
unsigned char c1, c2;
while (*a != '\0' && *b != '\0' && tolower(*a) == tolower(*b)) {
a++; b++;
}
c1 = tolower(*(unsigned char*) a);
c2 = tolower(*(unsigned char*) b);
return ((c1 < c2) ? -1 : (c1 > c2));
}
string strcpy (string s1, const string s2)
{
char *dst = s1;
const char *src = s2;
while ((*dst++ = *src++) != '\0') ;
return s1;
}
char* strchr (string s, int c)
{
while (*s != '\0' && *s != (char)c) s++;
return ((*s == (char)c) ? (char*)s : NULL);
}
char* strrchr (string s, int c)
{
string last = NULL;
if (c == '\0') return strchr(s, c);
while ((s = strchr(s, c)) != NULL)
last = s; s++;
return (char*)last;
}