openat: don’t close (-1)
[gnulib.git] / lib / astrxfrm.c
blobf25cf7d4932149cf06dae213885a8271c60a023f
1 /* Locale dependent string transformation for comparison.
2 Copyright (C) 2010-2024 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2010.
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 "astrxfrm.h"
23 #include <errno.h>
24 #include <stdlib.h>
25 #include <string.h>
27 /* Avoid false GCC warning "function may return address of local variable"
28 regarding result and tmpbuf. */
29 #if _GL_GNUC_PREREQ (4, 8)
30 # pragma GCC diagnostic ignored "-Wreturn-local-addr"
31 #endif
33 char *
34 astrxfrm (const char *s, char *resultbuf, size_t *lengthp)
36 char tmpbuf[4000];
37 char *result; /* either == resultbuf or == tmpbuf or freshly allocated
38 or NULL. */
39 size_t allocated; /* number of bytes allocated at result */
40 size_t length;
42 if (resultbuf != NULL)
44 result = resultbuf;
45 allocated = *lengthp;
47 else
49 result = NULL;
50 allocated = 0;
54 size_t l = strlen (s);
55 size_t k;
57 /* A call to strxfrm costs about 20 times more than a call to strdup of
58 the result. Therefore it is worth to try to avoid calling strxfrm
59 more than once on a given string, by making enough room before calling
60 strxfrm. The size of the strxfrm result, k, is likely to be between
61 l and 3 * l. */
62 if (3 * l + 1 > allocated)
64 /* Grow the result buffer. */
65 if (3 * l + 1 <= sizeof (tmpbuf))
67 result = tmpbuf;
68 allocated = sizeof (tmpbuf);
70 else
72 size_t new_allocated;
73 char *new_result;
75 new_allocated = 3 * l + 1;
76 if (new_allocated < 2 * allocated)
77 new_allocated = 2 * allocated;
78 new_result = (char *) malloc (new_allocated);
79 if (new_result != NULL)
81 allocated = new_allocated;
82 result = new_result;
87 errno = 0;
88 k = strxfrm (result, s, allocated);
89 if (errno != 0)
90 goto fail;
91 if (k >= allocated)
93 /* Grow the result buffer. */
94 if (result != resultbuf && result != tmpbuf)
95 free (result);
96 if (k + 1 <= sizeof (tmpbuf))
98 result = tmpbuf;
99 allocated = sizeof (tmpbuf);
101 else
103 size_t new_allocated;
104 char *new_result;
106 new_allocated = k + 1;
107 new_result = (char *) malloc (new_allocated);
108 if (new_result == NULL)
109 goto out_of_memory;
110 allocated = new_allocated;
111 result = new_result;
113 /* Here k < allocated. */
115 /* Try again. */
116 errno = 0;
117 if (strxfrm (result, s, allocated) != k)
118 /* strxfrm() is not producing reproducible results. */
119 abort ();
120 if (errno != 0)
121 goto fail;
124 /* Verify that strxfrm() has NUL-terminated the result. */
125 if (result[k] != '\0')
126 abort ();
127 length = k + 1;
130 /* Here length > 0. */
132 if (result == tmpbuf)
134 if (resultbuf != NULL && length <= *lengthp)
136 memcpy (resultbuf, result, length);
137 result = resultbuf;
139 else
141 char *memory = (char *) malloc (length);
143 if (memory == NULL)
144 goto out_of_memory;
145 memcpy (memory, result, length);
146 result = memory;
149 else
151 /* Shrink the allocated memory if possible. */
152 if (result != resultbuf && length < allocated)
154 if (length <= *lengthp)
156 memcpy (resultbuf, result, length);
157 free (result);
158 result = resultbuf;
160 else
162 char *memory = (char *) realloc (result, length);
163 if (memory != NULL)
164 result = memory;
169 *lengthp = length;
170 return result;
172 fail:
173 if (result != resultbuf && result != tmpbuf)
174 free (result);
175 return NULL;
177 out_of_memory:
178 errno = ENOMEM;
179 return NULL;