From 3c0fb5745f66c8920ed4cfa8d3ead55216b15ec1 Mon Sep 17 00:00:00 2001 From: Ulrich Drepper Date: Wed, 12 Nov 2003 17:47:46 +0000 Subject: Update. 2003-11-12 Jakub Jelinek * io/ftw.c (NFTW_NEW_NAME, NFTW_OLD_NAME): Add prototypes. 2003-11-12 Jakub Jelinek * posix/tst-regex.c (umemlen): New variable. (test_expr): Add expectedicase argument. Test case insensitive searches as well as backwards searches (case sensitive and insensitive) too. (run_test): Add icase argument. Use it to compute regcomp flags. (run_test_backwards): New function. (main): Cast read to size_t to avoid warning. Set umemlen. Add expectedicase arguments to test_expr. * posix/regex_internal.c (re_string_reconstruct): If is_utf8, find previous character by walking back instead of converting all chars from beginning. 2003-11-12 Jakub Jelinek * posix/regex_internal.h (struct re_string_t): Add is_utf8 and mb_cur_max fields. (struct re_dfa_t): Likewise. Reorder fields to make structure smaller on 64-bit arches. (re_string_allocate, re_string_construct): Add mb_cur_max and is_utf8 arguments. (re_string_char_size_at, re_string_wchar_at): Use pstr->mb_cur_max instead of MB_CUR_MAX. * posix/regcomp.c (re_compile_fastmap_iter): Use dfa->mb_cur_max instead of MB_CUR_MAX. (re_compile_internal): Pass new arguments to re_string_construct. (init_dfa): Initialize mb_cur_max and is_utf8 fields. (peek_token, peek_token_bracket): Use input->mb_cur_max instead of MB_CUR_MAX. (parse_expression, parse_bracket_exp, parse_charclass_op): Use dfa->mb_cur_max instead of MB_CUR_MAX. * posix/regex_internal.c (re_string_construct_common): Add mb_cur_max and is_utf8 arguments. Initialize fields with them. (re_string_allocate, re_string_construct): Add mb_cur_max and is_utf8 arguments, pass them to re_string_construct_common. Use mb_cur_max instead of MB_CUR_MAX. (re_string_realloc_buffers): Use pstr->mb_cur_max instead of MB_CUR_MAX. (re_string_reconstruct): Likewise. (re_string_context_at): Use input->mb_cur_max instead of MB_CUR_MAX. (create_ci_newstate, create_cd_newstate): Use dfa->mb_cur_max instead of MB_CUR_MAX. * posix/regexec.c (re_search_internal): Likewise. Pass new arguments to re_string_allocate. (check_matching, transit_state_sb): Use dfa->mb_cur_max instead of MB_CUR_MAX. (extend_buffers): Use pstr->mb_cur_max instead of MB_CUR_MAX. 2003-11-12 Jakub Jelinek * posix/Makefile (tests): Add bug-regex19. (bug-regex19-ENV): Add LOCPATH. * posix/bug-regex19.c: New test. --- ChangeLog | 60 +++++++++++++++++++++ io/ftw.c | 4 ++ posix/Makefile | 4 +- posix/bug-regex19.c | 112 +++++++++++++++++++++++++++++++++++++++ posix/regcomp.c | 34 +++++++----- posix/regex_internal.c | 86 +++++++++++++++++++++--------- posix/regex_internal.h | 23 ++++---- posix/regexec.c | 13 ++--- posix/tst-regex.c | 141 ++++++++++++++++++++++++++++++++++++++++++++----- 9 files changed, 409 insertions(+), 68 deletions(-) create mode 100644 posix/bug-regex19.c diff --git a/ChangeLog b/ChangeLog index e4bc7e1228..6021fb6620 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,63 @@ +2003-11-12 Jakub Jelinek + + * io/ftw.c (NFTW_NEW_NAME, NFTW_OLD_NAME): Add prototypes. + +2003-11-12 Jakub Jelinek + + * posix/tst-regex.c (umemlen): New variable. + (test_expr): Add expectedicase argument. Test case insensitive + searches as well as backwards searches (case sensitive and + insensitive) too. + (run_test): Add icase argument. Use it to compute regcomp flags. + (run_test_backwards): New function. + (main): Cast read to size_t to avoid warning. Set umemlen. + Add expectedicase arguments to test_expr. + * posix/regex_internal.c (re_string_reconstruct): If is_utf8, + find previous character by walking back instead of converting + all chars from beginning. + +2003-11-12 Jakub Jelinek + + * posix/regex_internal.h (struct re_string_t): Add is_utf8 + and mb_cur_max fields. + (struct re_dfa_t): Likewise. Reorder fields to make structure + smaller on 64-bit arches. + (re_string_allocate, re_string_construct): Add mb_cur_max and + is_utf8 arguments. + (re_string_char_size_at, re_string_wchar_at): Use pstr->mb_cur_max + instead of MB_CUR_MAX. + * posix/regcomp.c (re_compile_fastmap_iter): Use dfa->mb_cur_max + instead of MB_CUR_MAX. + (re_compile_internal): Pass new arguments to re_string_construct. + (init_dfa): Initialize mb_cur_max and is_utf8 fields. + (peek_token, peek_token_bracket): Use input->mb_cur_max instead + of MB_CUR_MAX. + (parse_expression, parse_bracket_exp, parse_charclass_op): Use + dfa->mb_cur_max instead of MB_CUR_MAX. + * posix/regex_internal.c (re_string_construct_common): Add + mb_cur_max and is_utf8 arguments. Initialize fields with them. + (re_string_allocate, re_string_construct): Add mb_cur_max and + is_utf8 arguments, pass them to re_string_construct_common. + Use mb_cur_max instead of MB_CUR_MAX. + (re_string_realloc_buffers): Use pstr->mb_cur_max instead of + MB_CUR_MAX. + (re_string_reconstruct): Likewise. + (re_string_context_at): Use input->mb_cur_max instead of + MB_CUR_MAX. + (create_ci_newstate, create_cd_newstate): Use dfa->mb_cur_max + instead of MB_CUR_MAX. + * posix/regexec.c (re_search_internal): Likewise. + Pass new arguments to re_string_allocate. + (check_matching, transit_state_sb): Use dfa->mb_cur_max instead of + MB_CUR_MAX. + (extend_buffers): Use pstr->mb_cur_max instead of MB_CUR_MAX. + +2003-11-12 Jakub Jelinek + + * posix/Makefile (tests): Add bug-regex19. + (bug-regex19-ENV): Add LOCPATH. + * posix/bug-regex19.c: New test. + 2003-11-11 Jakub Jelinek * posix/regcomp.c (re_compile_fastmap_iter): Handle RE_ICASE diff --git a/io/ftw.c b/io/ftw.c index c4d2157bd9..20cf694e24 100644 --- a/io/ftw.c +++ b/io/ftw.c @@ -757,6 +757,8 @@ NFTW_NAME (path, func, descriptors, flags) #include +int NFTW_NEW_NAME (const char *, NFTW_FUNC_T, int, int); + int NFTW_NEW_NAME (path, func, descriptors, flags) const char *path; @@ -779,6 +781,8 @@ versioned_symbol (libc, NFTW_NEW_NAME, NFTW_NAME, GLIBC_2_3_3); /* Older nftw* version just ignored all unknown flags. */ +int NFTW_OLD_NAME (const char *, NFTW_FUNC_T, int, int); + int NFTW_OLD_NAME (path, func, descriptors, flags) const char *path; diff --git a/posix/Makefile b/posix/Makefile index 10329577b5..b20e1c314e 100644 --- a/posix/Makefile +++ b/posix/Makefile @@ -77,7 +77,8 @@ tests := tstgetopt testfnm runtests runptests \ tst-gnuglob tst-regex bug-regex5 bug-regex6 bug-regex7 \ bug-regex8 bug-regex9 bug-regex10 bug-regex11 bug-regex12 \ bug-regex13 bug-regex14 bug-regex15 bug-regex16 \ - bug-regex17 bug-regex18 tst-nice tst-nanosleep transbug + bug-regex17 bug-regex18 bug-regex19 \ + tst-nice tst-nanosleep transbug ifeq (yes,$(build-shared)) test-srcs := globtest tests += wordexp-test tst-exec tst-spawn @@ -155,6 +156,7 @@ bug-regex5-ENV = LOCPATH=$(common-objpfx)localedata bug-regex6-ENV = LOCPATH=$(common-objpfx)localedata bug-regex17-ENV = LOCPATH=$(common-objpfx)localedata bug-regex18-ENV = LOCPATH=$(common-objpfx)localedata +bug-regex19-ENV = LOCPATH=$(common-objpfx)localedata testcases.h: TESTS TESTS2C.sed sed -f TESTS2C.sed < $< > $@T diff --git a/posix/bug-regex19.c b/posix/bug-regex19.c new file mode 100644 index 0000000000..837ab654bc --- /dev/null +++ b/posix/bug-regex19.c @@ -0,0 +1,112 @@ +/* Regular expression tests. + Copyright (C) 2003 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Jakub Jelinek , 2003. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. */ + +#include +#include +#include +#include +#include +#include +#include + +static struct +{ + int syntax; + const char *pattern; + const char *string; + int start, res; +} tests[] = { + /* \xc3\x84 LATIN CAPITAL LETTER A WITH DIAERESIS + \xc3\x96 LATIN CAPITAL LETTER O WITH DIAERESIS + \xe2\x80\x94 EM DASH */ + /* Should not match. */ + {RE_SYNTAX_POSIX_BASIC, "\\", "aAAO", 1, -1}, + {RE_SYNTAX_POSIX_BASIC, "\\bA", "aOAA", 2, -1}, + {RE_SYNTAX_POSIX_BASIC, "A\\b", "aAAO", 1, -1}, + {RE_SYNTAX_POSIX_BASIC, "\\<\xc3\x84", "a\xc3\x96\xc3\x84\xc3\x84", 3, -1}, + {RE_SYNTAX_POSIX_BASIC, "\xc3\x84\\>", "a\xc3\x84\xc3\x84\xc3\x96", 1, -1}, +#if 0 + /* XXX Not used since they fail so far. */ + {RE_SYNTAX_POSIX_BASIC, "\\b\xc3\x84", "a\xc3\x96\xc3\x84\xc3\x84", 3, -1}, + {RE_SYNTAX_POSIX_BASIC, "\xc3\x84\\b", "a\xc3\x84\xc3\x84\xc3\x96", 1, -1}, +#endif + /* Should match. */ + {RE_SYNTAX_POSIX_BASIC, "\\", "aAA-", 1, 2}, + {RE_SYNTAX_POSIX_BASIC, "A\\>", "aAA", 1, 2}, + {RE_SYNTAX_POSIX_BASIC, "\\bA", "AA", 0, 0}, + {RE_SYNTAX_POSIX_BASIC, "\\bA", "a-AA", 2, 2}, + {RE_SYNTAX_POSIX_BASIC, "A\\b", "aAA-", 1, 2}, + {RE_SYNTAX_POSIX_BASIC, "A\\b", "aAA", 1, 2}, +#if 0 + /* XXX Not used since they fail so far. */ + {RE_SYNTAX_POSIX_BASIC, "\\<\xc3\x84", "\xc3\x84\xc3\x84", 0, 0}, + {RE_SYNTAX_POSIX_BASIC, "\\<\xc3\x84", "a\xe2\x80\x94\xc3\x84\xc3\x84", 4, 4}, + {RE_SYNTAX_POSIX_BASIC, "\xc3\x84\\>", "a\xc3\x84\xc3\x84\xe2\x80\x94", 1, 3}, + {RE_SYNTAX_POSIX_BASIC, "\xc3\x84\\>", "a\xc3\x84\xc3\x84", 1, 3}, + {RE_SYNTAX_POSIX_BASIC, "\\b\xc3\x84", "\xc3\x84\xc3\x84", 0, 0}, + {RE_SYNTAX_POSIX_BASIC, "\\b\xc3\x84", "a\xe2\x80\x94\xc3\x84\xc3\x84", 4, 4}, + {RE_SYNTAX_POSIX_BASIC, "\xc3\x84\\b", "a\xc3\x84\xc3\x84\xe2\x80\x94", 1, 3}, + {RE_SYNTAX_POSIX_BASIC, "\xc3\x84\\b", "a\xc3\x84\xc3\x84", 1, 3} +#endif +}; + +int +main (void) +{ + struct re_pattern_buffer regbuf; + const char *err; + size_t i; + int ret = 0; + + mtrace (); + + setlocale (LC_ALL, "de_DE.UTF-8"); + for (i = 0; i < sizeof (tests) / sizeof (tests[0]); ++i) + { + int res; + re_set_syntax (tests[i].syntax); + memset (®buf, '\0', sizeof (regbuf)); + err = re_compile_pattern (tests[i].pattern, strlen (tests[i].pattern), + ®buf); + if (err != NULL) + { + printf ("re_compile_pattern failed: %s\n", err); + ret = 1; + continue; + } + + res = re_search (®buf, tests[i].string, strlen (tests[i].string), + tests[i].start, + strlen (tests[i].string) - tests[i].start, NULL); + if (res != tests[i].res) + { + printf ("re_search %zd failed: %d\n", i, res); + ret = 1; + regfree (®buf); + continue; + } + regfree (®buf); + } + + return ret; +} diff --git a/posix/regcomp.c b/posix/regcomp.c index 2b850378b2..82d4bb1c57 100644 --- a/posix/regcomp.c +++ b/posix/regcomp.c @@ -312,7 +312,7 @@ re_compile_fastmap_iter (bufp, init_state, fastmap) { re_dfa_t *dfa = (re_dfa_t *) bufp->buffer; int node_cnt; - int icase = (MB_CUR_MAX == 1 && (bufp->syntax & RE_ICASE)); + int icase = (dfa->mb_cur_max == 1 && (bufp->syntax & RE_ICASE)); for (node_cnt = 0; node_cnt < init_state->nodes.nelem; ++node_cnt) { int node = init_state->nodes.elems[node_cnt]; @@ -324,7 +324,7 @@ re_compile_fastmap_iter (bufp, init_state, fastmap) #ifdef RE_ENABLE_I18N if ((bufp->syntax & RE_ICASE) && !icase) { - unsigned char *buf = alloca (MB_CUR_MAX), *p; + unsigned char *buf = alloca (dfa->mb_cur_max), *p; wchar_t wc; mbstate_t state; @@ -376,7 +376,7 @@ re_compile_fastmap_iter (bufp, init_state, fastmap) re_set_fastmap (fastmap, icase, ch); } # else - if (MB_CUR_MAX > 1) + if (dfa->mb_cur_max > 1) for (i = 0; i < SBC_MAX; ++i) if (__btowc (i) == WEOF) re_set_fastmap (fastmap, icase, i); @@ -744,7 +744,8 @@ re_compile_internal (preg, pattern, length, syntax) #endif err = re_string_construct (®exp, pattern, length, preg->translate, - syntax & RE_ICASE); + syntax & RE_ICASE, dfa->mb_cur_max, + dfa->is_utf8); if (BE (err != REG_NOERROR, 0)) { re_free (dfa); @@ -812,6 +813,13 @@ init_dfa (dfa, pat_len) dfa->subexps = re_malloc (re_subexp_t, dfa->subexps_alloc); dfa->word_char = NULL; + dfa->mb_cur_max = MB_CUR_MAX; +#ifdef _LIBC + if (dfa->mb_cur_max > 1 + && strcmp (_NL_CURRENT (LC_CTYPE, _NL_CTYPE_CODESET_NAME), "UTF-8") == 0) + dfa->is_utf8 = 1; +#endif + if (BE (dfa->nodes == NULL || dfa->state_table == NULL || dfa->subexps == NULL, 0)) { @@ -1522,7 +1530,7 @@ peek_token (token, input, syntax) #ifdef RE_ENABLE_I18N token->mb_partial = 0; - if (MB_CUR_MAX > 1 && + if (input->mb_cur_max > 1 && !re_string_first_byte (input, re_string_cur_idx (input))) { token->type = CHARACTER; @@ -1738,7 +1746,7 @@ peek_token_bracket (token, input, syntax) token->opr.c = c; #ifdef RE_ENABLE_I18N - if (MB_CUR_MAX > 1 && + if (input->mb_cur_max > 1 && !re_string_first_byte (input, re_string_cur_idx (input))) { token->type = CHARACTER; @@ -1976,7 +1984,7 @@ parse_expression (regexp, preg, token, syntax, nest, err) return NULL; } #ifdef RE_ENABLE_I18N - if (MB_CUR_MAX > 1) + if (dfa->mb_cur_max > 1) { while (!re_string_eoi (regexp) && !re_string_first_byte (regexp, re_string_cur_idx (regexp))) @@ -2111,7 +2119,7 @@ parse_expression (regexp, preg, token, syntax, nest, err) *err = REG_ESPACE; return NULL; } - if (MB_CUR_MAX > 1) + if (dfa->mb_cur_max > 1) dfa->has_mb_node = 1; break; case OP_WORD: @@ -2883,7 +2891,7 @@ parse_bracket_exp (regexp, dfa, token, syntax, err) goto parse_bracket_exp_free_return; } #ifdef RE_ENABLE_I18N - if (MB_CUR_MAX > 1) + if (dfa->mb_cur_max > 1) for (i = 0; i < SBC_MAX; ++i) if (__btowc (i) == WEOF) bitset_set (sbcset, i); @@ -3042,8 +3050,8 @@ parse_bracket_exp (regexp, dfa, token, syntax, err) #ifdef RE_ENABLE_I18N if (mbcset->nmbchars || mbcset->ncoll_syms || mbcset->nequiv_classes - || mbcset->nranges || (MB_CUR_MAX > 1 && (mbcset->nchar_classes - || mbcset->non_match))) + || mbcset->nranges || (dfa->mb_cur_max > 1 && (mbcset->nchar_classes + || mbcset->non_match))) { re_token_t alt_token; bin_tree_t *mbc_tree; @@ -3377,7 +3385,7 @@ build_charclass_op (dfa, trans, class_name, extra, not, err) bitset_set(cset->sbcset, '\0'); */ mbcset->non_match = 1; - if (MB_CUR_MAX > 1) + if (dfa->mb_cur_max > 1) for (i = 0; i < SBC_MAX; ++i) if (__btowc (i) == WEOF) bitset_set (sbcset, i); @@ -3423,7 +3431,7 @@ build_charclass_op (dfa, trans, class_name, extra, not, err) goto build_word_op_espace; #ifdef RE_ENABLE_I18N - if (MB_CUR_MAX > 1) + if (dfa->mb_cur_max > 1) { re_token_t alt_token; bin_tree_t *mbc_tree; diff --git a/posix/regex_internal.c b/posix/regex_internal.c index e328243a57..329fc81de9 100644 --- a/posix/regex_internal.c +++ b/posix/regex_internal.c @@ -20,7 +20,8 @@ static void re_string_construct_common (const char *str, int len, re_string_t *pstr, - RE_TRANSLATE_TYPE trans, int icase); + RE_TRANSLATE_TYPE trans, int icase, + int mb_cur_max, int is_utf8); #ifdef RE_ENABLE_I18N static int re_string_skip_chars (re_string_t *pstr, int new_raw_idx, wint_t *last_wc); @@ -46,15 +47,17 @@ static unsigned int inline calc_state_hash (const re_node_set *nodes, re_string_reconstruct before using the object. */ static reg_errcode_t -re_string_allocate (pstr, str, len, init_len, trans, icase) +re_string_allocate (pstr, str, len, init_len, trans, icase, + mb_cur_max, is_utf8) re_string_t *pstr; const char *str; - int len, init_len, icase; + int len, init_len, icase, mb_cur_max, is_utf8; RE_TRANSLATE_TYPE trans; { reg_errcode_t ret; int init_buf_len = (len + 1 < init_len) ? len + 1: init_len; - re_string_construct_common (str, len, pstr, trans, icase); + re_string_construct_common (str, len, pstr, trans, icase, + mb_cur_max, is_utf8); pstr->stop = pstr->len; ret = re_string_realloc_buffers (pstr, init_buf_len); @@ -65,21 +68,22 @@ re_string_allocate (pstr, str, len, init_len, trans, icase) : (unsigned char *) str); pstr->mbs = MBS_ALLOCATED (pstr) ? pstr->mbs : pstr->mbs_case; pstr->valid_len = (MBS_CASE_ALLOCATED (pstr) || MBS_ALLOCATED (pstr) - || MB_CUR_MAX > 1) ? pstr->valid_len : len; + || mb_cur_max > 1) ? pstr->valid_len : len; return REG_NOERROR; } /* This function allocate the buffers, and initialize them. */ static reg_errcode_t -re_string_construct (pstr, str, len, trans, icase) +re_string_construct (pstr, str, len, trans, icase, mb_cur_max, is_utf8) re_string_t *pstr; const char *str; - int len, icase; + int len, icase, mb_cur_max, is_utf8; RE_TRANSLATE_TYPE trans; { reg_errcode_t ret; - re_string_construct_common (str, len, pstr, trans, icase); + re_string_construct_common (str, len, pstr, trans, icase, + mb_cur_max, is_utf8); pstr->stop = pstr->len; /* Set 0 so that this function can initialize whole buffers. */ pstr->valid_len = 0; @@ -97,7 +101,7 @@ re_string_construct (pstr, str, len, trans, icase) if (icase) { #ifdef RE_ENABLE_I18N - if (MB_CUR_MAX > 1) + if (mb_cur_max > 1) build_wcs_upper_buffer (pstr); else #endif /* RE_ENABLE_I18N */ @@ -106,7 +110,7 @@ re_string_construct (pstr, str, len, trans, icase) else { #ifdef RE_ENABLE_I18N - if (MB_CUR_MAX > 1) + if (mb_cur_max > 1) build_wcs_buffer (pstr); else #endif /* RE_ENABLE_I18N */ @@ -131,7 +135,7 @@ re_string_realloc_buffers (pstr, new_buf_len) int new_buf_len; { #ifdef RE_ENABLE_I18N - if (MB_CUR_MAX > 1) + if (pstr->mb_cur_max > 1) { wint_t *new_array = re_realloc (pstr->wcs, wint_t, new_buf_len); if (BE (new_array == NULL, 0)) @@ -163,18 +167,20 @@ re_string_realloc_buffers (pstr, new_buf_len) static void -re_string_construct_common (str, len, pstr, trans, icase) +re_string_construct_common (str, len, pstr, trans, icase, mb_cur_max, is_utf8) const char *str; int len; re_string_t *pstr; RE_TRANSLATE_TYPE trans; - int icase; + int icase, mb_cur_max, is_utf8; { memset (pstr, '\0', sizeof (re_string_t)); pstr->raw_mbs = (const unsigned char *) str; pstr->len = len; pstr->trans = trans; pstr->icase = icase ? 1 : 0; + pstr->mb_cur_max = mb_cur_max; + pstr->is_utf8 = is_utf8; } #ifdef RE_ENABLE_I18N @@ -372,7 +378,7 @@ re_string_translate_buffer (pstr) } /* This function re-construct the buffers. - Concretely, convert to wide character in case of MB_CUR_MAX > 1, + Concretely, convert to wide character in case of pstr->mb_cur_max > 1, convert to upper case in case of REG_ICASE, apply translation. */ static reg_errcode_t @@ -385,7 +391,7 @@ re_string_reconstruct (pstr, idx, eflags, newline) { /* Reset buffer. */ #ifdef RE_ENABLE_I18N - if (MB_CUR_MAX > 1) + if (pstr->mb_cur_max > 1) memset (&pstr->cur_state, '\0', sizeof (mbstate_t)); #endif /* RE_ENABLE_I18N */ pstr->len += pstr->raw_mbs_idx; @@ -409,7 +415,7 @@ re_string_reconstruct (pstr, idx, eflags, newline) pstr->tip_context = re_string_context_at (pstr, offset - 1, eflags, newline); #ifdef RE_ENABLE_I18N - if (MB_CUR_MAX > 1) + if (pstr->mb_cur_max > 1) memmove (pstr->wcs, pstr->wcs + offset, (pstr->valid_len - offset) * sizeof (wint_t)); #endif /* RE_ENABLE_I18N */ @@ -429,13 +435,43 @@ re_string_reconstruct (pstr, idx, eflags, newline) /* No, skip all characters until IDX. */ pstr->valid_len = 0; #ifdef RE_ENABLE_I18N - if (MB_CUR_MAX > 1) + if (pstr->mb_cur_max > 1) { int wcs_idx; - wint_t wc; - pstr->valid_len = re_string_skip_chars (pstr, idx, &wc) - idx; - for (wcs_idx = 0; wcs_idx < pstr->valid_len; ++wcs_idx) - pstr->wcs[wcs_idx] = WEOF; + wint_t wc = WEOF; + + if (pstr->is_utf8) + { + const unsigned char *raw, *p, *end; + + /* Special case UTF-8. Multi-byte chars start with any + byte other than 0x80 - 0xbf. */ + raw = pstr->raw_mbs + pstr->raw_mbs_idx; + end = raw + (pstr->valid_len > offset - pstr->mb_cur_max + ? pstr->valid_len : offset - pstr->mb_cur_max); + for (p = raw + offset - 1; p >= end; --p) + if ((*p & 0xc0) != 0x80) + { + mbstate_t cur_state; + wchar_t wc2; + + memset (&cur_state, 0, sizeof (cur_state)); + if (mbrtowc (&wc2, p, raw + offset - p, &cur_state) + == raw + offset - p) + { + memset (&pstr->cur_state, '\0', + sizeof (mbstate_t)); + wc = wc2; + } + break; + } + } + if (wc == WEOF) + { + pstr->valid_len = re_string_skip_chars (pstr, idx, &wc) - idx; + for (wcs_idx = 0; wcs_idx < pstr->valid_len; ++wcs_idx) + pstr->wcs[wcs_idx] = WEOF; + } if (pstr->trans && wc <= 0xff) wc = pstr->trans[wc]; pstr->tip_context = (IS_WIDE_WORD_CHAR (wc) ? CONTEXT_WORD @@ -467,7 +503,7 @@ re_string_reconstruct (pstr, idx, eflags, newline) /* Then build the buffers. */ #ifdef RE_ENABLE_I18N - if (MB_CUR_MAX > 1) + if (pstr->mb_cur_max > 1) { if (pstr->icase) build_wcs_upper_buffer (pstr); @@ -519,7 +555,7 @@ re_string_context_at (input, idx, eflags, newline_anchor) : CONTEXT_NEWLINE | CONTEXT_ENDBUF); } #ifdef RE_ENABLE_I18N - if (MB_CUR_MAX > 1) + if (input->mb_cur_max > 1) { wint_t wc; int wc_idx = idx; @@ -1157,7 +1193,7 @@ create_ci_newstate (dfa, nodes, hash) newstate->halt = 1; #ifdef RE_ENABLE_I18N else if (type == COMPLEX_BRACKET - || (type == OP_PERIOD && MB_CUR_MAX > 1)) + || (type == OP_PERIOD && dfa->mb_cur_max > 1)) newstate->accept_mb = 1; #endif /* RE_ENABLE_I18N */ else if (type == OP_BACK_REF) @@ -1208,7 +1244,7 @@ create_cd_newstate (dfa, nodes, context, hash) newstate->halt = 1; #ifdef RE_ENABLE_I18N else if (type == COMPLEX_BRACKET - || (type == OP_PERIOD && MB_CUR_MAX > 1)) + || (type == OP_PERIOD && dfa->mb_cur_max > 1)) newstate->accept_mb = 1; #endif /* RE_ENABLE_I18N */ else if (type == OP_BACK_REF) diff --git a/posix/regex_internal.h b/posix/regex_internal.h index 5e113d7c6a..40cf1f9b6a 100644 --- a/posix/regex_internal.h +++ b/posix/regex_internal.h @@ -334,6 +334,8 @@ struct re_string_t RE_TRANSLATE_TYPE trans; /* 1 if REG_ICASE. */ unsigned int icase : 1; + unsigned int is_utf8 : 1; + int mb_cur_max; }; typedef struct re_string_t re_string_t; /* In case of REG_ICASE, we allocate the buffer dynamically for mbs. */ @@ -345,10 +347,12 @@ typedef struct re_string_t re_string_t; static reg_errcode_t re_string_allocate (re_string_t *pstr, const char *str, int len, int init_len, - RE_TRANSLATE_TYPE trans, int icase); + RE_TRANSLATE_TYPE trans, int icase, + int mb_cur_max, int is_utf8); static reg_errcode_t re_string_construct (re_string_t *pstr, const char *str, int len, RE_TRANSLATE_TYPE trans, - int icase); + int icase, int mb_cur_max, + int is_utf8); static reg_errcode_t re_string_reconstruct (re_string_t *pstr, int idx, int eflags, int newline); static reg_errcode_t re_string_realloc_buffers (re_string_t *pstr, @@ -571,11 +575,7 @@ struct re_fail_stack_t struct re_dfa_t { re_bitset_ptr_t word_char; - - /* number of subexpressions `re_nsub' is in regex_t. */ - int subexps_alloc; re_subexp_t *subexps; - re_token_t *nodes; int nodes_alloc; int nodes_len; @@ -586,11 +586,14 @@ struct re_dfa_t re_node_set *eclosures; re_node_set *inveclosures; struct re_state_table_entry *state_table; - unsigned int state_hash_mask; re_dfastate_t *init_state; re_dfastate_t *init_state_word; re_dfastate_t *init_state_nl; re_dfastate_t *init_state_begbuf; + + /* number of subexpressions `re_nsub' is in regex_t. */ + int subexps_alloc; + unsigned int state_hash_mask; int states_alloc; int init_node; int nbackref; /* The number of backreference in this dfa. */ @@ -604,6 +607,8 @@ struct re_dfa_t a node which can accept multibyte character or multi character collating element. */ unsigned int has_mb_node : 1; + unsigned int is_utf8 : 1; + int mb_cur_max; }; typedef struct re_dfa_t re_dfa_t; @@ -700,7 +705,7 @@ re_string_char_size_at (pstr, idx) int idx; { int byte_idx; - if (MB_CUR_MAX == 1) + if (pstr->mb_cur_max == 1) return 1; for (byte_idx = 1; idx + byte_idx < pstr->valid_len; ++byte_idx) if (pstr->wcs[idx + byte_idx] != WEOF) @@ -713,7 +718,7 @@ re_string_wchar_at (pstr, idx) const re_string_t *pstr; int idx; { - if (MB_CUR_MAX == 1) + if (pstr->mb_cur_max == 1) return (wint_t) pstr->mbs[idx]; return (wint_t) pstr->wcs[idx]; } diff --git a/posix/regexec.c b/posix/regexec.c index 3cd8beaba1..e962275ba3 100644 --- a/posix/regexec.c +++ b/posix/regexec.c @@ -605,7 +605,8 @@ re_search_internal (preg, string, length, start, range, stop, nmatch, pmatch, fl_longest_match = (nmatch != 0 || dfa->nbackref); err = re_string_allocate (&input, string, length, dfa->nodes_len + 1, - preg->translate, preg->syntax & RE_ICASE); + preg->translate, preg->syntax & RE_ICASE, + dfa->mb_cur_max, dfa->is_utf8); if (BE (err != REG_NOERROR, 0)) goto free_return; input.stop = stop; @@ -643,7 +644,7 @@ re_search_internal (preg, string, length, start, range, stop, nmatch, pmatch, incr = (range < 0) ? -1 : 1; left_lim = (range < 0) ? start + range : start; right_lim = (range < 0) ? start : start + range; - sb = MB_CUR_MAX == 1; + sb = dfa->mb_cur_max == 1; fast_translate = sb || !(preg->syntax & RE_ICASE || preg->translate); for (;;) @@ -1018,7 +1019,7 @@ check_matching (preg, mctx, fl_search, fl_longest_match) /* Restart from initial state, since we are searching the point from where matching start. */ #ifdef RE_ENABLE_I18N - if (MB_CUR_MAX == 1 + if (dfa->mb_cur_max == 1 || re_string_first_byte (mctx->input, cur_str_idx)) #endif /* RE_ENABLE_I18N */ cur_state = acquire_init_state_context (&err, preg, mctx, @@ -2280,7 +2281,7 @@ transit_state_sb (err, preg, state, fl_search, mctx) { #ifdef RE_ENABLE_I18N int not_initial = 0; - if (MB_CUR_MAX > 1) + if (dfa->mb_cur_max > 1) for (node_cnt = 0; node_cnt < next_nodes.nelem; ++node_cnt) if (dfa->nodes[next_nodes.elems[node_cnt]].type == CHARACTER) { @@ -3772,7 +3773,7 @@ extend_buffers (mctx) if (pstr->icase) { #ifdef RE_ENABLE_I18N - if (MB_CUR_MAX > 1) + if (pstr->mb_cur_max > 1) build_wcs_upper_buffer (pstr); else #endif /* RE_ENABLE_I18N */ @@ -3781,7 +3782,7 @@ extend_buffers (mctx) else { #ifdef RE_ENABLE_I18N - if (MB_CUR_MAX > 1) + if (pstr->mb_cur_max > 1) build_wcs_buffer (pstr); else #endif /* RE_ENABLE_I18N */ diff --git a/posix/tst-regex.c b/posix/tst-regex.c index 3f42181dd7..adc2d8ab9a 100644 --- a/posix/tst-regex.c +++ b/posix/tst-regex.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2001 Free Software Foundation, Inc. +/* Copyright (C) 2001, 2003 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or @@ -44,10 +44,13 @@ static iconv_t cd; static char *mem; static char *umem; static size_t memlen; +static size_t umemlen; -static int test_expr (const char *expr, int expected); +static int test_expr (const char *expr, int expected, int expectedicase); static int run_test (const char *expr, const char *mem, size_t memlen, - int expected); + int icase, int expected); +static int run_test_backwards (const char *expr, const char *mem, + size_t memlen, int icase, int expected); int @@ -78,7 +81,7 @@ main (void) if (mem == NULL) error (EXIT_FAILURE, errno, "while allocating buffer"); - if (read (fd, mem, memlen) != memlen) + if ((size_t) read (fd, mem, memlen) != memlen) error (EXIT_FAILURE, 0, "cannot read entire file"); mem[memlen] = '\0'; @@ -102,6 +105,7 @@ main (void) outmem = umem; outlen = 2 * memlen - 1; iconv (cd, &inmem, &inlen, &outmem, &outlen); + umemlen = outmem - umem; if (inlen != 0) error (EXIT_FAILURE, errno, "cannot convert buffer"); @@ -116,11 +120,11 @@ main (void) /* Run the actual tests. All tests are run in a single-byte and a multi-byte locale. */ - result = test_expr ("[äáàâéèêíìîñöóòôüúùû]", 2); - result |= test_expr ("G.ran", 2); - result |= test_expr ("G.\\{1\\}ran", 2); - result |= test_expr ("G.*ran", 3); - result |= test_expr ("[äáàâ]", 0); + result = test_expr ("[äáàâéèêíìîñöóòôüúùû]", 2, 2); + result |= test_expr ("G.ran", 2, 3); + result |= test_expr ("G.\\{1\\}ran", 2, 3); + result |= test_expr ("G.*ran", 3, 44); + result |= test_expr ("[äáàâ]", 0, 0); /* Free the resources. */ free (umem); @@ -132,7 +136,7 @@ main (void) static int -test_expr (const char *expr, int expected) +test_expr (const char *expr, int expected, int expectedicase) { int result; char *inmem; @@ -146,7 +150,14 @@ test_expr (const char *expr, int expected) error (EXIT_FAILURE, 0, "cannot set locale de_DE.ISO-8859-1"); printf ("\nTest \"%s\" with 8-bit locale\n", expr); - result = run_test (expr, mem, memlen, expected); + result = run_test (expr, mem, memlen, 0, expected); + printf ("\nTest \"%s\" with 8-bit locale, case insensitive\n", expr); + result |= run_test (expr, mem, memlen, 1, expectedicase); + printf ("\nTest \"%s\" backwards with 8-bit locale\n", expr); + result |= run_test_backwards (expr, mem, memlen, 0, expected); + printf ("\nTest \"%s\" backwards with 8-bit locale, case insensitive\n", + expr); + result |= run_test_backwards (expr, mem, memlen, 1, expectedicase); /* Second test: search with an UTF-8 locale. */ if (setlocale (LC_ALL, "de_DE.UTF-8") == NULL) @@ -163,14 +174,22 @@ test_expr (const char *expr, int expected) /* Run the tests. */ printf ("\nTest \"%s\" with multi-byte locale\n", expr); - result |= run_test (uexpr, umem, 2 * memlen - outlen, expected); + result |= run_test (uexpr, umem, umemlen, 0, expected); + printf ("\nTest \"%s\" with multi-byte locale, case insensitive\n", expr); + result |= run_test (uexpr, umem, umemlen, 1, expectedicase); + printf ("\nTest \"%s\" backwards with multi-byte locale\n", expr); + result |= run_test_backwards (uexpr, umem, umemlen, 0, expected); + printf ("\nTest \"%s\" backwards with multi-byte locale, case insensitive\n", + expr); + result |= run_test_backwards (uexpr, umem, umemlen, 1, expectedicase); return result; } static int -run_test (const char *expr, const char *mem, size_t memlen, int expected) +run_test (const char *expr, const char *mem, size_t memlen, int icase, + int expected) { #ifdef _POSIX_CPUTIME struct timespec start; @@ -186,7 +205,7 @@ run_test (const char *expr, const char *mem, size_t memlen, int expected) use_clock = clock_gettime (cl, &start) == 0; #endif - err = regcomp (&re, expr, REG_NEWLINE); + err = regcomp (&re, expr, REG_NEWLINE | (icase ? REG_ICASE : 0)); if (err != REG_NOERROR) { char buf[200]; @@ -257,3 +276,97 @@ run_test (const char *expr, const char *mem, size_t memlen, int expected) expect. */ return cnt != expected; } + + +static int +run_test_backwards (const char *expr, const char *mem, size_t memlen, + int icase, int expected) +{ +#ifdef _POSIX_CPUTIME + struct timespec start; + struct timespec finish; +#endif + struct re_pattern_buffer re; + const char *err; + size_t offset; + int cnt; + +#ifdef _POSIX_CPUTIME + if (use_clock) + use_clock = clock_gettime (cl, &start) == 0; +#endif + + re_set_syntax ((RE_SYNTAX_POSIX_BASIC & ~RE_DOT_NEWLINE) + | RE_HAT_LISTS_NOT_NEWLINE + | (icase ? RE_ICASE : 0)); + + memset (&re, 0, sizeof (re)); + re.fastmap = malloc (256); + if (re.fastmap == NULL) + error (EXIT_FAILURE, errno, "cannot allocate fastmap"); + + err = re_compile_pattern (expr, strlen (expr), &re); + if (err != NULL) + error (EXIT_FAILURE, 0, "cannot compile expression: %s", err); + + if (re_compile_fastmap (&re)) + error (EXIT_FAILURE, 0, "couldn't compile fastmap"); + + cnt = 0; + offset = memlen; + assert (mem[memlen] == '\0'); + while (offset <= memlen) + { + int start; + const char *sp; + const char *ep; + + start = re_search (&re, mem, memlen, offset, -offset, NULL); + if (start == -1) + break; + + if (start == -2) + error (EXIT_FAILURE, 0, "internal error in re_search"); + + sp = mem + start; + while (sp > mem && sp[-1] != '\n') + --sp; + + ep = mem + start; + while (*ep != '\0' && *ep != '\n') + ++ep; + + printf ("match %d: \"%.*s\"\n", ++cnt, (int) (ep - sp), sp); + + offset = sp - 1 - mem; + } + + regfree (&re); + +#ifdef _POSIX_CPUTIME + if (use_clock) + { + use_clock = clock_gettime (cl, &finish) == 0; + if (use_clock) + { + if (finish.tv_nsec < start.tv_nsec) + { + finish.tv_nsec -= start.tv_nsec - 1000000000; + finish.tv_sec -= 1 + start.tv_sec; + } + else + { + finish.tv_nsec -= start.tv_nsec; + finish.tv_sec -= start.tv_sec; + } + + printf ("elapsed time: %ld.%09ld sec\n", + finish.tv_sec, finish.tv_nsec); + } + } +#endif + + /* Return an error if the number of matches found is not match we + expect. */ + return cnt != expected; +} -- cgit 1.4.1