>From http://www.lua.org/manual/5.3/manual.html#6.4:

  When indexing a string in Lua, the first character is at position 1 (not
  at 0, as in C).

The example of applying the pattern "()aa()" to the string "flaaap" in
the manpage shows that the empty captures "()" capture the positions
between the 'l' and the first 'a' and between the second and the third
'a's, which in C-style indexing would be positions 2 and 4, and in Lua
positions 3 and 5.

Maybe this difference should also be pointed out explicitly in the man
page for people familiar with Lua.

Positions 2 and 4 are also the numbers determined by `str_find', as
the following simple program shows:

--- 8< --- pattest.c
#include <err.h>
#include <stdio.h>

#include "patterns.h"

int
main(int argc, char *argv[])
{
        struct str_find          sm[MAXCAPTURES];
        const char              *string = "flaaap";
        const char              *pattern = "()aa()";
        const char              *errstr = NULL;
        int                      i, ret;

        ret = str_find(string, pattern, sm, MAXCAPTURES, &errstr);

        if (errstr != NULL)
                errx(1, "str_find, %s\n", errstr);

        printf("string: \"%s\", pattern: \"%s\", ret: %d\n", string,
            pattern, ret);
        for (i = 0; i < ret; i++)
                printf("sm[%d].sm_so = %lld, sm[%d].sm_eo = %lld\n",
                    i, sm[i].sm_so, i, sm[i].sm_eo);

        return 0;
}
--- >8 --- pattest.c

$ obj/pattest
string: "flaaap", pattern: "()aa()", ret: 3
sm[0].sm_so = 0, sm[0].sm_eo = 6
sm[1].sm_so = 2, sm[1].sm_eo = 2
sm[2].sm_so = 4, sm[2].sm_eo = 4
$

Thus, I suggest to modify the patterns(7) manual as follows:

Index: usr.sbin/httpd/patterns.7
===================================================================
RCS file: /cvs/src/usr.sbin/httpd/patterns.7,v
retrieving revision 1.2
diff -u -p -r1.2 patterns.7
--- usr.sbin/httpd/patterns.7   23 Jun 2015 17:29:19 -0000      1.2
+++ usr.sbin/httpd/patterns.7   25 Jun 2015 12:11:19 -0000
@@ -268,7 +268,7 @@ For instance, if we apply the pattern
 .Qq ()aa()
 on the string
 .Qq flaaap ,
-there will be two captures: 3 and 5.
+there will be two captures: 2 and 4.
 .Sh SEE ALSO
 .Xr fnmatch 3 ,
 .Xr re_format 7 ,

Reply via email to