259fd21b45814a4648e16828e9203fb90aaf55c5
[pspp-builds.git] / src / libpspp / str.h
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    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, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17    02110-1301, USA. */
18
19 #if !str_h
20 #define str_h 1
21
22 #include <assert.h>
23 #include <stdarg.h>
24 #include <stdbool.h>
25 #include <stdio.h>
26 #include <string.h>
27
28 #include "compiler.h"
29 #include "memcasecmp.h"
30 #include "xvasprintf.h"
31 \f
32 /* Miscellaneous. */
33
34 void buf_reverse (char *, size_t);
35 char *buf_find_reverse (const char *, size_t, const 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);
39 void buf_copy_rpad (char *, size_t, const char *, size_t);
40 void buf_copy_str_lpad (char *, size_t, const char *);
41 void buf_copy_str_rpad (char *, size_t, const char *);
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 char *spprintf (char *dst, const char *format, ...);
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 struct substring ss_empty (void);
77 struct substring ss_cstr (const char *);
78 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_alloc_substring_pool (struct substring *, struct substring,
89                               struct pool *);
90 void ss_alloc_uninit_pool (struct substring *, size_t, struct pool *);
91 void ss_dealloc (struct substring *);
92
93 /* Mutators.
94    Functions that advance the beginning of a string should not be
95    used if a substring is to be deallocated. */
96 void ss_truncate (struct substring *, size_t);
97 size_t ss_rtrim (struct substring *, struct substring trim_set);
98 size_t ss_ltrim (struct substring *, struct substring trim_set);
99 void ss_trim (struct substring *, struct substring trim_set);
100 bool ss_chomp (struct substring *, char);
101 bool ss_separate (struct substring src, struct substring delimiters,
102                   size_t *save_idx, struct substring *token);
103 bool ss_tokenize (struct substring src, struct substring delimiters,
104                   size_t *save_idx, struct substring *token);
105 void ss_advance (struct substring *, size_t);
106 bool ss_match_char (struct substring *, char);
107 bool ss_match_string (struct substring *, const struct substring);
108 int ss_get_char (struct substring *);
109 size_t ss_get_chars (struct substring *, size_t cnt, struct substring *);
110 bool ss_get_until (struct substring *, char delimiter, struct substring *);
111 size_t ss_get_long (struct substring *, long *);
112
113 /* Inspectors. */
114 bool ss_is_empty (struct substring);
115 size_t ss_length (struct substring);
116 char *ss_data (struct substring);
117 char *ss_end (struct substring);
118 int ss_at (struct substring, size_t idx);
119 int ss_first (struct substring);
120 int ss_last (struct substring);
121 size_t ss_span (struct substring, struct substring skip_set);
122 size_t ss_cspan (struct substring, struct substring stop_set);
123 size_t ss_find_char (struct substring, char);
124 int ss_compare (struct substring, struct substring);
125 int ss_compare_case (struct substring, struct substring);
126 int ss_equals (struct substring, struct substring);
127 int ss_equals_case (struct substring, struct substring);
128 size_t ss_pointer_to_position (struct substring, const char *);
129 char *ss_xstrdup (struct substring);
130 \f
131 /* Variable length strings. */
132
133 struct string
134   {
135     struct substring ss;
136
137     size_t capacity;    /* Allocated capacity, not including one
138                            extra byte allocated for null terminator. */
139   };
140
141 #define DS_EMPTY_INITIALIZER {SS_EMPTY_INITIALIZER, 0}
142
143 /* Constructors, destructors. */
144 void ds_init_empty (struct string *);
145 void ds_init_string (struct string *, const struct string *);
146 void ds_init_substring (struct string *, struct substring);
147 void ds_init_cstr (struct string *, const char *);
148 void ds_destroy (struct string *);
149 void ds_swap (struct string *, struct string *);
150
151 /* Pools. */
152 struct pool;
153 void ds_register_pool (struct string *, struct pool *);
154 void ds_unregister_pool (struct string *, struct pool *);
155
156 /* Replacement. */
157 void ds_assign_string (struct string *, const struct string *);
158 void ds_assign_substring (struct string *, struct substring);
159 void ds_assign_cstr (struct string *, const char *);
160
161 /* Shrink, extend. */
162 void ds_clear (struct string *);
163 void ds_extend (struct string *, size_t);
164 void ds_shrink (struct string *);
165 void ds_truncate (struct string *, size_t);
166
167 /* Padding, trimming. */
168 size_t ds_rtrim (struct string *, struct substring trim_set);
169 size_t ds_ltrim (struct string *, struct substring trim_set);
170 size_t ds_trim (struct string *, struct substring trim_set);
171 bool ds_chomp (struct string *, char);
172 bool ds_separate (const struct string *src, struct substring delimiters,
173                   size_t *save_idx, struct substring *token);
174 bool ds_tokenize (const struct string *src, struct substring delimiters,
175                   size_t *save_idx, struct substring *token);
176 void ds_rpad (struct string *, size_t length, char pad);
177 void ds_set_length (struct string *, size_t new_length, char pad);
178
179 /* Extracting substrings. */
180 struct substring ds_ss (const struct string *);
181 struct substring ds_substr (const struct string *, size_t start, size_t);
182 struct substring ds_head (const struct string *, size_t);
183 struct substring ds_tail (const struct string *, size_t);
184
185 /* Inspectors. */
186 bool ds_is_empty (const struct string *);
187 size_t ds_length (const struct string *);
188 char *ds_data (const struct string *);
189 char *ds_end (const struct string *);
190 int ds_at (const struct string *, size_t idx);
191 int ds_first (const struct string *);
192 int ds_last (const struct string *);
193 size_t ds_span (const struct string *, struct substring skip_set);
194 size_t ds_cspan (const struct string *, struct substring stop_set);
195 size_t ds_find_char (const struct string *, char);
196 int ds_compare (const struct string *, const struct string *);
197 size_t ds_pointer_to_position (const struct string *, const char *);
198 char *ds_xstrdup (const struct string *);
199
200 size_t ds_capacity (const struct string *);
201 char *ds_cstr (const struct string *);
202
203 /* File input. */
204 bool ds_read_line (struct string *, FILE *);
205 bool ds_read_config_line (struct string *, int *line_number, FILE *);
206 size_t ds_read_stream (struct string *, size_t size, size_t cnt, FILE *stream);
207
208 /* Append. */
209 void ds_put_char (struct string *, int ch);
210 void ds_put_char_multiple (struct string *, int ch, size_t);
211 void ds_put_cstr (struct string *, const char *);
212 void ds_put_substring (struct string *, struct substring);
213 void ds_put_vformat (struct string *st, const char *, va_list)
214      PRINTF_FORMAT (2, 0);
215 void ds_put_format (struct string *, const char *, ...)
216      PRINTF_FORMAT (2, 3);
217 char *ds_put_uninit (struct string *st, size_t incr);
218
219 #endif /* str_h */