m4-commit
[Top][All Lists]
Advanced

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

[SCM] GNU M4 source repository branch, master, updated. cvs-readonly-186


From: Eric Blake
Subject: [SCM] GNU M4 source repository branch, master, updated. cvs-readonly-186-g3aa8661
Date: Tue, 10 Feb 2009 13:20:15 +0000

This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "GNU M4 source repository".

http://git.sv.gnu.org/gitweb/?p=m4.git;a=commitdiff;h=3aa86615d46144acfff9b7ff60cdec4f7cc2a43d

The branch, master has been updated
       via  3aa86615d46144acfff9b7ff60cdec4f7cc2a43d (commit)
      from  3998b3ad447a3200ce8a040b860a8fd0e109850b (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 3aa86615d46144acfff9b7ff60cdec4f7cc2a43d
Author: Eric Blake <address@hidden>
Date:   Mon Feb 9 11:22:57 2009 -0700

    Enhance index to support starting offset.
    
    * modules/m4.c (index): Add optional third argument.
    * NEWS: Document this.
    * doc/m4.texinfo (Index macro): Likewise.
    (Macro Arguments): Adjust tests.
    
    Signed-off-by: Eric Blake <address@hidden>
    (cherry picked from commit b9c165b394c06d2da657caae33efd5843689ff3b)

-----------------------------------------------------------------------

Summary of changes:
 ChangeLog      |    8 ++++++++
 NEWS           |    4 ++++
 doc/m4.texinfo |   29 ++++++++++++++++++++++++-----
 modules/m4.c   |   27 +++++++++++++++++++++++----
 4 files changed, 59 insertions(+), 9 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 72d80e3..85e876b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2009-02-09  Eric Blake  <address@hidden>
+
+       Enhance index to support starting offset.
+       * modules/m4.c (index): Add optional third argument.
+       * NEWS: Document this.
+       * doc/m4.texinfo (Index macro): Likewise.
+       (Macro Arguments): Adjust tests.
+
 2009-01-24  Eric Blake  <address@hidden>
 
        Add URLs to --help output.
diff --git a/NEWS b/NEWS
index dd7e674..772216d 100644
--- a/NEWS
+++ b/NEWS
@@ -248,6 +248,10 @@ promoted to 2.0.
    example of how to recover M4 1.4.x behavior, as well as an example of
    simulating the new negative argument semantics with older M4.
 
+** The `index' builtin now takes an optional third argument as the index
+   to begin searching from, with a negative argument relative to the end of
+   the string.
+
 ** The `-d'/`--debug' command-line option now understands `-' and `+'
    modifiers, the way the builtin `debugmode' has always done; this allows
    `-d-V' to disable prior debug settings from the command line, similar to
diff --git a/doc/m4.texinfo b/doc/m4.texinfo
index fbe42f2..093f395 100644
--- a/doc/m4.texinfo
+++ b/doc/m4.texinfo
@@ -1692,8 +1692,8 @@ index(`abc')
 @result{}0
 index(`abc',)
 @result{}0
-index(`abc', `b', `ignored')
address@hidden:stdin:3: Warning: index: extra arguments ignored: 3 > 2
+index(`abc', `b', `0', `ignored')
address@hidden:stdin:3: Warning: index: extra arguments ignored: 4 > 3
 @result{}1
 @end example
 
@@ -1704,7 +1704,7 @@ index(`abc')
 @result{}0
 index(`abc',)
 @result{}0
-index(`abc', `b', `ignored')
+index(`abc', `b', `', `ignored')
 @result{}1
 @end example
 
@@ -6869,11 +6869,13 @@ len(`abcdef')
 @cindex substrings, locating
 Searching for substrings is done with @code{index}:
 
address@hidden {Builtin (m4)} index (@var{string}, @var{substring})
address@hidden {Builtin (m4)} index (@var{string}, @var{substring}, 
@ovar{offset})
 Expands to the index of the first occurrence of @var{substring} in
 @var{string}.  The first character in @var{string} has index 0.  If
 @var{substring} does not occur in @var{string}, @code{index} expands to
address@hidden
address@hidden  If @var{offset} is provided, it determines the index at
+which the search starts; a negative @var{offset} specifies the offset
+relative to the end of @var{string}.
 
 The macro @code{index} is recognized only with parameters.
 @end deffn
@@ -6898,6 +6900,23 @@ index(`abc', `b')
 @result{}1
 @end example
 
address@hidden @acronym{GNU} extensions
+As an extension, an @var{offset} can be provided to limit the search to
+the tail of the @var{string}.  A negative offset is interpreted relative
+to the end of @var{string}, and it is not an error if @var{offset}
+exceeds the bounds of @var{string}.
+
address@hidden
+index(`aba', `a', `1')
address@hidden
+index(`ababa', `ba', `-3')
address@hidden
+index(`abc', `ab', `4')
address@hidden
+index(`abc', `bc', `-4')
address@hidden
address@hidden example
+
 @node Regexp
 @section Searching for regular expressions
 
diff --git a/modules/m4.c b/modules/m4.c
index b7d23d0..177590d 100644
--- a/modules/m4.c
+++ b/modules/m4.c
@@ -76,7 +76,7 @@ extern void m4_make_temp     (m4 *, m4_obstack *, const 
m4_call_info *,
   BUILTIN (ifelse,     true,   true,   false,  1,      -1 )    \
   BUILTIN (include,    false,  true,   false,  1,      1  )    \
   BUILTIN (incr,       false,  true,   true,   1,      1  )    \
-  BUILTIN (index,      false,  true,   true,   2,      2  )    \
+  BUILTIN (index,      false,  true,   true,   2,      3  )    \
   BUILTIN (len,                false,  true,   true,   1,      1  )    \
   BUILTIN (m4exit,     false,  false,  false,  0,      1  )    \
   BUILTIN (m4wrap,     true,   true,   false,  1,      -1 )    \
@@ -906,18 +906,37 @@ M4BUILTIN_HANDLER (len)
   m4_shipout_int (obs, M4ARGLEN (1));
 }
 
-/* The macro expands to the first index of the second argument in the first
-   argument.  */
+/* The macro expands to the first index of the second argument in the
+   first argument.  As an extension, start the search at the index
+   indicated by the third argument.  */
 M4BUILTIN_HANDLER (index)
 {
   const char *haystack = M4ARG (1);
+  size_t haystack_len = M4ARGLEN (1);
   const char *needle = M4ARG (2);
   const char *result = NULL;
+  int offset = 0;
   int retval = -1;
 
+  if (!m4_arg_empty (argv, 3) && !m4_numeric_arg (context, m4_arg_info (argv),
+                                                 M4ARG (3), &offset))
+    return;
+  if (offset < 0)
+    {
+      offset += haystack_len;
+      if (offset < 0)
+       offset = 0;
+    }
+  else if (haystack_len < offset)
+    {
+      m4_shipout_int (obs, -1);
+      return;
+    }
+
   /* Rely on the optimizations guaranteed by gnulib's memmem
      module.  */
-  result = (char *) memmem (haystack, M4ARGLEN (1), needle, M4ARGLEN (2));
+  result = (char *) memmem (haystack + offset, haystack_len - offset, needle,
+                           M4ARGLEN (2));
   if (result)
     retval = result - haystack;
 


hooks/post-receive
--
GNU M4 source repository




reply via email to

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