C Standard Library Extensions 1.3
cxstring.h
1/*
2 * This file is part of the ESO C Extension Library
3 * Copyright (C) 2001-2026 European Southern Observatory
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, see <https://www.gnu.org/licenses/>.
17 */
18
19#ifndef CX_STRING_H_
20#define CX_STRING_H_
21
22#include <ctype.h>
23#include <stdarg.h>
24#include <string.h>
25
26#include "cxmacros.h"
27#include "cxtypes.h"
28
29
30CX_BEGIN_DECLS
31
32struct _cx_string_
33{
34 /* <private> */
35
36 cxchar *data;
37 cxsize sz;
38};
39
40
47
48typedef struct _cx_string_ cx_string;
49
50
51/*
52 * Create, copy and destroy operations
53 */
54
57cx_string *cx_string_create(const cxchar *);
59
60/*
61 * Non modifying operations
62 */
63
64cxsize cx_string_size(const cx_string *);
65cxbool cx_string_empty(const cx_string *);
66
67/*
68 * Data access
69 */
70
71const cxchar *cx_string_get(const cx_string *);
72
73/*
74 * Assignment operations
75 */
76
77void cx_string_set(cx_string *, const cxchar *);
78
79/*
80 * Modifying operations
81 */
82void cx_string_resize(cx_string *, cxsize, cxchar);
83void cx_string_extend(cx_string *, cxsize, cxchar);
84
85void cx_string_replace_character(cx_string *, cxsize, cxsize, cxchar, cxchar);
86
92
93/*
94 * Inserting and removing elements
95 */
96
97cx_string *cx_string_prepend(cx_string *, const cxchar *);
98cx_string *cx_string_append(cx_string *, const cxchar *);
99cx_string *cx_string_insert(cx_string *, cxssize, const cxchar *);
100cx_string *cx_string_erase(cx_string *, cxssize, cxssize);
102
103cx_string *cx_string_substr(const cx_string *, cxsize, cxsize);
104
105/*
106 * Comparison functions
107 */
108
109cxbool cx_string_equal(const cx_string *, const cx_string *);
110cxint cx_string_compare(const cx_string *, const cx_string *);
111cxint cx_string_casecmp(const cx_string *, const cx_string *);
112cxint cx_string_ncasecmp(const cx_string *, const cx_string *, cxsize);
113
114/*
115 * Search functions
116 */
117
118cxsize cx_string_find_first_not_of(const cx_string *, const cxchar *);
119cxsize cx_string_find_last_not_of(const cx_string *, const cxchar *);
120
121/*
122 * I/O functions
123 */
124
125cxint cx_string_sprintf(cx_string *, const cxchar *, ...) CX_GNUC_PRINTF(2, 3);
126cxint cx_string_vsprintf(cx_string *, const cxchar *, va_list)
127 CX_GNUC_PRINTF(2, 0);
128
129/*
130 * Debugging utilities
131 */
132
133void cx_string_print(const cx_string *);
134
135CX_END_DECLS
136
137#endif /* CX_STRING_H */
cx_string * cx_string_upper(cx_string *)
Converts the string into uppercase.
Definition cxstring.c:415
cx_string * cx_string_create(const cxchar *)
Create a new string from a standard C string.
Definition cxstring.c:262
cx_string * cx_string_truncate(cx_string *, cxsize)
Truncate the string.
Definition cxstring.c:785
cx_string * cx_string_insert(cx_string *, cxssize, const cxchar *)
Inserts a copy of a string at a given position.
Definition cxstring.c:659
cxint cx_string_vsprintf(cx_string *self, const cxchar *format, va_list args)
Write to the string from a variable-length argument list under format control.
Definition cxstring.c:982
cxint cx_string_sprintf(cx_string *self, const char *format,...)
Writes to a string under format control.
Definition cxstring.c:944
cx_string * cx_string_append(cx_string *, const cxchar *)
Append an array of characters to the string.
Definition cxstring.c:607
cx_string * cx_string_substr(const cx_string *, cxsize, cxsize)
Create a new string from a portion of a string.
Definition cxstring.c:1270
void cx_string_replace_character(cx_string *, cxsize, cxsize, cxchar, cxchar)
Replace a given character with a new character in a portion of a string.
Definition cxstring.c:1035
cxint cx_string_compare(const cx_string *, const cx_string *)
Compare two strings.
Definition cxstring.c:855
void cx_string_resize(cx_string *, cxsize, cxchar)
Resize a string to a given length.
Definition cxstring.c:1086
struct _cx_string_ cx_string
The cx_string data type.
Definition cxstring.h:48
cxsize cx_string_find_last_not_of(const cx_string *, const cxchar *)
Search a string for the last character that does not match any of the given characters.
Definition cxstring.c:1216
cx_string * cx_string_new(void)
Create a new, empty string container.
Definition cxstring.c:218
cx_string * cx_string_lower(cx_string *)
Converts the string into lowercase.
Definition cxstring.c:448
cx_string * cx_string_erase(cx_string *, cxssize, cxssize)
Erase a portion of the string.
Definition cxstring.c:716
const cxchar * cx_string_get(const cx_string *)
Get the string's value.
Definition cxstring.c:390
void cx_string_set(cx_string *, const cxchar *)
Assign a value to a string.
Definition cxstring.c:359
void cx_string_delete(cx_string *)
Destroy a string.
Definition cxstring.c:287
cx_string * cx_string_strip(cx_string *)
Remove leading and trailing whitespaces from the string.
Definition cxstring.c:531
cx_string * cx_string_trim(cx_string *)
Remove leading whitespaces from the string.
Definition cxstring.c:481
cx_string * cx_string_rtrim(cx_string *)
Remove trailing whitespaces from the string.
Definition cxstring.c:506
cxbool cx_string_empty(const cx_string *)
Checks whether a string contains any characters.
Definition cxstring.c:333
cx_string * cx_string_prepend(cx_string *, const cxchar *)
Prepend an array of characters to the string.
Definition cxstring.c:557
cx_string * cx_string_copy(const cx_string *)
Create a copy a cx_string.
Definition cxstring.c:234
cxbool cx_string_equal(const cx_string *, const cx_string *)
Compare two cx_string for equality.
Definition cxstring.c:812
void cx_string_print(const cx_string *string)
Print the value of a cx_string to the standard output.
Definition cxstring.c:1002
cxsize cx_string_size(const cx_string *)
Computes the length of the string.
Definition cxstring.c:312
cxsize cx_string_find_first_not_of(const cx_string *, const cxchar *)
Search a string for the first character that does not match any of the given characters.
Definition cxstring.c:1174
void cx_string_extend(cx_string *, cxsize, cxchar)
Extend a string to a given length.
Definition cxstring.c:1134
cxint cx_string_ncasecmp(const cx_string *, const cx_string *, cxsize)
Compare the first n characters of two strings ignoring the case of characters.
Definition cxstring.c:911
cxint cx_string_casecmp(const cx_string *, const cx_string *)
Compare two strings ignoring the case of characters.
Definition cxstring.c:878