util: Alternative implementation for standard c library string functions.

This commit is contained in:
José Fonseca
2008-05-05 23:58:37 +09:00
parent a3195e9d4e
commit 131a1fbc91
+112
View File
@@ -41,6 +41,8 @@
#include <stddef.h>
#include <stdarg.h>
#include "pipe/p_compiler.h"
#ifdef __cplusplus
extern "C" {
@@ -48,11 +50,121 @@ extern "C" {
#ifdef WIN32
int util_vsnprintf(char *, size_t, const char *, va_list);
int util_snprintf(char *str, size_t size, const char *format, ...);
static INLINE void
util_sprintf(char *str, const char *format, ...)
{
va_list ap;
va_start(ap, format);
util_vsnprintf(str, (size_t)-1, format, ap);
va_end(ap);
}
static INLINE char *
util_strchr(const char *s, char c)
{
while(*s) {
if(*s == c)
return (char *)s;
++s;
}
return NULL;
}
static INLINE char*
util_strncat(char *dst, const char *src, size_t n)
{
char *p = dst + strlen(dst);
const char *q = src;
size_t i;
for (i = 0; i < n && *q != '\0'; ++i)
*p++ = *q++;
*p = '\0';
return dst;
}
static INLINE int
util_strcmp(const char *s1, const char *s2)
{
unsigned char u1, u2;
while (1) {
u1 = (unsigned char) *s1++;
u2 = (unsigned char) *s2++;
if (u1 != u2)
return u1 - u2;
if (u1 == '\0')
return 0;
}
return 0;
}
static INLINE int
util_strncmp(const char *s1, const char *s2, size_t n)
{
unsigned char u1, u2;
while (n-- > 0) {
u1 = (unsigned char) *s1++;
u2 = (unsigned char) *s2++;
if (u1 != u2)
return u1 - u2;
if (u1 == '\0')
return 0;
}
return 0;
}
static INLINE char *
util_strstr(const char *haystack, const char *needle)
{
const char *p = haystack;
int len = strlen(needle);
for (; (p = util_strchr(p, *needle)) != 0; p++) {
if (util_strncmp(p, needle, len) == 0) {
return (char *)p;
}
}
return NULL;
}
static INLINE void *
util_memmove(void *dest, const void *src, size_t n)
{
char *p = (char *)dest;
const char *q = (const char *)src;
if (dest < src) {
while (n--)
*p++ = *q++;
}
else
{
p += n;
q += n;
while (n--)
*--p = *--q;
}
return dest;
}
#else
#define util_vsnprintf vsnprintf
#define util_snprintf snprintf
#define util_strchr strchr
#define util_strcmp strcmp
#define util_strncmp strncmp
#define util_strncat strncat
#define util_strstr strstr
#define util_memmove memmove
#endif