Branch
Hash :
6ca831b0
Author :
Date :
2025-09-16T18:57:41
stringeq: prefer memeq to memcmp in other modules * lib/argmatch.c, lib/argmatch.h, lib/backupfile.c, lib/bcp47.c: * lib/boot-time.c, lib/csharpcomp.c, lib/csharpexec.c: * lib/file-has-acl.c, lib/gen-uni-tables.c, lib/get_ppid_of.c: * lib/get_progname_of.c, lib/getlogin_r.c, lib/getprogname.c: * lib/getumask.c, lib/isnan.c, lib/mbchar.h, lib/mem-hash-map.c: * lib/memcoll.c, lib/progname.c, lib/progreloc.c: * lib/pthread_sigmask.c, lib/quotearg.c, lib/readutmp.c: * lib/same.c, lib/signbitd.c, lib/signbitf.c, lib/signbitl.c: * lib/string-desc.c, lib/string.c, lib/string.in.h: * lib/unictype/3level.h, lib/unictype/3levelbit.h: * lib/uniname/uniname.c, lib/vc-mtime.c: Prefer memeq to memcmp when either will do. Do not make this change to files shared with glibc. Do not make the change to test files, at least not for now. * lib/gen-uni-tables.c (memeq): New static function, in same style. * modules/argmatch, modules/backupfile, modules/bcp47: * modules/boot-time, modules/csharpcomp, modules/csharpexec: * modules/file-has-acl: * modules/get_ppid_of, modules/get_progname_of: * modules/getlogin_r, modules/getprogname, modules/getumask: * modules/isnan, modules/mbchar, modules/mem-hash-map: * modules/memcoll, modules/progname, modules/pthread_sigmask: * modules/quotearg, modules/readutmp, modules/relocatable-prog: * modules/relocatable-prog-wrapper, modules/same, modules/signbit: * modules/string-desc, modules/stringeq, modules/uniname/uniname: * modules/vc-mtime: (Depends-on): Add stringeq.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
/* String descriptors.
Copyright (C) 2023-2025 Free Software Foundation, Inc.
This file is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
/* Written by Bruno Haible <bruno@clisp.org>, 2023. */
#include <config.h>
#define GL_STRING_DESC_INLINE _GL_EXTERN_INLINE
/* Specification and inline definitions. */
#include "string-desc.h"
#include <limits.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include "c-ctype.h"
#include "ialloc.h"
#include "full-write.h"
/* ==== Side-effect-free operations on string descriptors ==== */
/* Return true if A and B are equal. */
bool
_sd_equals (idx_t a_nbytes, const char *a_data,
idx_t b_nbytes, const char *b_data)
{
return (a_nbytes == b_nbytes
&& (a_nbytes == 0 || memeq (a_data, b_data, a_nbytes)));
}
bool
_sd_startswith (idx_t s_nbytes, const char *s_data,
idx_t prefix_nbytes, const char *prefix_data)
{
return (s_nbytes >= prefix_nbytes
&& (prefix_nbytes == 0
|| memeq (s_data, prefix_data, prefix_nbytes)));
}
bool
_sd_endswith (idx_t s_nbytes, const char *s_data,
idx_t suffix_nbytes, const char *suffix_data)
{
return (s_nbytes >= suffix_nbytes
&& (suffix_nbytes == 0
|| memeq (s_data + (s_nbytes - suffix_nbytes), suffix_data,
suffix_nbytes)));
}
int
_sd_cmp (idx_t a_nbytes, const char *a_data,
idx_t b_nbytes, const char *b_data)
{
if (a_nbytes > b_nbytes)
{
if (b_nbytes == 0)
return 1;
return (memcmp (a_data, b_data, b_nbytes) < 0 ? -1 : 1);
}
else if (a_nbytes < b_nbytes)
{
if (a_nbytes == 0)
return -1;
return (memcmp (a_data, b_data, a_nbytes) > 0 ? 1 : -1);
}
else /* a_nbytes == b_nbytes */
{
if (a_nbytes == 0)
return 0;
return memcmp (a_data, b_data, a_nbytes);
}
}
int
_sd_c_casecmp (idx_t a_nbytes, const char *a_data,
idx_t b_nbytes, const char *b_data)
{
/* Don't use memcasecmp here, since it uses the current locale, not the
"C" locale. */
idx_t n = (a_nbytes < b_nbytes ? a_nbytes : b_nbytes);
idx_t i;
for (i = 0; i < n; i++)
{
int ac = c_tolower ((unsigned char) a_data[i]);
int bc = c_tolower ((unsigned char) b_data[i]);
if (ac != bc)
return (UCHAR_MAX <= INT_MAX ? ac - bc : _GL_CMP (ac, bc));
}
/* Here i = n = min (a_nbytes, b_nbytes). */
return _GL_CMP (a_nbytes, b_nbytes);
}
ptrdiff_t
_sd_index (idx_t s_nbytes, const char *s_data, char c)
{
if (s_nbytes > 0)
{
void *found = memchr (s_data, (unsigned char) c, s_nbytes);
if (found != NULL)
return (char *) found - s_data;
}
return -1;
}
ptrdiff_t
_sd_last_index (idx_t s_nbytes, const char *s_data, char c)
{
if (s_nbytes > 0)
{
void *found = memrchr (s_data, (unsigned char) c, s_nbytes);
if (found != NULL)
return (char *) found - s_data;
}
return -1;
}
string_desc_t
sd_new_empty (void)
{
string_desc_t result;
result._nbytes = 0;
result._data = NULL;
return result;
}
#if __GNUC__ >= 5
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wdiscarded-qualifiers"
#endif
string_desc_t
_sd_new_addr (idx_t n, const char *addr)
{
string_desc_t result;
result._nbytes = n;
/* No, it is not a good idea to canonicalize (0, non-NULL) to (0, NULL).
Some functions that return a string_desc_t use a return value of (0, NULL)
to denote failure. */
#if 0
if (n == 0)
result._data = NULL;
else
#endif
result._data = (char *) addr;
return result;
}
rw_string_desc_t
_rwsd_new_addr (idx_t n, /*!*/const/*!*/ char *addr)
{
rw_string_desc_t result;
result._nbytes = n;
/* No, it is not a good idea to canonicalize (0, non-NULL) to (0, NULL).
Some functions that return a rw_string_desc_t use a return value of
(0, NULL) to denote failure. */
#if 0
if (n == 0)
result._data = NULL;
else
#endif
result._data = (char *) addr;
return result;
}
string_desc_t
sd_from_c (const char *s)
{
string_desc_t result;
result._nbytes = strlen (s);
result._data = (char *) s;
return result;
}
#if __GNUC__ >= 5
# pragma GCC diagnostic pop
#endif
int
_sd_write (int fd, idx_t s_nbytes, const char *s_data)
{
if (s_nbytes > 0)
if (full_write (fd, s_data, s_nbytes) != s_nbytes)
/* errno is set here. */
return -1;
return 0;
}
int
_sd_fwrite (FILE *fp, idx_t s_nbytes, const char *s_data)
{
if (s_nbytes > 0)
if (fwrite (s_data, 1, s_nbytes, fp) != s_nbytes)
return -1;
return 0;
}
/* ==== Memory-allocating operations on string descriptors ==== */
int
sd_new (rw_string_desc_t *resultp, idx_t n)
{
rw_string_desc_t result;
if (!(n >= 0))
/* Invalid argument. */
abort ();
result._nbytes = n;
if (n == 0)
result._data = NULL;
else
{
result._data = (char *) imalloc (n);
if (result._data == NULL)
/* errno is set here. */
return -1;
}
*resultp = result;
return 0;
}
int
sd_new_filled (rw_string_desc_t *resultp, idx_t n, char c)
{
rw_string_desc_t result;
result._nbytes = n;
if (n == 0)
result._data = NULL;
else
{
result._data = (char *) imalloc (n);
if (result._data == NULL)
/* errno is set here. */
return -1;
memset (result._data, (unsigned char) c, n);
}
*resultp = result;
return 0;
}
int
_sd_copy (rw_string_desc_t *resultp, idx_t s_nbytes, const char *s_data)
{
rw_string_desc_t result;
idx_t n = s_nbytes;
result._nbytes = n;
if (n == 0)
result._data = NULL;
else
{
result._data = (char *) imalloc (n);
if (result._data == NULL)
/* errno is set here. */
return -1;
memcpy (result._data, s_data, n);
}
*resultp = result;
return 0;
}
int
sd_concat (rw_string_desc_t *resultp, idx_t n, /* [rw_]string_desc_t string1, */ ...)
{
if (n <= 0)
/* Invalid argument. */
abort ();
idx_t total = 0;
{
va_list strings;
va_start (strings, n);
string_desc_t string1 = va_arg (strings, string_desc_t);
total += string1._nbytes;
if (n > 1)
{
idx_t i;
for (i = n - 1; i > 0; i--)
{
string_desc_t arg = va_arg (strings, string_desc_t);
total += arg._nbytes;
}
}
va_end (strings);
}
char *combined = (char *) imalloc (total);
if (combined == NULL)
/* errno is set here. */
return -1;
idx_t pos = 0;
{
va_list strings;
va_start (strings, n);
string_desc_t string1 = va_arg (strings, string_desc_t);
memcpy (combined, string1._data, string1._nbytes);
pos += string1._nbytes;
if (n > 1)
{
idx_t i;
for (i = n - 1; i > 0; i--)
{
string_desc_t arg = va_arg (strings, string_desc_t);
if (arg._nbytes > 0)
memcpy (combined + pos, arg._data, arg._nbytes);
pos += arg._nbytes;
}
}
va_end (strings);
}
rw_string_desc_t result;
result._nbytes = total;
result._data = combined;
*resultp = result;
return 0;
}
char *
_sd_c (idx_t s_nbytes, const char *s_data)
{
idx_t n = s_nbytes;
char *result = (char *) imalloc (n + 1);
if (result == NULL)
/* errno is set here. */
return NULL;
if (n > 0)
memcpy (result, s_data, n);
result[n] = '\0';
return result;
}
/* ==== Operations with side effects on string descriptors ==== */
void
sd_set_char_at (rw_string_desc_t s, idx_t i, char c)
{
if (!(i >= 0 && i < s._nbytes))
/* Invalid argument. */
abort ();
s._data[i] = c;
}
void
sd_fill (rw_string_desc_t s, idx_t start, idx_t end, char c)
{
if (!(start >= 0 && start <= end))
/* Invalid arguments. */
abort ();
if (start < end)
memset (s._data + start, (unsigned char) c, end - start);
}
void
_sd_overwrite (rw_string_desc_t s, idx_t start, idx_t t_nbytes, const char *t_data)
{
if (!(start >= 0 && start + t_nbytes <= s._nbytes))
/* Invalid arguments. */
abort ();
if (t_nbytes > 0)
memcpy (s._data + start, t_data, t_nbytes);
}
void
sd_free (rw_string_desc_t s)
{
free (s._data);
}