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)
127 struct EST_dumb_string {
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);
220 memory = NON_CONST_CHUNKPTR(s.memory);
224 #if __FSF_COMPATIBILITY__
241 int space (
void)
const {
return memory.size(); }
243 const char *
str(
void)
const {
return size==0?
"":(
const char *)memory; }
246 void make_updatable(
void) { cp_make_updatable(memory, size+1);}
251 {
const char s[2] = { c, 0 };
return EST_String(s); }
257 static EST_String Number(
long i,
int base=10);
266 int Int(
bool &ok)
const {
return Int(&ok); }
267 int Int(
void)
const {
return Int((
bool *)NULL); }
271 long Long(
void)
const {
return Long((
bool *)NULL); }
275 float Float(
void)
const {
return Float((
bool *)NULL); }
279 double Double(
void)
const {
return Double((
bool *)NULL); }
285 {
return chop_internal(pos, len, Chop_Before); }
288 {
return chop_internal(s, safe_strlen(s), pos, Chop_Before); }
291 {
return chop_internal(s.
str(), s.size, pos, Chop_Before); }
294 {
return chop_internal(e, pos, Chop_Before); }
301 {
return EST_String(str(),size,from<0?(size+from):from,len); }
304 {
return chop_internal(s, safe_strlen(s), pos, Chop_At); }
307 {
return chop_internal(s.
str(), s.size, pos, Chop_At); }
310 {
return chop_internal(e, pos, Chop_At); }
317 {
return chop_internal(pos, len, Chop_After); }
320 {
return chop_internal(s, safe_strlen(s), pos, Chop_After); }
323 {
return chop_internal(s.
str(), s.size, pos, Chop_After); }
326 {
return chop_internal(e, pos, Chop_After); }
332 int search(
const char *s,
int len,
int &mlen,
int pos=0)
const
334 if (locate(s, len, pos, start, end))
335 { mlen=end-start;
return start; }
342 if (locate(s, pos, start, end))
343 { mlen=end-start;
return start; }
348 int search(
EST_Regex &re,
int &mlen,
int pos=0,
int *starts=NULL,
int *ends=NULL)
const
349 {
int start=0, end=0;
350 if (locate(re, pos, start, end, starts, ends))
351 { mlen=end-start;
return start; }
360 int index(
const char *s,
int pos=0)
const
361 {
int start, end;
return locate(s, safe_strlen(s), pos, start, end)?start:-1; }
364 {
int start, end;
return locate(s, pos, start, end)?start:-1; }
367 {
int start, end;
return locate(ex, pos, start, end)?start:-1; }
373 int contains(
const char *s,
int pos=-1)
const
374 {
int start, end;
return extract(s, safe_strlen(s), pos, start, end); }
377 {
int start, end;
return extract(s, pos, start, end); }
379 int contains(
const char c,
int pos=-1)
const
380 {
int start, end;
char s[2] = {c,0};
return extract(s, 1, pos, start, end); }
383 {
int start, end;
return extract(ex, pos, start, end); }
389 int matches(
const char *e,
int pos=0)
const;
391 int matches(
const EST_String &e,
int pos=0)
const;
393 int matches(
EST_Regex &e,
int pos=0,
int *starts=NULL,
int *ends=NULL)
const;
400 {
return gsub_internal(os, safe_strlen(os), s, s.size); }
402 int gsub(
const char *os,
const char *s)
403 {
return gsub_internal(os, safe_strlen(os), s, safe_strlen(s)); }
406 {
return gsub_internal(os, os.size, s, s.size); }
409 {
return gsub_internal(os, os.size, s, safe_strlen(s)); }
413 {
return gsub_internal(ex, s, s.size); }
416 {
return gsub_internal(ex, s, safe_strlen(s)); }
419 {
return gsub_internal(ex, NULL, bracket_num); }
422 int (&starts)[EST_Regex_max_subexpressions],
423 int (&ends)[EST_Regex_max_subexpressions]);
429 int freq(
const char *s)
const;
441 EST_String quote_if_needed(
const char quotec)
const;
445 EST_String unquote_if_needed(
const char quotec)
const;
448 #if __FSF_COMPATIBILITY__
449 const char operator [] (
int i)
const {
return memory[i]; }
450 char &operator () (
int i) {
return memory(i); }
455 const char operator () (
int i)
const {
return memory[i]; }
457 char &operator [] (
int i) {
return memory(i); }
461 operator const char*()
const {
return str(); }
462 operator const char*() {
return str(); }
464 operator char*() {
return updatable_str(); }
500 friend int operator == (
const char *a,
const EST_String &b);
502 friend int operator == (
const EST_String &a,
const char *b)
508 friend int operator != (
const char *a,
const EST_String &b)
511 friend int operator != (
const EST_String &a,
const char *b)
518 friend inline int operator < (
const char *a,
const EST_String &b)
519 {
return compare(a,b) < 0; }
521 friend inline int operator < (
const EST_String &a,
const char *b)
522 {
return compare(a,b) < 0; }
525 {
return compare(a,b) < 0; }
527 friend inline int operator > (
const char *a,
const EST_String &b)
528 {
return compare(a,b) > 0; }
530 friend inline int operator > (
const EST_String &a,
const char *b)
531 {
return compare(a,b) > 0; }
534 {
return compare(a,b) > 0; }
536 friend inline int operator <= (
const char *a,
const EST_String &b)
537 {
return compare(a,b) <= 0; }
539 friend inline int operator <= (
const EST_String &a,
const char *b)
540 {
return compare(a,b) <= 0; }
543 {
return compare(a,b) <= 0; }
545 friend inline int operator >= (
const char *a,
const EST_String &b)
546 {
return compare(a,b) >= 0; }
548 friend inline int operator >= (
const EST_String &a,
const char *b)
549 {
return compare(a,b) >= 0; }
552 {
return compare(a,b) >= 0; }
565 friend int compare(
const EST_String &a,
const char *b);
567 friend inline int compare(
const char *a,
const EST_String &b)
568 {
return -compare(b,a); }
577 const unsigned char *table);
580 {
return fcompare(a,b,NULL); }
583 friend int fcompare(
const EST_String &a,
const char *b,
584 const unsigned char *table);
588 {
return fcompare(a, b, (
const unsigned char *)(
const char *)table); }
602 int max,
const EST_String& seperator,
char quote=0)
603 {
return s.split_internal(result, max, (
const char *)seperator, seperator.
length(), NULL, quote); }
606 int max,
const char *seperator,
char quote=0)
607 {
return s.split_internal(result, max, seperator, strlen(seperator), NULL, quote); }
611 {
return s.split_internal(result, max, NULL, 0, &seperator, quote); }
637 friend ostream &operator << (ostream &s,
const EST_String &str);
643 EST_ChunkPtr chunk_allocate(
int bytes,
const char *initial,
int initial_len);
646 int operator == (
const char *a,
const EST_String &b);
650 #include "EST_Regex.h"