Commit 02757b7c 02757b7c737cb3072d4c42e1b96be33ea1dad708 by Sergey Poznyakoff

Update by gnulib-sync

1 parent 04014d13
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
15 along with this program; if not, write to the Free Software Foundation, 15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17 17
18 #if HAVE_CONFIG_H 18 #ifdef HAVE_CONFIG_H
19 # include <config.h> 19 # include <config.h>
20 #endif 20 #endif
21 21
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
17 17
18 /* written by Jim Meyering */ 18 /* written by Jim Meyering */
19 19
20 #if HAVE_CONFIG_H 20 #ifdef HAVE_CONFIG_H
21 # include <config.h> 21 # include <config.h>
22 #endif 22 #endif
23 #undef malloc 23 #undef malloc
......
...@@ -25,119 +25,10 @@ ...@@ -25,119 +25,10 @@
25 #include "strcase.h" 25 #include "strcase.h"
26 26
27 #include <ctype.h> 27 #include <ctype.h>
28 #include <limits.h>
28 29
29 #if HAVE_MBRTOWC 30 #if HAVE_MBRTOWC
30 31 # include "mbuiter.h"
31 #include "strnlen1.h"
32
33 /* Like mbiter.h, except it doesn't look at the entire string. */
34
35 #include "mbchar.h"
36
37 #include <assert.h>
38 #include <stdbool.h>
39 #include <stdlib.h>
40 #include <wchar.h>
41 #include <wctype.h>
42
43 struct mbiter_multi
44 {
45 bool at_end; /* true if the end of the string has been reached */
46 bool in_shift; /* true if next byte may not be interpreted as ASCII */
47 mbstate_t state; /* if in_shift: current shift state */
48 bool next_done; /* true if mbi_avail has already filled the following */
49 struct mbchar cur; /* the current character:
50 const char *cur.ptr pointer to current character
51 The following are only valid after mbi_avail.
52 size_t cur.bytes number of bytes of current character
53 bool cur.wc_valid true if wc is a valid wide character
54 wchar_t cur.wc if wc_valid: the current character
55 */
56 };
57
58 static inline void
59 mbiter_multi_next (struct mbiter_multi *iter)
60 {
61 if (iter->next_done)
62 return;
63 if (iter->in_shift)
64 goto with_shift;
65 /* Handle most ASCII characters quickly, without calling mbrtowc(). */
66 if (is_basic (*iter->cur.ptr))
67 {
68 /* These characters are part of the basic character set. ISO C 99
69 guarantees that their wide character code is identical to their
70 char code. */
71 iter->cur.bytes = 1;
72 iter->cur.wc = *iter->cur.ptr;
73 iter->cur.wc_valid = true;
74 }
75 else
76 {
77 assert (mbsinit (&iter->state));
78 iter->in_shift = true;
79 with_shift:
80 iter->cur.bytes = mbrtowc (&iter->cur.wc, iter->cur.ptr,
81 strnlen1 (iter->cur.ptr, MB_CUR_MAX),
82 &iter->state);
83 if (iter->cur.bytes == (size_t) -1)
84 {
85 /* An invalid multibyte sequence was encountered. */
86 iter->cur.bytes = 1;
87 iter->cur.wc_valid = false;
88 /* Whether to set iter->in_shift = false and reset iter->state
89 or not is not very important; the string is bogus anyway. */
90 }
91 else if (iter->cur.bytes == (size_t) -2)
92 {
93 /* An incomplete multibyte character at the end. */
94 iter->cur.bytes = strlen (iter->cur.ptr) + 1;
95 iter->cur.wc_valid = false;
96 /* Whether to set iter->in_shift = false and reset iter->state
97 or not is not important; the string end is reached anyway. */
98 }
99 else
100 {
101 if (iter->cur.bytes == 0)
102 {
103 /* A null wide character was encountered. */
104 iter->cur.bytes = 1;
105 assert (*iter->cur.ptr == '\0');
106 assert (iter->cur.wc == 0);
107 }
108 iter->cur.wc_valid = true;
109
110 /* When in the initial state, we can go back treating ASCII
111 characters more quickly. */
112 if (mbsinit (&iter->state))
113 iter->in_shift = false;
114 }
115 }
116 iter->next_done = true;
117 }
118
119 static inline void
120 mbiter_multi_reloc (struct mbiter_multi *iter, ptrdiff_t ptrdiff)
121 {
122 iter->cur.ptr += ptrdiff;
123 }
124
125 /* Iteration macros. */
126 typedef struct mbiter_multi mbi_iterator_t;
127 #define mbi_init(iter, startptr) \
128 ((iter).cur.ptr = (startptr), (iter).at_end = false, \
129 (iter).in_shift = false, memset (&(iter).state, '\0', sizeof (mbstate_t)), \
130 (iter).next_done = false)
131 #define mbi_avail(iter) \
132 (!(iter).at_end && (mbiter_multi_next (&(iter)), true))
133 #define mbi_advance(iter) \
134 ((mb_isnul ((iter).cur) ? ((iter).at_end = true) : 0), \
135 (iter).cur.ptr += (iter).cur.bytes, (iter).next_done = false)
136
137 /* Access to the current character. */
138 #define mbi_cur(iter) (iter).cur
139 #define mbi_cur_ptr(iter) (iter).cur.ptr
140
141 #endif 32 #endif
142 33
143 #define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch)) 34 #define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
...@@ -159,59 +50,26 @@ strcasecmp (const char *s1, const char *s2) ...@@ -159,59 +50,26 @@ strcasecmp (const char *s1, const char *s2)
159 #if HAVE_MBRTOWC 50 #if HAVE_MBRTOWC
160 if (MB_CUR_MAX > 1) 51 if (MB_CUR_MAX > 1)
161 { 52 {
162 mbi_iterator_t iter1; 53 mbui_iterator_t iter1;
163 mbi_iterator_t iter2; 54 mbui_iterator_t iter2;
164 55
165 mbi_init (iter1, s1); 56 mbui_init (iter1, s1);
166 mbi_init (iter2, s2); 57 mbui_init (iter2, s2);
167 58
168 while (mbi_avail (iter1) && mbi_avail (iter2)) 59 while (mbui_avail (iter1) && mbui_avail (iter2))
169 { 60 {
170 /* Sort invalid characters after all valid ones. */ 61 int cmp = mb_casecmp (mbui_cur (iter1), mbui_cur (iter2));
171 if (!mbi_cur (iter1).wc_valid) 62
172 { 63 if (cmp != 0)
173 if (!mbi_cur (iter2).wc_valid) 64 return cmp;
174 { 65
175 /* Compare two invalid characters. */ 66 mbui_advance (iter1);
176 int cmp; 67 mbui_advance (iter2);
177
178 if (mbi_cur (iter1).bytes > mbi_cur (iter2).bytes)
179 return 1;
180 if (mbi_cur (iter1).bytes < mbi_cur (iter2).bytes)
181 return -1;
182 cmp = memcmp (mbi_cur_ptr (iter1), mbi_cur_ptr (iter2),
183 mbi_cur (iter1).bytes);
184 if (cmp != 0)
185 return cmp;
186 }
187 else
188 /* mbi_cur (iter1) invalid, mbi_cur (iter2) valid. */
189 return 1;
190 }
191 else
192 {
193 if (!mbi_cur (iter2).wc_valid)
194 /* mbi_cur (iter1) valid, mbi_cur (iter2) invalid. */
195 return -1;
196 else
197 {
198 /* Compare two valid characters. */
199 wchar_t c1 = towlower (mbi_cur (iter1).wc);
200 wchar_t c2 = towlower (mbi_cur (iter2).wc);
201
202 if (c1 > c2)
203 return 1;
204 if (c1 < c2)
205 return -1;
206 }
207 }
208 mbi_advance (iter1);
209 mbi_advance (iter2);
210 } 68 }
211 if (mbi_avail (iter1)) 69 if (mbui_avail (iter1))
212 /* s2 terminated before s1. */ 70 /* s2 terminated before s1. */
213 return 1; 71 return 1;
214 if (mbi_avail (iter2)) 72 if (mbui_avail (iter2))
215 /* s1 terminated before s2. */ 73 /* s1 terminated before s2. */
216 return -1; 74 return -1;
217 return 0; 75 return 0;
...@@ -236,6 +94,12 @@ strcasecmp (const char *s1, const char *s2) ...@@ -236,6 +94,12 @@ strcasecmp (const char *s1, const char *s2)
236 } 94 }
237 while (c1 == c2); 95 while (c1 == c2);
238 96
239 return c1 - c2; 97 if (UCHAR_MAX <= INT_MAX)
98 return c1 - c2;
99 else
100 /* On machines where 'char' and 'int' are types of the same size, the
101 difference of two 'unsigned char' values - including the sign bit -
102 doesn't fit in an 'int'. */
103 return (c1 > c2 ? 1 : c1 < c2 ? -1 : 0);
240 } 104 }
241 } 105 }
......
1 /* strncasecmp.c -- case insensitive string comparator 1 /* strncasecmp.c -- case insensitive string comparator
2 Copyright (C) 1998, 1999 Free Software Foundation, Inc. 2 Copyright (C) 1998, 1999, 2005 Free Software Foundation, Inc.
3 3
4 This program is free software; you can redistribute it and/or modify 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 5 it under the terms of the GNU General Public License as published by
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
15 along with this program; if not, write to the Free Software Foundation, 15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17 17
18 #if HAVE_CONFIG_H 18 #ifdef HAVE_CONFIG_H
19 # include <config.h> 19 # include <config.h>
20 #endif 20 #endif
21 21
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
23 #include "strcase.h" 23 #include "strcase.h"
24 24
25 #include <ctype.h> 25 #include <ctype.h>
26 #include <limits.h>
26 27
27 #define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch)) 28 #define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
28 29
...@@ -54,5 +55,11 @@ strncasecmp (const char *s1, const char *s2, size_t n) ...@@ -54,5 +55,11 @@ strncasecmp (const char *s1, const char *s2, size_t n)
54 } 55 }
55 while (c1 == c2); 56 while (c1 == c2);
56 57
57 return c1 - c2; 58 if (UCHAR_MAX <= INT_MAX)
59 return c1 - c2;
60 else
61 /* On machines where 'char' and 'int' are types of the same size, the
62 difference of two 'unsigned char' values - including the sign bit -
63 doesn't fit in an 'int'. */
64 return (c1 > c2 ? 1 : c1 < c2 ? -1 : 0);
58 } 65 }
......
...@@ -36,9 +36,6 @@ gt_FUNC_SETENV ...@@ -36,9 +36,6 @@ gt_FUNC_SETENV
36 # snprintf 36 # snprintf
37 gl_FUNC_SNPRINTF 37 gl_FUNC_SNPRINTF
38 38
39 # xstrtol
40 gl_XSTRTOL
41
42 # vasprintf 39 # vasprintf
43 gl_FUNC_VASPRINTF 40 gl_FUNC_VASPRINTF
44 41
...@@ -72,6 +69,9 @@ gl_MD5 ...@@ -72,6 +69,9 @@ gl_MD5
72 # glob 69 # glob
73 gl_GLOB 70 gl_GLOB
74 71
72 # inttostr
73 gl_INTTOSTR
74
75 # extensions 75 # extensions
76 dnl gl_USE_SYSTEM_EXTENSIONS must be added quite early to configure.ac. 76 dnl gl_USE_SYSTEM_EXTENSIONS must be added quite early to configure.ac.
77 77
......
...@@ -6,9 +6,9 @@ dnl with or without modifications, as long as this notice is preserved. ...@@ -6,9 +6,9 @@ dnl with or without modifications, as long as this notice is preserved.
6 6
7 AC_DEFUN([gl_XSTRTOL], 7 AC_DEFUN([gl_XSTRTOL],
8 [ 8 [
9 AC_LIBSOURCES([xstrtol.c, xstrtol.h, xstrtoul.c, intprops.h]) 9 MU_LIBSOURCES([xstrtol.c, xstrtol.h, xstrtoul.c, intprops.h])
10 AC_LIBOBJ([xstrtol]) 10 MU_LIBOBJ([xstrtol])
11 AC_LIBOBJ([xstrtoul]) 11 MU_LIBOBJ([xstrtoul])
12 12
13 AC_REQUIRE([gl_PREREQ_XSTRTOL]) 13 AC_REQUIRE([gl_PREREQ_XSTRTOL])
14 AC_REQUIRE([gl_PREREQ_XSTRTOUL]) 14 AC_REQUIRE([gl_PREREQ_XSTRTOUL])
......