--- ../../grep-2.5.orig/src/search.c 2001-04-19 05:42:14.000000000 +0200 +++ src/search.c 2003-04-24 16:25:22.000000000 +0200 @@ -147,6 +147,32 @@ are not singlebyte character nor the first byte of a multibyte character. Caller must free the array. */ static char* +check_multibyte_string_stateful(char const *buf, size_t size, char *mb_properties, int *from, int *to, mbstate_t *cur_state) +{ + int i; + int j; + + for (i = *from; i <= *to ;) + { + size_t mbclen; + mbclen = mbrlen(buf + i, size - i, cur_state); + + if (mbclen == (size_t) -1 || mbclen == (size_t) -2 || mbclen == 0) + { + /* An invalid sequence, or a truncated multibyte character. + We treat it as a singlebyte character. */ + mbclen = 1; + } + mb_properties[i] = mbclen; + for (j = i+1; j < i + mbclen; j++) + mb_properties[j] = 0; + i += mbclen; + } + *to = i-1; + + return mb_properties; +} +static char* check_multibyte_string(char const *buf, size_t size) { char *mb_properties = malloc(size); @@ -332,6 +358,14 @@ kwsmusts (); } +static int maxInt(int m, int n) { + return ((m>n)?m:n); +} + +static int minInt(int m, int n) { + return ((m 1 && kwset) - mb_properties = check_multibyte_string(buf, size); + if (MB_CUR_MAX > 1 && kwset) { + memset(&cur_state, 0, sizeof(mbstate_t)); + mb_properties = malloc(size); + startpos = 0; + stoppos = minInt(size-1, 100); + check_multibyte_string_stateful(buf, size, mb_properties, &startpos, &stoppos, &cur_state); + } #endif /* MBS_SUPPORT */ buflim = buf + size; @@ -373,8 +415,15 @@ end = memchr(beg, eol, buflim - beg); end++; #ifdef MBS_SUPPORT - if (MB_CUR_MAX > 1 && mb_properties[beg - buf] == 0) - continue; + if (MB_CUR_MAX > 1) { + if ((beg-buf) > stoppos) { + startpos = stoppos + 1; + stoppos = minInt(size-1, beg-buf + 100); + check_multibyte_string_stateful(buf, size, mb_properties, &startpos, &stoppos, &cur_state); + } + if (mb_properties[beg - buf] == 0) + continue; + } #endif while (beg > buf && beg[-1] != eol) --beg;