34 #ifndef __EST_STRING_H__ 35 #define __EST_STRING_H__ 40 #define EST_Regex_max_subexpressions 10 46 #include "EST_Chunk.h" 47 #include "EST_strcasecmp.h" 50 extern "C" void abort(
void);
77 # define __FSF_COMPATIBILITY__ (0) 84 # define __GSUB_REENTRANT__ (1) 87 #define __STRING_ARG_GRIPE__ (1) 90 #define __GRIPE_FATAL__ (1) 93 # define gripe(WHAT) (cerr<< ("oops! " WHAT "\n"),abort()) 95 # define gripe(WHAT) (cerr<< ("oops! " WHAT "\n")) 98 #if __STRING_ARG_GRIPE__ 99 # define safe_strlen(S) ((S)?strlen(S):(gripe("null strlen"),0)) 100 # define CHECK_STRING_ARG(S) if (!(S)) gripe("null string arg") 102 # define safe_strlen(S) ((S)?strlen(S):0) 103 # define CHECK_STRING_ARG(S) 116 # define MAX_STRING_SIZE (INT_MAX) 122 EST_string_size size;
127 struct EST_dumb_string {
129 EST_string_size size;
133 enum EST_chop_direction {
150 int shareing (
void) {
return memory.shareing();}
155 int locate(
const char *it,
int len,
int from,
int &start,
int &end)
const;
157 int locate(
const EST_String &s,
int from,
int &start,
int &end)
const 158 {
return locate((
const char *)s.memory, s.size, from, start, end); }
160 int locate(
EST_Regex &ex,
int from,
int &start,
int &end,
int *starts=NULL,
int *ends=NULL)
const;
166 int extract(
const char *it,
int len,
int from,
int &start,
int &end)
const;
167 int extract(
const EST_String &s,
int from,
int &start,
int &end)
const 168 {
return extract((
const char *)s.memory, s.size, from, start, end); }
169 int extract(
EST_Regex &ex,
int from,
int &start,
int &end)
const;
175 EST_String chop_internal(
const char *s,
int length,
int pos, EST_chop_direction directionult)
const;
177 EST_String chop_internal(
int pos,
int length, EST_chop_direction directionult)
const;
186 int gsub_internal(
const char *os,
int olength,
const char *s,
int length);
188 int gsub_internal(
EST_Regex &ex,
const char *s,
int length);
192 int split_internal(
EST_String result[],
int max,
const char* s_seperator,
int slen,
EST_Regex *re_separator,
char quote)
const;
194 int Int(
bool *ok_p)
const;
195 long Long(
bool *ok_p)
const;
196 float Float(
bool *ok_p)
const;
197 double Double(
bool *ok_p)
const;
207 EST_String(
const char *s,
int start_or_fill,
int len);
210 EST_String(
const char *s,
int s_size,
int start,
int len);
221 memory = NON_CONST_CHUNKPTR(s.memory);
226 #if __FSF_COMPATIBILITY__ 243 int space (
void)
const {
return memory.size(); }
245 const char *
str(
void)
const {
return size==0?
"":(
const char *)memory; }
248 void make_updatable(
void) { cp_make_updatable(memory, size+1);}
253 {
const char s[2] = { c, 0 };
return EST_String(s); }
259 static EST_String Number(
long i,
int base=10);
268 int Int(
bool &ok)
const {
return Int(&ok); }
269 int Int(
void)
const {
return Int((
bool *)NULL); }
273 long Long(
void)
const {
return Long((
bool *)NULL); }
277 float Float(
void)
const {
return Float((
bool *)NULL); }
281 double Double(
void)
const {
return Double((
bool *)NULL); }
287 {
return chop_internal(pos, len, Chop_Before); }
290 {
return chop_internal(s, safe_strlen(s), pos, Chop_Before); }
293 {
return chop_internal(s.
str(), s.size, pos, Chop_Before); }
296 {
return chop_internal(e, pos, Chop_Before); }
303 {
return EST_String(str(),size,from<0?(size+from):from,len); }
306 {
return chop_internal(s, safe_strlen(s), pos, Chop_At); }
309 {
return chop_internal(s.
str(), s.size, pos, Chop_At); }
312 {
return chop_internal(e, pos, Chop_At); }
319 {
return chop_internal(pos, len, Chop_After); }
322 {
return chop_internal(s, safe_strlen(s), pos, Chop_After); }
325 {
return chop_internal(s.
str(), s.size, pos, Chop_After); }
328 {
return chop_internal(e, pos, Chop_After); }
334 int search(
const char *s,
int len,
int &mlen,
int pos=0)
const 336 if (locate(s, len, pos, start, end))
337 { mlen=end-start;
return start; }
344 if (locate(s, pos, start, end))
345 { mlen=end-start;
return start; }
350 int search(
EST_Regex &re,
int &mlen,
int pos=0,
int *starts=NULL,
int *ends=NULL)
const 351 {
int start=0, end=0;
352 if (locate(re, pos, start, end, starts, ends))
353 { mlen=end-start;
return start; }
362 int index(
const char *s,
int pos=0)
const 363 {
int start, end;
return locate(s, safe_strlen(s), pos, start, end)?start:-1; }
366 {
int start, end;
return locate(s, pos, start, end)?start:-1; }
369 {
int start, end;
return locate(ex, pos, start, end)?start:-1; }
376 {
int start, end;
return extract(s, safe_strlen(s), pos, start, end); }
379 {
int start, end;
return extract(s, pos, start, end); }
382 {
int start, end;
char s[2] = {c,0};
return extract(s, 1, pos, start, end); }
385 {
int start, end;
return extract(ex, pos, start, end); }
391 int matches(
const char *e,
int pos=0)
const;
393 int matches(
const EST_String &e,
int pos=0)
const;
395 int matches(
EST_Regex &e,
int pos=0,
int *starts=NULL,
int *ends=NULL)
const;
402 {
return gsub_internal(os, safe_strlen(os), s, s.size); }
404 int gsub(
const char *os,
const char *s)
405 {
return gsub_internal(os, safe_strlen(os), s, safe_strlen(s)); }
408 {
return gsub_internal(os, os.size, s, s.size); }
411 {
return gsub_internal(os, os.size, s, safe_strlen(s)); }
415 {
return gsub_internal(ex, s, s.size); }
418 {
return gsub_internal(ex, s, safe_strlen(s)); }
421 {
return gsub_internal(ex, NULL, bracket_num); }
424 int (&starts)[EST_Regex_max_subexpressions],
425 int (&ends)[EST_Regex_max_subexpressions]);
431 int freq(
const char *s)
const;
443 EST_String quote_if_needed(
const char quotec)
const;
447 EST_String unquote_if_needed(
const char quotec)
const;
450 #if __FSF_COMPATIBILITY__ 451 const char operator [] (
int i)
const {
return memory[i]; }
452 char &operator () (
int i) {
return memory(i); }
457 const char operator () (
int i)
const {
return memory[i]; }
459 char &operator [] (
int i) {
return memory(i); }
463 operator const char*()
const {
return str(); }
464 operator const char*() {
return str(); }
466 operator char*() {
return updatable_str(); }
504 friend int operator == (
const char *a,
const EST_String &b);
506 friend int operator == (
const EST_String &a,
const char *b)
512 friend int operator != (
const char *a,
const EST_String &b)
515 friend int operator != (
const EST_String &a,
const char *b)
522 friend inline int operator < (
const char *a,
const EST_String &b)
523 {
return compare(a,b) < 0; }
525 friend inline int operator < (
const EST_String &a,
const char *b)
526 {
return compare(a,b) < 0; }
529 {
return compare(a,b) < 0; }
531 friend inline int operator > (
const char *a,
const EST_String &b)
532 {
return compare(a,b) > 0; }
534 friend inline int operator > (
const EST_String &a,
const char *b)
535 {
return compare(a,b) > 0; }
538 {
return compare(a,b) > 0; }
540 friend inline int operator <= (
const char *a,
const EST_String &b)
541 {
return compare(a,b) <= 0; }
543 friend inline int operator <= (
const EST_String &a,
const char *b)
544 {
return compare(a,b) <= 0; }
547 {
return compare(a,b) <= 0; }
549 friend inline int operator >= (
const char *a,
const EST_String &b)
550 {
return compare(a,b) >= 0; }
552 friend inline int operator >= (
const EST_String &a,
const char *b)
553 {
return compare(a,b) >= 0; }
556 {
return compare(a,b) >= 0; }
569 friend int compare(
const EST_String &a,
const char *b);
571 friend inline int compare(
const char *a,
const EST_String &b)
572 {
return -compare(b,a); }
581 const unsigned char *table);
584 {
return fcompare(a,b,NULL); }
587 friend int fcompare(
const EST_String &a,
const char *b,
588 const unsigned char *table);
592 {
return fcompare(a, b, (
const unsigned char *)(
const char *)table); }
606 int max,
const EST_String& seperator,
char quote=0)
607 {
return s.split_internal(result, max, (
const char *)seperator, seperator.
length(), NULL, quote); }
610 int max,
const char *seperator,
char quote=0)
611 {
return s.split_internal(result, max, seperator, strlen(seperator), NULL, quote); }
615 {
return s.split_internal(result, max, NULL, 0, &seperator, quote); }
641 friend ostream &operator << (ostream &s,
const EST_String &str);
647 EST_ChunkPtr chunk_allocate(
int bytes,
const char *initial,
int initial_len);
650 int operator == (
const char *a,
const EST_String &b);
654 #include "EST_Regex.h" EST_String at(int from, int len=0) const
Return part at position.
int search(const char *s, int len, int &mlen, int pos=0) const
Find a substring.
EST_String(void)
Construct an empty string.
int gsub(EST_Regex &ex, const char *s)
Substitute string for matches of regular expression.
friend ostream & operator<<(ostream &s, const EST_Regex &str)
Stream output of regular expression.
int index(const EST_String &s, int pos=0) const
Position of substring (starting at pos)
EST_String after(EST_Regex &e, int pos=0) const
Part after match of regular expression.
int space(void) const
Size of underlying chunk.
static const char * version
Global version string.
EST_String before(EST_Regex &e, int pos=0) const
Part before first match of regexp after pos.
double Double(bool &ok) const
Convert to a double.
friend int split(const EST_String &s, EST_String result[], int max, EST_Regex &seperator, char quote=0)
Split at each match of the regular expression.
int contains(const char *s, int pos=-1) const
Does it contain this substring?
int gsub(EST_Regex &ex, const EST_String &s)
Substitute string for matches of regular expression.
int contains(const EST_String &s, int pos=-1) const
Does it contain this substring?
EST_String before(const EST_String &s, int pos=0) const
Part before first matching substring after pos.
EST_String at(const char *s, int pos=0) const
Return part where substring found (not useful, included for completeness)
int gsub(const char *os, const EST_String &s)
Substitute one string for another.
float Float(bool &ok) const
Convert to a float.
friend int split(const EST_String &s, EST_String result[], int max, const char *seperator, char quote=0)
Split at a given separator.
const char * str(void) const
Get a const-pointer to the actual memory.
int index(const char *s, int pos=0) const
Position of substring (starting at pos)
int index(EST_Regex &ex, int pos=0) const
Position of match of regexp (starting at pos)
int EST_string_size
Type of string size field.
EST_String after(const char *s, int pos=0) const
Part after substring.
friend int split(const EST_String &s, EST_String result[], int max, const EST_String &seperator, char quote=0)
Split at a given separator.
static EST_String FromChar(const char c)
Build string from a single character.
EST_String at(EST_Regex &e, int pos=0) const
Return part matching regexp.
EST_String before(int pos, int len=0) const
Part before position.
int gsub(const EST_String &os, const char *s)
Substitute one string for another.
EST_String before(const char *s, int pos=0) const
Part before first matching substring after pos.
int search(const EST_String s, int &mlen, int pos=0) const
Find a substring.
int Int(bool &ok) const
Convert to an integer.
EST_String after(int pos, int len=1) const
Part after pos+len.
EST_String at(const EST_String &s, int pos=0) const
Return part where substring found (not useful, included for completeness)
int search(EST_Regex &re, int &mlen, int pos=0, int *starts=NULL, int *ends=NULL) const
Find a match of the regular expression.
char * updatable_str(void)
Get a writable pointer to the actual memory.
EST_String after(const EST_String &s, int pos=0) const
Part after substring.
int gsub(const char *os, const char *s)
Substitute one string for another.
long Long(bool &ok) const
Convert to a long.
int contains(const char c, int pos=-1) const
Does it contain this character?
static const EST_String Empty
Constant empty string.
int contains(EST_Regex &ex, int pos=-1) const
Does it contain a match for this regular expression?
int gsub(EST_Regex &ex, int bracket_num)
Substitute string for matches of regular expression.
int length(void) const
Length of string ({not} length of underlying chunk)
int gsub(const EST_String &os, const EST_String &s)
Substitute one string for another.