str: New functions for checking for and removing string suffixes.
[pspp-builds.git] / src / libpspp / str.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2009, 2010, 2011 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 \f
33 /* Miscellaneous. */
34
35 void buf_reverse (char *, size_t);
36 int buf_compare_case (const char *, const char *, size_t);
37 int buf_compare_rpad (const char *, size_t, const char *, size_t);
38 void buf_copy_lpad (char *, size_t, const char *, size_t, char pad);
39 void buf_copy_rpad (char *, size_t, const char *, size_t, char pad);
40 void buf_copy_str_lpad (char *, size_t, const char *, char pad);
41 void buf_copy_str_rpad (char *, size_t, const char *, char pad);
42
43 int str_compare_rpad (const char *, const char *);
44 void str_copy_rpad (char *, size_t, const char *);
45 void str_copy_trunc (char *, size_t, const char *);
46 void str_copy_buf_trunc (char *, size_t, const char *, size_t);
47 void str_uppercase (char *);
48 void str_lowercase (char *);
49
50 bool str_format_26adic (unsigned long int number, char buffer[], size_t);
51
52 void *mempset (void *, int, size_t);
53 \f
54 /* Common character classes for use with substring and string functions. */
55
56 #define CC_SPACES " \t\v\r\n"
57 #define CC_DIGITS "0123456789"
58 #define CC_XDIGITS "0123456789abcdefABCDEF"
59 #define CC_LETTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
60 #define CC_ALNUM CC_LETTERS CC_DIGITS
61 \f
62 /* Substrings. */
63 struct substring
64   {
65     char *string;
66     size_t length;
67   };
68
69 #define SS_EMPTY_INITIALIZER {NULL, 0}
70 #define SS_LITERAL_INITIALIZER(LITERAL)                 \
71         {(char *) LITERAL, (sizeof LITERAL) - 1}
72
73 /* Constructors.
74    These functions do not allocate any memory, so the substrings
75    they create should not normally be destroyed. */
76 static inline struct substring ss_empty (void);
77 static inline struct substring ss_cstr (const char *);
78 static inline struct substring ss_buffer (const char *, size_t);
79 struct substring ss_substr (struct substring, size_t start, size_t);
80 struct substring ss_head (struct substring, size_t);
81 struct substring ss_tail (struct substring, size_t);
82
83 /* Constructors and destructor that allocate and deallocate
84    memory. */
85 struct pool;
86 void ss_alloc_substring (struct substring *, struct substring);
87 void ss_alloc_uninit (struct substring *, size_t);
88 void ss_realloc (struct substring *, size_t);
89 void ss_alloc_substring_pool (struct substring *, struct substring,
90                               struct pool *);
91 void ss_alloc_uninit_pool (struct substring *, size_t, struct pool *);
92 void ss_dealloc (struct substring *);
93
94 /* Mutators.
95    Functions that advance the beginning of a string should not be
96    used if a substring is to be deallocated. */
97 void ss_truncate (struct substring *, size_t);
98 size_t ss_rtrim (struct substring *, struct substring trim_set);
99 size_t ss_ltrim (struct substring *, struct substring trim_set);
100 void ss_trim (struct substring *, struct substring trim_set);
101 bool ss_chomp_byte (struct substring *, char);
102 bool ss_chomp (struct substring *, struct substring);
103 bool ss_separate (struct substring src, struct substring delimiters,
104                   size_t *save_idx, struct substring *token);
105 bool ss_tokenize (struct substring src, struct substring delimiters,
106                   size_t *save_idx, struct substring *token);
107 void ss_advance (struct substring *, size_t);
108 bool ss_match_byte (struct substring *, char);
109 int ss_match_byte_in (struct substring *, struct substring);
110 bool ss_match_string (struct substring *, const struct substring);
111 int ss_get_byte (struct substring *);
112 size_t ss_get_bytes (struct substring *, size_t cnt, struct substring *);
113 bool ss_get_until (struct substring *, char delimiter, struct substring *);
114 size_t ss_get_long (struct substring *, long *);
115
116 /* Inspectors. */
117 bool ss_is_empty (struct substring);
118 size_t ss_length (struct substring);
119 char *ss_data (struct substring);
120 char *ss_end (struct substring);
121 int ss_at (struct substring, size_t idx);
122 int ss_first (struct substring);
123 int ss_last (struct substring);
124 bool ss_ends_with (struct substring, struct substring suffix);
125 size_t ss_span (struct substring, struct substring skip_set);
126 size_t ss_cspan (struct substring, struct substring stop_set);
127 size_t ss_find_byte (struct substring, char);
128 int ss_compare (struct substring, struct substring);
129 int ss_compare_case (struct substring, struct substring);
130 int ss_equals (struct substring, struct substring);
131 int ss_equals_case (struct substring, struct substring);
132 size_t ss_pointer_to_position (struct substring, const char *);
133 char *ss_xstrdup (struct substring);
134
135 /* UTF-8. */
136 ucs4_t ss_first_mb (struct substring);
137 int ss_first_mblen (struct substring);
138 ucs4_t ss_get_mb (struct substring *);
139 ucs4_t ss_at_mb (struct substring, size_t ofs);
140 int ss_at_mblen (struct substring, size_t ofs);
141 \f
142 /* Variable length strings. */
143
144 struct string
145   {
146     struct substring ss;
147
148     size_t capacity;    /* Allocated capacity, not including one
149                            extra byte allocated for null terminator. */
150   };
151
152 #define DS_EMPTY_INITIALIZER {SS_EMPTY_INITIALIZER, 0}
153
154 /* Constructors, destructors. */
155 void ds_init_empty (struct string *);
156 void ds_init_string (struct string *, const struct string *);
157 void ds_init_substring (struct string *, struct substring);
158 void ds_init_cstr (struct string *, const char *);
159 void ds_destroy (struct string *);
160 void ds_swap (struct string *, struct string *);
161
162 /* Pools. */
163 struct pool;
164 void ds_register_pool (struct string *, struct pool *);
165 void ds_unregister_pool (struct string *, struct pool *);
166
167 /* Replacement. */
168 void ds_assign_string (struct string *, const struct string *);
169 void ds_assign_substring (struct string *, struct substring);
170 void ds_assign_cstr (struct string *, const char *);
171
172 /* Shrink, extend. */
173 void ds_clear (struct string *);
174 void ds_extend (struct string *, size_t);
175 void ds_shrink (struct string *);
176 void ds_truncate (struct string *, size_t);
177
178 /* Padding, trimming. */
179 size_t ds_rtrim (struct string *, struct substring trim_set);
180 size_t ds_ltrim (struct string *, struct substring trim_set);
181 size_t ds_trim (struct string *, struct substring trim_set);
182 bool ds_chomp_byte (struct string *, char);
183 bool ds_chomp (struct string *, struct substring);
184 bool ds_separate (const struct string *src, struct substring delimiters,
185                   size_t *save_idx, struct substring *token);
186 bool ds_tokenize (const struct string *src, struct substring delimiters,
187                   size_t *save_idx, struct substring *token);
188 void ds_rpad (struct string *, size_t length, char pad);
189 void ds_set_length (struct string *, size_t new_length, char pad);
190 void ds_remove (struct string *, size_t start, size_t n);
191
192 /* Extracting substrings. */
193 struct substring ds_ss (const struct string *);
194 struct substring ds_substr (const struct string *, size_t start, size_t);
195 struct substring ds_head (const struct string *, size_t);
196 struct substring ds_tail (const struct string *, size_t);
197
198 /* Inspectors. */
199 bool ds_is_empty (const struct string *);
200 size_t ds_length (const struct string *);
201 char *ds_data (const struct string *);
202 char *ds_end (const struct string *);
203 int ds_at (const struct string *, size_t idx);
204 int ds_first (const struct string *);
205 int ds_last (const struct string *);
206 bool ds_ends_with (const struct string *, struct substring suffix);
207 size_t ds_span (const struct string *, struct substring skip_set);
208 size_t ds_cspan (const struct string *, struct substring stop_set);
209 size_t ds_find_byte (const struct string *, char);
210 int ds_compare (const struct string *, const struct string *);
211 size_t ds_pointer_to_position (const struct string *, const char *);
212 char *ds_xstrdup (const struct string *);
213
214 size_t ds_capacity (const struct string *);
215 char *ds_cstr (const struct string *);
216 char *ds_steal_cstr (struct string *);
217
218 /* File input. */
219 bool ds_read_line (struct string *, FILE *, size_t max_length);
220 bool ds_read_config_line (struct string *, int *line_number, FILE *);
221 bool ds_read_stream (struct string *, size_t size, size_t cnt, FILE *stream);
222
223 /* Append. */
224 void ds_put_byte (struct string *, int ch);
225 void ds_put_byte_multiple (struct string *, int ch, size_t);
226 void ds_put_cstr (struct string *, const char *);
227 void ds_put_substring (struct string *, struct substring);
228 void ds_put_vformat (struct string *st, const char *, va_list)
229      PRINTF_FORMAT (2, 0);
230 void ds_put_format (struct string *, const char *, ...)
231      PRINTF_FORMAT (2, 3);
232 char *ds_put_uninit (struct string *st, size_t incr);
233
234 /* Other */
235 /* calls relocate from gnulib on ST */
236 void ds_relocate (struct string *st);
237
238
239 void u8_buf_copy_rpad (uint8_t *dst, size_t dst_size,
240                        const uint8_t *src, size_t src_size,
241                        char pad);
242 \f
243 struct substring
244 ss_empty (void)
245 {
246   struct substring ss;
247   ss.string = NULL;
248   ss.length = 0;
249   return ss;
250 }
251
252 /* Returns a substring whose contents are the given C-style
253    string CSTR. */
254 static inline struct substring
255 ss_cstr (const char *cstr)
256 {
257   return ss_buffer (cstr, strlen (cstr));
258 }
259
260 /* Returns a substring whose contents are the CNT characters in
261    BUFFER. */
262 static inline struct substring
263 ss_buffer (const char *buffer, size_t cnt)
264 {
265   struct substring ss;
266   ss.string = (char *) buffer;
267   ss.length = cnt;
268   return ss;
269 }
270
271 #endif /* str_h */