From 251cbb167f63b6d1cbaa47e6880137c9f1f1b381 Mon Sep 17 00:00:00 2001 From: Hartmut Birr Date: Wed, 15 Jun 2005 22:47:12 +0000 Subject: [PATCH] Add an optimized version of strpbrk to the string library. svn path=/trunk/; revision=15929 --- reactos/lib/string/string.xml | 1 + reactos/lib/string/strpbrk.c | 54 +++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 reactos/lib/string/strpbrk.c diff --git a/reactos/lib/string/string.xml b/reactos/lib/string/string.xml index d5de07d21f0..98c463c0d06 100644 --- a/reactos/lib/string/string.xml +++ b/reactos/lib/string/string.xml @@ -63,5 +63,6 @@ memccpy.c memcmp.c strcspn.c + strpbrk.c strspn.c diff --git a/reactos/lib/string/strpbrk.c b/reactos/lib/string/strpbrk.c new file mode 100644 index 00000000000..b0b6ed8e5a9 --- /dev/null +++ b/reactos/lib/string/strpbrk.c @@ -0,0 +1,54 @@ +/* + * $Id$ + */ +#include +#include + +#define BIT_SIZE (CHAR_BIT * sizeof(unsigned long) / sizeof(char)) + +char* strpbrk(const char *s1, const char *s2) +{ + if (*s2 == 0) + { + return 0; + } + if (*(s2+1) == 0) + { + return strchr(s1, *s2); + } + else if (*(s2+2) == 0) + { + char *s3, *s4; + s3 = strchr(s1, *s2); + s4 = strchr(s1, *(s2+1)); + if (s3 == 0) + { + return s4; + } + else if (s4 == 0) + { + return s3; + } + return s3 < s4 ? s3 : s4; + } + else + { + unsigned long char_map[(1 << CHAR_BIT) / BIT_SIZE] = {0, }; + register unsigned char* str = (unsigned char*)s1; + while (*s2) + { + char_map[*(unsigned char*)s2 / BIT_SIZE] |= (1 << (*(unsigned char*)s2 % BIT_SIZE)); + s2++; + } + while (*str) + { + if (char_map[*str / BIT_SIZE] & (1 << (*str % BIT_SIZE))) + { + return str; + } + str++; + } + } + return 0; +} +