updates
This commit is contained in:
@ -3,7 +3,20 @@
|
||||
namespace ams
|
||||
{
|
||||
|
||||
ams_chartype tolower(ams_chartype c)
|
||||
//isspace() is locale dependent. For determinism's sake, I am going to implement my own checks.
|
||||
/* Returns true for the 6 ASCII white-space characters: \t \n \v \f \r ' '. */
|
||||
bool amsstring_isspace(const ams_chartype c)
|
||||
{
|
||||
bool ret = 0;
|
||||
//isspace(c)||c==' '||c=='\t'||c=='\n'||c=='\r'||c=='\f'||c=='\v'||(int)c==9||(int)c==10||(int)c==11||(int)c==12
|
||||
//ret = (c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r' || c == ' ');
|
||||
|
||||
ret = ((c == '\t') || (c == '\n') || (c == '\v') || (c == '\f') || (c == '\r') || (c == ' ') || ((int)c==10) || ((int)c==11) || ((int)c==12) );
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
ams_chartype tolower(const ams_chartype c)
|
||||
{
|
||||
unsigned char c2 = (unsigned char) c;
|
||||
ams_chartype ret;
|
||||
@ -18,7 +31,7 @@ namespace ams
|
||||
return ret;
|
||||
}
|
||||
|
||||
ams_chartype toupper(ams_chartype c)
|
||||
ams_chartype toupper(const ams_chartype c)
|
||||
{
|
||||
unsigned char c2 = (unsigned char) c;
|
||||
ams_chartype ret;
|
||||
@ -32,8 +45,18 @@ namespace ams
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
//isspace() is locale dependent. For determinism's sake, I am going to implement my own checks.
|
||||
//returns true if a character is of a type used forming a decimal number
|
||||
bool amsstring_ischarnumeric(const ams_chartype c)
|
||||
{
|
||||
ams_chartype cl = ams::tolower(c);
|
||||
bool ret = 0;
|
||||
ret = ret || ( ((int)cl>=48) && ((int)cl<=57) ) || cl=='.' || cl=='e' || '-';
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int amsstring_strlen(const ams_chartype *c)
|
||||
int amsstring_strlen(const ams_chartype *c)
|
||||
{
|
||||
int I;
|
||||
if(c!=NULL)
|
||||
@ -1138,13 +1161,15 @@ namespace ams
|
||||
while(qf==0)
|
||||
{
|
||||
c = s->cstring[I];
|
||||
if(!(isspace(c)||c==' '||c=='\t')&&mode==0)
|
||||
//if(!(isspace(c)||c==' '||c=='\t')&&mode==0)
|
||||
if(!(amsstring_isspace(c))&&mode==0)
|
||||
{
|
||||
mode = 1;
|
||||
ind1 = I;
|
||||
ind1 = ams::max<int>(0,ind1);
|
||||
}
|
||||
if((isspace(c)||c==' '||c=='\t'||c=='\0')&&mode==1)
|
||||
//if((isspace(c)||c==' '||c=='\t'||c=='\0')&&mode==1)
|
||||
if((amsstring_isspace(c))&&mode==1)
|
||||
{
|
||||
mode = 0;
|
||||
ind2 = I;
|
||||
@ -1179,7 +1204,8 @@ namespace ams
|
||||
for(I=0;I<s->length;I++)
|
||||
{
|
||||
c = s->cstring[I];
|
||||
if(!(isspace(c)||c==' '||c=='\t'))
|
||||
//if(!(isspace(c)||c==' '||c=='\t'))
|
||||
if(!(amsstring_isspace(c)))
|
||||
{
|
||||
ind1 = I;
|
||||
break;
|
||||
@ -1188,7 +1214,8 @@ namespace ams
|
||||
for(I=s->length-1;I>=0;I--)
|
||||
{
|
||||
c = s->cstring[I];
|
||||
if(!(isspace(c)||c==' '||c=='\t'))
|
||||
//if(!(isspace(c)||c==' '||c=='\t'))
|
||||
if(!(amsstring_isspace(c)))
|
||||
{
|
||||
ind2 = I+1;
|
||||
break;
|
||||
@ -1219,7 +1246,8 @@ namespace ams
|
||||
for(I=0;I<s->length;I++)
|
||||
{
|
||||
c = s->cstring[I];
|
||||
if(!(isspace(c)||c==' '||c=='\t'||c=='\n'||c=='\r'||c=='\f'||c=='\v'||(int)c==9||(int)c==10||(int)c==11||(int)c==12))
|
||||
//if(!(isspace(c)||c==' '||c=='\t'||c=='\n'||c=='\r'||c=='\f'||c=='\v'||(int)c==9||(int)c==10||(int)c==11||(int)c==12))
|
||||
if(!(amsstring_isspace(c)))
|
||||
{
|
||||
q.cstring[J] = s->cstring[I];
|
||||
J = J + 1;
|
||||
|
||||
@ -94,22 +94,22 @@ amsarray<amsstring> split(const amsstring &s, const ams_chartype delimitchar)
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int amsstring_strlen(const ams_chartype *c)
|
||||
{
|
||||
int I;
|
||||
if(c!=NULL)
|
||||
{
|
||||
I = 0;
|
||||
while(c[I]!='\0')
|
||||
{
|
||||
I++;
|
||||
}
|
||||
return I;
|
||||
//return strlen((const char*) c);
|
||||
}
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
// static int amsstring_strlen(const ams_chartype *c)
|
||||
// {
|
||||
// int I;
|
||||
// if(c!=NULL)
|
||||
// {
|
||||
// I = 0;
|
||||
// while(c[I]!='\0')
|
||||
// {
|
||||
// I++;
|
||||
// }
|
||||
// return I;
|
||||
// //return strlen((const char*) c);
|
||||
// }
|
||||
// else
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
amsarray<amsstring> split(const amsstring &s, const ams_chartype *delimitstr)
|
||||
{
|
||||
@ -255,13 +255,15 @@ amsarray<amsstring> splitwhitespace(const amsstring &s)
|
||||
while(qf==0)
|
||||
{
|
||||
c = s.cstring[I];
|
||||
if(!(isspace(c)||c==' '||c=='\t')&&mode==0)
|
||||
//if(!(isspace(c)||c==' '||c=='\t')&&mode==0)
|
||||
if(!(amsstring_isspace(c))&&mode==0)
|
||||
{
|
||||
mode = 1;
|
||||
ind1 = I;
|
||||
ind1 = ams::max<int>(0,ind1);
|
||||
}
|
||||
if((isspace(c)||c==' '||c=='\t'||c=='\0')&&mode==1)
|
||||
//if((isspace(c)||c==' '||c=='\t'||c=='\0')&&mode==1)
|
||||
if((amsstring_isspace(c))&&mode==1)
|
||||
{
|
||||
mode = 0;
|
||||
ind2 = I;
|
||||
@ -295,7 +297,8 @@ amsstring stripwhitespace(const amsstring &s)
|
||||
for(I=0;I<s.length;I++)
|
||||
{
|
||||
c = s.cstring[I];
|
||||
if(!(isspace(c)||c==' '||c=='\t'))
|
||||
//if(!(isspace(c)||c==' '||c=='\t'))
|
||||
if(!(amsstring_isspace(c)))
|
||||
{
|
||||
ind1 = I;
|
||||
break;
|
||||
@ -304,7 +307,8 @@ amsstring stripwhitespace(const amsstring &s)
|
||||
for(I=s.length-1;I>=0;I--)
|
||||
{
|
||||
c = s.cstring[I];
|
||||
if(!(isspace(c)||c==' '||c=='\t'))
|
||||
//if(!(isspace(c)||c==' '||c=='\t'))
|
||||
if(!(amsstring_isspace(c)))
|
||||
{
|
||||
ind2 = I+1;
|
||||
break;
|
||||
@ -335,7 +339,8 @@ amsstring stripallwhitespace(const amsstring &s)
|
||||
for(I=0;I<s.length;I++)
|
||||
{
|
||||
c = s.cstring[I];
|
||||
if(!(isspace(c)||c==' '||c=='\t'||c=='\n'||c=='\r'||c=='\f'||c=='\v'||(int)c==9||(int)c==10||(int)c==11||(int)c==12))
|
||||
//if(!(isspace(c)||c==' '||c=='\t'||c=='\n'||c=='\r'||c=='\f'||c=='\v'||(int)c==9||(int)c==10||(int)c==11||(int)c==12))
|
||||
if(!(amsstring_isspace(c)))
|
||||
{
|
||||
ret.cstring[J] = s.cstring[I];
|
||||
J = J + 1;
|
||||
@ -426,6 +431,32 @@ amsstring select_between(const amsstring &s, const ams_chartype delimitleft, con
|
||||
return ret;
|
||||
}
|
||||
|
||||
//selects between the outermost set of delimitleft and delimitright chars
|
||||
amsstring select_between(const amsstring &s, const ams_chartype *delimitleft, const ams_chartype *delimitright)
|
||||
{
|
||||
amsstring ret;
|
||||
|
||||
int ind1,ind2;
|
||||
int slen1,slen2;
|
||||
|
||||
slen1 = amsstring_strlen(delimitleft);
|
||||
slen2 = amsstring_strlen(delimitleft);
|
||||
|
||||
ind1 = -1;
|
||||
ind2 = -1;
|
||||
if(slen1>0)
|
||||
ind1 = s.find(delimitleft);
|
||||
if(slen2>0)
|
||||
ind2 = s.findright(delimitright);
|
||||
|
||||
if(ind1<0) ind1=0;
|
||||
if(ind2<0) ind2=s.length;
|
||||
|
||||
ret = s.substring(ind1,ind2);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
amsstring concatenate(const amsarray<amsstring> &slist)
|
||||
{
|
||||
amsstring ret;
|
||||
@ -440,6 +471,60 @@ amsstring concatenate(const amsarray<amsstring> &slist)
|
||||
return ret;
|
||||
}
|
||||
|
||||
amsstring concat_lines(const amsarray<amsstring> &slist)
|
||||
{
|
||||
amsstring ret;
|
||||
amsstring s;
|
||||
int I;
|
||||
ret.resize(0);
|
||||
for(I=0;I<slist.length;I++)
|
||||
{
|
||||
s = slist[I];
|
||||
s=s+"\n"; //maybe check to see if a newline already exists for this line?
|
||||
ret.append(s);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
//splits a string into contiguous sets of alphanumeric characters delimited by any non-alphanum chars
|
||||
//for eventual conversion of space,tab,comma separated numbers into numeric tuples and arrays
|
||||
//This step does not check that each string is a valid number.
|
||||
amsarray<amsstring> splitalphanum(const amsstring &s)
|
||||
{
|
||||
amsarray<amsstring> sa;
|
||||
int I0,I1,I;
|
||||
int mode;
|
||||
ams_chartype c;
|
||||
amsstring s2;
|
||||
|
||||
mode = 0;
|
||||
for(I=0;I<s.length;I++)
|
||||
{
|
||||
c = s[I];
|
||||
if(mode==0 && amsstring_ischarnumeric(c))
|
||||
{
|
||||
mode = 1;
|
||||
I0 = I;
|
||||
}
|
||||
|
||||
if(mode==1 && (!amsstring_ischarnumeric(c) || I==length-1)
|
||||
{
|
||||
I1 = I;
|
||||
s2 = s.substring(I0,I1);
|
||||
sa.append(s2);
|
||||
mode = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return sa;
|
||||
}
|
||||
|
||||
/////////////
|
||||
// Testing //
|
||||
/////////////
|
||||
|
||||
void amsstring4_test_findright()
|
||||
{
|
||||
amsstring s1,s2;
|
||||
@ -594,8 +679,12 @@ void amsstring4_test_convenience1a()
|
||||
s1 = "/some/other/path";
|
||||
sa = splitpath(s1);
|
||||
printf("splitpath('%s') = ['%s','%s']\n",s1.cstring,sa[0].cstring,sa[1].cstring);
|
||||
|
||||
|
||||
}
|
||||
|
||||
void amsstring4_test_convenience1b()
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
};//end namespace ams
|
||||
@ -22,7 +22,8 @@ int main(int argc, char* argv[])
|
||||
|
||||
// amsstring4_test_concatenation_operators();
|
||||
|
||||
amsstring4_test_convenience1a();
|
||||
//amsstring4_test_convenience1a();
|
||||
amsstring4_test_convenience1b();
|
||||
|
||||
return ret;
|
||||
}
|
||||
Reference in New Issue
Block a user