83 lines
1.4 KiB
C
83 lines
1.4 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/*
|
|
* linux/lib/string.c
|
|
*
|
|
* Copyright (C) 1991, 1992 Linus Torvalds
|
|
*/
|
|
|
|
#include <string.h>
|
|
|
|
size_t strspn(const char *s, const char *accept)
|
|
{
|
|
const char *p;
|
|
const char *a;
|
|
size_t count = 0;
|
|
|
|
for (p = s; *p != '\0'; ++p) {
|
|
for (a = accept; *a != '\0'; ++a) {
|
|
if (*p == *a)
|
|
break;
|
|
}
|
|
if (*a == '\0')
|
|
return count;
|
|
++count;
|
|
}
|
|
return count;
|
|
}
|
|
|
|
size_t strcspn(const char *s, const char *reject)
|
|
{
|
|
const char *p;
|
|
const char *r;
|
|
size_t count = 0;
|
|
|
|
for (p = s; *p != '\0'; ++p) {
|
|
for (r = reject; *r != '\0'; ++r) {
|
|
if (*p == *r)
|
|
return count;
|
|
}
|
|
++count;
|
|
}
|
|
return count;
|
|
}
|
|
|
|
size_t strlen(const char *s)
|
|
{
|
|
const char *sc;
|
|
|
|
for (sc = s; *sc != '\0'; ++sc)
|
|
/* nothing */;
|
|
return sc - s;
|
|
}
|
|
|
|
char *strchr(const char *s, int c)
|
|
{
|
|
for (; *s != (char)c; ++s)
|
|
if (*s == '\0')
|
|
return NULL;
|
|
return (char *)s;
|
|
}
|
|
|
|
char *strcpy(char *dest, const char *src)
|
|
{
|
|
char *tmp = dest;
|
|
|
|
while ((*dest++ = *src++) != '\0')
|
|
/* nothing */;
|
|
return tmp;
|
|
}
|
|
|
|
int strcmp(const char *cs, const char *ct)
|
|
{
|
|
unsigned char c1, c2;
|
|
|
|
while (1) {
|
|
c1 = *cs++;
|
|
c2 = *ct++;
|
|
if (c1 != c2)
|
|
return c1 < c2 ? -1 : 1;
|
|
if (!c1)
|
|
break;
|
|
}
|
|
return 0;
|
|
}
|