1 /* searchutils.c - helper subroutines for grep's matchers.
2 Copyright 1992, 1998, 2000, 2007, 2009-2011 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
23 #define NCHAR (UCHAR_MAX + 1)
26 kwsinit (kwset_t *kwset)
28 static char trans[NCHAR];
37 for (i = 0; i < NCHAR; ++i)
38 trans[i] = tolower (i);
40 *kwset = kwsalloc (trans);
43 *kwset = kwsalloc (NULL);
50 /* Convert the *N-byte string, BEG, to lowercase, and write the
51 NUL-terminated result into malloc'd storage. Upon success, set *N
52 to the length (in bytes) of the resulting string (not including the
53 trailing NUL byte), and return a pointer to the lowercase string.
54 Upon memory allocation failure, this function exits.
55 Note that on input, *N must be larger than zero.
57 Note that while this function returns a pointer to malloc'd storage,
58 the caller must not free it, since this function retains a pointer
59 to the buffer and reuses it on any subsequent call. As a consequence,
60 this function is not thread-safe. */
62 mbtolower (const char *beg, size_t *n)
65 static size_t outalloc;
66 size_t outlen, mb_cur_max;
71 if (*n > outalloc || outalloc == 0)
73 outalloc = MAX(1, *n);
74 out = xrealloc (out, outalloc);
77 /* appease clang-2.6 */
82 memset (&is, 0, sizeof (is));
83 memset (&os, 0, sizeof (os));
86 mb_cur_max = MB_CUR_MAX;
92 size_t mbclen = mbrtowc(&wc, beg, end - beg, &is);
93 if (outlen + mb_cur_max >= outalloc)
95 out = x2nrealloc (out, &outalloc, 1);
99 if (mbclen == (size_t) -1 || mbclen == (size_t) -2 || mbclen == 0)
101 /* An invalid sequence, or a truncated multi-octet character.
102 We treat it as a single-octet character. */
105 memset (&is, 0, sizeof (is));
106 memset (&os, 0, sizeof (os));
111 mbclen = wcrtomb (p, towlower ((wint_t) wc), &os);
124 is_mb_middle (const char **good, const char *buf, const char *end,
127 const char *p = *good;
128 const char *prev = p;
131 /* TODO: can be optimized for UTF-8. */
132 memset(&cur_state, 0, sizeof(mbstate_t));
135 size_t mbclen = mbrlen(p, end - p, &cur_state);
137 /* Store the beginning of the previous complete multibyte character. */
138 if (mbclen != (size_t) -2)
141 if (mbclen == (size_t) -1 || mbclen == (size_t) -2 || mbclen == 0)
143 /* An invalid sequence, or a truncated multibyte character.
144 We treat it as a single byte character. */
146 memset(&cur_state, 0, sizeof cur_state);
157 return 0 < match_len && match_len < mbrlen (p, end - p, &cur_state);
159 #endif /* MBS_SUPPORT */