Date: Fri, 16 Sep 2011 12:13:04 +0000 (UTC) From: Gabor Kovesdan <gabor@FreeBSD.org> To: src-committers@freebsd.org, svn-src-user@freebsd.org Subject: svn commit: r225616 - user/gabor/tre-integration/contrib/tre/lib Message-ID: <201109161213.p8GCD4F9021600@svn.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: gabor Date: Fri Sep 16 12:13:03 2011 New Revision: 225616 URL: http://svn.freebsd.org/changeset/base/225616 Log: - Extract duplicated macro into a function to avoid duplication Modified: user/gabor/tre-integration/contrib/tre/lib/fastmatch.c user/gabor/tre-integration/contrib/tre/lib/regcomp.c user/gabor/tre-integration/contrib/tre/lib/tre-compile.c user/gabor/tre-integration/contrib/tre/lib/tre-internal.h Modified: user/gabor/tre-integration/contrib/tre/lib/fastmatch.c ============================================================================== --- user/gabor/tre-integration/contrib/tre/lib/fastmatch.c Fri Sep 16 12:12:26 2011 (r225615) +++ user/gabor/tre-integration/contrib/tre/lib/fastmatch.c Fri Sep 16 12:13:03 2011 (r225616) @@ -38,63 +38,6 @@ #include "tre-internal.h" #include "xmalloc.h" -/* XXX: avoid duplication */ -#define CONV_PAT \ - { \ - wregex = xmalloc(sizeof(tre_char_t) * (n + 1)); \ - if (wregex == NULL) \ - return REG_ESPACE; \ - \ - if (TRE_MB_CUR_MAX == 1) \ - { \ - unsigned int i; \ - const unsigned char *str = (const unsigned char *)regex; \ - tre_char_t *wstr = wregex; \ - \ - for (i = 0; i < n; i++) \ - *(wstr++) = *(str++); \ - wlen = n; \ - } \ - else \ - { \ - int consumed; \ - tre_char_t *wcptr = wregex; \ - mbstate_t state; \ - memset(&state, '\0', sizeof(state)); \ - while (n > 0) \ - { \ - consumed = tre_mbrtowc(wcptr, regex, n, &state); \ - \ - switch (consumed) \ - { \ - case 0: \ - if (*regex == '\0') \ - consumed = 1; \ - else \ - { \ - xfree(wregex); \ - return REG_BADPAT; \ - } \ - break; \ - case -1: \ - DPRINT(("mbrtowc: error %d: %s.\n", errno, \ - strerror(errno))); \ - xfree(wregex); \ - return REG_BADPAT; \ - case -2: \ - consumed = n; \ - break; \ - } \ - regex += consumed; \ - n -= consumed; \ - wcptr++; \ - } \ - wlen = wcptr - wregex; \ - } \ - \ - wregex[wlen] = L'\0'; \ - } - int tre_fixncomp(fastmatch_t *preg, const char *regex, size_t n, int cflags) { @@ -103,14 +46,17 @@ tre_fixncomp(fastmatch_t *preg, const ch size_t wlen; if (n != 0) - CONV_PAT + { + ret = tre_convert_pattern(regex, n, &wregex, &wlen); + if (ret != REG_OK) + return ret; + else + ret = tre_compile_literal(preg, wregex, wlen, cflags); + tre_free_pattern(wregex); + return ret; + } else return tre_compile_literal(preg, NULL, 0, cflags); - - ret = tre_compile_literal(preg, wregex, wlen, cflags); - xfree(wregex); - - return ret; } int @@ -121,16 +67,19 @@ tre_fastncomp(fastmatch_t *preg, const c size_t wlen; if (n != 0) - CONV_PAT + { + ret = tre_convert_pattern(regex, n, &wregex, &wlen); + if (ret != REG_OK) + return ret; + else + ret = (cflags & REG_LITERAL) + ? tre_compile_literal(preg, wregex, wlen, cflags) + : tre_compile_fast(preg, wregex, wlen, cflags); + tre_free_pattern(wregex); + return ret; + } else return tre_compile_literal(preg, NULL, 0, cflags); - - ret = (cflags & REG_LITERAL) ? - tre_compile_literal(preg, wregex, wlen, cflags) : - tre_compile_fast(preg, wregex, wlen, cflags); - xfree(wregex); - - return ret; } Modified: user/gabor/tre-integration/contrib/tre/lib/regcomp.c ============================================================================== --- user/gabor/tre-integration/contrib/tre/lib/regcomp.c Fri Sep 16 12:12:26 2011 (r225615) +++ user/gabor/tre-integration/contrib/tre/lib/regcomp.c Fri Sep 16 12:13:03 2011 (r225616) @@ -32,80 +32,15 @@ int tre_regncomp(regex_t *preg, const char *regex, size_t n, int cflags) { int ret; -#if TRE_WCHAR tre_char_t *wregex; size_t wlen; - wregex = xmalloc(sizeof(tre_char_t) * (n + 1)); - if (wregex == NULL) - return REG_ESPACE; - - /* If the current locale uses the standard single byte encoding of - characters, we don't do a multibyte string conversion. If we did, - many applications which use the default locale would break since - the default "C" locale uses the 7-bit ASCII character set, and - all characters with the eighth bit set would be considered invalid. */ -#if TRE_MULTIBYTE - if (TRE_MB_CUR_MAX == 1) -#endif /* TRE_MULTIBYTE */ - { - unsigned int i; - const unsigned char *str = (const unsigned char *)regex; - tre_char_t *wstr = wregex; - - for (i = 0; i < n; i++) - *(wstr++) = *(str++); - wlen = n; - } -#if TRE_MULTIBYTE + ret = tre_convert_pattern(regex, n, &wregex, &wlen); + if (ret != REG_OK) + return ret; else - { - int consumed; - tre_char_t *wcptr = wregex; -#ifdef HAVE_MBSTATE_T - mbstate_t state; - memset(&state, '\0', sizeof(state)); -#endif /* HAVE_MBSTATE_T */ - while (n > 0) - { - consumed = tre_mbrtowc(wcptr, regex, n, &state); - - switch (consumed) - { - case 0: - if (*regex == '\0') - consumed = 1; - else - { - xfree(wregex); - return REG_BADPAT; - } - break; - case -1: - DPRINT(("mbrtowc: error %d: %s.\n", errno, strerror(errno))); - xfree(wregex); - return REG_BADPAT; - case -2: - /* The last character wasn't complete. Let's not call it a - fatal error. */ - consumed = n; - break; - } - regex += consumed; - n -= consumed; - wcptr++; - } - wlen = wcptr - wregex; - } -#endif /* TRE_MULTIBYTE */ - - wregex[wlen] = L'\0'; - ret = tre_compile(preg, wregex, wlen, cflags); - xfree(wregex); -#else /* !TRE_WCHAR */ - ret = tre_compile(preg, (const tre_char_t *)regex, n, cflags); -#endif /* !TRE_WCHAR */ - + ret = tre_compile(preg, wregex, wlen, cflags); + tre_free_pattern(wregex); return ret; } Modified: user/gabor/tre-integration/contrib/tre/lib/tre-compile.c ============================================================================== --- user/gabor/tre-integration/contrib/tre/lib/tre-compile.c Fri Sep 16 12:12:26 2011 (r225615) +++ user/gabor/tre-integration/contrib/tre/lib/tre-compile.c Fri Sep 16 12:13:03 2011 (r225616) @@ -1842,6 +1842,96 @@ tre_ast_to_tnfa(tre_ast_node_t *node, tr return errcode; } +int +tre_convert_pattern(const char *regex, size_t n, tre_char_t **w, + size_t *wn) +{ +#if TRE_WCHAR + tre_char_t *wregex; + size_t wlen; + + wregex = xmalloc(sizeof(tre_char_t) * (n + 1)); + if (wregex == NULL) + return REG_ESPACE; + + /* If the current locale uses the standard single byte encoding of + characters, we don't do a multibyte string conversion. If we did, + many applications which use the default locale would break since + the default "C" locale uses the 7-bit ASCII character set, and + all characters with the eighth bit set would be considered invalid. */ +#if TRE_MULTIBYTE + if (TRE_MB_CUR_MAX == 1) +#endif /* TRE_MULTIBYTE */ + { + unsigned int i; + const unsigned char *str = (const unsigned char *)regex; + tre_char_t *wstr = wregex; + + for (i = 0; i < n; i++) + *(wstr++) = *(str++); + wlen = n; + } +#if TRE_MULTIBYTE + else + { + int consumed; + tre_char_t *wcptr = wregex; +#ifdef HAVE_MBSTATE_T + mbstate_t state; + memset(&state, '\0', sizeof(state)); +#endif /* HAVE_MBSTATE_T */ + while (n > 0) + { + consumed = tre_mbrtowc(wcptr, regex, n, &state); + + switch (consumed) + { + case 0: + if (*regex == '\0') + consumed = 1; + else + { + xfree(wregex); + return REG_BADPAT; + } + break; + case -1: + DPRINT(("mbrtowc: error %d: %s.\n", errno, strerror(errno))); + xfree(wregex); + return REG_BADPAT; + case -2: + /* The last character wasn't complete. Let's not call it a + fatal error. */ + consumed = n; + break; + } + regex += consumed; + n -= consumed; + wcptr++; + } + wlen = wcptr - wregex; + } +#endif /* TRE_MULTIBYTE */ + wregex[wlen] = L'\0'; + *w = wregex; + *wn = n; + return REG_OK; +#else /* !TRE_WCHAR */ + { + *w = (tre_char_t * const *)regex; + *wn = n; + return REG_OK; + } +#endif /* !TRE_WCHAR */ +} + +void +tre_free_pattern(tre_char_t *wregex) +{ +#if TRE_WCHAR + xfree(wregex); +#endif +} #define ERROR_EXIT(err) \ do \ Modified: user/gabor/tre-integration/contrib/tre/lib/tre-internal.h ============================================================================== --- user/gabor/tre-integration/contrib/tre/lib/tre-internal.h Fri Sep 16 12:12:26 2011 (r225615) +++ user/gabor/tre-integration/contrib/tre/lib/tre-internal.h Fri Sep 16 12:13:03 2011 (r225616) @@ -257,6 +257,13 @@ struct tnfa { }; int +tre_convert_pattern(const char *regex, size_t n, tre_char_t **w, + size_t *wn); + +void +tre_free_pattern(tre_char_t *wregex); + +int tre_compile(regex_t *preg, const tre_char_t *regex, size_t n, int cflags); void
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201109161213.p8GCD4F9021600>