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/>. */
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"
34 astrxfrm (const char *s
, char *resultbuf
, size_t *lengthp
)
37 char *result
; /* either == resultbuf or == tmpbuf or freshly allocated
39 size_t allocated
; /* number of bytes allocated at result */
42 if (resultbuf
!= NULL
)
54 size_t l
= strlen (s
);
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
62 if (3 * l
+ 1 > allocated
)
64 /* Grow the result buffer. */
65 if (3 * l
+ 1 <= sizeof (tmpbuf
))
68 allocated
= sizeof (tmpbuf
);
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
;
88 k
= strxfrm (result
, s
, allocated
);
93 /* Grow the result buffer. */
94 if (result
!= resultbuf
&& result
!= tmpbuf
)
96 if (k
+ 1 <= sizeof (tmpbuf
))
99 allocated
= sizeof (tmpbuf
);
103 size_t new_allocated
;
106 new_allocated
= k
+ 1;
107 new_result
= (char *) malloc (new_allocated
);
108 if (new_result
== NULL
)
110 allocated
= new_allocated
;
113 /* Here k < allocated. */
117 if (strxfrm (result
, s
, allocated
) != k
)
118 /* strxfrm() is not producing reproducible results. */
124 /* Verify that strxfrm() has NUL-terminated the result. */
125 if (result
[k
] != '\0')
130 /* Here length > 0. */
132 if (result
== tmpbuf
)
134 if (resultbuf
!= NULL
&& length
<= *lengthp
)
136 memcpy (resultbuf
, result
, length
);
141 char *memory
= (char *) malloc (length
);
145 memcpy (memory
, result
, length
);
151 /* Shrink the allocated memory if possible. */
152 if (result
!= resultbuf
&& length
< allocated
)
154 if (length
<= *lengthp
)
156 memcpy (resultbuf
, result
, length
);
162 char *memory
= (char *) realloc (result
, length
);
173 if (result
!= resultbuf
&& result
!= tmpbuf
)