Debian Codegraph @

faststring2.h

raw
1 /* BaitFisher (version 1.2.8) a program for designing DNA target enrichment baits
2 * BaitFilter (version 1.0.6) a program for selecting optimal bait regions
3 * Copyright 2013-2017 by Christoph Mayer
4 *
5 * This source file is part of the BaitFisher-package.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with BaitFisher. If not, see <http://www.gnu.org/licenses/>.
19 *
20 *
21 * For any enquiries send an Email to Christoph Mayer
22 * [email protected]
23 *
24 * When publishing work that is based on the results please cite:
25 * Mayer et al. 2016: BaitFisher: A software package for multi-species target DNA enrichment probe design
26 *
27 */
28 #ifndef FASTSTRING_H
29 #define FASTSTRING_H
30
31 #include <iostream>
32 #include <fstream>
33 #include <cstdlib>
34 #include <cstring>
35 #include <cctype>
36 #include <climits>
37 #include <stdarg.h>
38 #include <vector>
39 #include <cmath>
40 #include <cerrno>
41 #include <list>
42 #include <cfloat>
43 #include <climits>
44
45 #define _MINBUFFER_CAPACITY 4
46
47 // Uncomment when using a 64bit windows machine.
48 // #define _WIN64
49
50 // Includes the symbols which occur in the blosum matrices:
51 const char toupper_lookup[] = {
52 '\0', '\1', '\2', '\3', '\4', '\5', '\6', '\7', '\10', '\11', // 0- 9
53 '\12', '\13', '\14', '\15', '\16', '\17', '\20', '\21', '\22', '\23', // 10-19
54 '\24', '\25', '\26', '\27', '\30', '\31', '\32', '\33', '\34', '\35', // 20-29
55 '\36', '\37', '\40', '\41', '\42', '\43', '\44', '\45', '\46', '\47', // 30-39
56 '\50', '\51', '\52', '\53', '\54', '\55', '\56', '\57', '\60', '\61', // 40-49
57 '\62', '\63', '\64', '\65', '\66', '\67', '\70', '\71', '\72', '\73', // 50-59
58 '\74', '\75', '\76', '\77','\100','\101','\102','\103','\104','\105', // 60-69
59 '\106','\107','\110','\111','\112','\113','\114','\115','\116','\117', // 70-79
60 '\120','\121','\122','\123','\124','\125','\126','\127','\130','\131', // 80-89
61 '\132','\133','\134','\135','\136','\137','\140','\101','\102','\103', // 90-99
62 '\104','\105','\106','\107','\110','\111','\112','\113','\114','\115', // 100-109
63 '\116','\117','\120','\121','\122','\123','\124','\125','\126','\127', // 110-119
64 '\130','\131','\132','\173','\174','\175','\176','\177', // 120-127
65 // extended ascii code:
66 '\200','\201',
67 '\202','\203','\204','\205','\206','\207','\210','\211','\212','\213',
68 '\214','\215','\216','\217','\220','\221','\222','\223','\224','\225',
69 '\226','\227','\230','\231','\232','\233','\234','\235','\236','\237',
70 '\240','\241','\242','\243','\244','\245','\246','\247','\250','\251',
71 '\252','\253','\254','\255','\256','\257','\260','\261','\262','\263',
72 '\264','\265','\266','\267','\270','\271','\272','\273','\274','\275',
73 '\276','\277','\300','\301','\302','\303','\304','\305','\306','\307',
74 '\310','\311','\312','\313','\314','\315','\316','\317','\320','\321',
75 '\322','\323','\324','\325','\326','\327','\330','\331','\332','\333',
76 '\334','\335','\336','\337','\340','\341','\342','\343','\344','\345',
77 '\346','\347','\350','\351','\352','\353','\354','\355','\356','\357',
78 '\360','\361','\362','\363','\364','\365','\366','\367','\370','\371',
79 '\372','\373','\374','\375','\376','\377'
80 };
81
82 const char tolower_lookup[] = {
83 '\0', '\1', '\2', '\3', '\4', '\5', '\6', '\7', '\10', '\11', // 0- 9
84 '\12', '\13', '\14', '\15', '\16', '\17', '\20', '\21', '\22', '\23', // 10-19
85 '\24', '\25', '\26', '\27', '\30', '\31', '\32', '\33', '\34', '\35', // 20-29
86 '\36', '\37', '\40', '\41', '\42', '\43', '\44', '\45', '\46', '\47', // 30-39
87 '\50', '\51', '\52', '\53', '\54', '\55', '\56', '\57', '\60', '\61', // 40-49
88 '\62', '\63', '\64', '\65', '\66', '\67', '\70', '\71', '\72', '\73', // 50-59
89 '\74', '\75', '\76', '\77','\100','\141','\142','\143','\144','\145', // 60-69
90 '\146','\147','\150','\151','\152','\153','\154','\155','\156','\157', // 70-79
91 '\160','\161','\162','\163','\164','\165','\166','\167','\170','\171', // 80-89
92 '\172','\133','\134','\135','\136','\137','\140','\141','\142','\143', // 90-99
93 '\144','\145','\146','\147','\150','\151','\152','\153','\154','\155', // 100-109
94 '\156','\157','\160','\161','\162','\163','\164','\165','\166','\167', // 110-119
95 '\170','\171','\172','\173','\174','\175','\176','\177', // 120-127
96 // extended ascii code:
97 '\200','\201',
98 '\202','\203','\204','\205','\206','\207','\210','\211','\212','\213',
99 '\214','\215','\216','\217','\220','\221','\222','\223','\224','\225',
100 '\226','\227','\230','\231','\232','\233','\234','\235','\236','\237',
101 '\240','\241','\242','\243','\244','\245','\246','\247','\250','\251',
102 '\252','\253','\254','\255','\256','\257','\260','\261','\262','\263',
103 '\264','\265','\266','\267','\270','\271','\272','\273','\274','\275',
104 '\276','\277','\300','\301','\302','\303','\304','\305','\306','\307',
105 '\310','\311','\312','\313','\314','\315','\316','\317','\320','\321',
106 '\322','\323','\324','\325','\326','\327','\330','\331','\332','\333',
107 '\334','\335','\336','\337','\340','\341','\342','\343','\344','\345',
108 '\346','\347','\350','\351','\352','\353','\354','\355','\356','\357',
109 '\360','\361','\362','\363','\364','\365','\366','\367','\370','\371',
110 '\372','\373','\374','\375','\376','\377'
111 };
112
113
114
115 #define macromin(x,y) ((x)<(y) ? (x) : (y))
116 //#define macromax(x,y) ((x)>(y) ? (x) : (y))
117
118 class faststring;
119
120 template <typename Container>
121 int
122 split (Container &l, const faststring &s, char const * const ws = "\r\n\t\v\f ");
123
124 template <typename Container>
125 int
126 split_at (Container &l, const faststring &s, const faststring &delim);
127
128 template <typename Container>
129 int
130 split_respect (Container &l, const faststring &s, char const * const ws = "\r\n\t\v\f ");
131
132 template <typename Container>
133 int
134 split_strict (Container &l, const faststring &s, char const * const ws = " \t\n");
135
136
137
138 namespace {
139 inline bool is_in_symbol_list__(char c, const char* wstr="\r\n\t\v\f ")
140 {
141 return (strchr(wstr,c) != NULL);
142 }
143
144 inline bool is_in_symbol_list__(char c, const char* wstr, size_t n)
145 {
146 return (memchr(wstr,c,n) != NULL);
147 }
148
149 const char * find_fundamental ( const char *b1, const char *e1,
150 const char *b2, const char *e2)
151 {
152 for (; b1 < e1 ; ++b1)
153 {
154 const char * tb1 = b1;
155 const char * tb2 = b2;
156 for (; tb2 < e2 && tb1 < e1; ++tb2, ++tb1)
157 {
158 if (*tb1 != *tb2)
159 break;
160 }
161 if (tb2 == e2) // we have a match
162 {
163 return b1;
164 }
165 }
166 return NULL;
167 }
168
169 const char * rfind_fundamental ( const char *rb1, const char *re1,
170 const char *rb2, const char *re2)
171 {
172 for (; rb1 > re1 ; --rb1)
173 {
174 const char * trb1 = rb1;
175 const char * trb2 = rb2;
176 for (; trb2 > re2 && trb1 > re1; --trb2, --trb1)
177 {
178 if (*trb1 != *trb2)
179 break;
180 }
181 if (trb2 == re2) // we have a match
182 {
183 return trb1+1;
184 }
185 }
186 return NULL;
187 }
188
189
190 } // End namespace
191
192
193
194
195 class faststring
196 {
197 public:
198 // Shadow the "global" size_t typedef for this class.
199 typedef unsigned size_t;
200 static const size_t npos = UINT_MAX;
201
202 // Possibly even more efficient:
203 // Use pointer _end, _capacity_end instead of _len, _capacity
204
205
206 private:
207 char *_buf;
208 size_t _len;
209 size_t _capacity; // Capacity of container, i.e number of bytes available
210 // for writing. If a capacity is requested by the user,
211 // an additional byte is reserved in order to be able to append
212 // the additional \0 which is appended by some function calls.
213 // Consequently, the capacity reported to user is _capacity-1.
214
215 protected:
216
217 public:
218 ~faststring ()
219 {
220 if (_buf)
221 std::free(_buf);
222 }
223
224 faststring ():_buf(NULL), _len(0), _capacity(0) // :_len(0), _capacity(a)
225 {
226 // _buf = NULL(char *)malloc(_MINBUFFER_CAPACITY);
227 }
228
229 faststring (const char *str)
230 {
231 _len = strlen(str);
232 _capacity = _len+1;
233 _buf = (char *)malloc(_capacity);
234 if (_buf == 0)
235 {
236 std::cerr << "Critical error: malloc failed when requesting " << _capacity << " bytes in faststring constructor." << std::endl;
237 exit(-1);
238 }
239 memcpy(_buf, str, _len);
240
241 }
242
243 faststring (const char *str_begin, const char *str_end)
244 {
245 if (str_end > str_begin)
246 {
247 _len = str_end - str_begin;
248 _capacity = _len + 1;
249 _buf = (char *)malloc(_capacity);
250 if (_buf == 0)
251 {
252 std::cerr << "Critical error: malloc failed when requesting " << _capacity << " bytes in faststring constructor." << std::endl;
253 exit(-1);
254 }
255 memcpy(_buf, str_begin, _len);
256 }
257 else
258 {
259 _buf = NULL;
260 _len = _capacity = 0;
261 }
262 }
263
264 faststring (const faststring &a):_len(a._len),_capacity(a._len+1)
265 {
266 if (a._buf == NULL)
267 {
268 _buf = 0;
269 _len = 0;
270 _capacity = 0;
271 return;
272 }
273
274 _buf = (char *)malloc(_capacity);
275 if (_buf == 0)
276 {
277 std::cerr << "Critical error: malloc failed when requesting " << _capacity << " bytes in faststring constructor." << std::endl;
278 exit(-1);
279 }
280 memcpy(_buf, a._buf, _len);
281 }
282
283 faststring(char c, size_t n=1):_len(n),_capacity(n+1)
284 {
285 _buf = (char *)malloc(_capacity);
286 if (_buf == 0)
287 {
288 std::cerr << "Critical error: malloc failed when requesting " << _capacity << " bytes in faststring constructor." << std::endl;
289 exit(-1);
290 }
291
292 memset(_buf, c, _len);
293 /*
294 size_t i;
295 for (i=0; i<n; ++i)
296 _buf[i] = c; */
297 }
298
299 faststring(int i)
300 {
301 _capacity = (size_t)(sizeof(i)*8*.31+3); // bits * 1/log_2(10) + 3 for ciel and sign and \0
302 _buf = (char *)malloc(_capacity);
303 if (_buf == 0)
304 {
305 std::cerr << "Critical error: malloc failed when requesting " << _capacity << " bytes in faststring constructor." << std::endl;
306 exit(-1);
307 }
308 snprintf(_buf, _capacity, "%d", i);
309 _len = strlen(_buf);
310 // std::cout << "Capacity: " << _capacity << std::endl;
311 }
312
313 faststring(unsigned i)
314 {
315 _capacity = (size_t)(sizeof(i)*8*.31+3); // bits * 1/log_2(10) + 3 for ciel and sign and \0
316 _buf = (char *)malloc(_capacity);
317 if (_buf == 0)
318 {
319 std::cerr << "Critical error: malloc failed when requesting " << _capacity << " bytes in faststring constructor." << std::endl;
320 exit(-1);
321 }
322 snprintf(_buf, _capacity, "%d", i);
323 _len = strlen(_buf);
324 // std::cout << "Capacity: " << _capacity << std::endl;
325 }
326
327 // Prints the unsigned i into the string. The number appears right-justified.
328 // len is the minimum length of the field. If the number needs more digits it well get them.
329 // If the number need less than len digits they will be left padded with the fill char.
330 // Example: Calling constructor with 999, '#', 5 will result in the string "##999", for 999, '#', 2 we get "999".
331 faststring(unsigned i, char fill, int len)
332 {
333 int num_digits = (i>0) ? ((int) std::log10 ((double)i))+1 : 1; // We take care not to pass 0 to log10
334
335 _capacity = num_digits+1;
336 if (len >= num_digits)
337 _capacity = len+1;
338 _buf = (char *)malloc(_capacity);
339 if (_buf == 0)
340 {
341 std::cerr << "Critical error: malloc failed when requesting " << _capacity << " bytes in faststring constructor." << std::endl;
342 exit(-1);
343 }
344 snprintf(_buf,_capacity, "%0*u", len, i); // left padded with zeros (0)
345 // Replace left padded zeros with fill char
346
347 size_t pos = 0;
348 size_t e = _capacity-2;
349 while (pos < e && _buf[pos]=='0')
350 {
351 _buf[pos] = fill;
352 ++pos;
353 }
354 ++e;
355 while (pos < e && _buf[pos] )
356 ++pos;
357 _len = pos;
358 // std::cout << "Capacity: " << _capacity << std::endl;
359 }
360
361 faststring(long i)
362 {
363 _capacity = (size_t)(sizeof(i)*8*.31+3); // bits * 1/log_2(10) + 3 for ciel and sign and \0
364 _buf = (char *)malloc(_capacity);
365 if (_buf == 0)
366 {
367 std::cerr << "Critical error: malloc failed when requesting " << _capacity << " bytes in faststring constructor." << std::endl;
368 exit(-1);
369 }
370 snprintf(_buf, _capacity, "%ld", i);
371 _len = strlen(_buf);
372 }
373
374 faststring(unsigned long i)
375 {
376 _capacity = (size_t)(sizeof(i)*8*.31+3); // bits * 1/log_2(10) + 3 for ciel and sign and \0
377 _buf = (char *)malloc(_capacity);
378 if (_buf == 0)
379 {
380 std::cerr << "Critical error: malloc failed when requesting " << _capacity << " bytes in faststring constructor." << std::endl;
381 exit(-1);
382 }
383 snprintf(_buf, _capacity,"%lu", i);
384 _len = strlen(_buf);
385 }
386
387 faststring(float f, int pres=6)
388 {
389 _capacity = FLT_MAX_10_EXP + pres + 3; // +3 sign, decimal point and \0
390 _buf = (char *)malloc(_capacity);
391 if (_buf == 0)
392 {
393 std::cerr << "Critical error: malloc failed when requesting " << _capacity << " bytes in faststring constructor." << std::endl;
394 exit(-1);
395 }
396 snprintf(_buf, _capacity,"%.*f", pres, f);
397 _len = strlen(_buf);
398 }
399
400 faststring(double f, int pres=14)
401 {
402 _capacity = DBL_MAX_10_EXP + pres + 3; // +3 sign, decimal point and \0
403 _buf = (char *)malloc(_capacity);
404 if (_buf == 0)
405 {
406 std::cerr << "Critical error: malloc failed when requesting " << _capacity << " bytes in faststring constructor." << std::endl;
407 exit(-1);
408 }
409 snprintf(_buf,_capacity, "%.*f", pres, f);
410 _len = strlen(_buf);
411 }
412
413 // In the following function, the parameter bool b is not used directly. It has been introduced to distinguish this variant of the constructor
414 // from the variants declared above.
415 faststring(double f, int pres, bool b)
416 {
417 (void) b;
418
419 _capacity = DBL_MAX_10_EXP + pres + 5; // +5 sign, decimal point, E, sign and \0
420 _buf = (char *)malloc(_capacity);
421 if (_buf == 0)
422 {
423 std::cerr << "Critical error: malloc failed when requesting " << _capacity << " bytes in faststring constructor." << std::endl;
424 exit(-1);
425 }
426 snprintf(_buf,_capacity, "%.*g", pres, f);
427 _len = strlen(_buf);
428
429 // std::cerr << "faststring len: " << _len << std::endl;
430 }
431
432 faststring &operator+=(const faststring &s)
433 {
434 this->append(s);
435 return *this;
436 }
437
438 void push_back(char c)
439 {
440 if (_capacity == _len)
441 {
442 if (_buf == 0)
443 {
444 _capacity = _MINBUFFER_CAPACITY;
445 _buf = (char *)malloc(_capacity);
446 if (_buf == 0)
447 {
448 std::cerr << "Critical error: malloc failed when requesting " << _capacity << " bytes in faststring push_back." << std::endl;
449 exit(-1);
450 }
451 }
452 else
453 {
454 _capacity <<= 1;
455 _buf = (char *)realloc(_buf, _capacity);
456 if (_buf == 0)
457 {
458 std::cerr << "Critical error: realloc failed when requesting " << _capacity << " bytes in faststring push_back." << std::endl;
459 exit(-1);
460 }
461 }
462 }
463 _buf[_len] = c;
464 ++_len;
465 }
466
467
468
469 void pop_back()
470 {
471 if (_len >0)
472 --_len;
473 }
474
475 char get_last()
476 {
477 if (_len >0)
478 return _buf[_len-1];
479 else
480 return '\0';
481 }
482
483 char get_last_and_pop()
484 {
485 if (_len >0)
486 {
487 --_len;
488 return _buf[_len];
489 }
490 else
491 return '\0';
492 }
493
494 // Sometimes strings have to end with a certain character.
495 // If this is present, nothing will be done.
496 // If this is not present, the character will be appended.
497 void ensure_string_ends_with(char c)
498 {
499 if (get_last() != c)
500 {
501 push_back(c);
502 }
503 }
504
505 /* void ensure_string_does_not_end_with(char c) */
506 /* { */
507 /* while (_len > 0 && get_last() == c) */
508 /* { */
509 /* pop_back(); */
510 /* } */
511 /* } */
512
513 // chomp in perl: (Removing a trailing \0 is not allowed.
514 void pop_trailing_characters(char c)
515 {
516 // get_last() and pop_back() both ensure that _len>0
517 if (c == '\0') // Problem: get_last returns \0 if the len=0, so we would end up with an infinite loop if we remove \0 in an empty string.
518 return;
519
520 while (get_last() == c)
521 {
522 pop_back();
523 }
524 }
525
526 const char *c_str()
527 {
528 if (_capacity == 0) // We reserve 1 byte for the empty cstring.
529 {
530 _capacity = 1;
531 _len = 0;
532 _buf = (char *)malloc(_capacity);
533 if (_buf == 0)
534 {
535 std::cerr << "Critical error: malloc failed when requesting " << _capacity << " bytes in faststring c_str()." << std::endl;
536 exit(-1);
537 }
538 *_buf = '\0';
539 return _buf;
540 }
541
542 if (_capacity == _len)
543 {
544 ++_capacity;
545 _buf = (char *)realloc(_buf, _capacity);
546 }
547
548 // if (_buf[_len] == '\0')
549 // return _buf;
550
551 _buf[_len] = '\0';
552 return _buf;
553 }
554
555
556 char *begin() const
557 {
558 return _buf;
559 }
560
561 char *begin_str() const
562 {
563 return _buf;
564 }
565
566 char *data() const
567 {
568 return _buf;
569 }
570
571 char *end() const
572 {
573 return _buf+_len;
574 }
575
576 char *end_str() const
577 {
578 return _buf+_len;
579 }
580
581 char *rbegin() const
582 {
583 return _buf+_len-1;
584 }
585
586
587 char *rend() const
588 {
589 return _buf-1;
590 }
591
592 bool empty() const
593 {
594 return (_len == 0);
595 }
596
597 void reverse()
598 {
599 char *i1, *i2;
600 char c;
601
602 i1 = begin();
603 i2 = rbegin();
604
605 while (i1<i2)
606 {
607 c = *i1;
608 *i1 = *i2;
609 *i2 = c;
610 ++i1;
611 --i2;
612 }
613 }
614
615
616 //=================================================
617 // assign family:
618 //=================================================
619 // string& assign ( const string& str );
620 // string& assign ( const string& str, size_t pos, size_t n );
621 // string& assign ( const char* s, size_t n );
622 // string& assign ( const char* s );
623 // string& assign ( size_t n, char c );
624 // template <class InputIterator>
625 // string& assign ( InputIterator first, InputIterator last );
626
627 faststring& assign(const char *str)
628 {
629 _len = strlen(str);
630 reserve(_len);
631 memcpy(_buf, str, _len);
632 return *this;
633 }
634
635 // n is the maximum number of chars that will be assigned.
636 faststring& assign(const char *str, size_t n)
637 {
638 _len = n;
639 size_t ll = strlen(str);
640 if (_len > ll) // if n > ll, we have to reduce _len to ll
641 _len = ll;
642 reserve(_len);
643 memcpy(_buf, str, _len);
644 return *this;
645 }
646
647 faststring& assign(const char *str_begin, const char *str_end)
648 {
649 if (str_end > str_begin)
650 {
651 _len = str_end - str_begin;
652 reserve(_len);
653 memcpy(_buf, str_begin, _len);
654 }
655 else
656 {
657 clear();
658 }
659 return *this;
660 }
661
662 faststring& assign(const faststring &str)
663 {
664 _len = str._len;
665 reserve(_len);
666 memcpy(_buf, str._buf, _len);
667 return *this;
668 }
669
670 faststring& assign(const faststring &str, size_t pos, size_t n=npos )
671 {
672 if (n == npos || n+pos > str._len)
673 n = str._len-pos;
674 _len = n;
675 reserve(_len+1);
676 memcpy(_buf, str._buf+pos, n);
677 return *this;
678 }
679
680 // Could be made more efficient. TODO
681 faststring& assign( size_t n, char c )
682 {
683 clear();
684 reserve(n);
685 size_t i;
686 for (i=0; i<n; ++i)
687 push_back(c);
688 return *this;
689 }
690
691
692
693
694 //=================================================
695 // The family of append functions:
696 //=================================================
697 // basic_string& append(const basic_string& s) basic_string Append s to *this.
698 // basic_string& append(const basic_string& s,
699 // size_type pos, size_type n)
700 // basic_string Append a substring of s to *this.
701 // basic_string& append(const charT* s) basic_string Append s to *this.
702 // basic_string& append(const charT* s, size_type n) basic_string Append the first n characters of s to *this.
703 // basic_string& append(size_type n, charT c) basic_string Append n copies of c to *this.
704 // template <class InputIterator>
705 // basic_string& append(InputIterator first, InputIterator last)
706 // basic_string Append a range to *this.
707
708
709 // step imp-fin
710 faststring& append(const char *s)
711 {
712 int old_len = _len;
713 int s_len = strlen(s);
714 int new_len = old_len + s_len;
715
716 _len = new_len;
717
718 reserve(new_len);
719 memcpy(_buf+old_len, s, s_len);
720 return *this;
721 }
722
723 // step imp-fin
724 faststring& append(const faststring &s)
725 {
726 int old_len = _len;
727 int s_len = s._len;
728 int new_len = old_len + s_len;
729
730 _len = new_len;
731
732 reserve(new_len);
733 memcpy(_buf+old_len, s._buf, s_len);
734 return *this;
735 }
736
737 // step imp-fin
738 faststring& append(const faststring& s, size_t pos, size_t n=npos)
739 {
740 if (n==npos || pos+n > s.length() )
741 n = s.length()-pos;
742
743 int old_len = _len;
744 int new_len = old_len + n;
745
746 _len = new_len;
747
748 reserve(new_len);
749 memcpy(_buf+old_len, s._buf+pos, n);
750 return *this;
751 }
752
753 // step imp-fin
754 faststring& append(const char* s, size_t n)
755 {
756 int old_len = _len;
757 int new_len = old_len + n;
758
759 _len = new_len;
760
761 reserve(new_len);
762 memcpy(_buf+old_len, s, n);
763 return *this;
764 }
765
766 // step imp-fin
767 faststring& append(size_t n, char c)
768 {
769 size_t old_len = _len;
770 size_t new_len = old_len + n;
771
772 reserve(new_len);
773 size_t i;
774 for (i=0; i<n; ++i)
775 push_back(c);
776 return *this;
777 }
778
779 // step imp-fin
780 faststring& append(const char * first_it, const char * end_it)
781 {
782 size_t old_len = _len;
783 size_t n = end_it-first_it;
784 size_t new_len = old_len + n;
785
786 _len = new_len;
787
788 reserve(new_len);
789 memcpy(_buf+old_len, first_it, n);
790 return *this;
791 }
792
793
794
795 //---------------------------------------
796 // Side effects: Invalidates pointers.
797 //---------------------------------------
798 void reserve(size_t s)
799 {
800 if (_capacity >= s)
801 return;
802
803 ++s; // Reserve one more char in case we have to append a \0 at end of string later.
804 // Well this could be seen as a wast of memory.
805 if (_buf == NULL)
806 {
807 _buf = (char *)malloc(s);
808 if (_buf == 0)
809 {
810 std::cerr << "Critical error: Trying again to malloc " << s << " bytes in faststring reserve()." << std::endl;
811 _buf = (char *)malloc(s);
812 }
813 if (_buf == 0)
814 {
815 std::cerr << "Critical error: malloc failed when requesting " << s << " bytes in faststring reserve()." << std::endl;
816 exit(-1);
817 }
818 }
819 else
820 {
821 _buf = (char *)realloc(_buf, s);
822 if (_buf == 0)
823 {
824 std::cerr << "Critical error: realloc failed when requesting " << s << " bytes in faststring reserve()." << std::endl;
825 exit(-1);
826 }
827 }
828 _capacity = s;
829 }
830
831
832
833 // Returns the part of the string following the last occurrence of delim.
834 // If the character does not occur, the full string is returned.
835 // The second parameter can be used to specify a minimum number of characters that need to
836 // be found before the extension. fpos_minimum == 1: The extension cannot start at index 0.
837 faststring get_string_extension_or_all(char delim, size_t fpos_minimum = 0)
838 {
839 size_t fpos = this->rfind(delim);
840
841 if (fpos < fpos_minimum)
842 fpos = npos;
843
844 if (fpos == npos)
845 return *this;
846 else
847 {
848 faststring res(this->begin()+fpos+1, this->end());
849 return res;
850 }
851 }
852
853 // Returns the part of the string following the last occurrence of delim.
854 // If the character does not occur, the empty string is returned.
855 // The second parameter can be used to specify a minimum number of characters that need to
856 // be found before the extension. fpos_minimum == 1: The extension cannot start at index 0.
857 faststring get_string_extension(char delim, size_t fpos_minimum = 0)
858 {
859 size_t fpos = this->rfind(delim);
860
861 if (fpos < fpos_minimum)
862 fpos = npos;
863
864 if (fpos == npos)
865 return faststring();
866 else
867 {
868 faststring res(this->begin()+fpos+1, this->end());
869 return res;
870 }
871 }
872
873
874 // Returns the part of the string before the last occurrence of delim.
875 // If the character does not occur, the empty string is returned.
876 // The second parameter can be used to specify a minimum number of characters that need to
877 // be found before the extension. fpos_minimum == 1: The extension cannot start at index 0.
878 faststring get_string_before_extension(char delim, size_t fpos_minimum = 0)
879 {
880 size_t fpos = this->rfind(delim);
881
882 if (fpos < fpos_minimum)
883 fpos = npos;
884
885 if (fpos == npos)
886 return faststring();
887 else
888 {
889 faststring res(this->begin(), this->begin()+fpos);
890 return res;
891 }
892 }
893
894 // Returns the part of the string before the last occurrence of delim.
895 // If the character does not occur, the full string is returned.
896 // The second parameter can be used to specify a minimum number of characters that need to
897 // be found before the extension. fpos_minimum == 1: The extension cannot start at index 0.
898 faststring get_string_before_extension_or_all(char delim, size_t fpos_minimum = 0)
899 {
900 size_t fpos = this->rfind(delim);
901
902 if (fpos < fpos_minimum)
903 fpos = npos;
904
905 if (fpos == npos)
906 return *this;
907 else
908 {
909 faststring res(this->begin(), this->begin()+fpos);
910 return res;
911 }
912 }
913
914
915 // Filename functions:
916 // Depricated: should be removed soon. Wrong name.
917 faststring filename_base()
918 {
919 faststring res(*this);
920 res.shorten(res.rfind('.'));
921 return res;
922 }
923
924 faststring filename_dirname_and_basename()
925 {
926 faststring res = filename_dirname();
927 res += "/";
928 res += filename_basename();
929 return res;
930 }
931
932 // Acts like the linux command does, but accepts only one argument.
933 faststring filename_basename()
934 {
935 if (size() == 0) // If string is empty before removing anything we return ".".
936 return ".";
937
938 faststring tmp = *this;
939 tmp.removeSpacesBack(" /"); // Removes trailing spaces and "/" characters
940
941 if (tmp.size() == 0) // Note: Multiple //// are collapsed to "/"
942 return "/";
943
944 faststring res = tmp.get_string_extension_or_all('/');
945 return res;
946 }
947
948 // Acts like the linux command does, but accepts only one argument.
949 faststring filename_dirname()
950 {
951 if (size() == 0) // If string is empty before removing anything we return ".".
952 return ".";
953
954 faststring tmp = *this;
955 tmp.removeSpacesBack(" /"); // Removes trailing spaces and "/" characters
956
957 if (tmp.size() == 0) // Note: Multiple //// are collapsed to "/"
958 return "/";
959
960 faststring before_delim = tmp.get_string_before_extension('/');
961
962 if (before_delim.empty() )
963 {
964 if (tmp[0] == '/') // Example: /usr -> dirname must be "/". If the only / is the root, this is the dirname
965 return "/";
966
967 return ".";
968 }
969 return before_delim;
970 }
971
972
973 // Deprecated. - Replaced by filename_dirname() which mimics the dirname command in linux much better.
974 faststring filename_path()
975 {
976 faststring res(*this);
977 size_t fpos = res.rfind('/');
978
979 if (fpos == npos)
980 return ".";
981 else
982 {
983 res.shorten(fpos);
984 return res;
985 }
986 }
987
988 faststring filename_basename_without_extension()
989 {
990 faststring tmp = filename_basename();
991 return tmp.get_string_before_extension_or_all('.',1);
992 }
993
994 faststring filename_dirname_and_basename_without_extension()
995 {
996 faststring tmp = filename_dirname();
997 tmp += "/";
998 tmp += filename_basename_without_extension();
999 return tmp;
1000 }
1001
1002
1003
1004 // Returns the last part of the filename, i.e. the part after the last relevant "/".
1005 // If this sting contains the basename this is the base name with extension.
1006 // If the string ends with a directory, this is the directory.
1007 // Some special cases are catched.
1008 // Deprecated and replaced by the filename_basename function.
1009
1010 /* faststring filename_FileOrDirName() */
1011 /* { */
1012 /* unsigned fpos = this->rfind('/'); */
1013
1014 /* if (fpos == npos) */
1015 /* return *this; */
1016 /* else */
1017 /* { */
1018 /* faststring res(this->begin()+fpos+1, this->end()); */
1019 /* return res; */
1020 /* } */
1021 /* } */
1022
1023 faststring filename_extension()
1024 {
1025 // If we do not restrict the search to the basename, dots in the path can cause problems
1026 // if the file does not have a file extension.
1027 faststring tmp = filename_basename();
1028 return tmp.get_string_extension('.', 1);
1029 }
1030
1031 // END filename functions
1032
1033 // Searches the string backwards for delim. Divides the string at delim if delim is found in backward direction
1034 // before break_at. So if in forward direction delim occurs only before break_at or if delim does not occur, the
1035 // string cannot be divided in the requested way. In this case pre gets the string und post is empty.
1036
1037 bool backwards_search_divide_at_break_at(char delim, char break_at, faststring &pre, faststring &post) const
1038 {
1039 size_t len = length();
1040 size_t i = len-1;
1041 int mode = 0;
1042
1043 while (i > 0)
1044 {
1045 if (get_unckecked(i) == delim) // Yea, we found delim behind any occurrence of break_at
1046 {
1047 mode = 2;
1048 break;
1049 }
1050 if (get_unckecked(i) == break_at) // We could not find the delim before break_at, so we have to stop.
1051 {
1052 mode = 1;
1053 break;
1054 }
1055
1056 --i;
1057 }
1058
1059 if (mode == 2)
1060 {
1061 pre = substr(0, i);
1062 post = substr(i+1, npos);
1063 return true;
1064 }
1065 else // if mode == 0 or 1
1066 {
1067 pre = *this;
1068 post = "";
1069 return false;
1070 }
1071 }
1072
1073 bool divide_at(char delim, faststring &pre, faststring &post) const
1074 {
1075 size_t pos = find(delim,0);
1076 if (pos == npos)
1077 {
1078 post.clear();
1079 pre = *this;
1080 return false;
1081 }
1082 post = substr(pos+1, npos);
1083 pre = substr(0, pos);
1084 return true;
1085 }
1086
1087 bool divide_at_first_of(const char *delims, faststring &pre, faststring &post) const
1088 {
1089 size_t pos = find_first_of(delims,0);
1090 if (pos == npos)
1091 return false;
1092 post = substr(pos+1, npos);
1093 pre = substr(0, pos);
1094 return true;
1095 }
1096
1097 bool divide_at_last_of(const char *delims, faststring &pre, faststring &post) const
1098 {
1099 size_t pos = find_last_of(delims);
1100 if (pos == npos)
1101 return false;
1102 post = substr(pos+1, npos);
1103 pre = substr(0, pos);
1104 return true;
1105 }
1106
1107 bool divide_at(faststring delim, faststring &pre, faststring &post) const
1108 {
1109 size_t pos = find(delim,0);
1110 if (pos == npos)
1111 return false;
1112 post = substr(pos+delim.size(), npos);
1113 pre = substr(0, pos);
1114 return true;
1115 }
1116
1117 size_t size() const
1118 {
1119 return _len;
1120 }
1121
1122 size_t length() const
1123 {
1124 return _len;
1125 }
1126
1127 size_t capacity() const
1128 {
1129 return _capacity; // One char is reserved for the \0 at end of string
1130 }
1131
1132 void set_unckecked(size_t pos, char c)
1133 {
1134 _buf[pos] = c;
1135 }
1136
1137 bool check_pos(size_t pos)
1138 {
1139 return (pos < _len);
1140 }
1141
1142 bool set(size_t pos, char c)
1143 {
1144 if ( check_pos(pos) )
1145 {
1146 _buf[pos] = c;
1147 return true;
1148 }
1149 else
1150 {
1151 return false;
1152 }
1153 }
1154
1155
1156 void get_unckecked(size_t pos, char &c) const
1157 {
1158 c = _buf[pos];
1159 }
1160
1161 char get_unckecked(size_t pos) const
1162 {
1163 return _buf[pos];
1164 }
1165
1166 char & at(size_t pos)
1167 {
1168 return _buf[pos];
1169 }
1170
1171 bool get(size_t pos, char &c)
1172 {
1173 if ( check_pos(pos) )
1174 {
1175 c = _buf[pos];
1176 return true;
1177 }
1178 else
1179 {
1180 return false;
1181 }
1182 }
1183
1184
1185 faststring& ToLower()
1186 {
1187 tolower();
1188 return *this;
1189 }
1190
1191 faststring& ToUpper()
1192 {
1193 toupper();
1194 return *this;
1195 }
1196
1197 void toupper_slow()
1198 {
1199 char *b = begin();
1200 char *e = end();
1201
1202 while (b != e)
1203 {
1204 *b = std::toupper(*b);
1205 ++b;
1206 }
1207 }
1208
1209 void toupper()
1210 {
1211 unsigned char *b = (unsigned char *) begin();
1212 unsigned char *e = (unsigned char *) end();
1213
1214 // char *b = (char *) begin();
1215 // char *e = (char *) end();
1216
1217 while (b != e)
1218 {
1219 *b = toupper_lookup[*b];
1220 ++b;
1221 }
1222 }
1223
1224 void erase_front(size_t n)
1225 {
1226 if (n>0)
1227 {
1228 if (n > _len)
1229 {
1230 _len = 0;
1231 return;
1232 }
1233 else
1234 {
1235 _len -=n;
1236 }
1237 memmove(_buf, _buf+n, _len);
1238 }
1239 }
1240
1241 void erase_back(size_t n)
1242 {
1243 if (n > _len)
1244 _len = 0;
1245 else
1246 _len -= n;
1247 }
1248
1249 void tolower_slow()
1250 {
1251 char *b = begin();
1252 char *e = end();
1253
1254 while (b != e)
1255 {
1256 *b = std::tolower(*b);
1257 ++b;
1258 }
1259 }
1260
1261 void tolower()
1262 {
1263 unsigned char *b = (unsigned char *) begin();
1264 unsigned char *e = (unsigned char *) end();
1265
1266 while (b != e)
1267 {
1268 *b = tolower_lookup[*b];
1269 ++b;
1270 }
1271 }
1272
1273 void shorten(size_t new_len)
1274 {
1275 _len = macromin(_len, new_len);
1276 }
1277
1278 void shorten_to_first_occurrence_of(char c)
1279 {
1280 size_t pos = find(c,0);
1281 shorten(pos);
1282 }
1283
1284 // See also resize for a different version of this function.
1285 // Appends multiple characters to fill the string if it is shorter than wanted.
1286 void fill_if_shorter ( size_t n, char c )
1287 {
1288 if (n <= _len)
1289 return;
1290 append(n-_len,c);
1291 }
1292
1293 faststring &operator=(const faststring &a)
1294 {
1295 _len = a._len;
1296
1297 // std::cerr << "_len: " << _len << std::endl;
1298
1299 reserve(_len+1);
1300 memcpy(_buf, a._buf, _len);
1301 return *this;
1302 }
1303
1304 friend bool operator==(const faststring &a, const faststring &b)
1305 {
1306 if (a._len != b._len)
1307 return false;
1308 return (strncmp(a._buf, b._buf, a._len) == 0);
1309 }
1310
1311 friend bool operator!=(const faststring &a, const faststring &b)
1312 {
1313 if (a._len != b._len)
1314 return true;
1315 return (strncmp(a._buf, b._buf, a._len) != 0);
1316 }
1317
1318 // Has been corrected in 03.2011
1319 friend bool operator<(const faststring &a, const faststring &b)
1320 {
1321 int l = macromin(a._len, b._len );
1322 // int res = (strncmp(a._buf, b._buf, l) < 0);
1323 short res = strncmp(a._buf, b._buf, l);
1324
1325 if (res == 0)
1326 return (a._len < b._len );
1327 // return res; // Was wrong in earlier versions.
1328 return (res < 0);
1329 }
1330
1331 // Caution: strcmp assumes \0 terminated strings.
1332 // Since we do not have \0 terminated strings we
1333 // use strncmp with n such that we do not pass the ends
1334 // of the strings.
1335 friend int fstrcmp(const faststring &a, const faststring &b)
1336 {
1337 int l = macromin(a._len, b._len );
1338 short res = strncmp(a._buf, b._buf, l);
1339
1340 if (res == 0) // We only compared the first l chars.
1341 {
1342 if (a._len < b._len)
1343 return -1;
1344 if (a._len > b._len)
1345 return 1;
1346 return 0;
1347 }
1348 return res;
1349 }
1350
1351 friend int fstrncmp(const faststring &a, const faststring &b, int n)
1352 {
1353 int l = macromin(a._len, b._len );
1354 int m = macromin(l, n);
1355
1356 short res = strncmp(a._buf, b._buf, m);
1357
1358 // We do not have \0 terminated strings, so we have to restrict the initial
1359 // comparison to the length m.
1360
1361 // If n is larger than the length of any of the two strings
1362 // we need to check the lengths.
1363
1364 if (n <= l) // If n is smaller or equal to the shorter string, we have a result.
1365 return res; // This can be <,>, ==
1366 // else // if n > l
1367
1368 // In this case, n is larger than one of the two string lengths or larger than both.
1369 // Still in this case we could have res!=0 among the first m string positions.
1370 // If res!=0, this is the result.
1371
1372 if (res != 0)
1373 return res;
1374
1375 // else if res == 0, the two strings are equal among the first m positions
1376 // and thus they are equal over the whole length of the shorter string.
1377
1378 // If n is larger than both strings, the longer string is greater.
1379 // If n is larger than only one string, the longer string is greater.
1380 // => in all cases the shorter string is less.
1381 // if lengths are equal, we have to return 0
1382
1383 if (a._len < b._len)
1384 return -1;
1385 if (a._len > b._len)
1386 return 1;
1387 return 0;
1388 }
1389
1390
1391 // Has been corrected in 03.2011
1392 friend bool operator>(const faststring &a, const faststring &b)
1393 {
1394 int l = macromin(a._len, b._len );
1395 // int res = (strncmp(a._buf, b._buf, l) > 0);
1396 short res = strncmp(a._buf, b._buf, l);
1397
1398 if (res == 0)
1399 return (a._len > b._len );
1400 return (res > 0);
1401 }
1402
1403 void removeSpacesFront(const char* delims="\r\n\t\v\f ")
1404 {
1405 size_t i;
1406
1407 for (i=0; i < _len && is_in_symbol_list__(_buf[i], delims); ++i)
1408 {}
1409 erase_front(i);
1410 }
1411
1412
1413 void removeSpacesBack(const char* delims="\r\n\t\v\f ")
1414 {
1415 size_t i;
1416 size_t n_renomve=0;
1417
1418 if (_len > 0)
1419 {
1420 for (i=_len-1; is_in_symbol_list__(_buf[i], delims); --i)
1421 {
1422 ++n_renomve;
1423 if (i==0)
1424 break;
1425 }
1426 erase_back(n_renomve);
1427 }
1428 }
1429
1430 void removeSpaces(const char* delims="\r\n\t\v\f ")
1431 {
1432 char *i, *j, *e;
1433 e = end();
1434 i = begin();
1435
1436 // Skip symbols we do not remove
1437 while (i != e && !is_in_symbol_list__(*i, delims))
1438 ++i;
1439
1440 if (i == e) // Nothing was done.
1441 return;
1442
1443 j = i;
1444 ++i;
1445
1446 while (i != e)
1447 {
1448 if (is_in_symbol_list__(*i, delims)) // remove this
1449 {
1450 ++i;
1451 }
1452 else
1453 {
1454 *j = *i;
1455 ++i;
1456 ++j;
1457 }
1458 }
1459 _len -= i-j;
1460 }
1461
1462
1463 void remove_symbol(char c)
1464 {
1465 char *i, *j, *e;
1466 e = end();
1467 i = begin();
1468
1469 // Skip symbols we do not remove
1470 while (i != e && *i != c)
1471 ++i;
1472
1473 if (i == e) // Nothing was done.
1474 return;
1475
1476 j = i;
1477 ++i;
1478
1479 while (i != e)
1480 {
1481 if (*i == c) // remove this
1482 {
1483 ++i;
1484 }
1485 else
1486 {
1487 *j = *i;
1488 ++i;
1489 ++j;
1490 }
1491 }
1492 _len -= i-j;
1493 }
1494
1495 /*
1496 size_t count_symbol(char sym) const
1497 {
1498 size_t res=0;
1499 const char *pos_it = begin();
1500 const char *end_it = end();
1501
1502 while (pos_it != end_it)
1503 {
1504 if (*pos_it == sym)
1505 ++res;
1506
1507 ++pos_it;
1508 }
1509 return res;
1510 }
1511 */
1512
1513 size_t countChar(char sym) const
1514 {
1515 size_t res=0;
1516 const char *pos_it = begin();
1517 const char *end_it = end();
1518
1519 while (pos_it != end_it)
1520 {
1521 if (*pos_it == sym)
1522 ++res;
1523
1524 ++pos_it;
1525 }
1526 return res;
1527 }
1528
1529 void skip_symbols(size_t &pos, const char* delims="\r\n\t\v\f ") const
1530 {
1531 const char *theend = end();
1532 const char *it=begin()+pos;
1533
1534 while (it != theend && is_in_symbol_list__(*it, delims) )
1535 ++it;
1536 pos = it-begin();
1537 }
1538
1539
1540 void skip_symbol(size_t &pos, char c) const
1541 {
1542 const char *theend = end();
1543 const char *it=begin()+pos;
1544
1545 while (it != theend && *it == c)
1546 ++it;
1547 pos = it-begin();
1548 }
1549
1550
1551
1552 void skip_spaces(size_t &pos) const
1553 {
1554 skip_symbols(pos);
1555 }
1556
1557 void replace_char(char origChar, char newChar)
1558 {
1559 char *pos_it = begin();
1560 const char *end_it = end();
1561
1562 while (pos_it != end_it)
1563 {
1564 if (*pos_it == origChar)
1565 *pos_it = newChar;
1566 ++pos_it;
1567 }
1568 }
1569
1570 int replace_char_count(char origChar, char newChar)
1571 {
1572 unsigned count = 0;
1573 char *pos_it = begin();
1574 const char *end_it = end();
1575
1576 while (pos_it != end_it)
1577 {
1578 if (*pos_it == origChar)
1579 {
1580 ++count;
1581 *pos_it = newChar;
1582 }
1583 ++pos_it;
1584 }
1585 return count;
1586 }
1587
1588
1589 /*----------------------------------------------------------------------------------------------------------------------
1590 | Shortens stored string to n - 3 characters, setting the last three characters "...". If string is already <=
1591 | n characters in length, this function has no effect. This is useful when it is desirable to show some of the
1592 | contents of a string, even when the string will not fit in its entirety into the space available for displaying it.
1593 |*/
1594 faststring& ShortenTo(size_t n)
1595 {
1596 if (n <= 3 || _len <= n )
1597 return *this;
1598
1599 // _len > n && n>3 now:
1600 _len = n;
1601 _buf[n-1] = '.';
1602 _buf[n-2] = '.';
1603 _buf[n-3] = '.';
1604 return *this;
1605 }
1606
1607 // TODO: Use pointers to make this more efficient.
1608 bool isADouble() const
1609 {
1610 size_t i=0;
1611
1612 bool hasDigits = false;
1613 bool hasExp = false;
1614 bool hasExpDigits = false;
1615
1616 while (i< _len && isspace(_buf[i]))
1617 ++i;
1618
1619 // I am not sure this line makes it more efficient
1620 // if (i >= _len)
1621 // return false;
1622
1623 if (i < _len && (_buf[i] == '-' || _buf[i] == '+') )
1624 ++i;
1625
1626 while (i < _len && isdigit(_buf[i]) )
1627 {
1628 ++i;
1629 hasDigits = true;
1630 }
1631
1632 if (i < _len && _buf[i] == '.')
1633 ++i;
1634
1635 while (i < _len && isdigit(_buf[i]) )
1636 {
1637 ++i;
1638 hasDigits = true;
1639 }
1640
1641 if ( i < _len && (_buf[i] == 'e' || _buf[i] == 'E') )
1642 {
1643 hasExp = true;
1644 ++i;
1645 }
1646
1647 while (i < _len && isdigit(_buf[i]) )
1648 {
1649 ++i;
1650 hasExpDigits = true;
1651 }
1652
1653 while (i < _len && isspace(_buf[i]) )
1654 ++i;
1655
1656 // We passed all characters that could be interpreted as part of the double number.
1657 // If we have not reached the end of the string, the string contains characters that are not
1658 // part of a double number. If this is the case we return false.
1659 if (i < _len)
1660 return false;
1661
1662 if (hasDigits)
1663 {
1664 if (hasExp && hasExpDigits)
1665 return true;
1666 if (!hasExp && !hasExpDigits)
1667 return true;
1668 return false;
1669 }
1670 else
1671 return false;
1672 }
1673
1674 // TODO: Use pointers to make this more efficient.
1675 bool isAnInt() const
1676 {
1677 size_t i=0;
1678 bool hasDigits = false;
1679
1680 while (i < _len && _buf[i] == ' ')
1681 ++i;
1682
1683 if (i < _len && (_buf[i] == '-' || _buf[i] == '+') )
1684 ++i;
1685
1686 while (i < _len && isdigit(_buf[i]))
1687 {
1688 ++i;
1689 hasDigits = true;
1690 }
1691
1692 while (i < _len && _buf[i] == ' ')
1693 ++i;
1694
1695 if (i < _len)
1696 return false;
1697 else
1698 if (hasDigits)
1699 return true;
1700 else
1701 return false;
1702 }
1703
1704 bool isAnUnsigned() const
1705 {
1706 size_t i=0;
1707 bool hasDigits = false;
1708
1709 while (i < _len && _buf[i] == ' ')
1710 ++i;
1711
1712 if (i < _len && _buf[i] == '+')
1713 ++i;
1714
1715 while (i < _len && isdigit(_buf[i]))
1716 {
1717 ++i;
1718 hasDigits = true;
1719 }
1720
1721 while (i < _len && _buf[i] == ' ')
1722 ++i;
1723
1724 if (i < _len)
1725 return false;
1726 else
1727 if (hasDigits)
1728 return true;
1729 else
1730 return false;
1731 }
1732
1733 //---------------------------------------
1734 // Side effects: Invalidates pointers.
1735 //---------------------------------------
1736 faststring &AddQuotes(char quote_char = '\'')
1737 {
1738 reserve(_len + 2);
1739 memmove(_buf+1, _buf, _len);
1740 _buf[0] = quote_char;
1741 _buf[_len+1] = quote_char;
1742 _len += 2;
1743
1744 return *this;
1745 }
1746
1747 void consume(faststring &s)
1748 {
1749 _capacity = s._capacity;
1750 _len = s._len;
1751 _buf = s._buf;
1752
1753 s._buf = NULL;
1754 s._capacity = s._len = 0;
1755 }
1756
1757 void swap(faststring &s)
1758 {
1759 char *tmp_buf = _buf;
1760 size_t tmp_len = _len;
1761 size_t tmp_capacity = _capacity;
1762
1763 _buf = s._buf;
1764 _len = s._len;
1765 _capacity = s._capacity;
1766
1767 s._buf = tmp_buf;
1768 s._len = tmp_len;
1769 s._capacity = tmp_capacity;
1770 }
1771
1772
1773 faststring &doubleChar(char c)
1774 {
1775 faststring tmp;
1776
1777 size_t count_c = countChar(c);
1778
1779 if (count_c > 0)
1780 {
1781 tmp.reserve(_len + count_c);
1782
1783 size_t i;
1784 size_t j;
1785 for (i=0, j=0; i<_len; ++i, ++j)
1786 {
1787 if (c == (tmp._buf[j] = _buf[i]) )
1788 {
1789 ++j;
1790 tmp._buf[j] = _buf[i];
1791 }
1792 }
1793 tmp._len = _len + count_c;
1794
1795 consume(tmp);
1796 }
1797 return *this;
1798 }
1799
1800
1801 int PrintF(const char *format, ...)
1802 {
1803 char *tmp;
1804
1805 // Create and initialise list of optional arguments:
1806 va_list argList;
1807
1808 #if !defined (WIN32) && !defined (_WIN64)
1809 va_start(argList, format);
1810 int N = vasprintf(&tmp, format, argList);
1811 va_end(argList);
1812 #else // In case we are on a WIN32 or _WIN64 machine
1813 va_start(argList, format);
1814 tmp = new char [5000];
1815 int N = vsnprintf(tmp, 5000, format, argList);
1816 va_end(argList);
1817 #endif
1818 _buf = tmp;
1819 _capacity = N+1;
1820 _len = N;
1821 return N;
1822 }
1823
1824
1825 // Convert to unsigned long
1826 unsigned ToUnsigned()
1827 {
1828 char *end;
1829 return strtoul(c_str(), &end, 0);
1830 }
1831
1832 // Make sure you check errno yourself
1833 unsigned ToUnsigned(size_t beg_pos,
1834 size_t& end_pos)
1835 {
1836 const char *beg = c_str()+beg_pos;
1837 char *end;
1838 unsigned result;
1839
1840 // errno = 0;
1841 result = strtoul(beg, &end, 0);
1842 // if (errno != 0)
1843 // {
1844 // std::cerr << "Parse error in ToUnsigned." << std::endl;
1845 // }
1846 end_pos = end-begin_str();
1847 return result;
1848 }
1849
1850
1851
1852 // Convert to unsigned long
1853 unsigned long ToUnsignedLong()
1854 {
1855 char *end;
1856 return strtoul(c_str(), &end, 0);
1857 }
1858
1859 // Convert to int
1860 int ToInt()
1861 {
1862 char *end;
1863 return strtol(c_str(), &end, 0);
1864 }
1865
1866 // Convert to long
1867 long ToLong()
1868 {
1869 char *end;
1870 return strtol(c_str(), &end, 0);
1871 }
1872
1873 long ToLong(size_t beg_pos,
1874 size_t& end_pos)
1875 {
1876 const char *beg = c_str()+beg_pos;
1877 char *end;
1878 long result;
1879
1880 result = strtol(beg, &end, 0);
1881 end_pos = end-c_str();
1882 return result;
1883 }
1884
1885
1886 // Convert to double
1887 double ToDouble()
1888 {
1889 char *end;
1890 return strtod(c_str(), &end);
1891 }
1892
1893 double ToDouble(size_t beg_pos,
1894 size_t& end_pos)
1895 {
1896 const char *beg = c_str()+beg_pos;
1897 char *end;
1898 double result;
1899
1900 result = strtod(beg, &end);
1901 end_pos = end-c_str();
1902 return result;
1903 }
1904
1905 double ToDouble(size_t pos,
1906 char **end)
1907 {
1908 double result;
1909
1910 result = strtod(_buf+pos, end);
1911 return result;
1912 }
1913
1914
1915 bool EqualsCaseSensitive(const faststring &s)
1916 {
1917 if (_len != s._len)
1918 return false;
1919
1920 return (strncmp(_buf, s._buf, _len) == 0);
1921 }
1922
1923 bool EqualsCaseInsensitive(const faststring &s) const
1924 {
1925 if (_len != s._len)
1926 return false;
1927
1928 faststring a(*this);
1929 faststring b(s);
1930
1931 a.tolower();
1932 b.tolower();
1933
1934 return (strncmp(a._buf, b._buf, _len) == 0);
1935 }
1936
1937 //=================================================
1938 // The family of compare functions:
1939 //=================================================
1940 // int compare ( const string& str ) const;
1941 // int compare ( const char* s ) const;
1942 // int compare ( size_t pos1, size_t n1, const string& str ) const;
1943 // int compare ( size_t pos1, size_t n1, const char* s) const;
1944 // int compare ( size_t pos1, size_t n1, const string& str, size_t pos2, size_t n2 ) const;
1945 // int compare ( size_t pos1, size_t n1, const char* s, size_t n2) const;
1946 // ------------------------------------------------- all implemented
1947
1948 // Result: >: +1, <: -1, == 0
1949 int compare(const faststring &s)
1950 {
1951 size_t l = macromin(_len, s._len);
1952 int res = strncmp(_buf, s._buf, l);
1953
1954 if (res != 0)
1955 return res;
1956
1957 if (_len == s._len)
1958 return 0;
1959 else
1960 return (int)(_len > s._len);
1961 }
1962
1963 // Result: >: +1, <: -1, == 0
1964 int compare(const char *s)
1965 {
1966 size_t s_len = strlen(s);
1967 size_t l = macromin(_len, s_len);
1968 int res = strncmp(_buf, s, l);
1969
1970 if (res != 0)
1971 return res;
1972
1973 if (_len == s_len)
1974 return 0;
1975 else
1976 return (int)(_len > s_len);
1977 }
1978
1979
1980 // Result: >: +1, <: -1, == 0
1981 int compare(const char *t1, const char*t2, const char *s1, const char *s2)
1982 {
1983 // Out of range check:
1984 if (t1 > t2 || s1 > s2)
1985 return -2;
1986
1987 if (t1 < _buf || t2 > _buf+_len)
1988 return -3;
1989
1990 return compare_unchecked(t1,t2, s1, s2);
1991 }
1992
1993
1994 int compare ( size_t pos1, size_t n1, const faststring& str ) const
1995 {
1996 if (pos1 > _len || pos1 + n1 > _len)
1997 return -3;
1998
1999 return compare_unchecked(_buf+pos1, _buf+pos1+n1, str._buf, str._buf+str._len);
2000 }
2001
2002 int compare ( size_t pos1, size_t n1, const char* s) const
2003 {
2004 if (pos1 > _len || pos1 + n1 > _len)
2005 return -3;
2006
2007 const char *e = s + strlen(s);
2008
2009 return compare_unchecked(_buf+pos1, _buf+pos1+n1, s, e);
2010 }
2011
2012 int compare ( size_t pos1, size_t n1, const faststring& str, size_t pos2, size_t n2 ) const
2013 {
2014 if (pos1 > _len || pos1 + n1 > _len)
2015 return -3;
2016 if (pos2 > str._len || pos2 + n2 > str._len)
2017 return -4;
2018
2019 return compare_unchecked(_buf+pos1, _buf+pos1+n1, str._buf+pos2, str._buf+pos2+n2);
2020 }
2021
2022 int compare ( size_t pos1, size_t n1, const char* s, size_t n2) const
2023 {
2024 size_t s_len = strlen(s);
2025
2026 if (pos1 > _len || pos1 + n1 > _len)
2027 return -3;
2028 if (n2 > s_len)
2029 return -4;
2030
2031 return compare_unchecked(_buf+pos1, _buf+pos1+n1, s, s+n2);
2032 }
2033
2034
2035 // Result: >: +1, <: -1, == 0
2036 int compare_unchecked(const char *t1, const char*t2, const char *s1, const char *s2) const
2037 {
2038 size_t t_len = t2-t1;
2039 size_t s_len = s2-s1;
2040
2041 size_t l = macromin(t_len, s_len);
2042 int res = strncmp(t1, s1, l);
2043
2044 if (res != 0)
2045 return res;
2046
2047 if (t_len == s_len)
2048 return 0;
2049 else
2050 return (int)(t_len > s_len);
2051 }
2052
2053
2054 unsigned char * p_str(unsigned char *buffer) const //
2055 {
2056 if (_len > 255)
2057 {
2058 *buffer = '\0';
2059 return 0;
2060 }
2061
2062 memcpy(buffer+1, _buf, _len);
2063 buffer[0] = (unsigned char)_len;
2064 return buffer;
2065 }
2066
2067 void AsBinary(unsigned long l)
2068 {
2069 short bits = sizeof(l)*8;
2070 reserve(bits);
2071 _len = bits;
2072
2073 short i;
2074 for (i=(short)_len-1; i >= 0; --i)
2075 {
2076 // is l odd
2077 _buf[i] = (char)((char)(l%2)+'0');
2078 l/=2;
2079 }
2080 }
2081
2082 void AsBinary(unsigned long l, unsigned digits)
2083 {
2084 reserve(digits);
2085 _len = digits;
2086
2087 short i;
2088 for (i=(short)_len-1; i >= 0; --i)
2089 {
2090 // is l odd
2091 _buf[i] = (char)((char)(l%2)+'0');
2092 l/=2;
2093 }
2094 }
2095
2096
2097 void AsHex(unsigned long l)
2098 {
2099 short hexes = sizeof(l)*2;
2100 char remainder;
2101
2102 reserve(hexes);
2103 _len = hexes;
2104
2105 short i;
2106 for (i=(short)_len-1; i >= 0; --i)
2107 {
2108 remainder = l%16;
2109
2110 if (remainder < 10)
2111 _buf[i] = remainder+'0';
2112 else
2113 {
2114 _buf[i] = remainder+55;
2115 }
2116 l /= 16;
2117 }
2118 }
2119
2120 void AsHex(unsigned long l, unsigned digits)
2121 {
2122 char remainder;
2123
2124 reserve(digits);
2125 _len = digits;
2126
2127 short i;
2128 for (i=(short)_len-1; i >= 0; --i)
2129 {
2130 remainder = l%16;
2131
2132 if (remainder < 10)
2133 _buf[i] = remainder+'0';
2134 else
2135 {
2136 _buf[i] = remainder+55;
2137 }
2138 l /= 16;
2139 }
2140 }
2141
2142 faststring substr(size_t pos, size_t n=npos) const
2143 {
2144 const char * b = _buf+pos;
2145 const char * e = _buf+pos+n;
2146
2147 if (b > _buf+_len)
2148 b = _buf+_len;
2149
2150 if (n==npos || e > _buf+_len)
2151 {
2152 e = _buf+_len;
2153 }
2154
2155 faststring res(b, e);
2156 return res;
2157 }
2158
2159 //=================================================
2160 // The family of erase functions:
2161 //=================================================
2162 // string& erase ( size_t pos = 0, size_t n = npos );
2163 // iterator erase ( iterator position );
2164 // iterator erase ( iterator first, iterator last );
2165 // ------------------------------------------------- all implemented
2166
2167 faststring &erase ( size_t pos, size_t n = npos )
2168 {
2169 char *p1 = _buf+pos;
2170 char *p2 = _buf+pos+n;
2171
2172 if (p1 > _buf+_len)
2173 p1 = _buf+_len;
2174
2175 if (n==npos || p2 > _buf+_len)
2176 p2 = _buf+_len;
2177
2178 return erase(p1, p2);
2179 }
2180
2181 faststring &erase ( char *p1)
2182 {
2183 return erase(p1, p1+1);
2184 }
2185
2186 faststring &erase ( char *p1, char *p2 )
2187 {
2188 char *tmp1 = p1;
2189 char *tmp2 = p2;
2190 char *e = _buf + _len;
2191
2192 if (p1 >= p2)
2193 return *this;
2194
2195 if (p1 < _buf)
2196 p1 = _buf;
2197
2198 if (p2 > e)
2199 p2 = e;
2200
2201 // Copy chars to the left within the string.
2202 while (tmp2 < e)
2203 {
2204 *tmp1 = *tmp2;
2205 ++tmp1;
2206 ++tmp2;
2207 }
2208
2209 // Reduce length
2210 _len -= (p2-p1);
2211
2212 return *this;
2213 }
2214
2215 void erase()
2216 {
2217 _len = 0;
2218 }
2219
2220 void clear()
2221 {
2222 _len = 0;
2223 }
2224
2225 void reset()
2226 {
2227 if (_buf)
2228 {
2229 free(_buf);
2230 _buf = NULL;
2231 _capacity = 0;
2232 _len = 0;
2233 }
2234 }
2235
2236 //=================================================
2237 // The family of insert functions:
2238 //=================================================
2239 // - string& insert ( size_t pos1, const string& str );
2240 // - string& insert ( size_t pos1, const string& str, size_t pos2, size_t n );
2241 // - string& insert ( size_t pos1, const char* s, size_t n);
2242 // - string& insert ( size_t pos1, const char* s );
2243 // - string& insert ( size_t pos1, size_t n, char c );
2244 // - iterator insert ( iterator p, char c );
2245 // - void insert ( iterator p, size_t n, char c );
2246 //=================================================
2247
2248 //---------------------------------------
2249 // Side effects: Invalidates pointers.
2250 //---------------------------------------
2251 faststring &insert(size_t pos, const char *p1, const char *p2)
2252 {
2253 if (pos > _len || p1 >= p2)
2254 return *this;
2255
2256
2257 size_t N = p2-p1; // Number of characters to be inserted
2258 reserve(_len+N);
2259
2260 // char *b = _buf;
2261 char *e = _buf+_len; // This is still the old _len. We have reserved new space but we have not used it yet.
2262 char *tmp = e-1; // Pointer to the last char. We start at the last char which we move first.
2263 char *tmpN = tmp+N;
2264
2265 // Now, after having reserved new memory, we can work with pointers in _buf
2266 // again.
2267
2268 char *p_pos = _buf + pos;
2269
2270 // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
2271
2272 // Create the space needed:
2273 while (tmp >= p_pos )
2274 {
2275 *tmpN = *tmp;
2276 --tmp;
2277 --tmpN;
2278 }
2279 // *tmpN = *tmp;
2280 _len += N;
2281
2282 replace_intern(p_pos, p1, p2);
2283 return *this;
2284 }
2285
2286 faststring &insert(char *p_pos, const char *p1, const char *p2)
2287 {
2288 return insert(p_pos-_buf, p1, p2);
2289 }
2290
2291
2292 faststring &insert(size_t pos1, const faststring &str)
2293 {
2294 return insert(pos1, str.begin(), str.end());
2295 }
2296
2297 faststring& insert ( size_t pos1, const faststring& str, size_t pos2, size_t n )
2298 {
2299 char *b = str._buf + pos2;
2300 char *e = str._buf + pos2+n;
2301
2302 if (b > str.end() || e > str.end() )
2303 return *this;
2304
2305 return insert(pos1, b, e);
2306 }
2307
2308
2309 faststring& insert ( size_t pos1, const char* s, size_t n)
2310 {
2311 size_t s_len = strlen(s);
2312
2313 if (s_len < n)
2314 return *this;
2315
2316 return insert(pos1, s, s+n);
2317 }
2318
2319 faststring& insert( size_t pos1, const char* s)
2320 {
2321 size_t s_len = strlen(s);
2322
2323 return insert(pos1, s, s+s_len);
2324 }
2325
2326 faststring& insert( size_t pos1, size_t n, char c )
2327 {
2328 faststring str(c, n);
2329
2330 return insert(pos1, str.begin(), str.end() );
2331 }
2332
2333 faststring& insert( size_t pos1, char c )
2334 {
2335 return insert(pos1, &c, &c+1 );
2336 }
2337
2338 faststring& insert( char *p, size_t n, char c )
2339 {
2340 faststring str(c, n);
2341
2342 return insert(p-_buf, str.begin(), str.end() );
2343 }
2344
2345 char* insert( char *p, char c )
2346 {
2347 insert(p-_buf, &c, &c+1 );
2348 return p;
2349 }
2350
2351
2352 //=================================================
2353 // The family of replace functions:
2354 //=================================================
2355 // - string& replace ( size_t pos1, size_t n1, const string& str );
2356 // - string& replace ( iterator i1, iterator i2, const string& str );
2357 // - string& replace ( size_t pos1, size_t n1, const string& str, size_t pos2, size_t n2 );
2358 // - string& replace ( size_t pos1, size_t n1, const char* s, size_t n2 );
2359 // - string& replace ( iterator i1, iterator i2, const char* s, size_t n2 );
2360 // - string& replace ( size_t pos1, size_t n1, const char* s );
2361 // - string& replace ( iterator i1, iterator i2, const char* s );
2362 // - string& replace ( size_t pos1, size_t n1, size_t n2, char c );
2363 // - string& replace ( iterator i1, iterator i2, size_t n2, char c );
2364 // - template<class InputIterator> string& replace ( iterator i1, iterator i2, InputIterator j1, InputIterator j2 );
2365
2366 // replace_intern assumes that ranges have already been checked!!!
2367 // This simple version does no error checking and simply overwrites
2368 // part of the this object.
2369 void replace_intern(char *t1, const char *p1, const char *p2)
2370 {
2371 while (p1 < p2)
2372 {
2373 *t1 = *p1;
2374 ++t1;
2375 ++p1;
2376 }
2377 }
2378
2379
2380 //TODO: test
2381 // Overwrite range must "fit" into target string.
2382 // Otherwise, nothing is done.
2383 void overwrite(char *t1, char *p1, char *p2)
2384 {
2385 size_t N = p2-p1;
2386
2387 if (t1 < _buf || t1+N > _buf+_len)
2388 return;
2389
2390 replace_intern(t1, p1, p2);
2391 }
2392
2393 // erases the range from pos1 to pos1+n1 and then inserts str
2394 // if pos1 is not a valid position in the string, nothing will be done.
2395
2396 faststring& replace ( size_t pos1, size_t n1, const faststring& str )
2397 {
2398 if (pos1 > _len)
2399 return *this;
2400
2401 // Number of chars we want/can to "remove":
2402 size_t real_n1 = macromin(n1, _len-pos1);
2403
2404 replace_unchecked (_buf+pos1, _buf+pos1 + real_n1, str.begin(), str.length() );
2405
2406 return *this;
2407 }
2408
2409 faststring& replace ( char *i1, char *i2, const faststring& str )
2410 {
2411 if (i1 < _buf || i2 > _buf+_len)
2412 return *this;
2413
2414 replace_unchecked(i1, i2, str.begin(), str.length() );
2415 return *this;
2416 }
2417
2418 faststring& replace ( size_t pos1, size_t n1, const faststring& str, size_t pos2, size_t n2 )
2419 {
2420 if (pos1 >= _len)
2421 return *this;
2422
2423 if (pos2 >= str._len)
2424 return *this;
2425
2426 // Number of chars we can "remove":
2427 size_t real_n1 = macromin(n1, _len-pos1);
2428
2429 // Number of chars we can "insert":
2430 size_t real_n2 = macromin(n2, str._len-pos2);
2431
2432 replace_unchecked(_buf+pos1,_buf+pos1+real_n1, str.begin()+pos2, real_n2);
2433
2434 return *this;
2435 }
2436
2437 faststring& replace ( size_t pos1, size_t n1, const char* s, size_t n2 )
2438 {
2439 if (pos1 >= _len)
2440 return *this;
2441
2442 size_t s_len = strlen(s);
2443
2444 // if (n2 <= s_len )
2445 // return *this;
2446
2447 // Number of chars we can "remove":
2448 size_t real_n1 = macromin(n1, _len-pos1);
2449
2450 // Number of chars we can "insert":
2451 size_t real_n2 = macromin(n2, s_len);
2452
2453 replace_unchecked(_buf+pos1,_buf+pos1+real_n1, s, real_n2);
2454
2455 return *this;
2456 }
2457
2458 faststring& replace ( char *i1, char *i2, const char* s, size_t n2 )
2459 {
2460 if (i1 < _buf || i2 > _buf+_len)
2461 return *this;
2462
2463 size_t s_len = strlen(s);
2464
2465 if (n2 > s_len)
2466 n2 = s_len;
2467
2468 replace_unchecked (i1, i2, s, n2);
2469 return *this;
2470 }
2471
2472 faststring& replace(char *i1, char *i2, const char* s1, const char * s2)
2473 {
2474 if (i1 < _buf || i2 > _buf+_len || s2 < s1)
2475 return *this;
2476
2477 replace_unchecked (i1, i2, s1, s2-s1);
2478 return *this;
2479
2480 }
2481
2482 void replace_unchecked ( char *i1, char *i2, const char* s, size_t n2 )
2483 {
2484 // Number of chars we want/can to "remove":
2485 size_t n_remove = i2-i1;
2486
2487 // Number of chars we want to insert:
2488 size_t n_insert = n2;
2489
2490 if (n_remove == n_insert) // simple overwrite:
2491 {
2492 replace_intern(i1, s, s+n_insert);
2493 }
2494 else if (n_remove > n_insert) // We have too much space and need to erase some characters.
2495 {
2496 erase(i1, i1+n_remove - n_insert);
2497 replace_intern(i1, s, s+n_insert);
2498 }
2499 else // We have too little space and need to insert some characters.
2500 {
2501 size_t n_newspace = n_insert-n_remove;
2502
2503 insert(i1-_buf, s, s + n_newspace);
2504 replace_intern(i1 + n_newspace, s+n_newspace, s+n_insert);
2505 }
2506 }
2507
2508 faststring& replace ( size_t pos1, size_t n1, const char* s )
2509 {
2510 if (pos1 >= _len)
2511 return *this;
2512
2513 size_t s_len = strlen(s);
2514
2515 // if (n2 <= s_len )
2516 // return *this;
2517
2518 // Number of chars we can "remove":
2519 size_t real_n1 = macromin(n1, _len-pos1);
2520
2521 // Number of chars we can "insert":
2522 size_t real_n2 = s_len;
2523
2524 replace_unchecked(_buf+pos1,_buf+pos1+real_n1, s, real_n2);
2525
2526 return *this;
2527 }
2528
2529 faststring& replace ( char *i1, char *i2, const char* s )
2530 {
2531 if (i1 < _buf || i2 > _buf+_len)
2532 return *this;
2533
2534 size_t s_len = strlen(s);
2535
2536 replace_unchecked(i1, i2, s, s_len);
2537
2538 return *this;
2539 }
2540
2541 faststring& replace ( size_t pos1, size_t n1, size_t n2, char c )
2542 {
2543 if (pos1 >= _len)
2544 return *this;
2545
2546 size_t real_n1 = macromin(n1, _len-pos1);
2547
2548 faststring tmp(c, n2);
2549
2550 replace_unchecked(_buf+pos1, _buf+pos1+real_n1, tmp.begin(), n2);
2551
2552 return *this;
2553 }
2554
2555 faststring& replace ( char *i1, char *i2, size_t n2, char c )
2556 {
2557 if (i1 < _buf || i2 > _buf+_len)
2558 return *this;
2559
2560 faststring tmp(c, n2);
2561
2562 replace_unchecked(i1, i2, tmp.begin(), n2);
2563
2564 return *this;
2565 }
2566
2567 //=================================================
2568
2569
2570 static size_t max_size() { return npos; }
2571
2572 // Shorten string to n characters. If its length is less than n, we append c
2573 // as often as necessary to get a length of n.
2574 void resize ( size_t n, char c )
2575 {
2576 if (n < _len)
2577 shorten(n);
2578 else if (n > _len)
2579 append(n-_len,c);
2580 }
2581
2582
2583 void resize ( size_t n )
2584 {
2585 resize(n, char());
2586 }
2587
2588 char & operator[](size_t i)
2589 {
2590 return _buf[i];
2591 }
2592
2593 char operator[](size_t i) const
2594 {
2595 return _buf[i];
2596 }
2597
2598 size_t copy ( char* s, size_t n, size_t pos = 0) const
2599 {
2600 if (pos > _len)
2601 return -2;
2602
2603 int l = n;
2604 if (n+pos > _len)
2605 l = _len-pos;
2606
2607 memcpy(s, _buf+pos, l);
2608
2609 return l;
2610 }
2611
2612 //=================================================
2613 // The family of find functions:
2614 //=================================================
2615 // size_t find ( const string& str, size_t pos = 0 ) const;
2616 // size_t find ( const char* s, size_t pos, size_t n ) const;
2617 // size_t find ( const char* s, size_t pos = 0 ) const;
2618 // size_t find ( char c, size_t pos = 0 ) const;
2619
2620 size_t find ( const faststring& str, size_t pos = 0 ) const
2621 {
2622 const char *p = find_fundamental(begin()+pos, end(), str.begin(), str.end() );
2623
2624 if (p != NULL)
2625 return p-begin();
2626 else
2627 return npos;
2628 }
2629
2630 size_t find ( const char* s, size_t pos, size_t n ) const
2631 {
2632 const char *p = find_fundamental(begin()+pos, end(), s, s+n );
2633
2634 if (p != NULL)
2635 return p-begin();
2636 else
2637 return npos;
2638 }
2639
2640 size_t find ( const char* s, size_t pos=0) const
2641 {
2642 const char *p = find_fundamental(begin()+pos, end(), s, s+strlen(s) );
2643
2644 if (p != NULL)
2645 return p-begin();
2646 else
2647 return npos;
2648 }
2649
2650 // There is one good reason determining the position and not the pointer:
2651 // - The position is still valid after a realloc, the pointer is not.
2652 size_t find(char c, size_t pos=0) const
2653 {
2654 char *b = begin()+pos;
2655 char *e = end();
2656
2657 while (b < e && *b != c)
2658 ++b;
2659 if (b == e)
2660 return npos;
2661 else
2662 return b-begin();
2663 }
2664
2665 /* size_t find_forward(char c, size_t pos=0) const */
2666 /* { */
2667 /* char *b = begin()+pos; */
2668 /* char *e = end(); */
2669
2670 /* while (b < e && *b != c) */
2671 /* ++b; */
2672 /* if (b >= e) */
2673 /* return -1u; */
2674 /* else */
2675 /* return b-begin(); */
2676 /* } */
2677
2678
2679 //=================================================
2680 // The family of rfind functions:
2681 //=================================================
2682 // size_t rfind ( const string& str, size_t pos = npos ) const;
2683 // size_t rfind ( const char* s, size_t pos, size_t n ) const;
2684 // size_t rfind ( const char* s, size_t pos = npos ) const;
2685 // size_t rfind ( char c, size_t pos = npos ) const;
2686
2687 size_t rfind ( const faststring& str, size_t pos = npos ) const
2688 {
2689 if (pos >= _len)
2690 pos = _len-1;
2691
2692 const char *p = rfind_fundamental(begin()+pos,rend(),str.rbegin(),str.rend());
2693
2694 if (p != NULL)
2695 return p-begin();
2696 else
2697 return npos;
2698 }
2699
2700 size_t rfind ( const char* s, size_t pos, size_t n ) const
2701 {
2702 if (pos >= _len)
2703 pos = _len-1;
2704
2705 const char *p = rfind_fundamental(begin()+pos, rend(), s+n-1, s-1 );
2706
2707 if (p != NULL)
2708 return p-begin();
2709 else
2710 return npos;
2711 }
2712
2713 size_t rfind ( const char* s, size_t pos=npos) const
2714 {
2715 if (pos >= _len)
2716 pos = _len-1;
2717
2718 const char *p = rfind_fundamental(begin()+pos, rend(), s+strlen(s)-1, s-1 );
2719
2720 if (p != NULL)
2721 return p-begin();
2722 else
2723 return npos;
2724 }
2725
2726 size_t rfind(char c, size_t pos=npos) const
2727 {
2728 if (pos >= _len)
2729 pos = _len-1;
2730
2731 char *b = begin()+pos;
2732 char *e = begin()-1;
2733
2734 while (b > e && *b != c)
2735 --b;
2736 if (b == e) // should be == if nothing was found
2737 return npos;
2738 else // should be b > e if char was found
2739 return b-begin();
2740 }
2741
2742
2743 //=================================================
2744 // The family of find_first_of functions:
2745 //=================================================
2746 /* size_t find_first_of ( const string& str, size_t pos = 0 ) const; */
2747 /* size_t find_first_of ( const char* s, size_t pos, size_t n ) const; */
2748 /* size_t find_first_of ( const char* s, size_t pos = 0 ) const; */
2749 /* size_t find_first_of ( char c, size_t pos = 0 ) const; */
2750
2751 /* Consider the strcspn function here */
2752
2753 size_t find_first_of( const faststring& str, size_t pos = 0 ) const
2754 {
2755 const char *b = begin()+pos;
2756 const char *e = end();
2757
2758 while (b < e)
2759 {
2760 if (is_in_symbol_list__(*b, str.begin(), str.length() ))
2761 return b-begin();
2762 ++b;
2763 }
2764 return npos;
2765 }
2766
2767 size_t find_first_of( const char *str, size_t pos, size_t n ) const
2768 {
2769 const char *b = begin()+pos;
2770 const char *e = end();
2771
2772 while (b < e)
2773 {
2774 if (is_in_symbol_list__(*b, str, n))
2775 return b-begin();
2776 ++b;
2777 }
2778 return npos;
2779 }
2780
2781 size_t find_first_of( const char *str, size_t pos = 0 ) const
2782 {
2783 const char *b = begin()+pos;
2784 const char *e = end();
2785
2786 while (b < e)
2787 {
2788 if (is_in_symbol_list__(*b, str) )
2789 return b-begin();
2790 ++b;
2791 }
2792 return npos;
2793 }
2794
2795 size_t find_first_of ( char c, size_t pos = 0 ) const
2796 {
2797 return find(c, pos);
2798 }
2799
2800 //=================================================
2801 // The family of find_last_of functions:
2802 //=================================================
2803 /* size_t find_last_of ( const string& str, size_t pos = npos ) const; */
2804 /* size_t find_last_of ( const char* s, size_t pos, size_t n ) const; */
2805 /* size_t find_last_of ( const char* s, size_t pos = npos ) const; */
2806 /* size_t find_last_of ( char c, size_t pos = npos ) const; */
2807
2808 size_t find_last_of( const faststring& str, size_t pos = npos ) const
2809 {
2810 if (pos > _len)
2811 pos = _len;
2812
2813 const char *e = begin()-1;
2814 const char *b = begin()+pos-1;
2815
2816 while (b > e)
2817 {
2818 if (is_in_symbol_list__(*b, str.begin(), str.length() ) )
2819 return b-begin();
2820 --b;
2821 }
2822 return npos;
2823 }
2824
2825
2826 size_t find_last_of( const char *str, size_t pos, size_t n ) const
2827 {
2828 if (pos > _len)
2829 pos = _len;
2830
2831 const char *e = begin()-1;
2832 const char *b = begin()+pos-1;
2833
2834 while (b > e)
2835 {
2836 if (is_in_symbol_list__(*b, str, n))
2837 return b-begin();
2838 --b;
2839 }
2840 return npos;
2841 }
2842
2843
2844 size_t find_last_of( const char *str, size_t pos = npos ) const
2845 {
2846 if (pos > _len)
2847 pos = _len;
2848
2849 const char *e = begin()-1;
2850 const char *b = begin()+pos-1;
2851
2852 while (b > e)
2853 {
2854 if (is_in_symbol_list__(*b, str))
2855 return b-begin();
2856 --b;
2857 }
2858 return npos;
2859 }
2860
2861 size_t find_last_of ( char c, size_t pos = npos ) const
2862 {
2863 return rfind(c, pos);
2864 }
2865
2866 //=================================================
2867 // The family of find_first_not_of functions:
2868 //=================================================
2869 /* size_t find_first_not_of ( const string& str, size_t pos = 0 ) const; */
2870 /* size_t find_first_not_of ( const char* s, size_t pos, size_t n ) const; */
2871 /* size_t find_first_not_of ( const char* s, size_t pos = 0 ) const; */
2872 /* size_t find_first_not_of ( char c, size_t pos = 0 ) const; */
2873
2874 /* Also consider the strspn function for the implementation !!!!!!!!! */
2875
2876 size_t find_first_not_of( const faststring& str, size_t pos = 0 ) const
2877 {
2878 const char *b = begin()+pos;
2879 const char *e = end();
2880
2881 while (b < e)
2882 {
2883 if (!is_in_symbol_list__(*b, str.begin(), str.length() ))
2884 return b-begin();
2885 ++b;
2886 }
2887 return npos;
2888 }
2889
2890 size_t find_first_not_of( const char *str, size_t pos, size_t n ) const
2891 {
2892 const char *b = begin()+pos;
2893 const char *e = end();
2894
2895 while (b < e)
2896 {
2897 if (!is_in_symbol_list__(*b, str, n))
2898 return b-begin();
2899 ++b;
2900 }
2901 return npos;
2902 }
2903
2904 size_t find_first_not_of( const char *str, size_t pos = 0 ) const
2905 {
2906 const char *b = begin()+pos;
2907 const char *e = end();
2908
2909 while (b < e)
2910 {
2911 if (!is_in_symbol_list__(*b, str))
2912 return b-begin();
2913 ++b;
2914 }
2915 return npos;
2916 }
2917
2918 size_t find_first_not_of ( char c, size_t pos = 0 ) const
2919 {
2920 const char *b = begin()+pos;
2921 const char *e = end();
2922
2923 while (b < e)
2924 {
2925 if (*b != c)
2926 return b-begin();
2927 ++b;
2928 }
2929 return npos;
2930 }
2931
2932 //=================================================
2933 // The family of find_last_not_of functions:
2934 //=================================================
2935 /* size_t find_last_not_of ( const string& str, size_t pos = npos ) const; */
2936 /* size_t find_last_not_of ( const char* s, size_t pos, size_t n ) const; */
2937 /* size_t find_last_not_of ( const char* s, size_t pos = npos ) const; */
2938 /* size_t find_last_not_of ( char c, size_t pos = npos ) const; */
2939
2940 size_t find_last_not_of( const faststring& str, size_t pos = npos ) const
2941 {
2942 if (pos > _len)
2943 pos = _len;
2944
2945 const char *e = begin()-1;
2946 const char *b = begin()+pos-1;
2947
2948 while (b > e)
2949 {
2950 if (!is_in_symbol_list__(*b, str.begin(), str.length() ))
2951 return b-begin();
2952 --b;
2953 }
2954 return npos;
2955 }
2956
2957
2958 size_t find_last_not_of( const char *str, size_t pos, size_t n ) const
2959 {
2960 if (pos > _len)
2961 pos = _len;
2962
2963 const char *e = begin()-1;
2964 const char *b = begin()+pos-1;
2965
2966 while (b > e)
2967 {
2968 if (!is_in_symbol_list__(*b, str, n))
2969 return b-begin();
2970 --b;
2971 }
2972 return npos;
2973 }
2974
2975
2976 size_t find_last_not_of( const char *str, size_t pos = npos ) const
2977 {
2978 if (pos > _len)
2979 pos = _len;
2980
2981 const char *e = begin()-1;
2982 const char *b = begin()+pos-1;
2983
2984 while (b > e)
2985 {
2986 if (!is_in_symbol_list__(*b, str))
2987 return b-begin();
2988 --b;
2989 }
2990 return npos;
2991 }
2992
2993 size_t find_last_not_of ( char c, size_t pos = npos ) const
2994 {
2995 if (pos > _len)
2996 pos = _len;
2997
2998 const char *e = begin()-1;
2999 const char *b = begin()+pos-1;
3000
3001 while (b > e)
3002 {
3003 if (*b != c)
3004 return b-begin();
3005 --b;
3006 }
3007 return npos;
3008 }
3009
3010 void collapse_mono_symbol_repeats(char c)
3011 {
3012 faststring tmp;
3013
3014 const char *p1, *p2;
3015 p2 = p1 = begin();
3016 ++p2;
3017
3018 while (p2 != end() )
3019 {
3020 if (*p1 != *p2 || *p1 != c)
3021 tmp.push_back(*p1);
3022 p1 = p2;
3023 ++p2;
3024 }
3025 // When leaving the loop there is one character remaining that we need to append to tmp:
3026 tmp.push_back(*p1);
3027 swap(tmp);
3028 }
3029
3030 void unquote()
3031 {
3032 removeSpacesBack();
3033 removeSpacesFront();
3034 if (*begin() == '\"' && *(end()-1) == '\"' )
3035 {
3036 this->erase_front(1);
3037 this->erase_back(1);
3038
3039 /* this->erase(begin()); */
3040 /* this->erase(end()-1); */
3041 }
3042 else if (*begin() == '\'' && *(end()-1) == '\'' )
3043 {
3044 /* this->erase(begin()); */
3045 /* this->erase(end()-1); */
3046
3047 this->erase_front(1);
3048 this->erase_back(1);
3049 }
3050 }
3051
3052 // Unquote string with first char s and last char e
3053 void unquote(char s, char e)
3054 {
3055 removeSpacesBack();
3056 removeSpacesFront();
3057 if (*begin() == s && *(end()-1) == e )
3058 {
3059 this->erase_front(1);
3060 this->erase_back(1);
3061 }
3062 }
3063
3064
3065
3066 bool is_CAPITALIZED_abbreviation_of(const faststring &keyword) const
3067 {
3068 size_t i = 0;
3069 size_t max = size();
3070
3071 // max cannot be greater than keyword.size()
3072 if ( max > keyword.size() )
3073 return false;
3074
3075 // We start comparison in upper case region. Lets see how far we can go.
3076 while (i < max && std::toupper( _buf[i] ) == keyword[i] )
3077 {
3078 ++i;
3079 }
3080
3081 // First we check whether both strings have the same length and we matched all chars.
3082 // This means that we matched exactly the upper case region of keyword, which is the
3083 // complete keyword.
3084 if ( i == max && max == keyword.size() )
3085 return true;
3086
3087 // If we get here, i must be smaller than keyword.size() since we can only have
3088 // * i == max and i == keyword.size(): Not possible after previous if
3089 // * i < max and i == keyword.size(): Not possible since max <= keyword.size()
3090 // * i == max and i < keyword.size():
3091 // * i < max and i < keyword.size():
3092
3093 // Thus, keyword[i] is valid and we use it to check whether we are in the upper case region
3094 // (were all !alpha() should also be considered as upper case chars in this context!!).
3095 // We simply check whether we are in the !islower() region.
3096
3097 if ( !islower( keyword[i]) ) // We are still in non-lower case region
3098 return false;
3099
3100 // If we get here, we are in the in the lower-case region
3101 // There are two possibilities:
3102 // - i == max,
3103 if (i==max)
3104 return true;
3105 // - our string is longer than the upper case region, so we have to continue
3106 // our comparison.
3107
3108 // Let us move through lower case region
3109 while (i < max && ((*this)[i] == keyword[i] || std::tolower(_buf[i]) == keyword[i]) )
3110 ++i;
3111
3112 // Could we match all letters in str?
3113 if (i==max)
3114 return true;
3115 else
3116 return false;
3117 }
3118
3119 // get the next token and remove it from the called object.
3120 faststring& get_next_token(faststring &str, const char * ws = "\r\n\t\v\f ")
3121 {
3122 const size_t n = size();
3123 size_t i = 0;
3124
3125 str.clear();
3126
3127 // eat leading whitespace/delimiter -- this should be more efficient than
3128 // calling removeSpacesFront since we only need to erase in the front of the string once.
3129 while ( (i < n) && ( is_in_symbol_list__( (*this)[i], ws)) ) ++i;
3130
3131 if (i == n) // nothing left so re remove all spaces and the string will be empty
3132 {
3133 erase();
3134 }
3135 else
3136 {
3137 // find end of word
3138 size_t j = i+1;
3139 while ( (j < n) && (!is_in_symbol_list__( (*this)[j], ws)) ) ++j;
3140
3141 str = substr(i, j); // the next token
3142 erase(0, j); // remove token from called object
3143 }
3144
3145 return *this;
3146 }
3147
3148 void set_to_string_with_values(std::vector<double> v, faststring delim= " ", int precision=14)
3149 {
3150 int i, n=v.size();
3151 clear();
3152
3153 if (n == 0)
3154 return;
3155
3156 append(faststring(v[0], precision));
3157 removeSpacesBack();
3158
3159 for (i=1; i<n; ++i)
3160 {
3161 append(delim);
3162 append(faststring(v[i], precision));
3163 removeSpacesBack();
3164 }
3165 }
3166
3167 void set_to_string_with_values(std::vector<int> v, faststring delim= " ")
3168 {
3169 int i, n=v.size();
3170 clear();
3171
3172 if (n == 0)
3173 return;
3174
3175 append(faststring(v[0]));
3176 removeSpacesBack();
3177
3178 for (i=1; i<n; ++i)
3179 {
3180 append(delim);
3181 append(faststring(v[i]));
3182 removeSpacesBack();
3183 }
3184 }
3185
3186
3187 // Could be made more efficient.
3188 // A delimiter could be given as parameter.
3189 int get_vector_of_values(std::vector<double> &v)
3190 {
3191 int i, n;
3192 int bad_values=0;
3193 v.clear();
3194
3195 std::vector<faststring> vs;
3196 split(vs, *this);
3197 n = vs.size();
3198
3199 for(i=0; i<n; ++i)
3200 {
3201 if (vs[i].isADouble() )
3202 v.push_back(vs[i].ToDouble());
3203 else
3204 ++bad_values;
3205 }
3206 return bad_values;
3207 }
3208
3209 // Funktioniert noch nicht richtig:
3210 /* void search_replace_first(faststring s1, faststring s2, unsigned pos=0) */
3211 /* { */
3212 /* unsigned find_pos = find(s1, pos); */
3213
3214 /* std::cerr << "find_pos: " << find_pos << std::endl; */
3215 /* std::cerr << "s1.length(): " << s1.length() << std::endl; */
3216
3217
3218 /* if (find_pos != npos) */
3219 /* replace(find_pos, s1.length(), s2 ); */
3220 /* } */
3221
3222 void search_replace_all(faststring s1, faststring s2)
3223 {
3224 std::vector<faststring> v;
3225 split_at(v, *this, s1);
3226
3227 size_t i, N=v.size();
3228 faststring res;
3229
3230 if (N==1)
3231 return; // Just for efficiency. Otherwise we would copy the string v[0] back to this.
3232
3233 for (i=0; i<(N-1); ++i)
3234 {
3235 res += v[i];
3236 res += s2;
3237 }
3238 res += v[i];
3239
3240 *this = res;
3241 }
3242
3243
3244
3245 };
3246
3247 inline std::ostream &operator<<(std::ostream &out, faststring &str)
3248 {
3249 out << str.c_str();
3250 return out;
3251 }
3252
3253 inline std::ostream &operator<<(std::ostream &out, const faststring &str)
3254 {
3255 faststring fs = str;
3256 out << fs.c_str();
3257 return out;
3258 }
3259
3260 inline faststring operator+(faststring a, faststring b)
3261 {
3262 a += b;
3263 return a;
3264 }
3265
3266 inline std::istream &getline(std::istream &is, faststring &str)
3267 {
3268 int c;
3269
3270 str.clear();
3271
3272 c = is.get();
3273 if ( c == '\r' )
3274 {
3275 c = is.get();
3276 if ( c != '\n' )
3277 {
3278 is.putback(c);
3279 }
3280 c = '\n';
3281 }
3282 if (c == '\n')
3283 return is;
3284
3285 while (c != EOF)
3286 {
3287 str.push_back(c);
3288 c = is.get();
3289 if ( c == '\r' )
3290 {
3291 c = is.get();
3292 if ( c != '\n' )
3293 {
3294 is.putback(c);
3295 }
3296 c = '\n';
3297 return is;
3298 }
3299 if (c == '\n')
3300 {
3301 return is;
3302 }
3303 }
3304 return is;
3305 }
3306
3307 /* Can be optimised ?? */
3308 /* Function is declared inline since it is a global function in a header file. It is not declared */
3309 /* static in order not to get compiler warnings due to unused functions. */
3310 //
3311 // A note on efficiency: This is not the most efficient way to read a file.
3312 // Indeed the many push_back calls can lead to many reallocation events for the string buffer.
3313 // TODO: Could be made more efficient by measuring the file and by calling reserve.
3314 inline void readFileIntoString(std::ifstream &is, faststring &str)
3315 {
3316 int c;
3317
3318 str.clear();
3319
3320 c = is.get();
3321 while (true)
3322 {
3323 if ( c == '\r' )
3324 {
3325 str.push_back('\n');
3326 c = is.get();
3327 if ( c == '\n')
3328 {
3329 c = is.get();
3330 }
3331 }
3332 else if (c == '\n')
3333 {
3334 str.push_back('\n');
3335 c = is.get();
3336 }
3337 else
3338 {
3339 str.push_back(c);
3340 c = is.get();
3341 }
3342 if (c == EOF)
3343 return;
3344 }
3345 }
3346
3347 //***********************************************************************
3348 // Reads the content of a file and APPENDs it to he vector.
3349 // NOTE: A vector<faststring> can be extemely inefficient.
3350 // When expanding the size of the vector, the vector class might
3351 // destruct and reconstruct all items, when reallocating them.
3352 //***********************************************************************
3353 inline void appendFileToVector(std::ifstream &is, std::vector<faststring> &v)
3354 {
3355 faststring line;
3356
3357 getline(is, line);
3358 while (is)
3359 {
3360 v.push_back(line);
3361 getline(is, line);
3362 }
3363 }
3364
3365 inline void appendFileToList(std::ifstream &is, std::list<faststring> &l)
3366 {
3367 faststring line;
3368
3369 getline(is, line);
3370 while (is)
3371 {
3372 l.push_back(line);
3373 getline(is, line);
3374 }
3375 }
3376
3377
3378
3379
3380
3381 //*************************************************************
3382 // Tokenizer
3383 //*************************************************************
3384
3385 // Note on efficiency:
3386 // vector<faststring> can be inefficient as a container in the split functions.
3387 // Expanding the vector leads not only to a relocation of the memory but also to
3388 // the construction of copies and destruction of all existing elements.
3389 // For many splitting events, a list should be favoured.
3390
3391
3392 template <typename Container>
3393 int
3394 split (Container &l, const faststring &s, char const * const ws)
3395 {
3396 l.clear();
3397 const size_t S = s.size();
3398 size_t i = 0;
3399
3400 while (i < S) {
3401 // eat leading whitespace
3402 while ((i < S) && (is_in_symbol_list__(s[i],ws))) ++i;
3403 if (i == S) return l.size(); // nothing left but WS
3404
3405 // find end of word
3406 size_t j = i+1;
3407 while ((j < S) && (!is_in_symbol_list__(s[j],ws))) ++j;
3408
3409 // add word
3410 l.push_back(s.substr(i,j-i));
3411
3412 // set up for next loop
3413 i = j+1;
3414 }
3415 return l.size();
3416 }
3417
3418
3419 template <typename Container>
3420 int
3421 split_at (Container &l, const faststring &s, const faststring &delim)
3422 {
3423 size_t pos1=0;
3424 size_t pos2=0;
3425
3426 while (pos2 != faststring::npos)
3427 {
3428 pos2 = s.find(delim, pos2); // Start of next delimiter
3429
3430 if (pos2 >= s.size() )
3431 break;
3432 l.push_back(s.substr(pos1, pos2-pos1) );
3433 pos1 = pos2+delim.size(); // Start of next token
3434 pos2 = pos1; // Start for next search
3435 }
3436 l.push_back(s.substr(pos1, pos2) );
3437 return l.size();
3438 }
3439
3440
3441 //
3442 // Tokenizer which does not split braces.
3443 //
3444
3445 inline void
3446 skip_until_ws_respect(const faststring &s, size_t &pos, char const * const ws = "\r\n\t\v\f ")
3447 {
3448 char c;
3449
3450 unsigned count_parent = 0;
3451 unsigned count_brace = 0;
3452 unsigned count_curly = 0;
3453 unsigned count_sq = 0;
3454 unsigned count_dq = 0;
3455
3456 size_t size = s.size();
3457
3458 while (pos < size)
3459 {
3460 c = s[pos];
3461 if (c == '(') { ++count_parent; }
3462 else if (c == ')') { --count_parent; }
3463 else if (c == '[') { ++count_brace; }
3464 else if (c == ']') { --count_brace; }
3465 else if (c == '{') { ++count_curly; }
3466 else if (c == '}') { --count_curly; }
3467 else if (c == '\'') { count_sq = (count_sq+1)%2; }
3468 else if (c == '\"') { count_dq = (count_dq+1)%2; }
3469
3470 if (is_in_symbol_list__(c, ws) && count_parent==0 && count_brace==0 && count_curly==0 && count_sq==0 && count_dq==0)
3471 return;
3472
3473 ++pos;
3474 }
3475 }
3476
3477 template <typename Container>
3478 int
3479 split_respect (Container &l, const faststring &s, char const * const ws)
3480
3481 {
3482 l.clear();
3483 const size_t S = s.size();
3484 size_t i = 0;
3485
3486 while (i < S) {
3487 // eat leading whitespace
3488 while ((i < S) && (is_in_symbol_list__(s[i],ws))) ++i;
3489 if (i == S) return l.size(); // nothing left but WS
3490
3491 // find end of word
3492 size_t j = i; // We can't skip the first char which can be a brace.
3493 skip_until_ws_respect(s, j, ws);
3494
3495 // add word
3496 l.push_back(s.substr(i,j-i));
3497
3498 // set up for next loop
3499 i = j+1;
3500 }
3501 return l.size();
3502 }
3503
3504 // Multiple successive delimiter result in multiple hits.
3505 template <typename Container>
3506 int
3507 split_strict (Container &l, const faststring &s, char const * const ws)
3508 {
3509 l.clear();
3510 const size_t S = s.size();
3511 size_t i = 0;
3512
3513 while (i < S) {
3514 // find end of word
3515 size_t j = i;
3516 while ((j < S) && (!is_in_symbol_list__(s[j],ws))) ++j;
3517
3518 // add word
3519 l.push_back(s.substr(i,j-i));
3520
3521 // set up for next loop
3522 i = j+1;
3523
3524 if (i==S) // Only true of last string is empty since otherwise i==S+1
3525 {
3526 l.push_back("");
3527 }
3528 }
3529 return l.size();
3530 }
3531
3532 template <typename Container>
3533 int
3534 split_strict_respect (Container &l, const faststring &s, char const * const ws)
3535 {
3536 l.clear();
3537 const size_t S = s.size();
3538 size_t i = 0;
3539
3540 while (i < S) {
3541 // find end of word
3542 size_t j = i; // We can't skip the first char which can be a brace.
3543 skip_until_ws_respect(s, j, ws);
3544
3545 // add word
3546 l.push_back(s.substr(i,j-i));
3547
3548 // set up for next loop
3549 i = j+1;
3550
3551 if (i==S) // Only true of last string is empty since otherwise i==S+1
3552 {
3553 l.push_back("");
3554 }
3555 }
3556 return l.size();
3557 }
3558
3559
3560
3561 inline bool less_than_pointer_to_faststring(const faststring *a, const faststring *b)
3562 {
3563 return *a < *b;
3564 }
3565
3566 struct less_than_pointer_to_faststring_struct
3567 {
3568 bool operator()(const faststring* s1, const faststring* s2) const
3569 {
3570 return *s1 < *s2;
3571 }
3572 };
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584 #endif
3585
3586 /* String operations: */
3587 /* I do not know what to do with this function: get_allocator Get allocator (public member function) */
3588
3589
3590
3591