mini/string.c

158 lines
2.4 KiB
C
Raw Normal View History

2009-04-17 12:19:44 +02:00
/* string.c -- standard C string-manipulation functions.
Copyright (C) 2008 Segher Boessenkool <segher@kernel.crashing.org>
Copyright (C) 2009 Haxx Enterprises <bushing@gmail.com>
Portions taken from the Public Domain C Library (PDCLib).
https://negix.net/trac/pdclib
# This code is licensed to you under the terms of the GNU GPL, version 2;
# see file COPYING or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
*/
2008-12-28 14:35:37 +01:00
#include "string.h"
2009-04-17 12:19:44 +02:00
size_t strlen(const char *s)
2008-12-28 14:35:37 +01:00
{
2009-04-17 12:19:44 +02:00
size_t len;
2008-12-28 14:35:37 +01:00
2009-04-17 12:19:44 +02:00
for (len = 0; s[len]; len++)
;
return len;
2008-12-28 14:35:37 +01:00
}
2009-04-17 12:19:44 +02:00
size_t strnlen(const char *s, size_t count)
2008-12-28 14:35:37 +01:00
{
2009-04-17 12:19:44 +02:00
size_t len;
for (len = 0; s[len] && len < count; len++)
;
2008-12-28 14:35:37 +01:00
2009-04-17 12:19:44 +02:00
return len;
2008-12-28 14:35:37 +01:00
}
2009-04-17 12:19:44 +02:00
void *memset(void *b, int c, size_t len)
2008-12-28 14:35:37 +01:00
{
2009-04-17 12:19:44 +02:00
size_t i;
2008-12-28 14:35:37 +01:00
2009-04-17 12:19:44 +02:00
for (i = 0; i < len; i++)
((unsigned char *)b)[i] = c;
2008-12-28 14:35:37 +01:00
2009-04-17 12:19:44 +02:00
return b;
2008-12-28 14:35:37 +01:00
}
2009-04-17 12:19:44 +02:00
void *memcpy(void *dst, const void *src, size_t len)
2008-12-28 14:35:37 +01:00
{
2009-04-17 12:19:44 +02:00
size_t i;
2008-12-28 14:35:37 +01:00
2009-04-17 12:19:44 +02:00
for (i = 0; i < len; i++)
((unsigned char *)dst)[i] = ((unsigned char *)src)[i];
2008-12-28 14:35:37 +01:00
2009-04-17 12:19:44 +02:00
return dst;
2008-12-28 14:35:37 +01:00
}
2009-04-17 12:19:44 +02:00
int memcmp(const void *s1, const void *s2, size_t len)
2008-12-28 14:35:37 +01:00
{
2009-04-17 12:19:44 +02:00
size_t i;
const unsigned char * p1 = (const unsigned char *) s1;
const unsigned char * p2 = (const unsigned char *) s2;
for (i = 0; i < len; i++)
if (p1[i] != p2[i]) return p1[i] - p2[i];
return 0;
2008-12-28 14:35:37 +01:00
}
2009-04-17 12:19:44 +02:00
int strcmp(const char *s1, const char *s2)
{
2009-04-17 12:19:44 +02:00
size_t i;
for (i = 0; s1[i] && s1[i] == s2[i]; i++)
;
return s1[i] - s2[i];
}
2009-04-17 12:19:44 +02:00
int strncmp(const char *s1, const char *s2, size_t n)
2008-12-28 14:35:37 +01:00
{
2009-04-17 12:19:44 +02:00
size_t i;
2008-12-28 14:35:37 +01:00
2009-04-17 12:19:44 +02:00
for (i = 0; i < n && s1[i] && s1[i] == s2[i]; i++)
;
if (i == n) return 0;
return s1[i] - s2[i];
}
2008-12-28 14:35:37 +01:00
2009-04-17 12:19:44 +02:00
size_t strlcpy(char *dest, const char *src, size_t maxlen)
{
size_t len,needed;
len = needed = strnlen(src, maxlen-1) + 1;
if (len >= maxlen)
len = maxlen-1;
memcpy(dest, src, len);
dest[len]='\0';
return needed-1;
2008-12-28 14:35:37 +01:00
}
2009-04-17 12:19:44 +02:00
size_t strlcat(char *dest, const char *src, size_t maxlen)
2008-12-28 14:35:37 +01:00
{
2009-04-17 12:19:44 +02:00
size_t used;
2008-12-28 14:35:37 +01:00
2009-04-17 12:19:44 +02:00
used = strnlen(dest, maxlen-1);
return used + strlcpy(dest + used, src, maxlen - used);
}
2008-12-28 14:35:37 +01:00
2009-04-17 12:19:44 +02:00
char * strchr(const char *s, int c)
{
size_t i;
for (i = 0; s[i]; i++)
if (s[i] == (char)c) return (char *)s + i;
return NULL;
2008-12-28 14:35:37 +01:00
}
2009-04-17 12:19:44 +02:00
size_t strspn(const char *s1, const char *s2)
2008-12-28 14:35:37 +01:00
{
2009-04-17 12:19:44 +02:00
size_t len = 0;
const char *p;
while (s1[len]) {
p = s2;
while (*p) {
if (s1[len] == *p)
break;
++p;
}
if (!*p)
return len;
++len;
2008-12-28 14:35:37 +01:00
}
2009-04-17 12:19:44 +02:00
return len;
2008-12-28 14:35:37 +01:00
}
2009-04-17 12:19:44 +02:00
size_t strcspn(const char *s1, const char *s2)
{
2009-04-17 12:19:44 +02:00
size_t len = 0;
const char *p;
while (s1[len]) {
p = s2;
while (*p)
if (s1[len] == *p++)
return len;
++len;
}
return len;
}
2009-04-13 22:13:45 +02:00