Branch
Hash :
5656e32e
Author :
Date :
2025-05-10T03:16:52
string-desc: Distinguish writable strings from read-only strings. * lib/string-desc.h (HAVE_STATEMENT_EXPRESSIONS): New macro. (rw_string_desc_t): New type. (string_desc_t) [HAVE_STATEMENT_EXPRESSIONS]: Change field _data from 'char *' to 'const char *'. (sd_readonly, sd_readwrite): New inline functions. (sd_length): Define through a macro with _Generic. (sd_char_at): Define through a macro and an inline function. (sd_data, sd_is_empty): Define through a macro with _Generic. (sd_equals, sd_startswith, sd_endswith, sd_cmp, sd_c_casecmp, sd_index, sd_last_index, sd_contains): Define through a macro. (sd_new_addr): Define through a macro with _Generic. (sd_substring, sd_write, sd_fwrite): Define through a macro. (sd_new, sd_new_filled): Change parameter type. (sd_copy): Define through a macro. (sd_concat): Change parameter type. (sd_c): Define through a macro. (sd_set_char_at, sd_fill): Change parameter type. (sd_overwrite): Define through a macro. (sd_free): Change parameter type. * lib/string-desc.c (_sd_equals): Renamed from sd_equals. Take scalar parameters. (_sd_startswith): Renamed from sd_startswith. Take scalar parameters. (_sd_endswith): Renamed from sd_endswith. Take scalar parameters. (_sd_cmp): Renamed from sd_cmp. Take scalar parameters. (_sd_c_casecmp): Renamed from sd_c_casecmp. Take scalar parameters. (_sd_index): Renamed from sd_index. Take scalar parameters. (_sd_last_index): Renamed from sd_last_index. Take scalar parameters. (_sd_new_addr, _rwsd_new_addr): Renamed from sd_new_addr. (sd_substring): Remove function. (_sd_write): Renamed from sd_write. Take scalar parameters. (_sd_fwrite): Renamed from sd_fwrite. Take scalar parameters. (sd_new, sd_new_filled): Change parameter type. (_sd_copy): Renamed from sd_copy. Change parameter type. Take scalar parameters. (sd_concat): Change parameter type. (_sd_c): Renamed from sd_c. Take scalar parameters. (sd_set_char_at, sd_fill): Change parameter type. (_sd_overwrite): Renamed from sd_overwrite. Change parameter type. Take scalar parameters. (sd_free): Change parameter type. * lib/string-desc-contains.c (_sd_contains): Renamed from sd_contains. Take scalar parameters. * lib/xstring-desc.h (xsd_new, xsd_new_filled, xsd_copy, xsd_concat): Change return type to rw_string_desc_t. (xsd_c): Define through a macro. * lib/xstring-desc.c (xsd_concat): Change return type to rw_string_desc_t. * doc/string-desc.texi (Handling strings with NUL characters): Mention rw_string_desc_t and the sd_readonly() function. * lib/string-buffer.h (sb_dupfree, sb_xdupfree): Change return type to rw_string_desc_t. * lib/string-buffer.c (sb_contents): Add a cast to 'const char *'. (sb_dupfree): Change return type to rw_string_desc_t. * lib/xstring-buffer.c (sb_xdupfree): Change return type to rw_string_desc_t. * lib/string-buffer-reversed.h (sbr_dupfree, sbr_xdupfree): Change return type to rw_string_desc_t. * lib/string-buffer-reversed.c (sbr_contents): Add a cast to 'const char *'. (sbr_dupfree): Change return type to rw_string_desc_t. * lib/xstring-buffer-reversed.c (sbr_xdupfree): Change return type to rw_string_desc_t. * tests/test-string-desc.c (main): Use type rw_string_desc_t as appropriate. * tests/test-xstring-desc.c (main): Likewise. * tests/test-sf-istream.c (main): Remove cast in sd_new_addr argument. * tests/test-sfl-istream.c (main): Likewise. * NEWS: Mention the change.
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
/* String descriptors, with out-of-memory checking.
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 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
#include <config.h>
#define GL_XSTRING_DESC_INLINE _GL_EXTERN_INLINE
#include "xstring-desc.h"
#include "ialloc.h"
rw_string_desc_t
xsd_concat (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)
xalloc_die ();
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;
return result;
}