[SDK:CRT] Implement more string routines
This commit is contained in:
parent
120277161c
commit
b57dcc8078
@ -43,3 +43,87 @@ strnlen (
|
||||
|
||||
return len - sizeof(char);
|
||||
}
|
||||
|
||||
int
|
||||
strcmp (
|
||||
const char* s1,
|
||||
const char* s2
|
||||
)
|
||||
|
||||
{
|
||||
while (*s1 == *s2) {
|
||||
if (*s1 == '\0') {
|
||||
return 0;
|
||||
}
|
||||
|
||||
s1++;
|
||||
s2++;
|
||||
}
|
||||
|
||||
return *(unsigned char *)s1 - *(unsigned char*)s2;
|
||||
}
|
||||
|
||||
int
|
||||
strncmp (
|
||||
const char* s1,
|
||||
const char* s2,
|
||||
size_t n
|
||||
)
|
||||
|
||||
{
|
||||
while (n > 0) {
|
||||
if (*s1 != *s2) {
|
||||
return *(unsigned char *)s1 - *(unsigned char*)s2;
|
||||
}
|
||||
|
||||
if (*s1 == '\0') {
|
||||
return 0;
|
||||
}
|
||||
|
||||
n--;
|
||||
s1++;
|
||||
s2++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *
|
||||
strchr (
|
||||
const char *s,
|
||||
int c
|
||||
)
|
||||
|
||||
{
|
||||
while (*s != (char)c) {
|
||||
if (!*s) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
s++;
|
||||
}
|
||||
|
||||
return (char *)s;
|
||||
}
|
||||
|
||||
char *
|
||||
strstr (
|
||||
const char *haystack,
|
||||
const char *needle
|
||||
)
|
||||
|
||||
{
|
||||
const char *ptr = haystack;
|
||||
|
||||
if (!*needle) {
|
||||
return (char *)haystack;
|
||||
}
|
||||
|
||||
while ((ptr = strchr(ptr, *needle)) != NULL) {
|
||||
if (strcmp(ptr, needle) == 0) {
|
||||
return (char *)ptr;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
@ -43,3 +43,87 @@ wcsnlen (
|
||||
|
||||
return len - sizeof(wchar_t);
|
||||
}
|
||||
|
||||
int
|
||||
wcscmp (
|
||||
const wchar_t* s1,
|
||||
const wchar_t* s2
|
||||
)
|
||||
|
||||
{
|
||||
while (*s1 == *s2) {
|
||||
if (*s1 == '\0') {
|
||||
return 0;
|
||||
}
|
||||
|
||||
s1++;
|
||||
s2++;
|
||||
}
|
||||
|
||||
return *s1 - *s2;
|
||||
}
|
||||
|
||||
int
|
||||
wcsncmp (
|
||||
const wchar_t* s1,
|
||||
const wchar_t* s2,
|
||||
size_t n
|
||||
)
|
||||
|
||||
{
|
||||
while (n > 0) {
|
||||
if (*s1 != *s2) {
|
||||
return *s1 - *s2;
|
||||
}
|
||||
|
||||
if (*s1 == '\0') {
|
||||
return 0;
|
||||
}
|
||||
|
||||
n--;
|
||||
s1++;
|
||||
s2++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
wchar_t *
|
||||
wcschr (
|
||||
const wchar_t *wcs,
|
||||
wchar_t wc
|
||||
)
|
||||
|
||||
{
|
||||
while (*wcs != wc) {
|
||||
if (!*wcs) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
wcs++;
|
||||
}
|
||||
|
||||
return (wchar_t *)wcs;
|
||||
}
|
||||
|
||||
wchar_t *
|
||||
wcsstr (
|
||||
const wchar_t *haystack,
|
||||
const wchar_t *needle
|
||||
)
|
||||
|
||||
{
|
||||
const wchar_t *ptr = haystack;
|
||||
|
||||
if (!*needle) {
|
||||
return (wchar_t *)haystack;
|
||||
}
|
||||
|
||||
while ((ptr = wcschr(ptr, *needle)) != NULL) {
|
||||
if (wcscmp(ptr, needle) == 0) {
|
||||
return (wchar_t *)ptr;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
@ -26,6 +26,10 @@ extern "C" {
|
||||
|
||||
size_t strlen(const char *str);
|
||||
size_t strnlen(const char *str, size_t maxlen);
|
||||
int strcmp(const char* s1, const char* s2);
|
||||
int strncmp(const char* s1, const char* s2, size_t n);
|
||||
char *strchr(const char *s, int c);
|
||||
char *strstr(const char *haystack, const char *needle);
|
||||
|
||||
void *memset(void *dest, int c, size_t count);
|
||||
void *memcpy(void *dest, const void *src, size_t count);
|
||||
|
@ -26,6 +26,10 @@ extern "C" {
|
||||
|
||||
size_t wcslen(const wchar_t *str);
|
||||
size_t wcsnlen(const wchar_t *str, size_t maxlen);
|
||||
int wcscmp(const wchar_t* s1, const wchar_t* s2);
|
||||
int wcsncmp(const wchar_t* s1, const wchar_t* s2, size_t n);
|
||||
wchar_t *wcschr(const wchar_t *wcs, wchar_t wc);
|
||||
wchar_t *wcsstr(const wchar_t *haystack, const wchar_t *needle);
|
||||
|
||||
wchar_t *wmemset(wchar_t *dest, wchar_t c, size_t count);
|
||||
wchar_t *wmemcpy(wchar_t *dest, const wchar_t *src, size_t count);
|
||||
|
Loading…
Reference in New Issue
Block a user