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);
78 # define __FSF_COMPATIBILITY__ (0)
85 # define __GSUB_REENTRANT__ (1)
88 #define __STRING_ARG_GRIPE__ (1)
91 #define __GRIPE_FATAL__ (1)
94 # define gripe(WHAT) (cerr<< ("oops! " WHAT "\n"),abort())
96 # define gripe(WHAT) (cerr<< ("oops! " WHAT "\n"))
99 #if __STRING_ARG_GRIPE__
100 # define safe_strlen(S) ((S)?strlen(S):(gripe("null strlen"),0))
101 # define CHECK_STRING_ARG(S) if (!(S)) gripe("null string arg")
103 # define safe_strlen(S) ((S)?strlen(S):0)
104 # define CHECK_STRING_ARG(S)
117 # define MAX_STRING_SIZE (INT_MAX)
128 struct EST_dumb_string {
134 enum EST_chop_direction {
151 int shareing (
void) {
return memory.shareing();}
156 int locate(
const char *it,
int len,
int from,
int &start,
int &end)
const;
158 int locate(
const EST_String &s,
int from,
int &start,
int &end)
const
159 {
return locate((
const char *)s.memory, s.size, from, start, end); }
161 int locate(
EST_Regex &ex,
int from,
int &start,
int &end,
int *starts=NULL,
int *ends=NULL)
const;
167 int extract(
const char *it,
int len,
int from,
int &start,
int &end)
const;
168 int extract(
const EST_String &s,
int from,
int &start,
int &end)
const
169 {
return extract((
const char *)s.memory, s.size, from, start, end); }
170 int extract(
EST_Regex &ex,
int from,
int &start,
int &end)
const;
176 EST_String chop_internal(
const char *s,
int length,
int pos, EST_chop_direction directionult)
const;
178 EST_String chop_internal(
int pos,
int length, EST_chop_direction directionult)
const;
187 int gsub_internal(
const char *os,
int olength,
const char *s,
int length);
189 int gsub_internal(
EST_Regex &ex,
const char *s,
int length);
193 int split_internal(
EST_String result[],
int max,
const char* s_seperator,
int slen,
EST_Regex *re_separator,
char quote)
const;
195 int Int(
bool *ok_p)
const;
196 long Long(
bool *ok_p)
const;
197 float Float(
bool *ok_p)
const;
198 double Double(
bool *ok_p)
const;
208 EST_String(
const char *s,
int start_or_fill,
int len);
211 EST_String(
const char *s,
int s_size,
int start,
int len);
221 memory = NON_CONST_CHUNKPTR(s.memory);
225 #if __FSF_COMPATIBILITY__
242 int space (
void)
const {
return memory.size(); }
244 const char *
str(
void)
const {
return size==0?
"":(
const char *)memory; }
247 void make_updatable(
void) { cp_make_updatable(memory, size+1);}
252 {
const char s[2] = { c, 0 };
return EST_String(s); }
258 static EST_String Number(
long i,
int base=10);
267 int Int(
bool &ok)
const {
return Int(&ok); }
268 int Int(
void)
const {
return Int((
bool *)NULL); }
272 long Long(
void)
const {
return Long((
bool *)NULL); }
276 float Float(
void)
const {
return Float((
bool *)NULL); }
280 double Double(
void)
const {
return Double((
bool *)NULL); }
286 {
return chop_internal(pos, len, Chop_Before); }
289 {
return chop_internal(s, safe_strlen(s), pos, Chop_Before); }
292 {
return chop_internal(s.
str(), s.size, pos, Chop_Before); }
295 {
return chop_internal(e, pos, Chop_Before); }
302 {
return EST_String(str(),size,from<0?(size+from):from,len); }
305 {
return chop_internal(s, safe_strlen(s), pos, Chop_At); }
308 {
return chop_internal(s.
str(), s.size, pos, Chop_At); }
311 {
return chop_internal(e, pos, Chop_At); }
318 {
return chop_internal(pos, len, Chop_After); }
321 {
return chop_internal(s, safe_strlen(s), pos, Chop_After); }
324 {
return chop_internal(s.
str(), s.size, pos, Chop_After); }
327 {
return chop_internal(e, pos, Chop_After); }
333 int search(
const char *s,
int len,
int &mlen,
int pos=0)
const
335 if (locate(s, len, pos, start, end))
336 { mlen=end-start;
return start; }
343 if (locate(s, pos, start, end))
344 { mlen=end-start;
return start; }
349 int search(
EST_Regex &re,
int &mlen,
int pos=0,
int *starts=NULL,
int *ends=NULL)
const
350 {
int start=0, end=0;
351 if (locate(re, pos, start, end, starts, ends))
352 { mlen=end-start;
return start; }
361 int index(
const char *s,
int pos=0)
const
362 {
int start, end;
return locate(s, safe_strlen(s), pos, start, end)?start:-1; }
365 {
int start, end;
return locate(s, pos, start, end)?start:-1; }
368 {
int start, end;
return locate(ex, pos, start, end)?start:-1; }
375 {
int start, end;
return extract(s, safe_strlen(s), pos, start, end); }
378 {
int start, end;
return extract(s, pos, start, end); }
381 {
int start, end;
char s[2] = {c,0};
return extract(s, 1, pos, start, end); }
384 {
int start, end;
return extract(ex, pos, start, end); }
390 int matches(
const char *e,
int pos=0)
const;
392 int matches(
const EST_String &e,
int pos=0)
const;
394 int matches(
EST_Regex &e,
int pos=0,
int *starts=NULL,
int *ends=NULL)
const;
401 {
return gsub_internal(os, safe_strlen(os), s, s.size); }
403 int gsub(
const char *os,
const char *s)
404 {
return gsub_internal(os, safe_strlen(os), s, safe_strlen(s)); }
407 {
return gsub_internal(os, os.size, s, s.size); }
410 {
return gsub_internal(os, os.size, s, safe_strlen(s)); }
414 {
return gsub_internal(ex, s, s.size); }
417 {
return gsub_internal(ex, s, safe_strlen(s)); }
420 {
return gsub_internal(ex, NULL, bracket_num); }
423 int (&starts)[EST_Regex_max_subexpressions],
424 int (&ends)[EST_Regex_max_subexpressions]);
430 int freq(
const char *s)
const;
442 EST_String quote_if_needed(
const char quotec)
const;
446 EST_String unquote_if_needed(
const char quotec)
const;
451 #if __FSF_COMPATIBILITY__
452 const char operator [] (
int i)
const {
return memory[i]; }
453 char &operator () (
int i) {
return memory(i); }
456 const char operator () (
int i)
const {
return memory[i]; }
458 char &operator [] (
int i) {
return memory(i); }
462 operator const char*()
const {
return str(); }
463 operator const char*() {
return str(); }
465 operator char*() {
return updatable_str(); }
503 friend int operator == (
const char *a,
const EST_String &b);
505 friend int operator == (
const EST_String &a,
const char *b)
511 friend int operator != (
const char *a,
const EST_String &b)
514 friend int operator != (
const EST_String &a,
const char *b)
521 friend inline int operator < (
const char *a,
const EST_String &b)
522 {
return compare(a,b) < 0; }
524 friend inline int operator < (
const EST_String &a,
const char *b)
525 {
return compare(a,b) < 0; }
528 {
return compare(a,b) < 0; }
530 friend inline int operator > (
const char *a,
const EST_String &b)
531 {
return compare(a,b) > 0; }
533 friend inline int operator > (
const EST_String &a,
const char *b)
534 {
return compare(a,b) > 0; }
537 {
return compare(a,b) > 0; }
539 friend inline int operator <= (
const char *a,
const EST_String &b)
540 {
return compare(a,b) <= 0; }
542 friend inline int operator <= (
const EST_String &a,
const char *b)
543 {
return compare(a,b) <= 0; }
546 {
return compare(a,b) <= 0; }
548 friend inline int operator >= (
const char *a,
const EST_String &b)
549 {
return compare(a,b) >= 0; }
551 friend inline int operator >= (
const EST_String &a,
const char *b)
552 {
return compare(a,b) >= 0; }
555 {
return compare(a,b) >= 0; }
568 friend int compare(
const EST_String &a,
const char *b);
570 friend inline int compare(
const char *a,
const EST_String &b)
571 {
return -compare(b,a); }
580 const unsigned char *table=NULL);
582 friend int fcompare(
const EST_String &a,
const char *b,
583 const unsigned char *table=NULL);
587 {
return fcompare(a, b, (
const unsigned char *)(
const char *)table); }
601 int max,
const EST_String& seperator,
char quote=0)
602 {
return s.split_internal(result, max, (
const char *)seperator, seperator.
length(), NULL, quote); }
605 int max,
const char *seperator,
char quote=0)
606 {
return s.split_internal(result, max, seperator, strlen(seperator), NULL, quote); }
610 {
return s.split_internal(result, max, NULL, 0, &seperator, quote); }
636 friend ostream &operator << (ostream &s,
const EST_String &str);
642 EST_ChunkPtr chunk_allocate(
int bytes,
const char *initial,
int initial_len);
645 int operator == (
const char *a,
const EST_String &b);
649 #include "EST_Regex.h"
EST_String at(EST_Regex &e, int pos=0) const
Return part matching regexp.
EST_String(void)
Construct an empty string.
EST_String at(const EST_String &s, int pos=0) const
Return part where substring found (not useful, included for completeness)
EST_String after(const char *s, int pos=0) const
Part after substring.
int Int(bool &ok) const
Convert to an integer.
int index(const char *s, int pos=0) const
Position of substring (starting at pos)
int gsub(EST_Regex &ex, const char *s)
Substitute string for matches of regular expression.
A Regular expression class to go with the CSTR EST_String class.
friend ostream & operator<<(ostream &s, const EST_Regex &str)
Stream output of regular expression.
long Long(bool &ok) const
Convert to a long.
static const char * version
Global version string.
EST_String after(EST_Regex &e, int pos=0) const
Part after match of regular expression.
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 gsub(EST_Regex &ex, const EST_String &s)
Substitute string for matches of regular expression.
int index(const EST_String &s, int pos=0) const
Position of substring (starting at pos)
int contains(const EST_String &s, int pos=-1) const
Does it contain this substring?
int gsub(const char *os, const EST_String &s)
Substitute one string for another.
EST_String after(const EST_String &s, int pos=0) const
Part after substring.
friend int split(const EST_String &s, EST_String result[], int max, const char *seperator, char quote=0)
Split at a given separator.
int space(void) const
Size of underlying chunk.
int EST_string_size
Type of string size field.
int contains(const char c, int pos=-1) const
Does it contain this character?
friend int split(const EST_String &s, EST_String result[], int max, const EST_String &seperator, char quote=0)
Split at a given separator.
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)
static EST_String FromChar(const char c)
Build string from a single character.
int contains(EST_Regex &ex, int pos=-1) const
Does it contain a match for this regular expression?
int length(void) const
Length of string ({not} length of underlying chunk)
int gsub(const EST_String &os, const char *s)
Substitute one string for another.
int index(EST_Regex &ex, int pos=0) const
Position of match of regexp (starting at pos)
EST_String before(EST_Regex &e, int pos=0) const
Part before first match of regexp after pos.
EST_String before(const char *s, int pos=0) const
Part before first matching substring after pos.
int contains(const char *s, int pos=-1) const
Does it contain this substring?
const char * str(void) const
Get a const-pointer to the actual memory.
char * updatable_str(void)
Get a writable pointer to the actual memory.
int search(EST_Regex &re, int &mlen, int pos=0, int *starts=NULL, int *ends=NULL) const
Find a match of the regular expression.
int gsub(const char *os, const char *s)
Substitute one string for another.
double Double(bool &ok) const
Convert to a double.
EST_String after(int pos, int len=1) const
Part after pos+len.
int search(const EST_String s, int &mlen, int pos=0) const
Find a substring.
EST_String before(int pos, int len=0) const
Part before position.
int search(const char *s, int len, int &mlen, int pos=0) const
Find a substring.
EST_String at(int from, int len=0) const
Return part at position.
EST_String(const EST_String &s)
static const EST_String Empty
Constant empty string.
int gsub(EST_Regex &ex, int bracket_num)
Substitute string for matches of regular expression.
float Float(bool &ok) const
Convert to a float.
int gsub(const EST_String &os, const EST_String &s)
Substitute one string for another.