summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog5
-rw-r--r--lib/xstring-desc.c74
-rw-r--r--lib/xstring-desc.h114
-rw-r--r--modules/xstring-desc24
4 files changed, 217 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index 3f621249b8..d0e75ebec7 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
2023-03-28 Bruno Haible <bruno@clisp.org>
+ xstring-desc: New module.
+ * lib/xstring-desc.h: New file.
+ * lib/xstring-desc.c: New file.
+ * modules/xstring-desc: New file.
+
string-desc: Add tests.
* tests/test-string-desc.sh: New file.
* tests/test-string-desc.c: New file.
diff --git a/lib/xstring-desc.c b/lib/xstring-desc.c
new file mode 100644
index 0000000000..19c5ab6a00
--- /dev/null
+++ b/lib/xstring-desc.c
@@ -0,0 +1,74 @@
+/* String descriptors, with out-of-memory checking.
+ Copyright (C) 2023 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"
+
+string_desc_t
+xstring_desc_concat (idx_t n, string_desc_t string1, ...)
+{
+ if (n <= 0)
+ /* Invalid argument. */
+ abort ();
+
+ idx_t total = 0;
+ total += string1._nbytes;
+ if (n > 1)
+ {
+ va_list other_strings;
+ idx_t i;
+
+ va_start (other_strings, string1);
+ for (i = n - 1; i > 0; i--)
+ {
+ string_desc_t arg = va_arg (other_strings, string_desc_t);
+ total += arg._nbytes;
+ }
+ va_end (other_strings);
+ }
+
+ char *combined = (char *) imalloc (total);
+ if (combined == NULL)
+ xalloc_die ();
+ idx_t pos = 0;
+ memcpy (combined, string1._data, string1._nbytes);
+ pos += string1._nbytes;
+ if (n > 1)
+ {
+ va_list other_strings;
+ idx_t i;
+
+ va_start (other_strings, string1);
+ for (i = n - 1; i > 0; i--)
+ {
+ string_desc_t arg = va_arg (other_strings, string_desc_t);
+ if (arg._nbytes > 0)
+ memcpy (combined + pos, arg._data, arg._nbytes);
+ pos += arg._nbytes;
+ }
+ va_end (other_strings);
+ }
+
+ string_desc_t result;
+ result._nbytes = total;
+ result._data = combined;
+
+ return result;
+}
diff --git a/lib/xstring-desc.h b/lib/xstring-desc.h
new file mode 100644
index 0000000000..b07831baf4
--- /dev/null
+++ b/lib/xstring-desc.h
@@ -0,0 +1,114 @@
+/* String descriptors, with out-of-memory checking.
+ Copyright (C) 2023 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/>. */
+
+/* Written by Bruno Haible <bruno@clisp.org>, 2023. */
+
+#ifndef _XSTRING_DESC_H
+#define _XSTRING_DESC_H 1
+
+#include <string.h>
+#include "string-desc.h"
+#include "xalloc.h"
+
+
+#ifndef _GL_INLINE_HEADER_BEGIN
+ #error "Please include config.h first."
+#endif
+_GL_INLINE_HEADER_BEGIN
+#ifndef GL_XSTRING_DESC_INLINE
+# define GL_XSTRING_DESC_INLINE _GL_INLINE
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+/* ==== Memory-allocating operations on string descriptors ==== */
+
+/* Return a string of length N, with uninitialized contents. */
+#if 0 /* Defined inline below. */
+extern string_desc_t xstring_desc_new (idx_t n);
+#endif
+
+/* Return a string of length N, filled with C. */
+#if 0 /* Defined inline below. */
+extern string_desc_t xstring_desc_new_filled (idx_t n, char c);
+#endif
+
+/* Return a copy of string S. */
+#if 0 /* Defined inline below. */
+extern string_desc_t xstring_desc_copy (string_desc_t s);
+#endif
+
+/* Return the concatenation of N strings. N must be > 0. */
+extern string_desc_t xstring_desc_concat (idx_t n, string_desc_t string1, ...);
+
+/* Construct and return a copy of string S, as a NUL-terminated C string. */
+#if 0 /* Defined inline below. */
+extern char * xstring_desc_c (string_desc_t s) _GL_ATTRIBUTE_DEALLOC_FREE;
+#endif
+
+
+/* ==== Inline function definitions ==== */
+
+GL_XSTRING_DESC_INLINE string_desc_t
+xstring_desc_new (idx_t n)
+{
+ string_desc_t result;
+ if (string_desc_new (&result, n) < 0)
+ xalloc_die ();
+ return result;
+}
+
+GL_XSTRING_DESC_INLINE string_desc_t
+xstring_desc_new_filled (idx_t n, char c)
+{
+ string_desc_t result;
+ if (string_desc_new_filled (&result, n, c) < 0)
+ xalloc_die ();
+ return result;
+}
+
+GL_XSTRING_DESC_INLINE string_desc_t
+xstring_desc_copy (string_desc_t s)
+{
+ string_desc_t result;
+ if (string_desc_copy (&result, s) < 0)
+ xalloc_die ();
+ return result;
+}
+
+GL_XSTRING_DESC_INLINE
+_GL_ATTRIBUTE_DEALLOC_FREE
+char *
+xstring_desc_c (string_desc_t s)
+{
+ char *result = string_desc_c (s);
+ if (result == NULL)
+ xalloc_die ();
+ return result;
+}
+
+
+#ifdef __cplusplus
+}
+#endif
+
+_GL_INLINE_HEADER_END
+
+
+#endif /* _XSTRING_DESC_H */
diff --git a/modules/xstring-desc b/modules/xstring-desc
new file mode 100644
index 0000000000..d0cda2d23e
--- /dev/null
+++ b/modules/xstring-desc
@@ -0,0 +1,24 @@
+Description:
+String descriptors, with out-of-memory checking.
+
+Files:
+lib/xstring-desc.h
+lib/xstring-desc.c
+
+Depends-on:
+string-desc
+xalloc-die
+
+configure.ac:
+
+Makefile.am:
+lib_SOURCES += xstring-desc.c
+
+Include:
+"xstring-desc.h"
+
+License:
+GPL
+
+Maintainer:
+all