1 /* Locale dependent memory area transformation for comparison.
2 Copyright (C) 2009 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 memxfrm (char *s, size_t n, size_t *lengthp)
30 /* Result accumulator. */
37 /* Initial memory allocation. */
38 allocated = (n > 0 ? n : 1);
39 result = (char *) malloc (allocated);
44 /* Add sentinel.byte. */
48 /* Iterate through S, transforming each NUL terminated segment.
49 Accumulate the resulting transformed segments in result, separated by
52 const char *p_end = s + n + 1;
58 /* Search next NUL byte. */
59 const char *q = p + strlen (p);
66 k = strxfrm (result + length, p, allocated - length);
69 if (k >= allocated - length)
71 /* Grow the result buffer. */
74 allocated = 2 * allocated;
75 new_result = (char *) realloc (result, allocated);
76 if (new_result == NULL)
90 result[length] = '\0';
95 /* Shrink the allocated memory if possible. */
96 if ((length > 0 ? length : 1) < allocated)
98 char *memory = (char *) realloc (result, length > 0 ? length : 1);
103 s[n] = orig_sentinel;
109 int saved_errno = errno;
111 s[n] = orig_sentinel;
118 s[n] = orig_sentinel;