CVSROOT: /sources/m4
Module name: m4
Branch: branch-1_4
Changes by: Eric Blake <ericb> 07/10/02 22:05:53
Index: src/builtin.c
===================================================================
RCS file: /sources/m4/m4/src/Attic/builtin.c,v
retrieving revision 1.1.1.1.2.63
retrieving revision 1.1.1.1.2.64
diff -u -b -r1.1.1.1.2.63 -r1.1.1.1.2.64
--- src/builtin.c 30 Sep 2007 00:35:35 -0000 1.1.1.1.2.63
+++ src/builtin.c 2 Oct 2007 22:05:53 -0000 1.1.1.1.2.64
@@ -1677,8 +1677,9 @@
m4_index (struct obstack *obs, int argc, token_data **argv)
{
const char *haystack;
- const char *result;
- int retval;
+ const char *needle;
+ const char *result = NULL;
+ int retval = -1;
if (bad_argc (argv[0], argc, 3, 3))
{
@@ -1689,8 +1690,18 @@
}
haystack = ARG (1);
- result = strstr (haystack, ARG (2));
- retval = result ? result - haystack : -1;
+ needle = ARG (2);
+
+ /* Optimize searching for the empty string (always 0) and one byte
+ (strchr tends to be more efficient than strstr). */
+ if (!needle[0])
+ retval = 0;
+ else if (!needle[1])
+ result = strchr (haystack, *needle);
+ else
+ result = strstr (haystack, needle);
+ if (result)
+ retval = result - haystack;
shipout_int (obs, retval);
}