summaryrefslogtreecommitdiff
path: root/lib/xstring-desc.h
blob: b07831baf43860b06ff5dc2486d60b9b0dfc76b7 (plain)
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
/* 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 */