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 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
/* Test of string or file based input stream with line number.
Copyright (C) 2024-2025 Free Software Foundation, Inc.
This program 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 2, or (at your option)
any later version.
This program 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/>. */
/* Written by Bruno Haible <bruno@clisp.org>, 2024. */
#include <config.h>
/* Specification. */
#include "sfl-istream.h"
#include <unistd.h>
#include "macros.h"
#define CONTENTS_LEN 9
#define CONTENTS "Hello\377\n\nW"
static void
test_open_stream (sfl_istream_t *stream)
{
int c;
ASSERT (sfl_get_line_number (stream) == 1);
c = sfl_getc (stream);
ASSERT (c == 'H');
ASSERT (sfl_get_line_number (stream) == 1);
c = sfl_getc (stream);
ASSERT (c == 'e');
ASSERT (sfl_get_line_number (stream) == 1);
c = sfl_getc (stream);
ASSERT (c == 'l');
ASSERT (sfl_get_line_number (stream) == 1);
c = sfl_getc (stream);
ASSERT (c == 'l');
ASSERT (sfl_get_line_number (stream) == 1);
c = sfl_getc (stream);
ASSERT (c == 'o');
ASSERT (sfl_get_line_number (stream) == 1);
sfl_ungetc (stream, c);
c = sfl_getc (stream);
ASSERT (c == 'o');
ASSERT (sfl_get_line_number (stream) == 1);
c = sfl_getc (stream);
ASSERT (c == 0xff);
ASSERT (sfl_get_line_number (stream) == 1);
sfl_ungetc (stream, c);
c = sfl_getc (stream);
ASSERT (c == 0xff);
ASSERT (sfl_get_line_number (stream) == 1);
c = sfl_getc (stream);
ASSERT (c == '\n');
ASSERT (sfl_get_line_number (stream) == 2);
c = sfl_getc (stream);
ASSERT (c == '\n');
ASSERT (sfl_get_line_number (stream) == 3);
sfl_ungetc (stream, c);
ASSERT (sfl_get_line_number (stream) == 2);
c = sfl_getc (stream);
ASSERT (c == '\n');
ASSERT (sfl_get_line_number (stream) == 3);
c = sfl_getc (stream);
ASSERT (c == 'W');
ASSERT (sfl_get_line_number (stream) == 3);
c = sfl_getc (stream);
ASSERT (c == EOF);
ASSERT (sfl_get_line_number (stream) == 3);
c = sfl_getc (stream);
ASSERT (c == EOF);
ASSERT (sfl_get_line_number (stream) == 3);
sfl_ungetc (stream, c);
c = sfl_getc (stream);
ASSERT (c == EOF);
ASSERT (sfl_get_line_number (stream) == 3);
ASSERT (!sfl_ferror (stream));
}
int
main ()
{
char const contents[CONTENTS_LEN] _GL_ATTRIBUTE_NONSTRING = CONTENTS;
/* Test reading from a file. */
{
const char *filename = "test-sfl-istream.tmp";
unlink (filename);
{
FILE *fp = fopen (filename, "wb");
ASSERT (fwrite (contents, 1, CONTENTS_LEN, fp) == CONTENTS_LEN);
ASSERT (fclose (fp) == 0);
}
{
FILE *fp = fopen (filename, "rb");
sfl_istream_t stream;
sfl_istream_init_from_file (&stream, fp);
test_open_stream (&stream);
sfl_free (&stream);
}
unlink (filename);
}
/* Test reading from a string in memory. */
{
sfl_istream_t stream;
sfl_istream_init_from_string_desc (&stream,
sd_new_addr (CONTENTS_LEN, contents));
test_open_stream (&stream);
sfl_free (&stream);
}
/* Test reading from a NUL-terminated string in memory. */
{
sfl_istream_t stream;
sfl_istream_init_from_string (&stream, CONTENTS);
test_open_stream (&stream);
sfl_free (&stream);
}
return 0;
}