Lolly 1.4.28
Loading...
Searching...
No Matches
Functions
string.cpp File Reference
#include "string.hpp"
#include "basic.hpp"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
Include dependency graph for string.cpp:

Go to the source code of this file.

Functions

static int round_length (int n)
 
string copy (string s)
 
stringoperator<< (string &a, char x)
 
stringoperator<< (string &a, string b)
 
string operator* (string a, string b)
 
string operator* (const char *a, string b)
 
string operator* (string a, const char *b)
 
bool operator< (string s1, string s2)
 
bool operator<= (string s1, string s2)
 
int hash (string s)
 
bool as_bool (string s)
 
int as_int (string s)
 
long int as_long_int (string s)
 
double as_double (string s)
 
charas_charp (string s)
 
string as_string_bool (bool f)
 
string as_string (int16_t i)
 
string as_string (int32_t i)
 
string as_string (int64_t i)
 
string as_string (unsigned int i)
 
string as_string (unsigned long int i)
 
string as_string (double x)
 
string as_string (const char *s)
 
bool is_empty (string s)
 
bool is_bool (string s)
 
bool is_int (string s)
 
bool is_double (string s)
 
bool is_charp (string s)
 
bool is_quoted (string s)
 
bool is_id (string s)
 

Function Documentation

◆ round_length()

static int round_length ( int n)
inlinestatic

Definition at line 25 of file string.cpp.

25 {
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}

◆ copy()

string copy ( string s)

Definition at line 131 of file string.cpp.

131 {
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}
int N(array< T > a)
Get the length of the array.
Definition array.hpp:170
The list class represents a linked list.
Definition list.hpp:48

◆ operator<<() [1/2]

string & operator<< ( string & a,
char x )

Definition at line 139 of file string.cpp.

140 {
141 a->resize (N (a) + 1);
142 a[N (a) - 1]= x;
143 return a;
144}

◆ operator<<() [2/2]

string & operator<< ( string & a,
string b )

Definition at line 146 of file string.cpp.

147 {
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}
blackbox b[13]

◆ operator*() [1/3]

Definition at line 156 of file string.cpp.

156 {
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}

◆ operator*() [2/3]

Definition at line 167 of file string.cpp.

167 {
168 return string (a) * b;
169}

◆ operator*() [3/3]

Definition at line 172 of file string.cpp.

172 {
173 return a * string (b);
174}

◆ operator<()

bool operator< ( string s1,
string s2 )

Definition at line 176 of file string.cpp.

177 {
178 return ((string_view<char>) s1) < ((string_view<char>) s2);
179}

◆ operator<=()

bool operator<= ( string s1,
string s2 )

Definition at line 181 of file string.cpp.

182 {
183 return ((string_view<char>) s1) <= ((string_view<char>) s2);
184}

◆ hash()

int hash ( string s)

Definition at line 187 of file string.cpp.

187 {
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}

◆ as_bool()

bool as_bool ( string s)

Definition at line 201 of file string.cpp.

201 {
202 return (s == "true" || s == "#t");
203}

◆ as_int()

int as_int ( string s)

Definition at line 206 of file string.cpp.

206 {
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}

◆ as_long_int()

long int as_long_int ( string s)

Definition at line 223 of file string.cpp.

223 {
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}

◆ as_double()

double as_double ( string s)

Definition at line 241 of file string.cpp.

241 {
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}
#define STACK_NEW_ARRAY(name, T, size)
Definition basic.hpp:43
#define STACK_DELETE_ARRAY(name)
Definition basic.hpp:44

◆ as_charp()

char * as_charp ( string s)

Definition at line 256 of file string.cpp.

256 {
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}

◆ as_string_bool()

string as_string_bool ( bool f)

Definition at line 266 of file string.cpp.

266 {
267 if (f) return string ("true");
268 else return string ("false");
269}

◆ as_string() [1/7]

string as_string ( int16_t i)

Definition at line 272 of file string.cpp.

272 {
273 return string ((std::to_string (i)).c_str ());
274}

◆ as_string() [2/7]

string as_string ( int32_t i)

Definition at line 277 of file string.cpp.

277 {
278 return string ((std::to_string (i)).c_str ());
279}

◆ as_string() [3/7]

string as_string ( int64_t i)

Definition at line 282 of file string.cpp.

282 {
283 return string ((std::to_string (i)).c_str ());
284}

◆ as_string() [4/7]

string as_string ( unsigned int i)

Definition at line 287 of file string.cpp.

287 {
288 char buf[64];
289 sprintf (buf, "%u", i);
290 // sprintf (buf, "%u\0", i);
291 return string (buf);
292}

◆ as_string() [5/7]

string as_string ( unsigned long int i)

Definition at line 295 of file string.cpp.

295 {
296 char buf[64];
297 sprintf (buf, "%lu", i);
298 // sprintf (buf, "%lu\0", i);
299 return string (buf);
300}

◆ as_string() [6/7]

string as_string ( double x)

Definition at line 303 of file string.cpp.

303 {
304 char buf[64];
305 sprintf (buf, "%g", x);
306 // sprintf (buf, "%g\0", x);
307 return string (buf);
308}

◆ as_string() [7/7]

string as_string ( const char * s)

Definition at line 311 of file string.cpp.

311 {
312 return string (s);
313}

◆ is_empty()

bool is_empty ( string s)

Definition at line 316 of file string.cpp.

316 {
317 return N (s) == 0;
318}

◆ is_bool()

bool is_bool ( string s)

Definition at line 321 of file string.cpp.

321 {
322 return (s == "true") || (s == "false");
323}

◆ is_int()

bool is_int ( string s)

Definition at line 326 of file string.cpp.

326 {
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}

◆ is_double()

bool is_double ( string s)

Definition at line 338 of file string.cpp.

338 {
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}

◆ is_charp()

bool is_charp ( string s)

Definition at line 364 of file string.cpp.

364 {
365 (void) s;
366 return true;
367}

◆ is_quoted()

bool is_quoted ( string s)

Definition at line 370 of file string.cpp.

370 {
371 int n= N (s);
372 return (n >= 2) && (s[0] == '\"') && (s[n - 1] == '\"');
373}

◆ is_id()

bool is_id ( string s)

Definition at line 376 of file string.cpp.

376 {
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}