xref: /onnv-gate/usr/src/lib/libast/common/string/strpsearch.c (revision 12068:08a39a083754)
14887Schin /***********************************************************************
24887Schin *                                                                      *
34887Schin *               This software is part of the ast package               *
4*12068SRoger.Faulkner@Oracle.COM *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
54887Schin *                      and is licensed under the                       *
64887Schin *                  Common Public License, Version 1.0                  *
78462SApril.Chin@Sun.COM *                    by AT&T Intellectual Property                     *
84887Schin *                                                                      *
94887Schin *                A copy of the License is available at                 *
104887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
114887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
124887Schin *                                                                      *
134887Schin *              Information and Software Systems Research               *
144887Schin *                            AT&T Research                             *
154887Schin *                           Florham Park NJ                            *
164887Schin *                                                                      *
174887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
184887Schin *                  David Korn <dgk@research.att.com>                   *
194887Schin *                   Phong Vo <kpv@research.att.com>                    *
204887Schin *                                                                      *
214887Schin ***********************************************************************/
224887Schin #pragma prototyped
234887Schin /*
244887Schin  * Glenn Fowler
254887Schin  * AT&T Research
264887Schin  */
274887Schin 
284887Schin #include <ast.h>
294887Schin #include <ccode.h>
304887Schin #include <ctype.h>
314887Schin 
324887Schin #if CC_NATIVE == CC_ASCII
334887Schin #define MAP(m,c)	(c)
344887Schin #else
354887Schin #define MAP(m,c)	m[c]
364887Schin #endif
374887Schin 
384887Schin /*
394887Schin  * return a pointer to the isalpha() identifier matching
404887Schin  * name in the CC_ASCII sorted tab of num elements of
414887Schin  * size siz where the first member of each
424887Schin  * element is a char*
434887Schin  *
444887Schin  * [xxx] brackets optional identifier characters
454887Schin  * * starts optional identifier characters
464887Schin  *
474887Schin  * 0 returned if name not found
484887Schin  * otherwise if next!=0 then it points to the next
494887Schin  * unmatched char in name
504887Schin  */
514887Schin 
524887Schin void*
strpsearch(const void * tab,size_t num,size_t siz,const char * name,char ** next)534887Schin strpsearch(const void* tab, size_t num, size_t siz, const char* name, char** next)
544887Schin {
554887Schin 	register char*		lo = (char*)tab;
564887Schin 	register char*		hi = lo + (num - 1) * siz;
574887Schin 	register char*		mid;
584887Schin #if CC_NATIVE != CC_ASCII
594887Schin 	register unsigned char*	m;
604887Schin #endif
614887Schin 	register unsigned char*	s;
624887Schin 	register unsigned char*	t;
634887Schin 	register int		c;
644887Schin 	register int		v;
654887Schin 	int			sequential = 0;
664887Schin 
674887Schin #if CC_NATIVE != CC_ASCII
684887Schin 	m = ccmap(CC_NATIVE, CC_ASCII);
694887Schin #endif
704887Schin 	c = MAP(m, *((unsigned char*)name));
714887Schin 	while (lo <= hi)
724887Schin 	{
734887Schin 		mid = lo + (sequential ? 0 : (((hi - lo) / siz) / 2) * siz);
744887Schin 		if (!(v = c - MAP(m, *(s = *((unsigned char**)mid)))) || *s == '[' && !(v = c - MAP(m, *++s)) && (v = 1))
754887Schin 		{
764887Schin 			t = (unsigned char*)name;
774887Schin 			for (;;)
784887Schin 			{
794887Schin 				if (!v && (*s == '[' || *s == '*'))
804887Schin 				{
814887Schin 					v = 1;
824887Schin 					s++;
834887Schin 				}
844887Schin 				else if (v && *s == ']')
854887Schin 				{
864887Schin 					v = 0;
874887Schin 					s++;
884887Schin 				}
894887Schin 				else if (!isalpha(*t))
904887Schin 				{
914887Schin 					if (v || !*s)
924887Schin 					{
934887Schin 						if (next)
944887Schin 							*next = (char*)t;
954887Schin 						return (void*)mid;
964887Schin 					}
974887Schin 					if (!sequential)
984887Schin 					{
994887Schin 						while ((mid -= siz) >= lo && (s = *((unsigned char**)mid)) && ((c == MAP(m, *s)) || *s == '[' && c == MAP(m, *(s + 1))));
1004887Schin 						sequential = 1;
1014887Schin 					}
1024887Schin 					v = 1;
1034887Schin 					break;
1044887Schin 				}
1054887Schin 				else if (*t != *s)
1064887Schin 				{
1074887Schin 					v = MAP(m, *t) - MAP(m, *s);
1084887Schin 					break;
1094887Schin 				}
1104887Schin 				else
1114887Schin 				{
1124887Schin 					t++;
1134887Schin 					s++;
1144887Schin 				}
1154887Schin 			}
1164887Schin 		}
1174887Schin 		else if (sequential)
1184887Schin 			break;
1194887Schin 		if (v > 0)
1204887Schin 			lo = mid + siz;
1214887Schin 		else
1224887Schin 			hi = mid - siz;
1234887Schin 	}
1244887Schin 	return 0;
1254887Schin }
126