poll tests: Avoid test failure on BSD and Solaris systems.
[gnulib.git] / lib / amemxfrm.c
blobfcdec9121b7011954a1350420682f4ce0f93ccbe
1 /* Locale dependent memory area transformation for comparison.
2 Copyright (C) 2009-2020 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 <https://www.gnu.org/licenses/>. */
18 #include <config.h>
20 /* Specification. */
21 #include "amemxfrm.h"
23 #include <errno.h>
24 #include <stdlib.h>
25 #include <string.h>
27 char *
28 amemxfrm (char *s, size_t n, char *resultbuf, size_t *lengthp)
30 /* Result accumulator. */
31 char *result;
32 size_t length;
33 size_t allocated;
35 char orig_sentinel;
37 /* Initial memory allocation. */
38 if (resultbuf != NULL && *lengthp > 0)
40 result = resultbuf;
41 allocated = *lengthp;
43 else
45 allocated = (n > 0 ? n : 1);
46 result = (char *) malloc (allocated);
47 if (result == NULL)
48 goto out_of_memory_2;
50 length = 0;
52 /* Add sentinel.byte. */
53 orig_sentinel = s[n];
54 s[n] = '\0';
56 /* Iterate through S, transforming each NUL terminated segment.
57 Accumulate the resulting transformed segments in result, separated by
58 NULs. */
60 const char *p_end = s + n + 1;
61 const char *p;
63 p = s;
64 for (;;)
66 /* Search next NUL byte. */
67 size_t l = strlen (p);
69 for (;;)
71 size_t k;
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
78 l and 3 * l. */
79 if (3 * l >= allocated - length)
81 /* Grow the result buffer. */
82 size_t new_allocated;
83 char *new_result;
85 new_allocated = length + 3 * l + 1;
86 if (new_allocated < 2 * allocated)
87 new_allocated = 2 * allocated;
88 if (new_allocated < 64)
89 new_allocated = 64;
90 if (result == resultbuf)
91 new_result = (char *) malloc (new_allocated);
92 else
93 new_result = (char *) realloc (result, new_allocated);
94 if (new_result != NULL)
96 allocated = new_allocated;
97 result = new_result;
101 errno = 0;
102 k = strxfrm (result + length, p, allocated - length);
103 if (errno != 0)
104 goto fail;
105 if (k >= allocated - length)
107 /* Grow the result buffer. */
108 size_t new_allocated;
109 char *new_result;
111 new_allocated = length + k + 1;
112 if (new_allocated < 2 * allocated)
113 new_allocated = 2 * allocated;
114 if (new_allocated < 64)
115 new_allocated = 64;
116 if (result == resultbuf)
117 new_result = (char *) malloc (new_allocated);
118 else
119 new_result = (char *) realloc (result, new_allocated);
120 if (new_result == NULL)
121 goto out_of_memory_1;
122 allocated = new_allocated;
123 result = new_result;
125 else
127 length += k;
128 break;
132 p = p + l + 1;
133 if (p == p_end)
134 break;
135 result[length] = '\0';
136 length++;
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);
148 free (result);
149 result = resultbuf;
151 else
153 char *memory = (char *) realloc (result, length > 0 ? length : 1);
154 if (memory != NULL)
155 result = memory;
159 s[n] = orig_sentinel;
160 *lengthp = length;
161 return result;
163 fail:
165 int saved_errno = errno;
166 if (result != resultbuf)
167 free (result);
168 s[n] = orig_sentinel;
169 errno = saved_errno;
170 return NULL;
173 out_of_memory_1:
174 if (result != resultbuf)
175 free (result);
176 s[n] = orig_sentinel;
177 out_of_memory_2:
178 errno = ENOMEM;
179 return NULL;