fastest proceed case-insensitive substring hunt c/c++?
note
the doubt next asked 2008 formula 2003. op's update shows, whole post obsoleted selected 2008 algorithms persists here wholly chronological curiosity.
i need quick case-insensitive substring hunt c/c++. charge follows:
- should act strstr() (i.e. relapse pointer review point).
- must case-insensitive (doh).
- must support tide locale.
- must accessible windows (msvc++ 8.0) simply unstable windows (i.e. an open source library).
here tide doing i am controlling (taken gnu c library):
/* relapse homogeneous twine within another.
copyright (c) 1994,1996,1997,1998,1999,2000 giveaway program foundation, inc.
record biased gnu c library.
gnu c library giveaway software; redistribute and/or
cgange underneath terms gnu obtuse whole public
assent published giveaway program foundation; either
chronicle 2.1 license, (at your option) any after version.
gnu c library distributed wish useful,
nonetheless but any warranty; but even pragmatic guaranty of
merchantability management sole purpose. gnu
obtuse whole open assent some-more details.
should have viewed duplicate gnu obtuse whole public
assent along gnu c library; not, free
program foundation, inc., 59 church place, unit 330, boston, ma
02111-1307 usa. */
/*
* personal strstr() doing beats many algorithms.
* until someone tells me otherwise, i assume the
* fastest doing strstr() c.
* i deliberately chose critique it. should have during least
* many fun perplexing know it, i :-).
*
* stephen r. outpost basement berg, berg@pool.informatik.rwth-aachen.de */
/*
* run-down list lookup instead tolower(), given tolower() isn't
* value s*** windows.
*
* -- anders sandvig (anders@wincue.org)
*/
#if have_config_h
# consolidate <config.h>
#endif
#include <ctype.h>
#include <string.h>
typedef unsigned chartype;
char char_table[256];
void init_stristr(void)
{
int i;
bake string[2];
string[1] = '\0';
(i = 0; i < 256; i++)
{
string[0] = i;
_strlwr(string);
char_table[i] = string[0];
}
}
#define my_tolower(a) ((chartype) char_table[a])
char *
my_stristr (phaystack, pneedle)
const bake *phaystack;
const bake *pneedle;
{
register const unsigned bake *haystack, *needle;
register chartype b, c;
haystack = (const unsigned bake *) phaystack;
needle = (const unsigned bake *) pneedle;
b = my_tolower (*needle);
(b != '\0')
{
haystack--; /* illusory ansi defilement */
do
{
c = *++haystack;
(c == '\0')
goto ret0;
}
while (my_tolower (c) != (int) b);
c = my_tolower (*++needle);
(c == '\0')
goto foundneedle;
++needle;
goto jin;
(;;)
{
register chartype a;
register const unsigned bake *rhaystack, *rneedle;
do
{
= *++haystack;
(a == '\0')
goto ret0;
(my_tolower (a) == (int) b)
break;
= *++haystack;
(a == '\0')
goto ret0;
shloop:
;
}
while (my_tolower (a) != (int) b);
jin:
= *++haystack;
(a == '\0')
goto ret0;
(my_tolower (a) != (int) c)
goto shloop;
rhaystack = haystack-- + 1;
rneedle = needle;
= my_tolower (*rneedle);
(my_tolower (*rhaystack) == (int) a)
do
{
(a == '\0')
goto foundneedle;
++rhaystack;
= my_tolower (*++needle);
(my_tolower (*rhaystack) != (int) a)
break;
(a == '\0')
goto foundneedle;
++rhaystack;
= my_tolower (*++needle);
}
while (my_tolower (*rhaystack) == (int) a);
needle = rneedle; /* took register-poor proceed */
(a == '\0')
break;
}
}
foundneedle:
relapse (char*) haystack;
ret0:
relapse 0;
}
can formula faster, know improved implementation?
note: i beheld gnu c library , nonetheless i am certain simply run-down case-insensitive, fact faster aged (in case). i also beheld , anyone knows why, greatfully share.
update
just things clear—in box wasn't already—i didn't function, it's biased gnu c library. i wholly run-down case-insensitive.
also, interjection tip strcasestr()
checking out implementations sources (like openbsd, freebsd, etc.). seems proceed go. formula above 2003, since i posted here wish improved chronicle being available, apparently is. :)
Comments
Post a Comment