34#ifndef __EST_STRING_H__
35#define __EST_STRING_H__
40#define EST_Regex_max_subexpressions 10
47#include "EST_strcasecmp.h"
50extern "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); }
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;
186 int gsub_internal(
const char *
os,
int olength,
const char *s,
int length);
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;
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); }
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); }
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); }
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; }
351 {
int start=0, end=0;
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;
402 {
return gsub_internal(
os, safe_strlen(
os), s, s.size); }
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)); }
424 int (&
starts)[EST_Regex_max_subexpressions],
425 int (&
ends)[EST_Regex_max_subexpressions]);
431 int freq(
const char *s)
const;
450#if __FSF_COMPATIBILITY__
451 const char operator [] (
int i)
const {
return memory[i]; }
463 operator const char*()
const {
return str(); }
464 operator const char*() {
return str(); }
523 {
return compare(a,b) < 0; }
526 {
return compare(a,b) < 0; }
529 {
return compare(a,b) < 0; }
532 {
return compare(a,b) > 0; }
535 {
return compare(a,b) > 0; }
538 {
return compare(a,b) > 0; }
541 {
return compare(a,b) <= 0; }
544 {
return compare(a,b) <= 0; }
547 {
return compare(a,b) <= 0; }
550 {
return compare(a,b) >= 0; }
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);
588 const unsigned char *
table);
592 {
return fcompare(a, b, (
const unsigned char *)(
const char *)
table); }
610 int max,
const char *
seperator,
char quote=0)
615 {
return s.split_internal(result, max, NULL, 0, &
seperator,
quote); }
654#include "EST_Regex.h"
friend ostream & operator<<(ostream &s, const EST_Regex &str)
Stream output of regular expression.
static EST_String Number(int i, int base=10)
Build string from an integer.
static EST_String FromChar(const char c)
Build string from a single character.
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(const char *os, const EST_String &s)
Substitute one string for another.
EST_String at(const char *s, int pos=0) const
Return part where substring found (not useful, included for completeness)
EST_String before(EST_Regex &e, int pos=0) const
Part before first match of regexp after pos.
friend EST_String operator+(const EST_String &a, const EST_String &b)
Concatenate two EST_Strings.
EST_String unquote_if_needed(const char quotec) const
Remove quotes if any.
int gsub(const EST_String &os, const char *s)
Substitute one string for another.
static const EST_String Empty
Constant empty string.
EST_String unquote(const char quotec) const
Remove quotes and unprotect internal quotes.
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 after(const EST_String &s, int pos=0) const
Part after substring.
int contains(const EST_String &s, int pos=-1) const
Does it contain this substring?
friend EST_String downcase(const EST_String &s)
Convert to lower case.
int freq(const char *s) const
Number of occurrences of substring.
EST_String before(const EST_String &s, int pos=0) const
Part before first matching substring after pos.
EST_String(void)
Construct an empty string.
int gsub(const char *os, const char *s)
Substitute one string for another.
int EST_string_size
Type of string size field.
EST_String before(int pos, int len=0) const
Part before position.
EST_String after(const char *s, int pos=0) const
Part after substring.
const char operator()(int i) const
Function style access to constant strings.
float Float(bool &ok) const
Convert to a float.
friend int fcompare(const EST_String &a, const EST_String &b, const unsigned char *table)
EST_String quote(const char quotec) const
Return the string in quotes with internal quotes protected.
int contains(EST_Regex &ex, int pos=-1) const
Does it contain a match for this regular expression?
static EST_String cat(const EST_String s1, const EST_String s2=Empty, const EST_String s3=Empty, const EST_String s4=Empty, const EST_String s5=Empty, const EST_String s6=Empty, const EST_String s7=Empty, const EST_String s8=Empty, const EST_String s9=Empty)
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(EST_Regex &ex, const char *s)
Substitute string for matches of regular expression.
int search(const char *s, int len, int &mlen, int pos=0) const
Find a substring.
friend ostream & operator<<(ostream &s, const EST_String &str)
Stream output for EST_String.
EST_String before(const char *s, int pos=0) const
Part before first matching substring after pos.
char * updatable_str(void)
Get a writable pointer to the actual memory.
int space(void) const
Size of underlying chunk.
int index(EST_Regex &ex, int pos=0) const
Position of match of regexp (starting at pos)
int length(void) const
Length of string ({not} length of underlying chunk)
int index(const EST_String &s, int pos=0) const
Position of substring (starting at pos)
EST_String & operator+=(const char *b)
Add C string to end of EST_String.
EST_String after(EST_Regex &e, int pos=0) const
Part after match of regular expression.
friend EST_String upcase(const EST_String &s)
Convert to upper case.
int contains(const char c, int pos=-1) const
Does it contain this character?
int contains(const char *s, int pos=-1) const
Does it contain this substring?
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.
double Double(bool &ok) const
Convert to a double.
EST_String quote_if_needed(const char quotec) const
Return in quotes if there is something to protect (e.g. spaces)
static const char * version
Global version string.
int gsub(EST_Regex &ex, int bracket_num)
Substitute string for matches of regular expression.
long Long(bool &ok) const
Convert to a long.
int Int(bool &ok) const
Convert to an integer.
friend int split(const EST_String &s, EST_String result[], int max, const char *seperator, char quote=0)
Split at a given separator.
int matches(const char *e, int pos=0) const
Exactly match this string?
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)
EST_String at(EST_Regex &e, int pos=0) const
Return part matching regexp.
char & operator[](int i)
Array style access to writable strings.
friend EST_String operator*(const EST_String &s, int n)
Repeat string N times.
int gsub(const EST_String &os, const EST_String &s)
Substitute one string for another.
int gsub(EST_Regex &ex, const EST_String &s)
Substitute string for matches of regular expression.
EST_String at(int from, int len=0) const
Return part at position.
EST_String & operator=(const char *str)
Assign C string to EST_String.
EST_String at(const EST_String &s, int pos=0) const
Return part where substring found (not useful, included for completeness)