8cde5779143773e5dd3aeb02d914a01a4740ae16
[pspp] / src / libpspp / str.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2009, 2010, 2011, 2012, 2014 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #if !str_h
18 #define str_h 1
19
20 #include <assert.h>
21 #include <stdarg.h>
22 #include <stdbool.h>
23 #include <stdint.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <unitypes.h>
27
28 #include "compiler.h"
29 #include "memcasecmp.h"
30 #include "xstrndup.h"
31 #include "xvasprintf.h"
32
33 #include "gl/xalloc.h"
34 \f
35 /* Miscellaneous. */
36
37 struct substring;
38
39 void buf_reverse (char *, size_t);
40 int buf_compare_case (const char *, const char *, size_t);
41 int buf_compare_rpad (const char *, size_t, const char *, size_t);
42 void buf_copy_lpad (char *, size_t, const char *, size_t, char pad);
43 void buf_copy_rpad (char *, size_t, const char *, size_t, char pad);
44 void buf_copy_str_lpad (char *, size_t, const char *, char pad);
45 void buf_copy_str_rpad (char *, size_t, const char *, char pad);
46
47 int str_compare_rpad (const char *, const char *);
48 void str_copy_rpad (char *, size_t, const char *);
49 void str_copy_trunc (char *, size_t, const char *);
50 void str_copy_buf_trunc (char *, size_t, const char *, size_t);
51 void str_uppercase (char *);
52 void str_lowercase (char *);
53
54 bool str_format_26adic (unsigned long int number, bool uppercase,
55                         char buffer[], size_t);
56
57 void str_ellipsize (struct substring in, char *out, size_t out_size);
58
59 static inline char *xstrdup_if_nonnull (const char *);
60
61 void *mempset (void *, int, size_t);
62 \f
63 /* Common character classes for use with substring and string functions. */
64
65 #define CC_SPACES " \t\v\r\n"
66 #define CC_DIGITS "0123456789"
67 #define CC_XDIGITS "0123456789abcdefABCDEF"
68 #define CC_LETTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
69 #define CC_ALNUM CC_LETTERS CC_DIGITS
70 \f
71 /* Substrings. */
72 struct substring
73   {
74     char *string;
75     size_t length;
76   };
77
78 #define SS_EMPTY_INITIALIZER {NULL, 0}
79 #define SS_LITERAL_INITIALIZER(LITERAL)                 \
80         {(char *) LITERAL, (sizeof LITERAL) - 1}
81
82 /* Constructors.
83    These functions do not allocate any memory, so the substrings
84    they create should not normally be destroyed. */
85 static inline struct substring ss_empty (void);
86 static inline struct substring ss_cstr (const char *);
87 static inline struct substring ss_buffer (const char *, size_t);
88 struct substring ss_substr (struct substring, size_t start, size_t);
89 struct substring ss_head (struct substring, size_t);
90 struct substring ss_tail (struct substring, size_t);
91
92 /* Constructors and destructor that allocate and deallocate
93    memory. */
94 struct pool;
95 void ss_alloc_substring (struct substring *, struct substring);
96 void ss_alloc_uninit (struct substring *, size_t);
97 void ss_realloc (struct substring *, size_t);
98 void ss_alloc_substring_pool (struct substring *, struct substring,
99                               struct pool *);
100 void ss_alloc_uninit_pool (struct substring *, size_t, struct pool *);
101 void ss_dealloc (struct substring *);
102
103 /* Mutators.
104    Functions that advance the beginning of a string should not be
105    used if a substring is to be deallocated. */
106 void ss_swap (struct substring *, struct substring *);
107 void ss_truncate (struct substring *, size_t);
108 size_t ss_rtrim (struct substring *, struct substring trim_set);
109 size_t ss_ltrim (struct substring *, struct substring trim_set);
110 void ss_trim (struct substring *, struct substring trim_set);
111 bool ss_chomp_byte (struct substring *, char);
112 bool ss_chomp (struct substring *, struct substring);
113 bool ss_separate (struct substring src, struct substring delimiters,
114                   size_t *save_idx, struct substring *token);
115 bool ss_tokenize (struct substring src, struct substring delimiters,
116                   size_t *save_idx, struct substring *token);
117 void ss_advance (struct substring *, size_t);
118 bool ss_match_byte (struct substring *, char);
119 int ss_match_byte_in (struct substring *, struct substring);
120 bool ss_match_string (struct substring *, const struct substring);
121 int ss_get_byte (struct substring *);
122 size_t ss_get_bytes (struct substring *, size_t cnt, struct substring *);
123 bool ss_get_until (struct substring *, char delimiter, struct substring *);
124 size_t ss_get_long (struct substring *, long *);
125
126 /* Inspectors. */
127 bool ss_is_empty (struct substring);
128 size_t ss_length (struct substring);
129 char *ss_data (struct substring);
130 char *ss_end (struct substring);
131 int ss_at (struct substring, size_t idx);
132 int ss_first (struct substring);
133 int ss_last (struct substring);
134 bool ss_starts_with (struct substring, struct substring prefix);
135 bool ss_ends_with (struct substring, struct substring suffix);
136 size_t ss_span (struct substring, struct substring skip_set);
137 size_t ss_cspan (struct substring, struct substring stop_set);
138 size_t ss_find_byte (struct substring, char);
139 size_t ss_find_substring (struct substring, struct substring);
140 int ss_compare (struct substring, struct substring);
141 int ss_compare_case (struct substring, struct substring);
142 int ss_equals (struct substring, struct substring);
143 int ss_equals_case (struct substring, struct substring);
144 size_t ss_pointer_to_position (struct substring, const char *);
145 char *ss_xstrdup (struct substring);
146
147 /* UTF-8. */
148 ucs4_t ss_first_mb (struct substring);
149 int ss_first_mblen (struct substring);
150 ucs4_t ss_get_mb (struct substring *);
151 ucs4_t ss_at_mb (struct substring, size_t ofs);
152 int ss_at_mblen (struct substring, size_t ofs);
153 \f
154 /* Variable length strings. */
155
156 struct string
157   {
158     struct substring ss;
159
160     size_t capacity;    /* Allocated capacity, not including one
161                            extra byte allocated for null terminator. */
162   };
163
164 #define DS_EMPTY_INITIALIZER {SS_EMPTY_INITIALIZER, 0}
165
166 /* Constructors, destructors. */
167 void ds_init_empty (struct string *);
168 void ds_init_string (struct string *, const struct string *);
169 void ds_init_substring (struct string *, struct substring);
170 void ds_init_cstr (struct string *, const char *);
171 void ds_destroy (struct string *);
172 void ds_swap (struct string *, struct string *);
173
174 /* Pools. */
175 struct pool;
176 void ds_register_pool (struct string *, struct pool *);
177 void ds_unregister_pool (struct string *, struct pool *);
178
179 /* Replacement. */
180 void ds_assign_string (struct string *, const struct string *);
181 void ds_assign_substring (struct string *, struct substring);
182 void ds_assign_cstr (struct string *, const char *);
183
184 /* Shrink, extend. */
185 void ds_clear (struct string *);
186 void ds_extend (struct string *, size_t);
187 void ds_shrink (struct string *);
188 void ds_truncate (struct string *, size_t);
189
190 /* Padding, trimming. */
191 size_t ds_rtrim (struct string *, struct substring trim_set);
192 size_t ds_ltrim (struct string *, struct substring trim_set);
193 size_t ds_trim (struct string *, struct substring trim_set);
194 bool ds_chomp_byte (struct string *, char);
195 bool ds_chomp (struct string *, struct substring);
196 bool ds_separate (const struct string *src, struct substring delimiters,
197                   size_t *save_idx, struct substring *token);
198 bool ds_tokenize (const struct string *src, struct substring delimiters,
199                   size_t *save_idx, struct substring *token);
200 void ds_rpad (struct string *, size_t length, char pad);
201 void ds_set_length (struct string *, size_t new_length, char pad);
202 void ds_remove (struct string *, size_t start, size_t n);
203
204 /* Extracting substrings. */
205 struct substring ds_ss (const struct string *);
206 struct substring ds_substr (const struct string *, size_t start, size_t);
207 struct substring ds_head (const struct string *, size_t);
208 struct substring ds_tail (const struct string *, size_t);
209
210 /* Inspectors. */
211 bool ds_is_empty (const struct string *);
212 size_t ds_length (const struct string *);
213 char *ds_data (const struct string *);
214 char *ds_end (const struct string *);
215 int ds_at (const struct string *, size_t idx);
216 int ds_first (const struct string *);
217 int ds_last (const struct string *);
218 bool ds_ends_with (const struct string *, struct substring suffix);
219 size_t ds_span (const struct string *, struct substring skip_set);
220 size_t ds_cspan (const struct string *, struct substring stop_set);
221 size_t ds_find_byte (const struct string *, char);
222 int ds_compare (const struct string *, const struct string *);
223 size_t ds_pointer_to_position (const struct string *, const char *);
224 char *ds_xstrdup (const struct string *);
225
226 size_t ds_capacity (const struct string *);
227 char *ds_cstr (const struct string *);
228 char *ds_steal_cstr (struct string *);
229
230 /* File input. */
231 bool ds_read_line (struct string *, FILE *, size_t max_length);
232 bool ds_read_config_line (struct string *, int *line_number, FILE *);
233 bool ds_read_stream (struct string *, size_t size, size_t cnt, FILE *stream);
234
235 /* Append. */
236 void ds_put_byte (struct string *, int ch);
237 void ds_put_byte_multiple (struct string *, int ch, size_t);
238 void ds_put_unichar (struct string *, ucs4_t uc);
239 void ds_put_cstr (struct string *, const char *);
240 void ds_put_substring (struct string *, struct substring);
241 void ds_put_vformat (struct string *st, const char *, va_list)
242      PRINTF_FORMAT (2, 0);
243 void ds_put_c_vformat (struct string *st, const char *, va_list)
244      PRINTF_FORMAT (2, 0);
245
246 void ds_put_format (struct string *, const char *, ...)
247      PRINTF_FORMAT (2, 3);
248 void ds_put_c_format (struct string *, const char *, ...)
249      PRINTF_FORMAT (2, 3);
250
251 char *ds_put_uninit (struct string *st, size_t incr);
252
253 char *ds_splice_uninit (struct string *, size_t ofs, size_t old_len,
254                         size_t new_len);
255
256 /* Other */
257 /* calls relocate from gnulib on ST */
258 void ds_relocate (struct string *st);
259
260
261 void u8_buf_copy_rpad (uint8_t *dst, size_t dst_size,
262                        const uint8_t *src, size_t src_size,
263                        char pad);
264 \f
265 static inline struct substring
266 ss_empty (void)
267 {
268   struct substring ss;
269   ss.string = NULL;
270   ss.length = 0;
271   return ss;
272 }
273
274 /* Returns a substring whose contents are the given C-style
275    string CSTR. */
276 static inline struct substring
277 ss_cstr (const char *cstr)
278 {
279   return ss_buffer (cstr, strlen (cstr));
280 }
281
282 /* Returns a substring whose contents are the CNT characters in
283    BUFFER. */
284 static inline struct substring
285 ss_buffer (const char *buffer, size_t cnt)
286 {
287   struct substring ss;
288   ss.string = (char *) buffer;
289   ss.length = cnt;
290   return ss;
291 }
292
293 static inline char *
294 xstrdup_if_nonnull (const char *s)
295 {
296   return s ? xstrdup (s) : NULL;
297 }
298
299 #endif /* str_h */