|
Lubomir Rintel |
0efa5e |
From aac37e1939632dbc7d2ade6f991af7ce103b0cba Mon Sep 17 00:00:00 2001
|
|
Lubomir Rintel |
0efa5e |
From: Tim Waugh <twaugh@redhat.com>
|
|
Lubomir Rintel |
0efa5e |
Date: Sun, 23 Nov 2008 17:30:59 +0100
|
|
Lubomir Rintel |
0efa5e |
Subject: [PATCH] EGF Speedup
|
|
Lubomir Rintel |
0efa5e |
|
|
Lubomir Rintel |
0efa5e |
The full story behind this patch is that grep-2.5.1a does not handle UTF-8 gracefully at all. The basic plan with handling UTF-8 in 2.5.1a is:
|
|
Lubomir Rintel |
0efa5e |
|
|
Lubomir Rintel |
0efa5e |
* whenever a buffer is parsed, go through the entire buffer deciding how many bytes make up each character
|
|
Lubomir Rintel |
0efa5e |
* use this information when necessary
|
|
Lubomir Rintel |
0efa5e |
|
|
Lubomir Rintel |
0efa5e |
This patch changes that to:
|
|
Lubomir Rintel |
0efa5e |
|
|
Lubomir Rintel |
0efa5e |
* when information about how many bytes make up a character is needed, work it out on demand
|
|
Lubomir Rintel |
0efa5e |
|
|
Lubomir Rintel |
0efa5e |
On the face of it, this is a small obvious improvement. In fact it is much better than that, because the original scheme would calculate character lengths several times for each buffer: in fact, one full pass for every single potential match!
|
|
Lubomir Rintel |
0efa5e |
|
|
Lubomir Rintel |
0efa5e |
For a full discussion of this patch, as well as dfa-optional, including benchmarking results, see the mailing list.
|
|
Lubomir Rintel |
0efa5e |
|
|
Lubomir Rintel |
0efa5e |
Upstream ticket: https://savannah.gnu.org/patch/?3803
|
|
Lubomir Rintel |
0efa5e |
Debian: 64-egf-speedup.patch
|
|
Lubomir Rintel |
0efa5e |
Debian: 66-match_icase.patch
|
|
Lubomir Rintel |
0efa5e |
---
|
|
Lubomir Rintel |
0efa5e |
lib/posix/regex.h | 4 +
|
|
Lubomir Rintel |
0efa5e |
src/search.c | 652 +++++++++++++++++++++++++++++++++++++++++++----------
|
|
Lubomir Rintel |
0efa5e |
2 files changed, 535 insertions(+), 121 deletions(-)
|
|
Lubomir Rintel |
0efa5e |
|
|
Lubomir Rintel |
0efa5e |
diff --git a/lib/posix/regex.h b/lib/posix/regex.h
|
|
Lubomir Rintel |
0efa5e |
index f4c4150..98df2cb 100644
|
|
Lubomir Rintel |
0efa5e |
--- a/lib/posix/regex.h
|
|
Lubomir Rintel |
0efa5e |
+++ b/lib/posix/regex.h
|
|
Lubomir Rintel |
0efa5e |
@@ -165,6 +165,10 @@ typedef unsigned long int reg_syntax_t;
|
|
Lubomir Rintel |
0efa5e |
treated as 'a\{1'. */
|
|
Lubomir Rintel |
0efa5e |
#define RE_INVALID_INTERVAL_ORD (RE_DEBUG << 1)
|
|
Lubomir Rintel |
0efa5e |
|
|
Lubomir Rintel |
0efa5e |
+/* If this bit is set, then ignore case when matching.
|
|
Lubomir Rintel |
0efa5e |
+ If not set, then case is significant. */
|
|
Lubomir Rintel |
0efa5e |
+#define RE_ICASE (RE_INVALID_INTERVAL_ORD << 1)
|
|
Lubomir Rintel |
0efa5e |
+
|
|
Lubomir Rintel |
0efa5e |
/* This global variable defines the particular regexp syntax to use (for
|
|
Lubomir Rintel |
0efa5e |
some interfaces). When a regexp is compiled, the syntax used is
|
|
Lubomir Rintel |
0efa5e |
stored in the pattern buffer, so changing this does not affect
|
|
Lubomir Rintel |
0efa5e |
diff --git a/src/search.c b/src/search.c
|
|
Lubomir Rintel |
0efa5e |
index 7f5f187..9691fb8 100644
|
|
Lubomir Rintel |
0efa5e |
--- a/src/search.c
|
|
Lubomir Rintel |
0efa5e |
+++ b/src/search.c
|
|
Lubomir Rintel |
0efa5e |
@@ -18,10 +18,15 @@
|
|
Tim Waugh |
ff7a53 |
|
|
Tim Waugh |
ff7a53 |
/* Written August 1992 by Mike Haertel. */
|
|
Tim Waugh |
ff7a53 |
|
|
Tim Waugh |
ff7a53 |
+#ifndef _GNU_SOURCE
|
|
Tim Waugh |
ff7a53 |
+# define _GNU_SOURCE 1
|
|
Tim Waugh |
ff7a53 |
+#endif
|
|
Tim Waugh |
e7e163 |
#ifdef HAVE_CONFIG_H
|
|
Tim Waugh |
e7e163 |
# include <config.h>
|
|
Tim Waugh |
e7e163 |
#endif
|
|
Lubomir Rintel |
0efa5e |
|
|
Tim Waugh |
e7e163 |
+#include <assert.h>
|
|
Lubomir Rintel |
0efa5e |
+
|
|
Tim Waugh |
e7e163 |
#include <sys/types.h>
|
|
Lubomir Rintel |
0efa5e |
|
|
Lubomir Rintel |
0efa5e |
#include "mbsupport.h"
|
|
Lubomir Rintel |
0efa5e |
@@ -43,6 +48,9 @@
|
|
Tim Waugh |
c3fd6f |
#ifdef HAVE_LIBPCRE
|
|
Tim Waugh |
c3fd6f |
# include <pcre.h>
|
|
Tim Waugh |
c3fd6f |
#endif
|
|
Tim Waugh |
c3fd6f |
+#ifdef HAVE_LANGINFO_CODESET
|
|
Tim Waugh |
c3fd6f |
+# include <langinfo.h>
|
|
Tim Waugh |
c3fd6f |
+#endif
|
|
Tim Waugh |
c3fd6f |
|
|
Tim Waugh |
c3fd6f |
#define NCHAR (UCHAR_MAX + 1)
|
|
Tim Waugh |
c3fd6f |
|
|
Lubomir Rintel |
0efa5e |
@@ -68,6 +76,19 @@ kwsinit (void)
|
|
Lubomir Rintel |
0efa5e |
error (2, 0, _("memory exhausted"));
|
|
Lubomir Rintel |
0efa5e |
}
|
|
Tim Waugh |
e1ca94 |
|
|
Tim Waugh |
c3fd6f |
+/* UTF-8 encoding allows some optimizations that we can't otherwise
|
|
Tim Waugh |
c3fd6f |
+ assume in a multibyte encoding. */
|
|
Tim Waugh |
c3fd6f |
+static int using_utf8;
|
|
Tim Waugh |
c3fd6f |
+
|
|
Lubomir Rintel |
0efa5e |
+void
|
|
Tim Waugh |
c3fd6f |
+check_utf8 (void)
|
|
Tim Waugh |
c3fd6f |
+{
|
|
Tim Waugh |
c3fd6f |
+#ifdef HAVE_LANGINFO_CODESET
|
|
Tim Waugh |
c3fd6f |
+ if (strcmp (nl_langinfo (CODESET), "UTF-8") == 0)
|
|
Tim Waugh |
c3fd6f |
+ using_utf8 = 1;
|
|
Tim Waugh |
c3fd6f |
+#endif
|
|
Tim Waugh |
c3fd6f |
+}
|
|
Tim Waugh |
c3fd6f |
+
|
|
Lubomir Rintel |
0efa5e |
#ifndef FGREP_PROGRAM
|
|
Lubomir Rintel |
0efa5e |
/* DFA compiled regexp. */
|
|
Lubomir Rintel |
0efa5e |
static struct dfa dfa;
|
|
Lubomir Rintel |
0efa5e |
@@ -134,49 +155,6 @@ kwsmusts (void)
|
|
Tim Waugh |
e1ca94 |
}
|
|
Lubomir Rintel |
0efa5e |
#endif /* !FGREP_PROGRAM */
|
|
Tim Waugh |
e1ca94 |
|
|
Tim Waugh |
e1ca94 |
-#ifdef MBS_SUPPORT
|
|
Tim Waugh |
e1ca94 |
-/* This function allocate the array which correspond to "buf".
|
|
Tim Waugh |
e1ca94 |
- Then this check multibyte string and mark on the positions which
|
|
Lubomir Rintel |
0efa5e |
- are not single byte character nor the first byte of a multibyte
|
|
Tim Waugh |
e1ca94 |
- character. Caller must free the array. */
|
|
Tim Waugh |
e1ca94 |
-static char*
|
|
Tim Waugh |
e1ca94 |
-check_multibyte_string(char const *buf, size_t size)
|
|
Tim Waugh |
e1ca94 |
-{
|
|
Tim Waugh |
e1ca94 |
- char *mb_properties = xmalloc(size);
|
|
Tim Waugh |
e1ca94 |
- mbstate_t cur_state;
|
|
Tim Waugh |
e1ca94 |
- wchar_t wc;
|
|
Tim Waugh |
e1ca94 |
- int i;
|
|
Lubomir Rintel |
0efa5e |
-
|
|
Tim Waugh |
e1ca94 |
- memset(&cur_state, 0, sizeof(mbstate_t));
|
|
Tim Waugh |
e1ca94 |
- memset(mb_properties, 0, sizeof(char)*size);
|
|
Lubomir Rintel |
0efa5e |
-
|
|
Tim Waugh |
e1ca94 |
- for (i = 0; i < size ;)
|
|
Tim Waugh |
e1ca94 |
- {
|
|
Tim Waugh |
e1ca94 |
- size_t mbclen;
|
|
Tim Waugh |
e1ca94 |
- mbclen = mbrtowc(&wc, buf + i, size - i, &cur_state);
|
|
Tim Waugh |
e1ca94 |
-
|
|
Tim Waugh |
e1ca94 |
- if (mbclen == (size_t) -1 || mbclen == (size_t) -2 || mbclen == 0)
|
|
Tim Waugh |
e1ca94 |
- {
|
|
Tim Waugh |
e1ca94 |
- /* An invalid sequence, or a truncated multibyte character.
|
|
Lubomir Rintel |
0efa5e |
- We treat it as a single byte character. */
|
|
Tim Waugh |
e1ca94 |
- mbclen = 1;
|
|
Tim Waugh |
e1ca94 |
- }
|
|
Tim Waugh |
e1ca94 |
- else if (match_icase)
|
|
Tim Waugh |
e1ca94 |
- {
|
|
Tim Waugh |
e1ca94 |
- if (iswupper((wint_t)wc))
|
|
Tim Waugh |
e1ca94 |
- {
|
|
Tim Waugh |
e1ca94 |
- wc = towlower((wint_t)wc);
|
|
Tim Waugh |
e1ca94 |
- wcrtomb(buf + i, wc, &cur_state);
|
|
Tim Waugh |
e1ca94 |
- }
|
|
Tim Waugh |
e1ca94 |
- }
|
|
Tim Waugh |
e1ca94 |
- mb_properties[i] = mbclen;
|
|
Tim Waugh |
e1ca94 |
- i += mbclen;
|
|
Tim Waugh |
e1ca94 |
- }
|
|
Tim Waugh |
e1ca94 |
-
|
|
Tim Waugh |
e1ca94 |
- return mb_properties;
|
|
Tim Waugh |
e1ca94 |
-}
|
|
Lubomir Rintel |
0efa5e |
-#endif /* MBS_SUPPORT */
|
|
Tim Waugh |
e1ca94 |
-
|
|
Lubomir Rintel |
0efa5e |
#if defined(GREP_PROGRAM) || defined(EGREP_PROGRAM)
|
|
Lubomir Rintel |
0efa5e |
#ifdef EGREP_PROGRAM
|
|
Lubomir Rintel |
0efa5e |
COMPILE_FCT(Ecompile)
|
|
Lubomir Rintel |
0efa5e |
@@ -193,10 +171,9 @@ GEAcompile (char const *pattern, size_t size, reg_syntax_t syntax_bits)
|
|
Tim Waugh |
c3fd6f |
size_t total = size;
|
|
Tim Waugh |
c3fd6f |
char const *motif = pattern;
|
|
Tim Waugh |
c3fd6f |
|
|
Lubomir Rintel |
0efa5e |
-#if 0
|
|
Tim Waugh |
c3fd6f |
+ check_utf8 ();
|
|
Lubomir Rintel |
0efa5e |
if (match_icase)
|
|
Lubomir Rintel |
0efa5e |
syntax_bits |= RE_ICASE;
|
|
Lubomir Rintel |
0efa5e |
-#endif
|
|
Lubomir Rintel |
0efa5e |
re_set_syntax (syntax_bits);
|
|
Lubomir Rintel |
0efa5e |
dfasyntax (syntax_bits, match_icase, eolbyte);
|
|
Tim Waugh |
c3fd6f |
|
|
Lubomir Rintel |
0efa5e |
@@ -303,20 +280,9 @@ EXECUTE_FCT(EGexecute)
|
|
Tim Waugh |
e1ca94 |
struct kwsmatch kwsm;
|
|
Tim Waugh |
e1ca94 |
size_t i, ret_val;
|
|
Tim Waugh |
e1ca94 |
#ifdef MBS_SUPPORT
|
|
Tim Waugh |
e1ca94 |
- char *mb_properties = NULL;
|
|
Tim Waugh |
e1ca94 |
- if (MB_CUR_MAX > 1)
|
|
Tim Waugh |
e1ca94 |
- {
|
|
Tim Waugh |
e1ca94 |
- if (match_icase)
|
|
Tim Waugh |
e1ca94 |
- {
|
|
Tim Waugh |
e1ca94 |
- char *case_buf = xmalloc(size);
|
|
Tim Waugh |
e1ca94 |
- memcpy(case_buf, buf, size);
|
|
Lubomir Rintel |
0efa5e |
- if (start_ptr)
|
|
Lubomir Rintel |
0efa5e |
- start_ptr = case_buf + (start_ptr - buf);
|
|
Tim Waugh |
e1ca94 |
- buf = case_buf;
|
|
Tim Waugh |
e1ca94 |
- }
|
|
Tim Waugh |
e1ca94 |
- if (kwset)
|
|
Tim Waugh |
e1ca94 |
- mb_properties = check_multibyte_string(buf, size);
|
|
Tim Waugh |
e1ca94 |
- }
|
|
Tim Waugh |
1b8d44 |
+ int mb_cur_max = MB_CUR_MAX;
|
|
Tim Waugh |
e1ca94 |
+ mbstate_t mbs;
|
|
Tim Waugh |
e1ca94 |
+ memset (&mbs, '\0', sizeof (mbstate_t));
|
|
Tim Waugh |
e1ca94 |
#endif /* MBS_SUPPORT */
|
|
Tim Waugh |
e1ca94 |
|
|
Tim Waugh |
e1ca94 |
buflim = buf + size;
|
|
Lubomir Rintel |
0efa5e |
@@ -329,21 +295,63 @@ EXECUTE_FCT(EGexecute)
|
|
Tim Waugh |
9fa0ec |
if (kwset)
|
|
Tim Waugh |
9fa0ec |
{
|
|
Tim Waugh |
9fa0ec |
/* Find a possible match using the KWset matcher. */
|
|
Tim Waugh |
8443e1 |
- size_t offset = kwsexec (kwset, beg, buflim - beg, &kwsm);
|
|
Tim Waugh |
9fa0ec |
+#ifdef MBS_SUPPORT
|
|
Tim Waugh |
9fa0ec |
+ size_t bytes_left = 0;
|
|
Tim Waugh |
9fa0ec |
+#endif /* MBS_SUPPORT */
|
|
Tim Waugh |
8443e1 |
+ size_t offset;
|
|
Tim Waugh |
8443e1 |
+#ifdef MBS_SUPPORT
|
|
Tim Waugh |
a5ca95 |
+ /* kwsexec doesn't work with match_icase and multibyte input. */
|
|
Tim Waugh |
1b8d44 |
+ if (match_icase && mb_cur_max > 1)
|
|
Tim Waugh |
8443e1 |
+ /* Avoid kwset */
|
|
Tim Waugh |
8443e1 |
+ offset = 0;
|
|
Tim Waugh |
8443e1 |
+ else
|
|
Tim Waugh |
8443e1 |
+#endif /* MBS_SUPPORT */
|
|
Tim Waugh |
8443e1 |
+ offset = kwsexec (kwset, beg, buflim - beg, &kwsm);
|
|
Tim Waugh |
e1ca94 |
if (offset == (size_t) -1)
|
|
Lubomir Rintel |
0efa5e |
- goto failure;
|
|
Lubomir Rintel |
0efa5e |
+ return (size_t)-1;
|
|
Tim Waugh |
e1ca94 |
+#ifdef MBS_SUPPORT
|
|
Tim Waugh |
1b8d44 |
+ if (mb_cur_max > 1 && !using_utf8)
|
|
Tim Waugh |
e1ca94 |
+ {
|
|
Tim Waugh |
9fa0ec |
+ bytes_left = offset;
|
|
Tim Waugh |
9fa0ec |
+ while (bytes_left)
|
|
Tim Waugh |
e1ca94 |
+ {
|
|
Tim Waugh |
b077d6 |
+ size_t mlen = mbrlen (beg, bytes_left, &mbs);
|
|
Tim Waugh |
b077d6 |
+ if (mlen == (size_t) -1 || mlen == 0)
|
|
Tim Waugh |
6f2e16 |
+ {
|
|
Tim Waugh |
2bb286 |
+ /* Incomplete character: treat as single-byte. */
|
|
Tim Waugh |
6f2e16 |
+ memset (&mbs, '\0', sizeof (mbstate_t));
|
|
Tim Waugh |
2bb286 |
+ beg++;
|
|
Tim Waugh |
2bb286 |
+ bytes_left--;
|
|
Tim Waugh |
2bb286 |
+ continue;
|
|
Tim Waugh |
6f2e16 |
+ }
|
|
Tim Waugh |
e1ca94 |
+
|
|
Tim Waugh |
b077d6 |
+ if (mlen == (size_t) -2)
|
|
Tim Waugh |
2bb286 |
+ /* Offset points inside multibyte character:
|
|
Tim Waugh |
2bb286 |
+ * no good. */
|
|
Tim Waugh |
2bb286 |
+ break;
|
|
Tim Waugh |
2bb286 |
+
|
|
Tim Waugh |
b077d6 |
+ beg += mlen;
|
|
Tim Waugh |
b077d6 |
+ bytes_left -= mlen;
|
|
Tim Waugh |
e1ca94 |
+ }
|
|
Tim Waugh |
e1ca94 |
+ }
|
|
Tim Waugh |
6f2e16 |
+ else
|
|
Tim Waugh |
6f2e16 |
+#endif /* MBS_SUPPORT */
|
|
Tim Waugh |
e1ca94 |
beg += offset;
|
|
Tim Waugh |
e1ca94 |
/* Narrow down to the line containing the candidate, and
|
|
Tim Waugh |
e1ca94 |
run it through DFA. */
|
|
Tim Waugh |
e1ca94 |
end = memchr(beg, eol, buflim - beg);
|
|
Tim Waugh |
e1ca94 |
end++;
|
|
Tim Waugh |
9fa0ec |
#ifdef MBS_SUPPORT
|
|
Tim Waugh |
e1ca94 |
- if (MB_CUR_MAX > 1 && mb_properties[beg - buf] == 0)
|
|
Tim Waugh |
1b8d44 |
+ if (mb_cur_max > 1 && bytes_left)
|
|
Tim Waugh |
9fa0ec |
continue;
|
|
Lubomir Rintel |
0efa5e |
#endif
|
|
Tim Waugh |
e1ca94 |
while (beg > buf && beg[-1] != eol)
|
|
Tim Waugh |
e1ca94 |
--beg;
|
|
Tim Waugh |
a5ca95 |
- if (kwsm.index < kwset_exact_matches)
|
|
Tim Waugh |
e5092c |
+ if (
|
|
Tim Waugh |
e5092c |
+#ifdef MBS_SUPPORT
|
|
Tim Waugh |
1b8d44 |
+ !(match_icase && mb_cur_max > 1) &&
|
|
Tim Waugh |
e5092c |
+#endif /* MBS_SUPPORT */
|
|
Tim Waugh |
a5ca95 |
+ (kwsm.index < kwset_exact_matches))
|
|
Lubomir Rintel |
0efa5e |
goto success;
|
|
Tim Waugh |
a5ca95 |
if (dfaexec (&dfa, beg, end - beg, &backref) == (size_t) -1)
|
|
Tim Waugh |
a5ca95 |
continue;
|
|
Lubomir Rintel |
0efa5e |
@@ -351,13 +359,47 @@ EXECUTE_FCT(EGexecute)
|
|
Tim Waugh |
9fa0ec |
else
|
|
Tim Waugh |
9fa0ec |
{
|
|
Tim Waugh |
9fa0ec |
/* No good fixed strings; start with DFA. */
|
|
Tim Waugh |
9fa0ec |
+#ifdef MBS_SUPPORT
|
|
Tim Waugh |
9fa0ec |
+ size_t bytes_left = 0;
|
|
Tim Waugh |
9fa0ec |
+#endif /* MBS_SUPPORT */
|
|
Tim Waugh |
9fa0ec |
size_t offset = dfaexec (&dfa, beg, buflim - beg, &backref);
|
|
Tim Waugh |
e1ca94 |
if (offset == (size_t) -1)
|
|
Tim Waugh |
e1ca94 |
break;
|
|
Tim Waugh |
e1ca94 |
/* Narrow down to the line we've found. */
|
|
Tim Waugh |
e1ca94 |
+#ifdef MBS_SUPPORT
|
|
Tim Waugh |
1b8d44 |
+ if (mb_cur_max > 1 && !using_utf8)
|
|
Tim Waugh |
e1ca94 |
+ {
|
|
Tim Waugh |
9fa0ec |
+ bytes_left = offset;
|
|
Tim Waugh |
9fa0ec |
+ while (bytes_left)
|
|
Tim Waugh |
e1ca94 |
+ {
|
|
Tim Waugh |
b077d6 |
+ size_t mlen = mbrlen (beg, bytes_left, &mbs);
|
|
Tim Waugh |
b077d6 |
+ if (mlen == (size_t) -1 || mlen == 0)
|
|
Tim Waugh |
6f2e16 |
+ {
|
|
Tim Waugh |
2bb286 |
+ /* Incomplete character: treat as single-byte. */
|
|
Tim Waugh |
6f2e16 |
+ memset (&mbs, '\0', sizeof (mbstate_t));
|
|
Tim Waugh |
2bb286 |
+ beg++;
|
|
Tim Waugh |
2bb286 |
+ bytes_left--;
|
|
Tim Waugh |
2bb286 |
+ continue;
|
|
Tim Waugh |
6f2e16 |
+ }
|
|
Tim Waugh |
e1ca94 |
+
|
|
Tim Waugh |
b077d6 |
+ if (mlen == (size_t) -2)
|
|
Tim Waugh |
2bb286 |
+ /* Offset points inside multibyte character:
|
|
Tim Waugh |
2bb286 |
+ * no good. */
|
|
Tim Waugh |
2bb286 |
+ break;
|
|
Tim Waugh |
2bb286 |
+
|
|
Tim Waugh |
b077d6 |
+ beg += mlen;
|
|
Tim Waugh |
b077d6 |
+ bytes_left -= mlen;
|
|
Tim Waugh |
e1ca94 |
+ }
|
|
Tim Waugh |
e1ca94 |
+ }
|
|
Tim Waugh |
6f2e16 |
+ else
|
|
Tim Waugh |
6f2e16 |
+#endif /* MBS_SUPPORT */
|
|
Tim Waugh |
e1ca94 |
beg += offset;
|
|
Tim Waugh |
e1ca94 |
end = memchr (beg, eol, buflim - beg);
|
|
Tim Waugh |
e1ca94 |
end++;
|
|
Tim Waugh |
9fa0ec |
+#ifdef MBS_SUPPORT
|
|
Tim Waugh |
1b8d44 |
+ if (mb_cur_max > 1 && bytes_left)
|
|
Tim Waugh |
9fa0ec |
+ continue;
|
|
Tim Waugh |
9fa0ec |
+#endif /* MBS_SUPPORT */
|
|
Tim Waugh |
9fa0ec |
while (beg > buf && beg[-1] != eol)
|
|
Tim Waugh |
9fa0ec |
--beg;
|
|
Tim Waugh |
9fa0ec |
}
|
|
Lubomir Rintel |
0efa5e |
@@ -475,24 +517,144 @@ EXECUTE_FCT(EGexecute)
|
|
Lubomir Rintel |
0efa5e |
*match_size = len;
|
|
Lubomir Rintel |
0efa5e |
ret_val = beg - buf;
|
|
Lubomir Rintel |
0efa5e |
out:
|
|
Tim Waugh |
e1ca94 |
-#ifdef MBS_SUPPORT
|
|
Tim Waugh |
e1ca94 |
- if (MB_CUR_MAX > 1)
|
|
Tim Waugh |
e1ca94 |
- {
|
|
Tim Waugh |
e1ca94 |
- if (match_icase)
|
|
Lubomir Rintel |
0efa5e |
- free((char*)buf);
|
|
Tim Waugh |
e1ca94 |
- if (mb_properties)
|
|
Lubomir Rintel |
0efa5e |
- free(mb_properties);
|
|
Tim Waugh |
e1ca94 |
- }
|
|
Tim Waugh |
e1ca94 |
-#endif /* MBS_SUPPORT */
|
|
Lubomir Rintel |
0efa5e |
return ret_val;
|
|
Tim Waugh |
e1ca94 |
}
|
|
Lubomir Rintel |
0efa5e |
#endif /* defined(GREP_PROGRAM) || defined(EGREP_PROGRAM) */
|
|
Tim Waugh |
e7e163 |
|
|
Tim Waugh |
ff7a53 |
+#ifdef MBS_SUPPORT
|
|
Tim Waugh |
e7e163 |
+static int f_i_multibyte; /* whether we're using the new -Fi MB method */
|
|
Tim Waugh |
ff7a53 |
+static struct
|
|
Tim Waugh |
ff7a53 |
+{
|
|
Tim Waugh |
ff7a53 |
+ wchar_t **patterns;
|
|
Tim Waugh |
ff7a53 |
+ size_t count, maxlen;
|
|
Tim Waugh |
ff7a53 |
+ unsigned char *match;
|
|
Tim Waugh |
ff7a53 |
+} Fimb;
|
|
Tim Waugh |
ff7a53 |
+#endif
|
|
Tim Waugh |
e7e163 |
+
|
|
Lubomir Rintel |
0efa5e |
#if defined(GREP_PROGRAM) || defined(FGREP_PROGRAM)
|
|
Lubomir Rintel |
0efa5e |
COMPILE_FCT(Fcompile)
|
|
Tim Waugh |
c3fd6f |
{
|
|
Tim Waugh |
1b8d44 |
+ int mb_cur_max = MB_CUR_MAX;
|
|
Tim Waugh |
c3fd6f |
char const *beg, *lim, *err;
|
|
Tim Waugh |
c3fd6f |
|
|
Tim Waugh |
c3fd6f |
+ check_utf8 ();
|
|
Tim Waugh |
e7e163 |
+#ifdef MBS_SUPPORT
|
|
Tim Waugh |
e7e163 |
+ /* Support -F -i for UTF-8 input. */
|
|
Tim Waugh |
1b8d44 |
+ if (match_icase && mb_cur_max > 1)
|
|
Tim Waugh |
e7e163 |
+ {
|
|
Tim Waugh |
ff7a53 |
+ mbstate_t mbs;
|
|
Tim Waugh |
ff7a53 |
+ wchar_t *wcpattern = xmalloc ((size + 1) * sizeof (wchar_t));
|
|
Tim Waugh |
ff7a53 |
+ const char *patternend = pattern;
|
|
Tim Waugh |
ff7a53 |
+ size_t wcsize;
|
|
Tim Waugh |
ff7a53 |
+ kwset_t fimb_kwset = NULL;
|
|
Tim Waugh |
ff7a53 |
+ char *starts = NULL;
|
|
Tim Waugh |
ff7a53 |
+ wchar_t *wcbeg, *wclim;
|
|
Tim Waugh |
ff7a53 |
+ size_t allocated = 0;
|
|
Tim Waugh |
ff7a53 |
+
|
|
Tim Waugh |
ff7a53 |
+ memset (&mbs, '\0', sizeof (mbs));
|
|
Tim Waugh |
ff7a53 |
+# ifdef __GNU_LIBRARY__
|
|
Tim Waugh |
ff7a53 |
+ wcsize = mbsnrtowcs (wcpattern, &patternend, size, size, &mbs);
|
|
Tim Waugh |
ff7a53 |
+ if (patternend != pattern + size)
|
|
Tim Waugh |
ff7a53 |
+ wcsize = (size_t) -1;
|
|
Tim Waugh |
ff7a53 |
+# else
|
|
Tim Waugh |
ff7a53 |
+ {
|
|
Tim Waugh |
ff7a53 |
+ char *patterncopy = xmalloc (size + 1);
|
|
Tim Waugh |
ff7a53 |
+
|
|
Tim Waugh |
ff7a53 |
+ memcpy (patterncopy, pattern, size);
|
|
Tim Waugh |
ff7a53 |
+ patterncopy[size] = '\0';
|
|
Tim Waugh |
ff7a53 |
+ patternend = patterncopy;
|
|
Tim Waugh |
ff7a53 |
+ wcsize = mbsrtowcs (wcpattern, &patternend, size, &mbs);
|
|
Tim Waugh |
ff7a53 |
+ if (patternend != patterncopy + size)
|
|
Tim Waugh |
ff7a53 |
+ wcsize = (size_t) -1;
|
|
Tim Waugh |
ff7a53 |
+ free (patterncopy);
|
|
Tim Waugh |
ff7a53 |
+ }
|
|
Tim Waugh |
ff7a53 |
+# endif
|
|
Tim Waugh |
ff7a53 |
+ if (wcsize + 2 <= 2)
|
|
Tim Waugh |
e7e163 |
+ {
|
|
Tim Waugh |
ff7a53 |
+fimb_fail:
|
|
Tim Waugh |
ff7a53 |
+ free (wcpattern);
|
|
Tim Waugh |
ff7a53 |
+ free (starts);
|
|
Tim Waugh |
ff7a53 |
+ if (fimb_kwset)
|
|
Tim Waugh |
ff7a53 |
+ kwsfree (fimb_kwset);
|
|
Tim Waugh |
ff7a53 |
+ free (Fimb.patterns);
|
|
Tim Waugh |
ff7a53 |
+ Fimb.patterns = NULL;
|
|
Tim Waugh |
ff7a53 |
+ }
|
|
Tim Waugh |
ff7a53 |
+ else
|
|
Tim Waugh |
ff7a53 |
+ {
|
|
Tim Waugh |
ff7a53 |
+ if (!(fimb_kwset = kwsalloc (NULL)))
|
|
Tim Waugh |
ff7a53 |
+ error (2, 0, _("memory exhausted"));
|
|
Tim Waugh |
ff7a53 |
+
|
|
Tim Waugh |
1b8d44 |
+ starts = xmalloc (mb_cur_max * 3);
|
|
Tim Waugh |
ff7a53 |
+ wcbeg = wcpattern;
|
|
Tim Waugh |
ff7a53 |
+ do
|
|
Tim Waugh |
e7e163 |
+ {
|
|
Tim Waugh |
ff7a53 |
+ int i;
|
|
Tim Waugh |
ff7a53 |
+ size_t wclen;
|
|
Tim Waugh |
e7e163 |
+
|
|
Tim Waugh |
ff7a53 |
+ if (Fimb.count >= allocated)
|
|
Tim Waugh |
e7e163 |
+ {
|
|
Tim Waugh |
ff7a53 |
+ if (allocated == 0)
|
|
Tim Waugh |
ff7a53 |
+ allocated = 128;
|
|
Tim Waugh |
ff7a53 |
+ else
|
|
Tim Waugh |
ff7a53 |
+ allocated *= 2;
|
|
Tim Waugh |
ff7a53 |
+ Fimb.patterns = xrealloc (Fimb.patterns,
|
|
Tim Waugh |
ff7a53 |
+ sizeof (wchar_t *) * allocated);
|
|
Tim Waugh |
e7e163 |
+ }
|
|
Tim Waugh |
ff7a53 |
+ Fimb.patterns[Fimb.count++] = wcbeg;
|
|
Tim Waugh |
ff7a53 |
+ for (wclim = wcbeg;
|
|
Tim Waugh |
ff7a53 |
+ wclim < wcpattern + wcsize && *wclim != L'\n'; ++wclim)
|
|
Tim Waugh |
ff7a53 |
+ *wclim = towlower (*wclim);
|
|
Tim Waugh |
ff7a53 |
+ *wclim = L'\0';
|
|
Tim Waugh |
ff7a53 |
+ wclen = wclim - wcbeg;
|
|
Tim Waugh |
ff7a53 |
+ if (wclen > Fimb.maxlen)
|
|
Tim Waugh |
ff7a53 |
+ Fimb.maxlen = wclen;
|
|
Tim Waugh |
ff7a53 |
+ if (wclen > 3)
|
|
Tim Waugh |
ff7a53 |
+ wclen = 3;
|
|
Tim Waugh |
ff7a53 |
+ if (wclen == 0)
|
|
Tim Waugh |
e7e163 |
+ {
|
|
Tim Waugh |
ff7a53 |
+ if ((err = kwsincr (fimb_kwset, "", 0)) != 0)
|
|
Tim Waugh |
ff7a53 |
+ error (2, 0, err);
|
|
Tim Waugh |
e7e163 |
+ }
|
|
Tim Waugh |
ff7a53 |
+ else
|
|
Tim Waugh |
ff7a53 |
+ for (i = 0; i < (1 << wclen); i++)
|
|
Tim Waugh |
ff7a53 |
+ {
|
|
Tim Waugh |
ff7a53 |
+ char *p = starts;
|
|
Tim Waugh |
ff7a53 |
+ int j, k;
|
|
Tim Waugh |
ff7a53 |
+
|
|
Tim Waugh |
ff7a53 |
+ for (j = 0; j < wclen; ++j)
|
|
Tim Waugh |
ff7a53 |
+ {
|
|
Tim Waugh |
ff7a53 |
+ wchar_t wc = wcbeg[j];
|
|
Tim Waugh |
ff7a53 |
+ if (i & (1 << j))
|
|
Tim Waugh |
ff7a53 |
+ {
|
|
Tim Waugh |
ff7a53 |
+ wc = towupper (wc);
|
|
Tim Waugh |
ff7a53 |
+ if (wc == wcbeg[j])
|
|
Tim Waugh |
ff7a53 |
+ continue;
|
|
Tim Waugh |
ff7a53 |
+ }
|
|
Tim Waugh |
ff7a53 |
+ k = wctomb (p, wc);
|
|
Tim Waugh |
ff7a53 |
+ if (k <= 0)
|
|
Tim Waugh |
ff7a53 |
+ goto fimb_fail;
|
|
Tim Waugh |
ff7a53 |
+ p += k;
|
|
Tim Waugh |
ff7a53 |
+ }
|
|
Tim Waugh |
ff7a53 |
+ if ((err = kwsincr (fimb_kwset, starts, p - starts)) != 0)
|
|
Tim Waugh |
ff7a53 |
+ error (2, 0, err);
|
|
Tim Waugh |
ff7a53 |
+ }
|
|
Tim Waugh |
ff7a53 |
+ if (wclim < wcpattern + wcsize)
|
|
Tim Waugh |
ff7a53 |
+ ++wclim;
|
|
Tim Waugh |
ff7a53 |
+ wcbeg = wclim;
|
|
Tim Waugh |
e7e163 |
+ }
|
|
Tim Waugh |
ff7a53 |
+ while (wcbeg < wcpattern + wcsize);
|
|
Tim Waugh |
e7e163 |
+ f_i_multibyte = 1;
|
|
Tim Waugh |
ff7a53 |
+ kwset = fimb_kwset;
|
|
Tim Waugh |
ff7a53 |
+ free (starts);
|
|
Tim Waugh |
ff7a53 |
+ Fimb.match = xmalloc (Fimb.count);
|
|
Tim Waugh |
ff7a53 |
+ if ((err = kwsprep (kwset)) != 0)
|
|
Tim Waugh |
ff7a53 |
+ error (2, 0, err);
|
|
Tim Waugh |
ff7a53 |
+ return;
|
|
Tim Waugh |
e7e163 |
+ }
|
|
Tim Waugh |
e7e163 |
+ }
|
|
Tim Waugh |
e7e163 |
+#endif /* MBS_SUPPORT */
|
|
Tim Waugh |
e7e163 |
+
|
|
Tim Waugh |
e7e163 |
+
|
|
Tim Waugh |
c3fd6f |
kwsinit ();
|
|
Tim Waugh |
c3fd6f |
beg = pattern;
|
|
Tim Waugh |
c3fd6f |
do
|
|
Lubomir Rintel |
0efa5e |
@@ -511,6 +673,76 @@ COMPILE_FCT(Fcompile)
|
|
Tim Waugh |
ff7a53 |
error (2, 0, err);
|
|
Tim Waugh |
e7e163 |
}
|
|
Tim Waugh |
e7e163 |
|
|
Tim Waugh |
ff7a53 |
+#ifdef MBS_SUPPORT
|
|
Tim Waugh |
ff7a53 |
+static int
|
|
Tim Waugh |
ff7a53 |
+Fimbexec (const char *buf, size_t size, size_t *plen, int exact)
|
|
Tim Waugh |
e7e163 |
+{
|
|
Tim Waugh |
ff7a53 |
+ size_t len, letter, i;
|
|
Tim Waugh |
ff7a53 |
+ int ret = -1;
|
|
Tim Waugh |
e7e163 |
+ mbstate_t mbs;
|
|
Tim Waugh |
ff7a53 |
+ wchar_t wc;
|
|
Tim Waugh |
ff7a53 |
+ int patterns_left;
|
|
Tim Waugh |
e7e163 |
+
|
|
Tim Waugh |
e7e163 |
+ assert (match_icase && f_i_multibyte == 1);
|
|
Tim Waugh |
e7e163 |
+ assert (MB_CUR_MAX > 1);
|
|
Tim Waugh |
e7e163 |
+
|
|
Tim Waugh |
e7e163 |
+ memset (&mbs, '\0', sizeof (mbs));
|
|
Tim Waugh |
ff7a53 |
+ memset (Fimb.match, '\1', Fimb.count);
|
|
Tim Waugh |
ff7a53 |
+ letter = len = 0;
|
|
Tim Waugh |
ff7a53 |
+ patterns_left = 1;
|
|
Tim Waugh |
ff7a53 |
+ while (patterns_left && len <= size)
|
|
Tim Waugh |
e7e163 |
+ {
|
|
Tim Waugh |
ff7a53 |
+ size_t c;
|
|
Tim Waugh |
e7e163 |
+
|
|
Tim Waugh |
ff7a53 |
+ patterns_left = 0;
|
|
Tim Waugh |
ff7a53 |
+ if (len < size)
|
|
Tim Waugh |
ff7a53 |
+ {
|
|
Tim Waugh |
ff7a53 |
+ c = mbrtowc (&wc, buf + len, size - len, &mbs);
|
|
Tim Waugh |
ff7a53 |
+ if (c + 2 <= 2)
|
|
Tim Waugh |
ff7a53 |
+ return ret;
|
|
Tim Waugh |
e7e163 |
+
|
|
Tim Waugh |
e7e163 |
+ wc = towlower (wc);
|
|
Tim Waugh |
ff7a53 |
+ }
|
|
Tim Waugh |
ff7a53 |
+ else
|
|
Tim Waugh |
ff7a53 |
+ {
|
|
Tim Waugh |
ff7a53 |
+ c = 1;
|
|
Tim Waugh |
ff7a53 |
+ wc = L'\0';
|
|
Tim Waugh |
ff7a53 |
+ }
|
|
Tim Waugh |
ff7a53 |
+
|
|
Tim Waugh |
ff7a53 |
+ for (i = 0; i < Fimb.count; i++)
|
|
Tim Waugh |
ff7a53 |
+ {
|
|
Tim Waugh |
ff7a53 |
+ if (Fimb.match[i])
|
|
Tim Waugh |
e7e163 |
+ {
|
|
Tim Waugh |
ff7a53 |
+ if (Fimb.patterns[i][letter] == L'\0')
|
|
Tim Waugh |
e7e163 |
+ {
|
|
Tim Waugh |
ff7a53 |
+ /* Found a match. */
|
|
Tim Waugh |
ff7a53 |
+ *plen = len;
|
|
Tim Waugh |
ff7a53 |
+ if (!exact && !match_words)
|
|
Tim Waugh |
ff7a53 |
+ return 0;
|
|
Tim Waugh |
ff7a53 |
+ else
|
|
Tim Waugh |
e7e163 |
+ {
|
|
Tim Waugh |
ff7a53 |
+ /* For -w or exact look for longest match. */
|
|
Tim Waugh |
ff7a53 |
+ ret = 0;
|
|
Tim Waugh |
ff7a53 |
+ Fimb.match[i] = '\0';
|
|
Tim Waugh |
ff7a53 |
+ continue;
|
|
Tim Waugh |
e7e163 |
+ }
|
|
Tim Waugh |
e7e163 |
+ }
|
|
Tim Waugh |
e7e163 |
+
|
|
Tim Waugh |
ff7a53 |
+ if (Fimb.patterns[i][letter] == wc)
|
|
Tim Waugh |
ff7a53 |
+ patterns_left = 1;
|
|
Tim Waugh |
ff7a53 |
+ else
|
|
Tim Waugh |
ff7a53 |
+ Fimb.match[i] = '\0';
|
|
Tim Waugh |
ff7a53 |
+ }
|
|
Tim Waugh |
e7e163 |
+ }
|
|
Tim Waugh |
e7e163 |
+
|
|
Tim Waugh |
ff7a53 |
+ len += c;
|
|
Tim Waugh |
ff7a53 |
+ letter++;
|
|
Tim Waugh |
e7e163 |
+ }
|
|
Tim Waugh |
ff7a53 |
+
|
|
Tim Waugh |
ff7a53 |
+ return ret;
|
|
Tim Waugh |
e7e163 |
+}
|
|
Tim Waugh |
ff7a53 |
+#endif /* MBS_SUPPORT */
|
|
Tim Waugh |
e7e163 |
+
|
|
Lubomir Rintel |
0efa5e |
EXECUTE_FCT(Fexecute)
|
|
Tim Waugh |
e7e163 |
{
|
|
Lubomir Rintel |
0efa5e |
register char const *beg, *try, *end;
|
|
Lubomir Rintel |
0efa5e |
@@ -519,69 +751,256 @@ EXECUTE_FCT(Fexecute)
|
|
Tim Waugh |
e1ca94 |
struct kwsmatch kwsmatch;
|
|
Tim Waugh |
e1ca94 |
size_t ret_val;
|
|
Tim Waugh |
e1ca94 |
#ifdef MBS_SUPPORT
|
|
Tim Waugh |
e1ca94 |
- char *mb_properties = NULL;
|
|
Tim Waugh |
e1ca94 |
- if (MB_CUR_MAX > 1)
|
|
Tim Waugh |
e1ca94 |
- {
|
|
Tim Waugh |
e1ca94 |
- if (match_icase)
|
|
Tim Waugh |
e1ca94 |
- {
|
|
Tim Waugh |
e1ca94 |
- char *case_buf = xmalloc(size);
|
|
Tim Waugh |
e1ca94 |
- memcpy(case_buf, buf, size);
|
|
Lubomir Rintel |
0efa5e |
- if (start_ptr)
|
|
Lubomir Rintel |
0efa5e |
- start_ptr = case_buf + (start_ptr - buf);
|
|
Tim Waugh |
e1ca94 |
- buf = case_buf;
|
|
Tim Waugh |
e1ca94 |
- }
|
|
Tim Waugh |
e1ca94 |
- mb_properties = check_multibyte_string(buf, size);
|
|
Tim Waugh |
e1ca94 |
- }
|
|
Tim Waugh |
ff7a53 |
+ int mb_cur_max = MB_CUR_MAX;
|
|
Tim Waugh |
e1ca94 |
+ mbstate_t mbs;
|
|
Tim Waugh |
e1ca94 |
+ memset (&mbs, '\0', sizeof (mbstate_t));
|
|
Tim Waugh |
ff7a53 |
+ const char *last_char = NULL;
|
|
Tim Waugh |
e1ca94 |
#endif /* MBS_SUPPORT */
|
|
Tim Waugh |
e1ca94 |
|
|
Lubomir Rintel |
0efa5e |
for (beg = start_ptr ? start_ptr : buf; beg <= buf + size; beg++)
|
|
Tim Waugh |
e7e163 |
{
|
|
Lubomir Rintel |
0efa5e |
size_t offset = kwsexec (kwset, beg, buf + size - beg, &kwsmatch);
|
|
Tim Waugh |
e1ca94 |
if (offset == (size_t) -1)
|
|
Lubomir Rintel |
0efa5e |
- goto failure;
|
|
Lubomir Rintel |
0efa5e |
+ return offset;
|
|
Tim Waugh |
e1ca94 |
#ifdef MBS_SUPPORT
|
|
Tim Waugh |
e1ca94 |
- if (MB_CUR_MAX > 1 && mb_properties[offset+beg-buf] == 0)
|
|
Tim Waugh |
e1ca94 |
- continue; /* It is a part of multibyte character. */
|
|
Tim Waugh |
ff7a53 |
+ if (mb_cur_max > 1 && !using_utf8)
|
|
Tim Waugh |
e1ca94 |
+ {
|
|
Tim Waugh |
9fa0ec |
+ size_t bytes_left = offset;
|
|
Tim Waugh |
9fa0ec |
+ while (bytes_left)
|
|
Tim Waugh |
e1ca94 |
+ {
|
|
Tim Waugh |
ff7a53 |
+ size_t mlen = mbrlen (beg, bytes_left, &mbs);
|
|
Tim Waugh |
ff7a53 |
+
|
|
Tim Waugh |
ff7a53 |
+ last_char = beg;
|
|
Tim Waugh |
ff7a53 |
+ if (mlen == (size_t) -1 || mlen == 0)
|
|
Tim Waugh |
6f2e16 |
+ {
|
|
Tim Waugh |
2bb286 |
+ /* Incomplete character: treat as single-byte. */
|
|
Tim Waugh |
6f2e16 |
+ memset (&mbs, '\0', sizeof (mbstate_t));
|
|
Tim Waugh |
2bb286 |
+ beg++;
|
|
Tim Waugh |
2bb286 |
+ bytes_left--;
|
|
Tim Waugh |
2bb286 |
+ continue;
|
|
Tim Waugh |
6f2e16 |
+ }
|
|
Tim Waugh |
6f2e16 |
+
|
|
Tim Waugh |
ff7a53 |
+ if (mlen == (size_t) -2)
|
|
Tim Waugh |
2bb286 |
+ /* Offset points inside multibyte character: no good. */
|
|
Tim Waugh |
2bb286 |
+ break;
|
|
Tim Waugh |
2bb286 |
+
|
|
Tim Waugh |
ff7a53 |
+ beg += mlen;
|
|
Tim Waugh |
ff7a53 |
+ bytes_left -= mlen;
|
|
Tim Waugh |
e1ca94 |
+ }
|
|
Tim Waugh |
e1ca94 |
+
|
|
Tim Waugh |
9fa0ec |
+ if (bytes_left)
|
|
Tim Waugh |
6f2e16 |
+ continue;
|
|
Tim Waugh |
e1ca94 |
+ }
|
|
Tim Waugh |
6f2e16 |
+ else
|
|
Tim Waugh |
6f2e16 |
#endif /* MBS_SUPPORT */
|
|
Tim Waugh |
e1ca94 |
beg += offset;
|
|
Tim Waugh |
e7e163 |
+#ifdef MBS_SUPPORT
|
|
Tim Waugh |
ff7a53 |
+ /* For f_i_multibyte, the string at beg now matches first 3 chars of
|
|
Tim Waugh |
ff7a53 |
+ one of the search strings (less if there are shorter search strings).
|
|
Tim Waugh |
ff7a53 |
+ See if this is a real match. */
|
|
Tim Waugh |
ff7a53 |
+ if (f_i_multibyte
|
|
Lubomir Rintel |
0efa5e |
+ && Fimbexec (beg, buf + size - beg, &kwsmatch.size[0], start_ptr == NULL))
|
|
Tim Waugh |
ff7a53 |
+ goto next_char;
|
|
Tim Waugh |
ff7a53 |
+#endif /* MBS_SUPPORT */
|
|
Tim Waugh |
ff7a53 |
len = kwsmatch.size[0];
|
|
Lubomir Rintel |
0efa5e |
if (start_ptr && !match_words)
|
|
Tim Waugh |
ff7a53 |
goto success_in_beg_and_len;
|
|
Tim Waugh |
ff7a53 |
if (match_lines)
|
|
Tim Waugh |
ff7a53 |
{
|
|
Tim Waugh |
ff7a53 |
if (beg > buf && beg[-1] != eol)
|
|
Tim Waugh |
ff7a53 |
- continue;
|
|
Tim Waugh |
ff7a53 |
+ goto next_char;
|
|
Tim Waugh |
ff7a53 |
if (beg + len < buf + size && beg[len] != eol)
|
|
Tim Waugh |
ff7a53 |
- continue;
|
|
Tim Waugh |
ff7a53 |
+ goto next_char;
|
|
Tim Waugh |
ff7a53 |
goto success;
|
|
Tim Waugh |
ff7a53 |
}
|
|
Tim Waugh |
ff7a53 |
else if (match_words)
|
|
Tim Waugh |
ff7a53 |
- for (try = beg; len; )
|
|
Tim Waugh |
ff7a53 |
- {
|
|
Tim Waugh |
ff7a53 |
- if (try > buf && WCHAR((unsigned char) try[-1]))
|
|
Tim Waugh |
ff7a53 |
- break;
|
|
Tim Waugh |
ff7a53 |
- if (try + len < buf + size && WCHAR((unsigned char) try[len]))
|
|
Tim Waugh |
ff7a53 |
- {
|
|
Tim Waugh |
ff7a53 |
- offset = kwsexec (kwset, beg, --len, &kwsmatch);
|
|
Tim Waugh |
ff7a53 |
- if (offset == (size_t) -1)
|
|
Lubomir Rintel |
0efa5e |
- break;
|
|
Lubomir Rintel |
0efa5e |
- try = beg + offset;
|
|
Lubomir Rintel |
0efa5e |
- len = kwsmatch.size[0];
|
|
Lubomir Rintel |
0efa5e |
- }
|
|
Lubomir Rintel |
0efa5e |
- else if (!start_ptr)
|
|
Lubomir Rintel |
0efa5e |
- goto success;
|
|
Lubomir Rintel |
0efa5e |
- else
|
|
Lubomir Rintel |
0efa5e |
- goto success_in_beg_and_len;
|
|
Lubomir Rintel |
0efa5e |
- } /* for (try) */
|
|
Lubomir Rintel |
0efa5e |
- else
|
|
Tim Waugh |
ff7a53 |
+ {
|
|
Tim Waugh |
e4c786 |
+ while (len)
|
|
Tim Waugh |
ff7a53 |
+ {
|
|
Tim Waugh |
ff7a53 |
+ int word_match = 0;
|
|
Tim Waugh |
ff7a53 |
+ if (beg > buf)
|
|
Tim Waugh |
ff7a53 |
+ {
|
|
Lubomir Rintel |
0efa5e |
+#ifdef MBS_SUPPORT
|
|
Tim Waugh |
ff7a53 |
+ if (mb_cur_max > 1)
|
|
Tim Waugh |
ff7a53 |
+ {
|
|
Tim Waugh |
ff7a53 |
+ const char *s;
|
|
Tim Waugh |
ff7a53 |
+ int mr;
|
|
Tim Waugh |
ff7a53 |
+ wchar_t pwc;
|
|
Tim Waugh |
ff7a53 |
+
|
|
Tim Waugh |
ff7a53 |
+ if (using_utf8)
|
|
Tim Waugh |
ff7a53 |
+ {
|
|
Tim Waugh |
ff7a53 |
+ s = beg - 1;
|
|
Tim Waugh |
ff7a53 |
+ while (s > buf
|
|
Tim Waugh |
ff7a53 |
+ && (unsigned char) *s >= 0x80
|
|
Tim Waugh |
ff7a53 |
+ && (unsigned char) *s <= 0xbf)
|
|
Tim Waugh |
ff7a53 |
+ --s;
|
|
Tim Waugh |
ff7a53 |
+ }
|
|
Tim Waugh |
ff7a53 |
+ else
|
|
Tim Waugh |
ff7a53 |
+ s = last_char;
|
|
Tim Waugh |
ff7a53 |
+ mr = mbtowc (&pwc, s, beg - s);
|
|
Tim Waugh |
ff7a53 |
+ if (mr <= 0)
|
|
Tim Waugh |
ff7a53 |
+ memset (&mbs, '\0', sizeof (mbstate_t));
|
|
Tim Waugh |
ff7a53 |
+ else if ((iswalnum (pwc) || pwc == L'_')
|
|
Tim Waugh |
ff7a53 |
+ && mr == (int) (beg - s))
|
|
Tim Waugh |
ff7a53 |
+ goto next_char;
|
|
Tim Waugh |
ff7a53 |
+ }
|
|
Tim Waugh |
e7e163 |
+ else
|
|
Lubomir Rintel |
0efa5e |
+#endif /* MBS_SUPPORT */
|
|
Tim Waugh |
b566e4 |
+ if (WCHAR ((unsigned char) beg[-1]))
|
|
Tim Waugh |
ff7a53 |
+ goto next_char;
|
|
Tim Waugh |
ff7a53 |
+ }
|
|
Tim Waugh |
ff7a53 |
+#ifdef MBS_SUPPORT
|
|
Tim Waugh |
ff7a53 |
+ if (mb_cur_max > 1)
|
|
Tim Waugh |
ff7a53 |
+ {
|
|
Tim Waugh |
ff7a53 |
+ wchar_t nwc;
|
|
Tim Waugh |
ff7a53 |
+ int mr;
|
|
Tim Waugh |
ff7a53 |
+
|
|
Tim Waugh |
ff7a53 |
+ mr = mbtowc (&nwc, beg + len, buf + size - beg - len);
|
|
Tim Waugh |
ff7a53 |
+ if (mr <= 0)
|
|
Tim Waugh |
ff7a53 |
+ {
|
|
Tim Waugh |
ff7a53 |
+ memset (&mbs, '\0', sizeof (mbstate_t));
|
|
Tim Waugh |
ff7a53 |
+ word_match = 1;
|
|
Tim Waugh |
ff7a53 |
+ }
|
|
Tim Waugh |
ff7a53 |
+ else if (!iswalnum (nwc) && nwc != L'_')
|
|
Tim Waugh |
ff7a53 |
+ word_match = 1;
|
|
Tim Waugh |
ff7a53 |
+ }
|
|
Tim Waugh |
ff7a53 |
+ else
|
|
Tim Waugh |
e7e163 |
+#endif /* MBS_SUPPORT */
|
|
Tim Waugh |
23c991 |
+ if (beg + len >= buf + size || !WCHAR ((unsigned char) beg[len]))
|
|
Tim Waugh |
ff7a53 |
+ word_match = 1;
|
|
Tim Waugh |
ff7a53 |
+ if (word_match)
|
|
Tim Waugh |
ff7a53 |
+ {
|
|
Lubomir Rintel |
0efa5e |
+ if (start_ptr == NULL)
|
|
Tim Waugh |
ff7a53 |
+ /* Returns the whole line now we know there's a word match. */
|
|
Tim Waugh |
ff7a53 |
+ goto success;
|
|
Lubomir Rintel |
0efa5e |
+ else {
|
|
Tim Waugh |
ff7a53 |
+ /* Returns just this word match. */
|
|
Lubomir Rintel |
0efa5e |
+ *match_size = len;
|
|
Lubomir Rintel |
0efa5e |
+ return beg - buf;
|
|
Lubomir Rintel |
0efa5e |
+ }
|
|
Tim Waugh |
ff7a53 |
+ }
|
|
Tim Waugh |
ff7a53 |
+ if (len > 0)
|
|
Tim Waugh |
ff7a53 |
+ {
|
|
Tim Waugh |
ff7a53 |
+ /* Try a shorter length anchored at the same place. */
|
|
Tim Waugh |
ff7a53 |
+ --len;
|
|
Tim Waugh |
ff7a53 |
+ offset = kwsexec (kwset, beg, len, &kwsmatch);
|
|
Tim Waugh |
e7e163 |
+
|
|
Tim Waugh |
ff7a53 |
+ if (offset == -1)
|
|
Tim Waugh |
ff7a53 |
+ goto next_char; /* Try a different anchor. */
|
|
Tim Waugh |
e1ca94 |
+#ifdef MBS_SUPPORT
|
|
Lubomir Rintel |
0efa5e |
+
|
|
Tim Waugh |
ff7a53 |
+ if (mb_cur_max > 1 && !using_utf8)
|
|
Tim Waugh |
e1ca94 |
+ {
|
|
Tim Waugh |
9fa0ec |
+ size_t bytes_left = offset;
|
|
Tim Waugh |
9fa0ec |
+ while (bytes_left)
|
|
Tim Waugh |
e1ca94 |
+ {
|
|
Tim Waugh |
ff7a53 |
+ size_t mlen = mbrlen (beg, bytes_left, &mbs);
|
|
Tim Waugh |
ff7a53 |
+
|
|
Tim Waugh |
ff7a53 |
+ last_char = beg;
|
|
Tim Waugh |
ff7a53 |
+ if (mlen == (size_t) -1 || mlen == 0)
|
|
Tim Waugh |
6f2e16 |
+ {
|
|
Tim Waugh |
2bb286 |
+ /* Incomplete character: treat as single-byte. */
|
|
Tim Waugh |
6f2e16 |
+ memset (&mbs, '\0', sizeof (mbstate_t));
|
|
Tim Waugh |
2bb286 |
+ beg++;
|
|
Tim Waugh |
2bb286 |
+ bytes_left--;
|
|
Tim Waugh |
2bb286 |
+ continue;
|
|
Tim Waugh |
6f2e16 |
+ }
|
|
Tim Waugh |
6f2e16 |
+
|
|
Tim Waugh |
ff7a53 |
+ if (mlen == (size_t) -2)
|
|
Tim Waugh |
ff7a53 |
+ {
|
|
Tim Waugh |
ff7a53 |
+ /* Offset points inside multibyte character:
|
|
Tim Waugh |
ff7a53 |
+ * no good. */
|
|
Tim Waugh |
ff7a53 |
+ break;
|
|
Tim Waugh |
ff7a53 |
+ }
|
|
Tim Waugh |
2bb286 |
+
|
|
Tim Waugh |
ff7a53 |
+ beg += mlen;
|
|
Tim Waugh |
ff7a53 |
+ bytes_left -= mlen;
|
|
Tim Waugh |
e1ca94 |
+ }
|
|
Tim Waugh |
e1ca94 |
+
|
|
Tim Waugh |
9fa0ec |
+ if (bytes_left)
|
|
Tim Waugh |
ff7a53 |
+ {
|
|
Tim Waugh |
ff7a53 |
+ memset (&mbs, '\0', sizeof (mbstate_t));
|
|
Tim Waugh |
ff7a53 |
+ goto next_char; /* Try a different anchor. */
|
|
Tim Waugh |
ff7a53 |
+ }
|
|
Tim Waugh |
e1ca94 |
+ }
|
|
Tim Waugh |
6f2e16 |
+ else
|
|
Tim Waugh |
e1ca94 |
+#endif /* MBS_SUPPORT */
|
|
Tim Waugh |
ff7a53 |
+ beg += offset;
|
|
Tim Waugh |
ff7a53 |
+#ifdef MBS_SUPPORT
|
|
Tim Waugh |
ff7a53 |
+ /* The string at beg now matches first 3 chars of one of
|
|
Tim Waugh |
ff7a53 |
+ the search strings (less if there are shorter search
|
|
Tim Waugh |
ff7a53 |
+ strings). See if this is a real match. */
|
|
Tim Waugh |
ff7a53 |
+ if (f_i_multibyte
|
|
Tim Waugh |
ff7a53 |
+ && Fimbexec (beg, len - offset, &kwsmatch.size[0],
|
|
Lubomir Rintel |
0efa5e |
+ start_ptr == NULL))
|
|
Tim Waugh |
ff7a53 |
+ goto next_char;
|
|
Tim Waugh |
ff7a53 |
+#endif /* MBS_SUPPORT */
|
|
Tim Waugh |
ff7a53 |
+ len = kwsmatch.size[0];
|
|
Tim Waugh |
ff7a53 |
+ }
|
|
Tim Waugh |
ff7a53 |
+ }
|
|
Tim Waugh |
ff7a53 |
+ }
|
|
Lubomir Rintel |
0efa5e |
+ else
|
|
Tim Waugh |
ff7a53 |
goto success;
|
|
Lubomir Rintel |
0efa5e |
- } /* for (beg in buf) */
|
|
Tim Waugh |
ff7a53 |
+next_char:;
|
|
Tim Waugh |
ff7a53 |
+#ifdef MBS_SUPPORT
|
|
Tim Waugh |
ff7a53 |
+ /* Advance to next character. For MB_CUR_MAX == 1 case this is handled
|
|
Tim Waugh |
ff7a53 |
+ by ++beg above. */
|
|
Tim Waugh |
ff7a53 |
+ if (mb_cur_max > 1)
|
|
Tim Waugh |
ff7a53 |
+ {
|
|
Tim Waugh |
ff7a53 |
+ if (using_utf8)
|
|
Tim Waugh |
ff7a53 |
+ {
|
|
Tim Waugh |
ff7a53 |
+ unsigned char c = *beg;
|
|
Tim Waugh |
ff7a53 |
+ if (c >= 0xc2)
|
|
Tim Waugh |
ff7a53 |
+ {
|
|
Tim Waugh |
ff7a53 |
+ if (c < 0xe0)
|
|
Tim Waugh |
ff7a53 |
+ ++beg;
|
|
Tim Waugh |
ff7a53 |
+ else if (c < 0xf0)
|
|
Tim Waugh |
ff7a53 |
+ beg += 2;
|
|
Tim Waugh |
ff7a53 |
+ else if (c < 0xf8)
|
|
Tim Waugh |
ff7a53 |
+ beg += 3;
|
|
Tim Waugh |
ff7a53 |
+ else if (c < 0xfc)
|
|
Tim Waugh |
ff7a53 |
+ beg += 4;
|
|
Tim Waugh |
ff7a53 |
+ else if (c < 0xfe)
|
|
Tim Waugh |
ff7a53 |
+ beg += 5;
|
|
Tim Waugh |
ff7a53 |
+ }
|
|
Tim Waugh |
ff7a53 |
+ }
|
|
Tim Waugh |
ff7a53 |
+ else
|
|
Tim Waugh |
ff7a53 |
+ {
|
|
Tim Waugh |
ff7a53 |
+ size_t l = mbrlen (beg, buf + size - beg, &mbs);
|
|
Lubomir Rintel |
0efa5e |
|
|
Lubomir Rintel |
0efa5e |
- failure:
|
|
Lubomir Rintel |
0efa5e |
- ret_val = -1;
|
|
Lubomir Rintel |
0efa5e |
- goto out;
|
|
Tim Waugh |
ff7a53 |
+ last_char = beg;
|
|
Tim Waugh |
ff7a53 |
+ if (l + 2 >= 2)
|
|
Tim Waugh |
ff7a53 |
+ beg += l - 1;
|
|
Tim Waugh |
ff7a53 |
+ else
|
|
Tim Waugh |
ff7a53 |
+ memset (&mbs, '\0', sizeof (mbstate_t));
|
|
Tim Waugh |
ff7a53 |
+ }
|
|
Tim Waugh |
ff7a53 |
+ }
|
|
Tim Waugh |
ff7a53 |
+#endif /* MBS_SUPPORT */
|
|
Lubomir Rintel |
0efa5e |
+ }
|
|
Tim Waugh |
c3fd6f |
+
|
|
Lubomir Rintel |
0efa5e |
+ return -1;
|
|
Lubomir Rintel |
0efa5e |
|
|
Lubomir Rintel |
0efa5e |
success:
|
|
Lubomir Rintel |
0efa5e |
+#ifdef MBS_SUPPORT
|
|
Tim Waugh |
ff7a53 |
+ if (mb_cur_max > 1 && !using_utf8)
|
|
Lubomir Rintel |
0efa5e |
+ {
|
|
Tim Waugh |
c3fd6f |
+ end = beg + len;
|
|
Tim Waugh |
c3fd6f |
+ while (end < buf + size)
|
|
Tim Waugh |
e1ca94 |
+ {
|
|
Tim Waugh |
b077d6 |
+ size_t mlen = mbrlen (end, buf + size - end, &mbs);
|
|
Tim Waugh |
b077d6 |
+ if (mlen == (size_t) -1 || mlen == (size_t) -2 || mlen == 0)
|
|
Tim Waugh |
c3fd6f |
+ {
|
|
Tim Waugh |
c3fd6f |
+ memset (&mbs, '\0', sizeof (mbstate_t));
|
|
Tim Waugh |
b077d6 |
+ mlen = 1;
|
|
Tim Waugh |
c3fd6f |
+ }
|
|
Tim Waugh |
b077d6 |
+ if (mlen == 1 && *end == eol)
|
|
Tim Waugh |
c3fd6f |
+ break;
|
|
Tim Waugh |
e1ca94 |
+
|
|
Tim Waugh |
b077d6 |
+ end += mlen;
|
|
Tim Waugh |
c3fd6f |
+ }
|
|
Lubomir Rintel |
0efa5e |
+ }
|
|
Tim Waugh |
c3fd6f |
+ else
|
|
Lubomir Rintel |
0efa5e |
+ #endif /* MBS_SUPPORT */
|
|
Tim Waugh |
e1ca94 |
end = memchr (beg + len, eol, (buf + size) - (beg + len));
|
|
Tim Waugh |
e1ca94 |
end++;
|
|
Tim Waugh |
e1ca94 |
while (buf < beg && beg[-1] != eol)
|
|
Lubomir Rintel |
0efa5e |
@@ -591,15 +1010,6 @@ EXECUTE_FCT(Fexecute)
|
|
Tim Waugh |
e1ca94 |
*match_size = len;
|
|
Lubomir Rintel |
0efa5e |
ret_val = beg - buf;
|
|
Lubomir Rintel |
0efa5e |
out:
|
|
Tim Waugh |
e1ca94 |
-#ifdef MBS_SUPPORT
|
|
Tim Waugh |
e1ca94 |
- if (MB_CUR_MAX > 1)
|
|
Tim Waugh |
e1ca94 |
- {
|
|
Tim Waugh |
e1ca94 |
- if (match_icase)
|
|
Lubomir Rintel |
0efa5e |
- free((char*)buf);
|
|
Lubomir Rintel |
0efa5e |
- if (mb_properties)
|
|
Lubomir Rintel |
0efa5e |
- free(mb_properties);
|
|
Tim Waugh |
e1ca94 |
- }
|
|
Tim Waugh |
e1ca94 |
-#endif /* MBS_SUPPORT */
|
|
Lubomir Rintel |
0efa5e |
return ret_val;
|
|
Tim Waugh |
e1ca94 |
}
|
|
Lubomir Rintel |
0efa5e |
#endif /* defined(GREP_PROGRAM) || defined(FGREP_PROGRAM) */
|
|
Lubomir Rintel |
0efa5e |
--
|
|
Lubomir Rintel |
0efa5e |
1.5.5.1
|
|
Lubomir Rintel |
0efa5e |
|