2021-09-14 15:48:57 +00:00
|
|
|
/*
|
|
|
|
* str_ops.c
|
|
|
|
*
|
|
|
|
* Created on: Aug 27, 2011
|
|
|
|
* Author: Tiberiu
|
|
|
|
*/
|
|
|
|
#include <types.h>
|
2021-09-14 15:51:43 +00:00
|
|
|
#include <ctype.h>
|
2021-09-14 15:48:57 +00:00
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2021-09-14 15:52:47 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2021-09-14 15:51:43 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2021-09-14 15:48:57 +00:00
|
|
|
string strcpy (string s1, const string s2)
|
|
|
|
{
|
|
|
|
char *dst = s1;
|
|
|
|
const char *src = s2;
|
|
|
|
|
|
|
|
while ((*dst++ = *src++) != '\0') ;
|
|
|
|
|
|
|
|
return s1;
|
|
|
|
}
|
2021-09-14 15:50:50 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|