Lolly 1.4.28
Loading...
Searching...
No Matches
string.cpp
Go to the documentation of this file.
1
2/******************************************************************************
3 * MODULE : string.cpp
4 * DESCRIPTION: Fixed size strings with reference counting.
5 * Zero characters can be part of string.
6 * COPYRIGHT : (C) 1999 Joris van der Hoeven
7 *******************************************************************************
8 * This software falls under the GNU general public license version 3 or later.
9 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
10 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ******************************************************************************/
12
13#include "string.hpp"
14#include "basic.hpp"
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <string>
19
20/******************************************************************************
21 * Low level routines and constructors
22 ******************************************************************************/
23
24static inline int
25round_length (int n) {
26 n= (n + 3) & (0xfffffffc);
27 if (n < 24) return n;
28 int i= 32;
29 while (n > i)
30 i<<= 1;
31 return i;
32}
33
35 : n (n2),
36 a ((n == 0) ? ((char*) NULL) : tm_new_array<char> (round_length (n))) {}
37
38void
40 int nn= round_length (n);
41 int mm= round_length (m);
42 if (mm != nn) {
43 if (mm != 0) {
44 if (nn != 0) {
46 }
47 else {
49 }
50 }
51 else if (nn != 0) tm_delete_array (a);
52 }
53 n= m;
54}
55
58 rep->a[0]= c;
59}
60
61string::string (char c, int n) {
63 for (int i= 0; i < n; i++)
64 rep->a[i]= c;
65}
66
67string::string (const char* a) {
68 int i, n= strlen (a);
70 for (i= 0; i < n; i++)
71 rep->a[i]= a[i];
72}
73
74string::string (const char* a, int n) {
75 int i;
77 for (i= 0; i < n; i++)
78 rep->a[i]= a[i];
79}
80
81/******************************************************************************
82 * Common routines for strings
83 ******************************************************************************/
84
85bool
86string::operator== (const char* s) {
87 int i, n= rep->n;
88 char* S= rep->a;
89 for (i= 0; i < n; i++) {
90 if (s[i] != S[i]) return false;
91 if (s[i] == '\0') return false;
92 }
93 return (s[i] == '\0');
94}
95
96bool
97string::operator!= (const char* s) {
98 int i, n= rep->n;
99 char* S= rep->a;
100 for (i= 0; i < n; i++) {
101 if (s[i] != S[i]) return true;
102 if (s[i] == '\0') return true;
103 }
104 return (s[i] != '\0');
105}
106
107bool
109 return ((string_view<char>) *this) == ((string_view<char>) a);
110}
111
112bool
114 return ((string_view<char>) *this) != ((string_view<char>) a);
115}
116
117string
118string::operator() (int begin, int end) {
119 if (end <= begin) return string ();
120
121 int i;
122 begin= max (min (rep->n, begin), 0);
123 end = max (min (rep->n, end), 0);
124 string r (end - begin);
125 for (i= begin; i < end; i++)
126 r[i - begin]= rep->a[i];
127 return r;
128}
129
130string
131copy (string s) {
132 int i, n= N (s);
133 string r (n);
134 for (i= 0; i < n; i++)
135 r[i]= s[i];
136 return r;
137}
138
139string&
140operator<< (string& a, char x) {
141 a->resize (N (a) + 1);
142 a[N (a) - 1]= x;
143 return a;
144}
145
146string&
147operator<< (string& a, string b) {
148 int i, k1= N (a), k2= N (b);
149 a->resize (k1 + k2);
150 for (i= 0; i < k2; i++)
151 a[i + k1]= b[i];
152 return a;
153}
154
155string
156operator* (string a, string b) {
157 int i, n1= N (a), n2= N (b);
158 string c (n1 + n2);
159 for (i= 0; i < n1; i++)
160 c[i]= a[i];
161 for (i= 0; i < n2; i++)
162 c[i + n1]= b[i];
163 return c;
164}
165
166string
167operator* (const char* a, string b) {
168 return string (a) * b;
169}
170
171string
172operator* (string a, const char* b) {
173 return a * string (b);
174}
175
176bool
177operator< (string s1, string s2) {
178 return ((string_view<char>) s1) < ((string_view<char>) s2);
179}
180
181bool
182operator<= (string s1, string s2) {
183 return ((string_view<char>) s1) <= ((string_view<char>) s2);
184}
185
186int
187hash (string s) {
188 int h= 0;
189 for (char ch : s) {
190 h= (h << 9) + (h >> 23);
191 h= h + ((int) ch);
192 }
193 return h;
194}
195
196/******************************************************************************
197 * Conversion routines
198 ******************************************************************************/
199
200bool
201as_bool (string s) {
202 return (s == "true" || s == "#t");
203}
204
205int
206as_int (string s) {
207 int i= 0, n= N (s), val= 0;
208 if (n == 0) return 0;
209 if (s[0] == '+') i++;
210 if (s[0] == '-') i++;
211 while (i < n) {
212 if (s[i] < '0') break;
213 if (s[i] > '9') break;
214 val*= 10;
215 val+= (int) (s[i] - '0');
216 i++;
217 }
218 if (s[0] == '-') val= -val;
219 return val;
220}
221
222long int
223as_long_int (string s) {
224 int i= 0, n= N (s);
225 long int val= 0;
226 if (n == 0) return 0;
227 if (s[0] == '+') i++;
228 if (s[0] == '-') i++;
229 while (i < n) {
230 if (s[i] < '0') break;
231 if (s[i] > '9') break;
232 val*= 10;
233 val+= (int) (s[i] - '0');
234 i++;
235 }
236 if (s[0] == '-') val= -val;
237 return val;
238}
239
240double
241as_double (string s) {
242 double x= 0.0;
243 {
244 int i, n= N (s);
245 STACK_NEW_ARRAY (buf, char, n + 1);
246 for (i= 0; i < n; i++)
247 buf[i]= s[i];
248 buf[n]= '\0';
249 sscanf (buf, "%lf", &x);
250 STACK_DELETE_ARRAY (buf);
251 } // in order to avoid segmentation fault due to compiler bug
252 return x;
253}
254
255char*
256as_charp (string s) {
257 int i, n= N (s);
258 char* s2= tm_new_array<char> (n + 1);
259 for (i= 0; i < n; i++)
260 s2[i]= s[i];
261 s2[n]= '\0';
262 return s2;
263}
264
265string
267 if (f) return string ("true");
268 else return string ("false");
269}
270
271string
273 return string ((std::to_string (i)).c_str ());
274}
275
276string
278 return string ((std::to_string (i)).c_str ());
279}
280
281string
283 return string ((std::to_string (i)).c_str ());
284}
285
286string
287as_string (unsigned int i) {
288 char buf[64];
289 sprintf (buf, "%u", i);
290 // sprintf (buf, "%u\0", i);
291 return string (buf);
292}
293
294string
295as_string (unsigned long int i) {
296 char buf[64];
297 sprintf (buf, "%lu", i);
298 // sprintf (buf, "%lu\0", i);
299 return string (buf);
300}
301
302string
303as_string (double x) {
304 char buf[64];
305 sprintf (buf, "%g", x);
306 // sprintf (buf, "%g\0", x);
307 return string (buf);
308}
309
310string
311as_string (const char* s) {
312 return string (s);
313}
314
315bool
316is_empty (string s) {
317 return N (s) == 0;
318}
319
320bool
321is_bool (string s) {
322 return (s == "true") || (s == "false");
323}
324
325bool
326is_int (string s) {
327 int i= 0, n= N (s);
328 if (n == 0) return false;
329 if (s[i] == '+') i++;
330 if (s[i] == '-') i++;
331 if (i == n) return false;
332 for (; i < n; i++)
333 if ((s[i] < '0') || (s[i] > '9')) return false;
334 return true;
335}
336
337bool
338is_double (string s) {
339 int i= 0, n= N (s);
340 if (n == 0) return false;
341 if (s[i] == '+') i++;
342 if (s[i] == '-') i++;
343 if (i == n) return false;
344 for (; i < n; i++)
345 if ((s[i] < '0') || (s[i] > '9')) break;
346 if (i == n) return true;
347 if (s[i] == '.') {
348 i++;
349 if (i == n) return false;
350 for (; i < n; i++)
351 if ((s[i] < '0') || (s[i] > '9')) break;
352 }
353 if (i == n) return true;
354 if (s[i++] != 'e') return false;
355 if (s[i] == '+') i++;
356 if (s[i] == '-') i++;
357 if (i == n) return false;
358 for (; i < n; i++)
359 if ((s[i] < '0') || (s[i] > '9')) return false;
360 return true;
361}
362
363bool
364is_charp (string s) {
365 (void) s;
366 return true;
367}
368
369bool
370is_quoted (string s) {
371 int n= N (s);
372 return (n >= 2) && (s[0] == '\"') && (s[n - 1] == '\"');
373}
374
375bool
376is_id (string s) {
377 int i= 0, n= N (s);
378 if (n == 0) return false;
379 for (i= 0; i < n; i++) {
380 if ((i > 0) && (s[i] >= '0') && (s[i] <= '9')) continue;
381 if ((s[i] >= 'a') && (s[i] <= 'z')) continue;
382 if ((s[i] >= 'A') && (s[i] <= 'Z')) continue;
383 if (s[i] == '_') continue;
384 return false;
385 }
386 return true;
387}
int N(array< T > a)
Get the length of the array.
Definition array.hpp:170
int round_length(int n, size_t s)
Definition array.ipp:21
#define STACK_NEW_ARRAY(name, T, size)
Definition basic.hpp:43
#define STACK_DELETE_ARRAY(name)
Definition basic.hpp:44
blackbox b[13]
The list class represents a linked list.
Definition list.hpp:48
char * a
Definition string.hpp:26
void resize(int n)
Definition string.cpp:39
string()
Definition string.hpp:42
bool operator!=(const char *s)
Definition string.cpp:97
char * end()
Definition string.hpp:55
char * begin()
Definition string.hpp:54
bool operator==(const char *s)
Definition string.cpp:86
string operator()(int start, int end)
Definition string.cpp:118
C * tm_new_array(int n)
void tm_delete_array(C *Ptr)
SI min(SI i, SI j)
Returns the minimum of two signed integers.
Definition minmax.hpp:27
SI max(SI i, SI j)
Returns the maximum of two signed integers.
Definition minmax.hpp:40
int as_int(string s)
Definition string.cpp:206
bool operator<(string s1, string s2)
Definition string.cpp:177
bool is_charp(string s)
Definition string.cpp:364
bool operator<=(string s1, string s2)
Definition string.cpp:182
static int round_length(int n)
Definition string.cpp:25
string as_string_bool(bool f)
Definition string.cpp:266
char * as_charp(string s)
Definition string.cpp:256
bool is_double(string s)
Definition string.cpp:338
bool as_bool(string s)
Definition string.cpp:201
string as_string(int16_t i)
Definition string.cpp:272
bool is_int(string s)
Definition string.cpp:326
long int as_long_int(string s)
Definition string.cpp:223
double as_double(string s)
Definition string.cpp:241
bool is_bool(string s)
Definition string.cpp:321
string operator*(string a, string b)
Definition string.cpp:156
string copy(string s)
Definition string.cpp:131
bool is_id(string s)
Definition string.cpp:376
string & operator<<(string &a, char x)
Definition string.cpp:140
bool is_quoted(string s)
Definition string.cpp:370
bool is_empty(string s)
Definition string.cpp:316
int hash(string s)
Definition string.cpp:187
base class of resources
Definition resource.hpp:23