m4-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Changes to m4/src/Attic/builtin.c,v [branch-1_4]


From: Eric Blake
Subject: Changes to m4/src/Attic/builtin.c,v [branch-1_4]
Date: Tue, 31 Oct 2006 14:13:43 +0000

CVSROOT:        /sources/m4
Module name:    m4
Branch:         branch-1_4
Changes by:     Eric Blake <ericb>      06/10/31 14:13:41

Index: src/builtin.c
===================================================================
RCS file: /sources/m4/m4/src/Attic/builtin.c,v
retrieving revision 1.1.1.1.2.47
retrieving revision 1.1.1.1.2.48
diff -u -b -r1.1.1.1.2.47 -r1.1.1.1.2.48
--- src/builtin.c       29 Oct 2006 15:22:42 -0000      1.1.1.1.2.47
+++ src/builtin.c       31 Oct 2006 14:13:41 -0000      1.1.1.1.2.48
@@ -27,6 +27,7 @@
 extern FILE *popen ();
 
 #include "regex.h"
+#include "strstr.h"
 
 #if HAVE_SYS_WAIT_H
 # include <sys/wait.h>
@@ -1575,8 +1576,9 @@
 static void
 m4_index (struct obstack *obs, int argc, token_data **argv)
 {
-  const char *cp, *last;
-  int l1, l2, retval;
+  const char *haystack;
+  const char *result;
+  int retval;
 
   if (bad_argc (argv[0], argc, 3, 3))
     {
@@ -1586,17 +1588,9 @@
       return;
     }
 
-  l1 = strlen (ARG (1));
-  l2 = strlen (ARG (2));
-
-  last = ARG (1) + l1 - l2;
-
-  for (cp = ARG (1); cp <= last; cp++)
-    {
-      if (strncmp (cp, ARG (2), l2) == 0)
-       break;
-    }
-  retval = (cp <= last) ? cp - ARG (1) : -1;
+  haystack = ARG (1);
+  result = strstr (haystack, ARG (2));
+  retval = result ? result - haystack : -1;
 
   shipout_int (obs, retval);
 }
@@ -1693,9 +1687,11 @@
 static void
 m4_translit (struct obstack *obs, int argc, token_data **argv)
 {
-  register const char *data, *tmp;
-  const char *from, *to;
-  int tolen;
+  const unsigned char *data;
+  const unsigned char *from;
+  const unsigned char *to;
+  char map[256] = {0};
+  char found[256] = {0};
 
   if (bad_argc (argv[0], argc, 3, 4))
     {
@@ -1713,8 +1709,6 @@
        return;
     }
 
-  if (argc >= 4)
-    {
       to = ARG (3);
       if (strchr (to, '-') != NULL)
        {
@@ -1722,24 +1716,30 @@
          if (to == NULL)
            return;
        }
-    }
-  else
-    to = "";
 
-  tolen = strlen (to);
-
-  for (data = ARG (1); *data; data++)
+  /* Calling strchr(from) for each character in data is quadratic,
+     since both strings can be arbitrarily long.  Instead, create a
+     from-to mapping in one pass of data, then use that map in one
+     pass of from, for linear behavior.  Traditional behavior is that
+     only the first instance of a character in from is consulted,
+     hence the found map.  */
+  for ( ; *from; from++)
     {
-      tmp = strchr (from, *data);
-      if (tmp == NULL)
+      if (! found[*from])
        {
-         obstack_1grow (obs, *data);
+         found[*from] = 1;
+         map[*from] = *to;
        }
-      else
-       {
-         if (tmp - from < tolen)
-           obstack_1grow (obs, *(to + (tmp - from)));
+      if (*to != '\0')
+       to++;
        }
+
+  for (data = ARG (1); *data; data++)
+    {
+      if (! found[*data])
+       obstack_1grow (obs, *data);
+      else if (map[*data])
+       obstack_1grow (obs, map[*data]);
     }
 }
 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]