71 #if !__GSUB_REENTRANT__ 72 static struct subst *substitutions=
NULL;
85 size_t EST_String::locate(
const char *s,
size_t len,
ssize_t from,
size_t &
start,
size_t &
end)
const 94 if (from < 0 && -from < (
ssize_t) size)
96 size_t endpos=size+from+1;
100 while ((nextsub=strstr(str()+p, s)))
108 else if (from>=0 && from <= (
ssize_t) size)
109 sub= strstr(str()+from, s);
124 size_t EST_String::locate(
EST_Regex &ex,
ssize_t from,
size_t &
start,
size_t &
end,
size_t *starts,
size_t *ends)
const 126 size_t match_start, match_end;
128 if (from < 0 && -from < (
ssize_t) size)
130 size_t endpos=size+from+1;
134 while (ex.
run(str(), p, match_start, match_end, starts, ends))
145 else if (from >=0 && from <= (
ssize_t) size)
147 if (ex.
run(str(), from, match_start, match_end, starts, ends))
160 int EST_String::extract(
const char *s,
size_t len,
ssize_t pos,
size_t &
start,
size_t &
end)
const 168 return locate(s, len, 0,
start, end);
170 if (pos <= (
ssize_t) (size-len) && memcmp(str()+pos, s, len)==0)
182 size_t match_start, match_end;
185 return locate(ex, 0,
start, end);
187 if (pos < (
ssize_t) size && ex.
run(str(), pos, match_start, match_end) && match_start == (size_t)pos)
197 EST_String EST_String::chop_internal(
ssize_t from,
size_t len, EST_chop_direction mode)
const 212 if (end <=size && size > 0)
216 return EST_String(str(), size, 0, start);
break;
218 return EST_String(str(), size, start, end-start);
break;
226 EST_String EST_String::chop_internal(
const char *it,
size_t len,
ssize_t from, EST_chop_direction mode)
const 232 if (it && locate(it, len, from, start, end))
236 return EST_String(str(), size, 0, start);
break;
238 return EST_String(str(), size, start, end-start);
break;
250 if (locate(it, from, start,
end))
254 return EST_String(str(), size, 0, start);
break;
265 int EST_String::gsub_internal (
const char *os,
int olength,
const char *s,
int length)
270 int pos=0, n=0, change=0;
276 #if __GSUB_REENTRANT__ 279 } *substitutions=
NULL;
284 from = (
const char *)memory;
285 if (s && os && size > 0 && *os !=
'\0' && from)
289 while (locate(os, olength, pos, start, end))
291 if (num_substitutions <= n)
292 substitutions =
wrealloc(substitutions,
struct subst, (num_substitutions +=10));
294 substitutions[n].start =
start;
295 substitutions[n].end =
end;
297 change += length - (end-
start);
323 int start = substitutions[i].start;
324 int end = substitutions[i].end;
325 memcpy(p, from+at, start-at);
327 memcpy(p, s, length);
332 memcpy(p, from+at, size-at);
346 #if __GSUB_REENTRANT__ 348 wfree(substitutions);
355 int EST_String::gsub_internal (
EST_Regex &ex,
const char *s,
int length)
361 bracket_num = length;
363 int pos=0, n=0, change=0;
366 const char *from = (
const char*)memory;
369 #if __GSUB_REENTRANT__ 370 struct subst *substitutions=
NULL;
377 if (size > 0 && from !=
NULL)
384 if (num_substitutions <= n)
385 substitutions =
wrealloc(substitutions,
struct subst, (num_substitutions +=10));
387 substitutions[n].start =
start;
388 substitutions[n].end = start+mlen;
391 change += length - mlen;
394 int slen = ends[bracket_num]-starts[bracket_num];
395 change += slen - mlen;
396 substitutions[n].slen = slen;
397 substitutions[n].s =
walloc(
char, slen);
398 memcpy(substitutions[n].s, (
const char *)memory+starts[bracket_num], slen);
426 int start = substitutions[i].start;
427 int end = substitutions[i].end;
428 memcpy(p, from+at, start-at);
432 memcpy(p, s, length);
437 memcpy(p, substitutions[i].s, substitutions[i].slen);
438 wfree(substitutions[i].s);
439 substitutions[i].s=
NULL;
440 p += substitutions[i].slen;
444 if ( (from + at) != p)
445 memcpy(p, from+at, size-at);
456 #if __GSUB_REENTRANT__ 458 wfree(substitutions);
467 size_t (&ends)[EST_Regex_max_subexpressions])
476 #if __GSUB_REENTRANT__ 477 struct subst *substitutions=
NULL;
489 for(i=0; i<size; i++)
493 if (memory[i] >=
'0' &&memory[i] <=
'9')
495 int snum = memory[i] -
'0';
498 if (num_substitutions <= n)
499 substitutions =
wrealloc(substitutions,
struct subst, (num_substitutions +=10));
501 substitutions[n].start = i-1;
502 substitutions[n].end = i+1;
503 substitutions[n].s = ((
char *)(
void *)(
const char *)source.memory) + starts[snum];
504 substitutions[n].slen = ends[snum] - starts[snum];
505 change += substitutions[n].slen - 2;
512 else if (memory[i] ==
'\\')
519 from = (
const char *)memory;
538 int start = substitutions[i].start;
539 int end = substitutions[i].end;
540 memcpy(p, from+at, start-at);
543 memcpy(p, substitutions[i].s, substitutions[i].slen);
544 substitutions[i].s=
NULL;
545 p += substitutions[i].slen;
548 memcpy(p, from+at, size-at);
559 #if __GSUB_REENTRANT__ 561 wfree(substitutions);
572 const char *s_seperator,
size_t slen,
583 while (pos < length())
587 if ((*
this)(pos) == quote)
591 while (pos < length())
593 if ((*
this)(pos) == quote)
596 if ((*
this)(pos) != quote)
608 size_t mstart, mend, matched;
610 matched = locate(s_seperator, slen, pos, mstart, mend);
612 matched = locate(*re_seperator, pos, mstart, mend);
622 else if (pos ==lastspace)
642 result[n++] =
EST_String(*
this, start, end-start);
662 if (
extract(s, len, pos, start, end))
663 return ((
ssize_t)start)==pos && end==len;
681 return e.
run_match(
"", pos, starts, ends) >0;
683 return e.
run_match(str(), pos, starts, ends) >0;
702 memmove((
char *)c + al, b, bl);
720 memmove((
char *)c+al,b.
str(),bl);
740 memmove((
char *)c + al, b.
str(), bl);
758 src = (
const char *)s;
760 if (src==
NULL || dest ==
NULL)
return "";
763 if (l <= 0)
return it;
766 memcpy(dest+j*l, src, l);
788 memmove((
char *)memory + size,b,bl);
789 memory(size+bl)=
'\0';
802 memory = NON_CONST_CHUNKPTR(b.memory);
810 memmove((
char *)memory + size,b.
str(),bl);
812 memory(size+bl)=
'\0';
836 int start= start_or_fill;
848 char fill = start_or_fill;
855 for(
int j=0; j<len;j++)
882 actual_len = s.size-
start;
888 if (start == 0 && actual_len == s.size)
889 memory = NON_CONST_CHUNKPTR(s.memory);
915 else if (!shareing() && len < size)
916 memcpy((
char *)memory, str, len+1);
934 memory = NON_CONST_CHUNKPTR(s.memory);
937 *(
struct EST_dumb_string *)
this = *(
struct EST_dumb_string *)(&s);
947 for (i=0; i < s.
length(); i++)
949 t[i] = tolower(s(i));
960 for (i=0; i < s.
length(); i++)
962 t[i] = toupper(s(i));
976 while (locate(s, pos, start, end))
994 while (locate(s, len, pos, start, end))
1009 while (locate(ex, pos, start, end))
1020 const char quotequote[3] = {quotec, quotec,
'\0'};
1024 result.
gsub(quotequote+1, quotequote+0);
1032 const char quotequote[3] = {quotec, quotec,
'\0'};
1038 result.
gsub(quotequote+0, quotequote+1);
1042 if (result[0] == quotec && result[result.
length()-1] == quotec )
1049 return result.
at(1, result.
length()-2);
1060 return quote(quotec);
1069 if ((*
this)(0) == quotec && (*
this)(length()-1) == quotec )
1070 return unquote(quotec);
1079 return (s << str.
str());
1105 { strncpy((
char *)result.memory + p, (
const char *)s2, s2.
length()); p += s2.
length(); }
1107 { strncpy((
char *)result.memory + p, (
const char *)s3, s3.
length()); p += s3.
length(); }
1109 { strncpy((
char *)result.memory + p, (
const char *)s4, s4.
length()); p += s4.
length(); }
1111 { strncpy((
char *)result.memory + p, (
const char *)s5, s5.
length()); p += s5.
length(); }
1113 { strncpy((
char *)result.memory + p, (
const char *)s6, s6.
length()); p += s6.
length(); }
1115 { strncpy((
char *)result.memory + p, (
const char *)s7, s7.
length()); p += s7.
length(); }
1117 { strncpy((
char *)result.memory + p, (
const char *)s8, s8.
length()); p += s8.
length(); }
1119 { strncpy((
char *)result.memory + p, (
const char *)s9, s9.
length()); p += s9.
length(); }
1121 result.memory(p) =
'\0';
1128 if (a.size == 0 && b.size == 0)
1130 else if (a.size == 0)
1132 else if (b.size == 0)
1135 return strcmp(a.
str(), b.
str());
1140 if (a.size == 0 && (b==
NULL || *b ==
'\0'))
1142 else if (a.size == 0)
1144 else if (b ==
NULL || *b ==
'\0')
1147 return strcmp(a.
str(), b);
1151 const unsigned char *table)
1153 if (a.size == 0 && b.size == 0)
1155 else if (a.size == 0)
1157 else if (b.size == 0)
1164 const unsigned char *table)
1166 int bsize = (b ? strlen((
const char *)b) : 0);
1167 if (a.size == 0 && bsize == 0)
1169 else if (a.size == 0)
1171 else if (bsize == 0)
1186 return (*a == b(0)) && strcmp(a, b.
str())==0;
1193 else if (b.size == 0)
1196 return a.size == b.size && a(0) == b(0) && memcmp(a.
str(),b.
str(),a.size)==0;
1219 sprintf(buf, format, i);
1244 sprintf(buf, format, i);
1253 sprintf(buf,
"%f", f);
1262 sprintf(buf,
"%f", d);
1271 long val = strtol(str(), &end, 10);
1273 if (end==
NULL|| *end !=
'\0')
1282 printf(
"bad integer number format '%s'\n",
1283 (
const char *)str());
1296 long val = Long(valid);
1298 if (valid && !*valid)
1301 if (val > INT_MAX || val < INT_MIN)
1310 printf(
"number out of range for integer %ld",
1323 double val = strtod(str(), &end);
1325 if (end==
NULL|| *end !=
'\0')
1334 printf(
"bad decimal number format '%s'",
1335 (
const char *)str());
1348 double val = Double(valid);
1350 if (valid && !*valid)
1362 printf(
"number out of range for float %f",
float end(const EST_Item &item)
#define walloc(TYPE, SIZE)
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.
EST_String unquote(const char quotec) const
Remove quotes and unprotect internal quotes.
EST_String(void)
Construct an empty string.
void grow_chunk(EST_ChunkPtr &cp, EST_Chunk::EST_chunk_size newsize)
EST_String quote_if_needed(const char quotec) const
Return in quotes if there is something to protect (e.g. spaces)
static EST_String Number(int i, int base=10)
Build string from an integer.
size_t freq(const char *s) const
Number of occurrences of substring.
A Regular expression class to go with the CSTR EST_String class.
ostream & operator<<(ostream &s, const EST_String &str)
#define EST_Regex_max_subexpressions
EST_String operator+(const EST_String &a, const char *b)
static const char * version
Global version string.
void extract(EST_Track &orig, float start, float end, EST_Track &res)
EST_String upcase(const EST_String &s)
size_t EST_string_size
Type of string size field.
int contains(EST_TList< int > &l, int n)
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)
float max(float a, float b)
int gsub(const char *os, const EST_String &s)
Substitute one string for another.
EST_String EST_String_nullString
EST_String downcase(const EST_String &s)
EST_FMatrix sub(const EST_FMatrix &a, ssize_t row, ssize_t col)
EST_Regex RXwhite("[ \n\t\r]+")
White space.
int run_match(const char *on, size_t from=0, size_t *starts=NULL, size_t *ends=NULL)
Run to see if it matches the entire string.
int operator==(const char *a, const EST_String &b)
#define EST_STRING_ERR_IDX
#define wrealloc(PTR, TYPE, SIZE)
int fcompare(const EST_String &a, const EST_String &b, const unsigned char *table)
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.
void cp_make_updatable(EST_ChunkPtr &cp, EST_Chunk::EST_chunk_size inuse)
EST_String operator*(const EST_String &s, int n)
#define CHECK_STRING_ARG(S)
float start(const EST_Item &item)
EST_ChunkPtr chunk_allocate(size_t bytes)
size_t length(void) const
Length of string ({not} length of underlying chunk)
EST_String & operator=(const char *str)
Assign C string to EST_String.
int EST_strcasecmp(const char *s1, const char *s2, const unsigned char *charmap)
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.
int compare(const EST_String &a, const EST_String &b)
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.
static const EST_String Empty
Constant empty string.
int run(const char *on, size_t from, size_t &start, size_t &end, size_t *starts=NULL, size_t *ends=NULL)
Run to find a matching substring.