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);
82 # define __FSF_COMPATIBILITY__ (0)
89 # define __GSUB_REENTRANT__ (1)
92 #define __STRING_ARG_GRIPE__ (1)
95 #define __GRIPE_FATAL__ (1)
98 # define gripe(WHAT) (cerr<< ("oops! " WHAT "\n"),abort())
100 # define gripe(WHAT) (cerr<< ("oops! " WHAT "\n"))
103 #if __STRING_ARG_GRIPE__
104 # define safe_strlen(S) ((S)?strlen(S):(gripe("null strlen"),0))
105 # define CHECK_STRING_ARG(S) if (!(S)) gripe("null string arg")
107 # define safe_strlen(S) ((S)?strlen(S):0)
108 # define CHECK_STRING_ARG(S)
121 # define MAX_STRING_SIZE (INT_MAX)
132 struct EST_dumb_string {
138 enum EST_chop_direction {
155 int shareing (
void) {
return memory.shareing();}
160 int locate(
const char *it,
int len,
int from,
int &start,
int &end)
const;
162 int locate(
const EST_String &s,
int from,
int &start,
int &end)
const
163 {
return locate((
const char *)s.memory, s.size, from, start, end); }
165 int locate(
EST_Regex &ex,
int from,
int &start,
int &end,
int *starts=NULL,
int *ends=NULL)
const;
171 int extract(
const char *it,
int len,
int from,
int &start,
int &end)
const;
172 int extract(
const EST_String &s,
int from,
int &start,
int &end)
const
173 {
return extract((
const char *)s.memory, s.size, from, start, end); }
174 int extract(
EST_Regex &ex,
int from,
int &start,
int &end)
const;
180 EST_String chop_internal(
const char *s,
int length,
int pos, EST_chop_direction directionult)
const;
182 EST_String chop_internal(
int pos,
int length, EST_chop_direction directionult)
const;
191 int gsub_internal(
const char *os,
int olength,
const char *s,
int length);
193 int gsub_internal(
EST_Regex &ex,
const char *s,
int length);
197 int split_internal(
EST_String result[],
int max,
const char* s_seperator,
int slen,
EST_Regex *re_separator,
char quote)
const;
199 int Int(
bool *ok_p)
const;
200 long Long(
bool *ok_p)
const;
201 float Float(
bool *ok_p)
const;
202 double Double(
bool *ok_p)
const;
212 EST_String(
const char *s,
int start_or_fill,
int len);
215 EST_String(
const char *s,
int s_size,
int start,
int len);
225 memory = NON_CONST_CHUNKPTR(s.memory);
229 #if __FSF_COMPATIBILITY__
246 int space (
void)
const {
return memory.size(); }
248 const char *
str(
void)
const {
return size==0?
"":(
const char *)memory; }
251 void make_updatable(
void) { cp_make_updatable(memory, size+1);}
256 {
const char s[2] = { c, 0 };
return EST_String(s); }
262 static EST_String Number(
long i,
int base=10);
271 int Int(
bool &ok)
const {
return Int(&ok); }
272 int Int(
void)
const {
return Int((
bool *)NULL); }
276 long Long(
void)
const {
return Long((
bool *)NULL); }
280 float Float(
void)
const {
return Float((
bool *)NULL); }
284 double Double(
void)
const {
return Double((
bool *)NULL); }
290 {
return chop_internal(pos, len, Chop_Before); }
293 {
return chop_internal(s, safe_strlen(s), pos, Chop_Before); }
296 {
return chop_internal(s.
str(), s.size, pos, Chop_Before); }
299 {
return chop_internal(e, pos, Chop_Before); }
306 {
return EST_String(str(),size,from<0?(size+from):from,len); }
309 {
return chop_internal(s, safe_strlen(s), pos, Chop_At); }
312 {
return chop_internal(s.
str(), s.size, pos, Chop_At); }
315 {
return chop_internal(e, pos, Chop_At); }
322 {
return chop_internal(pos, len, Chop_After); }
325 {
return chop_internal(s, safe_strlen(s), pos, Chop_After); }
328 {
return chop_internal(s.
str(), s.size, pos, Chop_After); }
331 {
return chop_internal(e, pos, Chop_After); }
337 int search(
const char *s,
int len,
int &mlen,
int pos=0)
const
339 if (locate(s, len, pos, start, end))
340 { mlen=end-start;
return start; }
347 if (locate(s, pos, start, end))
348 { mlen=end-start;
return start; }
353 int search(
EST_Regex &re,
int &mlen,
int pos=0,
int *starts=NULL,
int *ends=NULL)
const
354 {
int start=0, end=0;
355 if (locate(re, pos, start, end, starts, ends))
356 { mlen=end-start;
return start; }
365 int index(
const char *s,
int pos=0)
const
366 {
int start, end;
return locate(s, safe_strlen(s), pos, start, end)?start:-1; }
369 {
int start, end;
return locate(s, pos, start, end)?start:-1; }
372 {
int start, end;
return locate(ex, pos, start, end)?start:-1; }
379 {
int start, end;
return extract(s, safe_strlen(s), pos, start, end); }
382 {
int start, end;
return extract(s, pos, start, end); }
385 {
int start, end;
char s[2] = {c,0};
return extract(s, 1, pos, start, end); }
388 {
int start, end;
return extract(ex, pos, start, end); }
394 int matches(
const char *e,
int pos=0)
const;
396 int matches(
const EST_String &e,
int pos=0)
const;
398 int matches(
EST_Regex &e,
int pos=0,
int *starts=NULL,
int *ends=NULL)
const;
405 {
return gsub_internal(os, safe_strlen(os), s, s.size); }
407 int gsub(
const char *os,
const char *s)
408 {
return gsub_internal(os, safe_strlen(os), s, safe_strlen(s)); }
411 {
return gsub_internal(os, os.size, s, s.size); }
414 {
return gsub_internal(os, os.size, s, safe_strlen(s)); }
418 {
return gsub_internal(ex, s, s.size); }
421 {
return gsub_internal(ex, s, safe_strlen(s)); }
424 {
return gsub_internal(ex, NULL, bracket_num); }
427 int (&starts)[EST_Regex_max_subexpressions],
428 int (&ends)[EST_Regex_max_subexpressions]);
434 int freq(
const char *s)
const;
446 EST_String quote_if_needed(
const char quotec)
const;
450 EST_String unquote_if_needed(
const char quotec)
const;
455 #if __FSF_COMPATIBILITY__
456 const char operator [] (
int i)
const {
return memory[i]; }
457 char &operator () (
int i) {
return memory(i); }
460 const char operator () (
int i)
const {
return memory[i]; }
462 char &operator [] (
int i) {
return memory(i); }
466 operator const char*()
const {
return str(); }
467 operator const char*() {
return str(); }
469 operator char*() {
return updatable_str(); }
507 friend int operator == (
const char *a,
const EST_String &b);
509 friend int operator == (
const EST_String &a,
const char *b)
515 friend int operator != (
const char *a,
const EST_String &b)
518 friend int operator != (
const EST_String &a,
const char *b)
525 friend inline int operator < (
const char *a,
const EST_String &b)
526 {
return compare(a,b) < 0; }
528 friend inline int operator < (
const EST_String &a,
const char *b)
529 {
return compare(a,b) < 0; }
532 {
return compare(a,b) < 0; }
534 friend inline int operator > (
const char *a,
const EST_String &b)
535 {
return compare(a,b) > 0; }
537 friend inline int operator > (
const EST_String &a,
const char *b)
538 {
return compare(a,b) > 0; }
541 {
return compare(a,b) > 0; }
543 friend inline int operator <= (
const char *a,
const EST_String &b)
544 {
return compare(a,b) <= 0; }
546 friend inline int operator <= (
const EST_String &a,
const char *b)
547 {
return compare(a,b) <= 0; }
550 {
return compare(a,b) <= 0; }
552 friend inline int operator >= (
const char *a,
const EST_String &b)
553 {
return compare(a,b) >= 0; }
555 friend inline int operator >= (
const EST_String &a,
const char *b)
556 {
return compare(a,b) >= 0; }
559 {
return compare(a,b) >= 0; }
572 friend int compare(
const EST_String &a,
const char *b);
574 friend inline int compare(
const char *a,
const EST_String &b)
575 {
return -compare(b,a); }
584 const unsigned char *table);
586 friend int fcompare(
const EST_String &a,
const char *b,
587 const unsigned char *table);
591 {
return fcompare(a, b, (
const unsigned char *)(
const char *)table); }
605 int max,
const EST_String& seperator,
char quote=0)
606 {
return s.split_internal(result, max, (
const char *)seperator, seperator.
length(), NULL, quote); }
609 int max,
const char *seperator,
char quote=0)
610 {
return s.split_internal(result, max, seperator, strlen(seperator), NULL, quote); }
614 {
return s.split_internal(result, max, NULL, 0, &seperator, quote); }
640 friend ostream &operator << (ostream &s,
const EST_String &str);
646 EST_ChunkPtr chunk_allocate(
int bytes,
const char *initial,
int initial_len);
649 int operator == (
const char *a,
const EST_String &b);
653 #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.
friend int fcompare(const EST_String &a, const EST_String &b, const unsigned char *table)
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.