34 #ifndef __EST_STRING_H__ 35 #define __EST_STRING_H__ 40 #define EST_Regex_max_subexpressions 10 83 # define __GSUB_REENTRANT__ (1) 86 #define __STRING_ARG_GRIPE__ (1) 89 #define __GRIPE_FATAL__ (1) 92 # define gripe(WHAT) (std::cerr<< ("oops! " WHAT "\n"),abort()) 94 # define gripe(WHAT) (std::cerr<< ("oops! " WHAT "\n")) 97 #if __STRING_ARG_GRIPE__ 98 # define safe_strlen(S) ((S)?strlen(S):(gripe("null strlen"),0)) 99 # define CHECK_STRING_ARG(S) if (!(S)) gripe("null string arg") 101 # define safe_strlen(S) ((S)?strlen(S):0) 102 # define CHECK_STRING_ARG(S) 115 # define MAX_STRING_SIZE (std::numeric_limits<std::size_t>::max()-1) 116 # define EST_STRING_ERR_IDX ((std::numeric_limits<std::size_t>::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 size_t locate(
const char *it,
size_t len,
ssize_t from,
size_t &
start,
size_t &
end)
const;
157 size_t locate(
const EST_String &s,
ssize_t from,
size_t &start,
size_t &end)
const 158 {
return locate((
const char *)s.memory, s.size, from, start, end); }
160 size_t locate(
EST_Regex &ex,
ssize_t from,
size_t &start,
size_t &end,
size_t *starts=
NULL,
size_t *ends=
NULL)
const;
166 int extract(
const char *it,
size_t len,
ssize_t from,
size_t &start,
size_t &end)
const;
168 {
return extract((
const char *)s.memory, s.size, from, start, end); }
177 EST_String chop_internal(
ssize_t pos,
size_t 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);
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;
220 memory = NON_CONST_CHUNKPTR(s.memory);
231 size_t length(
void)
const {
return size; }
235 const char *
str(
void)
const {
return size==0?
"":(
const char *)memory; }
243 {
const char s[2] = { c, 0 };
return EST_String(s); }
258 int Int(
bool &ok)
const {
return Int(&ok); }
277 {
return chop_internal(pos, len, Chop_Before); }
280 {
return chop_internal(s,
safe_strlen(s), pos, Chop_Before); }
283 {
return chop_internal(s.
str(), s.size, pos, Chop_Before); }
286 {
return chop_internal(e, pos, Chop_Before); }
293 {
return EST_String(
str(),size,from<0?(size+from):from,len); }
296 {
return chop_internal(s,
safe_strlen(s), pos, Chop_At); }
299 {
return chop_internal(s.
str(), s.size, pos, Chop_At); }
302 {
return chop_internal(e, pos, Chop_At); }
309 {
return chop_internal(pos, len, Chop_After); }
312 {
return chop_internal(s,
safe_strlen(s), pos, Chop_After); }
315 {
return chop_internal(s.
str(), s.size, pos, Chop_After); }
318 {
return chop_internal(e, pos, Chop_After); }
326 if (locate(s, len, pos, start, end))
334 if (locate(s, pos, start, end))
341 {
size_t start=0, end=0;
342 if (locate(re, pos, start, end, starts, ends))
369 {
size_t start,
end;
return extract(s, pos, start, end); }
372 {
size_t start,
end;
char s[2] = {c,0};
return extract(s, 1, pos, start, end); }
375 {
size_t start,
end;
return extract(ex, pos, start, end); }
392 {
return gsub_internal(os,
safe_strlen(os), s, s.size); }
394 int gsub(
const char *os,
const char *s)
398 {
return gsub_internal(os, os.size, s, s.size); }
401 {
return gsub_internal(os, os.size, s,
safe_strlen(s)); }
405 {
return gsub_internal(ex, s, s.size); }
411 {
return gsub_internal(ex,
NULL, bracket_num); }
415 size_t (&ends)[EST_Regex_max_subexpressions]);
421 size_t freq(
const char *s)
const;
449 operator const char*()
const {
return str(); }
450 operator const char*() {
return str(); }
567 const unsigned char *table);
570 const unsigned char *table);
574 {
return fcompare(a, b, (
const unsigned char *)(
const char *)table); }
588 int max,
const EST_String& seperator,
char quote=0)
589 {
return s.split_internal(result, max, (
const char *)seperator, seperator.
length(),
NULL,
quote); }
592 int max,
const char *seperator,
char quote=0)
593 {
return s.split_internal(result, max, seperator, strlen(seperator),
NULL, quote); }
597 {
return s.split_internal(result, max,
NULL, 0, &seperator, quote); }
EST_String at(EST_Regex &e, int pos=0) const
Return part matching regexp.
float end(const EST_Item &item)
friend int compare(const EST_String &a, const EST_String &b)
int subst(EST_String source, size_t(&starts)[EST_Regex_max_subexpressions], size_t(&ends)[EST_Regex_max_subexpressions])
Substitute the result of a match into a string.
friend EST_String operator+(const EST_String &a, const EST_String &b)
Concatenate two EST_Strings.
EST_String unquote(const char quotec) const
Remove quotes and unprotect internal quotes.
int contains(const char *s, ssize_t pos=-1) const
Does it contain this substring?
EST_String(void)
Construct an empty string.
EST_String quote_if_needed(const char quotec) const
Return in quotes if there is something to protect (e.g. spaces)
EST_String at(const EST_String &s, int pos=0) const
Return part where substring found (not useful, included for completeness)
static EST_String Number(int i, int base=10)
Build string from an integer.
EST_String after(const char *s, int pos=0) const
Part after substring.
friend int operator>(const char *a, const EST_String &b)
size_t freq(const char *s) const
Number of occurrences of substring.
int Int(bool &ok) const
Convert to an integer.
int contains(const char c, ssize_t pos=-1) const
Does it contain this character?
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 int operator>=(const char *a, const EST_String &b)
friend int operator==(const char *a, const EST_String &b)
long Long(bool &ok) const
Convert to a long.
friend int fcompare(const EST_String &a, const EST_String &b, const unsigned char *table)
size_t index(EST_Regex &ex, ssize_t pos=0) const
Position of match of regexp (starting at pos)
static const char * version
Global version string.
EST_String after(EST_Regex &e, int pos=0) const
Part after match of regular expression.
size_t index(const char *s, ssize_t pos=0) const
Position of substring (starting at pos)
friend EST_String operator*(const EST_String &s, int n)
Repeat string N times.
void extract(EST_Track &orig, float start, float end, EST_Track &res)
size_t EST_string_size
Type of string size field.
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.
friend ostream & operator<<(ostream &s, const EST_String &str)
Stream output for EST_String.
int gsub(EST_Regex &ex, const EST_String &s)
Substitute string for matches of regular expression.
friend int fcompare(const EST_String &a, const EST_String &b, const EST_String &table)
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)
size_t index(const EST_String &s, ssize_t pos=0) const
Position of substring (starting at pos)
float max(float a, float b)
size_t search(const char *s, size_t len, size_t &mlen, ssize_t pos=0) const
Find a substring.
int contains(EST_Regex &ex, ssize_t pos=-1) const
Does it contain a match for this regular expression?
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.
char operator()(int i) const
Function style access to constant strings.
friend int compare(const char *a, const EST_String &b)
int space(void) const
Size of underlying chunk.
size_t search(EST_Regex &re, size_t &mlen, ssize_t pos=0, size_t *starts=NULL, size_t *ends=NULL) const
Find a match of the regular expression.
char & operator[](int i)
Array style access to writable strings.
#define EST_STRING_ERR_IDX
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.
friend EST_String upcase(const EST_String &s)
Convert to upper case.
size_t search(const EST_String s, size_t &mlen, ssize_t pos=0) const
Find a substring.
friend EST_String downcase(const EST_String &s)
Convert to lower case.
int matches(const char *e, ssize_t pos=0) const
Exactly match this string?
EST_String & operator+=(const char *b)
Add C string to end of EST_String.
int fcompare(const EST_String &a, const EST_String &b, const unsigned char *table=NULL)
int gsub(const EST_String &os, const char *s)
Substitute one string for another.
int contains(const EST_String &s, ssize_t pos=-1) const
Does it contain this substring?
void cp_make_updatable(EST_ChunkPtr &cp, EST_Chunk::EST_chunk_size inuse)
EST_String before(EST_Regex &e, int pos=0) const
Part before first match of regexp after pos.
friend int operator!=(const char *a, const EST_String &b)
float start(const EST_Item &item)
EST_String before(const char *s, int pos=0) const
Part before first matching substring after pos.
size_t length(void) const
Length of string ({not} length of underlying chunk)
friend int operator<(const char *a, const EST_String &b)
EST_String & operator=(const char *str)
Assign C string to EST_String.
const char * str(void) const
Get a const-pointer to the actual memory.
double Double(void) const
EST_String unquote_if_needed(const char quotec) const
Remove quotes if any.
char * updatable_str(void)
Get a writable pointer to the actual memory.
void make_updatable(void)
friend int operator<=(const char *a, const EST_String &b)
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.
EST_String before(int pos, int len=0) const
Part before position.
EST_String at(int from, int len=0) const
Return part at position.
EST_String quote(const char quotec) const
Return the string in quotes with internal quotes protected.
EST_String(const EST_String &s)
static const EST_String Empty
Constant empty string.
#define EST_Regex_max_subexpressions
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.
EST_String & ignore_volatile(void) volatile