1 /* Locale dependent memory area transformation for comparison.
2 Copyright (C) 2009-2014 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2009.
5 This program is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Lesser General Public License as published
7 by the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28 amemxfrm (char *s, size_t n, char *resultbuf, size_t *lengthp)
30 /* Result accumulator. */
37 /* Initial memory allocation. */
38 if (resultbuf != NULL && *lengthp > 0)
45 allocated = (n > 0 ? n : 1);
46 result = (char *) malloc (allocated);
52 /* Add sentinel.byte. */
56 /* Iterate through S, transforming each NUL terminated segment.
57 Accumulate the resulting transformed segments in result, separated by
60 const char *p_end = s + n + 1;
66 /* Search next NUL byte. */
67 size_t l = strlen (p);
73 /* A call to strxfrm costs about 20 times more than a call to
74 strdup of the result. Therefore it is worth to try to avoid
75 calling strxfrm more than once on a given string, by making
76 enough room before calling strxfrm.
77 The size of the strxfrm result, k, is likely to be between
79 if (3 * l >= allocated - length)
81 /* Grow the result buffer. */
85 new_allocated = length + 3 * l + 1;
86 if (new_allocated < 2 * allocated)
87 new_allocated = 2 * allocated;
88 if (new_allocated < 64)
90 if (result == resultbuf)
91 new_result = (char *) malloc (new_allocated);
93 new_result = (char *) realloc (result, new_allocated);
94 if (new_result != NULL)
96 allocated = new_allocated;
102 k = strxfrm (result + length, p, allocated - length);
105 if (k >= allocated - length)
107 /* Grow the result buffer. */
108 size_t new_allocated;
111 new_allocated = length + k + 1;
112 if (new_allocated < 2 * allocated)
113 new_allocated = 2 * allocated;
114 if (new_allocated < 64)
116 if (result == resultbuf)
117 new_result = (char *) malloc (new_allocated);
119 new_result = (char *) realloc (result, new_allocated);
120 if (new_result == NULL)
121 goto out_of_memory_1;
122 allocated = new_allocated;
135 result[length] = '\0';
140 /* Shrink the allocated memory if possible.
141 It is not worth calling realloc when length + 1 == allocated; it would
142 save just one byte. */
143 if (result != resultbuf && length + 1 < allocated)
145 if ((length > 0 ? length : 1) <= *lengthp)
147 memcpy (resultbuf, result, length);
153 char *memory = (char *) realloc (result, length > 0 ? length : 1);
159 s[n] = orig_sentinel;
165 int saved_errno = errno;
166 if (result != resultbuf)
168 s[n] = orig_sentinel;
174 if (result != resultbuf)
176 s[n] = orig_sentinel;