stringy strings
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -136,7 +136,21 @@ public:
|
||||
double strtonum() const;
|
||||
|
||||
//updated convenience functions as class members
|
||||
//todo
|
||||
amsarray<amsstring> splitlines() const;
|
||||
amsarray<amsstring> split(const ams_chartype delimitchar) const;
|
||||
amsarray<amsstring> split(const ams_chartype *delimitstr) const;
|
||||
amsarray<amsstring> splitfirst(const ams_chartype delimitchar) const; //guaranteed size 2 array
|
||||
amsarray<amsstring> splitfirst(const ams_chartype *delimitstr) const; //guaranteed size 2 array
|
||||
amsarray<amsstring> splitwhitespace() const;
|
||||
amsstring stripwhitespace() const;
|
||||
amsstring stripallwhitespace() const;
|
||||
amsstring stripnewlines() const;
|
||||
amsarray<amsstring> splitalphanum() const;
|
||||
amsarray<amsstring> path_splitext() const;
|
||||
amsarray<amsstring> path_split() const;
|
||||
amsstring select_between(const ams_chartype *delimitleft, const ams_chartype *delimitright) const;
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
@ -180,6 +194,12 @@ amsarray<amsstring> splitwhitespace(const amsstring &s);
|
||||
amsstring stripwhitespace(const amsstring &s);
|
||||
amsstring stripallwhitespace(const amsstring &s);
|
||||
|
||||
//strips \r and \n from the right side of the string
|
||||
amsstring stripnewlines(const amsstring &s);
|
||||
|
||||
//strips \r and \n from the right side of the strings
|
||||
void stripnewlines(amsarray<amsstring> *sa_inout);
|
||||
|
||||
//splits a string into contiguous sets of alphanumeric characters ignoring non-alphanums
|
||||
amsarray<amsstring> splitalphanum(const amsstring &s);
|
||||
|
||||
@ -204,8 +224,6 @@ amsstring concatenate(const amsarray<amsstring> &slist);
|
||||
//adds \n between each line in the concatenated string
|
||||
amsstring concat_lines(const amsarray<amsstring> &lns);
|
||||
|
||||
|
||||
|
||||
void freadline(FILE *fp, amsstring *s);
|
||||
void freadlines(FILE *fp, amsarray<amsstring> *lines);
|
||||
void fwritelines(FILE *fp, amsstring *s);
|
||||
|
||||
@ -423,10 +423,10 @@ amsstring select_between(const amsstring &s, const ams_chartype delimitleft, con
|
||||
ind1 = s.find(delimitleft);
|
||||
ind2 = s.findright(delimitright);
|
||||
|
||||
if(ind1<0) ind1=0;
|
||||
if(ind1<0) ind1=-1;
|
||||
if(ind2<0) ind2=s.length;
|
||||
|
||||
ret = s.substring(ind1,ind2);
|
||||
ret = s.substring(ind1+1,ind2);
|
||||
|
||||
return ret;
|
||||
}
|
||||
@ -440,7 +440,7 @@ amsstring select_between(const amsstring &s, const ams_chartype *delimitleft, co
|
||||
int slen1,slen2;
|
||||
|
||||
slen1 = amsstring_strlen(delimitleft);
|
||||
slen2 = amsstring_strlen(delimitleft);
|
||||
slen2 = amsstring_strlen(delimitright);
|
||||
|
||||
ind1 = -1;
|
||||
ind2 = -1;
|
||||
@ -449,10 +449,10 @@ amsstring select_between(const amsstring &s, const ams_chartype *delimitleft, co
|
||||
if(slen2>0)
|
||||
ind2 = s.findright(delimitright);
|
||||
|
||||
if(ind1<0) ind1=0;
|
||||
if(ind1<0) ind1=-slen1;
|
||||
if(ind2<0) ind2=s.length;
|
||||
|
||||
ret = s.substring(ind1,ind2);
|
||||
ret = s.substring(ind1+slen1,ind2);
|
||||
|
||||
return ret;
|
||||
}
|
||||
@ -521,6 +521,105 @@ amsarray<amsstring> splitalphanum(const amsstring &s)
|
||||
return sa;
|
||||
}
|
||||
|
||||
//strips \r and \n from the right side of the string
|
||||
amsstring stripnewlines(const amsstring &s)
|
||||
{
|
||||
amsstring ret;
|
||||
int I0,I;
|
||||
I0 = s.length;
|
||||
for(I=s.length-1;I>=0;I--)
|
||||
{
|
||||
if(s[I]!='\n' && s[I]!='\r')
|
||||
{
|
||||
I0 = I+1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
ret = s.substring(0,I0);
|
||||
return ret;
|
||||
}
|
||||
|
||||
//strips \r and \n from the right side of the strings
|
||||
void stripnewlines(amsarray<amsstring> *sa_inout)
|
||||
{
|
||||
int I;
|
||||
for(I=0;I<sa_inout->length;I++)
|
||||
{
|
||||
sa_inout->at(I) = stripnewlines(sa_inout->at(I));
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////
|
||||
// Member Function Versions of Convenience Functions //
|
||||
///////////////////////////////////////////////////////
|
||||
|
||||
amsarray<amsstring> amsstring::splitlines() const
|
||||
{
|
||||
return ams::splitlines(*this);
|
||||
}
|
||||
|
||||
amsarray<amsstring> amsstring::split(const ams_chartype delimitchar) const
|
||||
{
|
||||
return ams::split(*this,delimitchar);
|
||||
}
|
||||
|
||||
amsarray<amsstring> amsstring::split(const ams_chartype *delimitstr) const
|
||||
{
|
||||
return ams::split(*this,delimitstr);
|
||||
}
|
||||
|
||||
amsarray<amsstring> amsstring::splitfirst(const ams_chartype delimitchar) const
|
||||
{
|
||||
return ams::splitfirst(*this,delimitchar);
|
||||
}
|
||||
|
||||
amsarray<amsstring> amsstring::splitfirst(const ams_chartype *delimitstr) const
|
||||
{
|
||||
return ams::splitfirst(*this,delimitstr);
|
||||
}
|
||||
|
||||
amsarray<amsstring> amsstring::splitwhitespace() const
|
||||
{
|
||||
return ams::splitwhitespace(*this);
|
||||
}
|
||||
|
||||
amsstring amsstring::stripwhitespace() const
|
||||
{
|
||||
return ams::stripwhitespace(*this);
|
||||
}
|
||||
|
||||
amsstring amsstring::stripallwhitespace() const
|
||||
{
|
||||
return ams::stripallwhitespace(*this);
|
||||
}
|
||||
|
||||
amsstring amsstring::stripnewlines() const
|
||||
{
|
||||
return ams::stripnewlines(*this);
|
||||
}
|
||||
|
||||
amsarray<amsstring> amsstring::splitalphanum() const
|
||||
{
|
||||
return ams::splitalphanum(*this);
|
||||
}
|
||||
|
||||
amsarray<amsstring> amsstring::path_splitext() const
|
||||
{
|
||||
return ams::path_splitext(*this);
|
||||
}
|
||||
|
||||
amsarray<amsstring> amsstring::path_split() const
|
||||
{
|
||||
return ams::path_split(*this);
|
||||
}
|
||||
|
||||
amsstring amsstring::select_between(const ams_chartype *delimitleft, const ams_chartype *delimitright) const
|
||||
{
|
||||
return ams::select_between(*this,delimitleft,delimitright);
|
||||
}
|
||||
|
||||
|
||||
/////////////
|
||||
// Testing //
|
||||
/////////////
|
||||
@ -694,6 +793,57 @@ void amsstring4_test_convenience1b()
|
||||
printf("s[%d] = '%s'\n",I,sa[I].cstring);
|
||||
}
|
||||
|
||||
s1 = "arandomtuple = (apples,1.2,pi);";
|
||||
s2 = select_between(s1,"(",")");
|
||||
printf("s1='%s'\ns2='%s'\n",s1.cstring,s2.cstring);
|
||||
|
||||
s1 = "arandomtuple = {apples,1.2,pi);";
|
||||
s2 = select_between(s1,"(",")");
|
||||
printf("s1='%s'\ns2='%s'\n",s1.cstring,s2.cstring);
|
||||
|
||||
s1 = "arandomtuple = (apples,1.2,pi};";
|
||||
s2 = select_between(s1,"(",")");
|
||||
printf("s1='%s'\ns2='%s'\n",s1.cstring,s2.cstring);
|
||||
|
||||
s1 = "arandomtuple) = (apples,1.2,pi};";
|
||||
s2 = select_between(s1,"(",")");
|
||||
printf("s1='%s'\ns2='%s'\n",s1.cstring,s2.cstring);
|
||||
|
||||
s1 = "arandomtuple) = (apples,1.2,pi};";
|
||||
s2 = select_between(s1,'(',')');
|
||||
printf("s1='%s'\ns2='%s'\n",s1.cstring,s2.cstring);
|
||||
|
||||
s1 = "kernellaunch<<<nblocks,nthreads>>>(apples,1.2,pi);";
|
||||
s2 = select_between(s1,"<<<",">>>");
|
||||
printf("s1='%s'\ns2='%s'\n",s1.cstring,s2.cstring);
|
||||
|
||||
s1 = "kernellaunch<<<nblocks,nthreads>>>(apples,1.2,pi);";
|
||||
s2 = select_between(s1,"<<<","");
|
||||
printf("s1='%s'\ns2='%s'\n",s1.cstring,s2.cstring);
|
||||
|
||||
s1 = "kernellaunch<<<nblocks,nthreads>>>(apples,1.2,pi);";
|
||||
s2 = select_between(s1,"",">>>");
|
||||
printf("s1='%s'\ns2='%s'\n",s1.cstring,s2.cstring);
|
||||
|
||||
sa.resize(0);
|
||||
sa.append("this");
|
||||
sa.append(" is");
|
||||
sa.append("a ");
|
||||
sa.append("series\n");
|
||||
sa.append("of\r\n");
|
||||
sa.append("strings.");
|
||||
|
||||
s1 = concatenate(sa);
|
||||
printf("\nconcatenate(sa) = '%s'\n---\n",s1.cstring);
|
||||
|
||||
s1 = concat_lines(sa);
|
||||
printf("\nconcat_lines(sa) = '%s'\n----\n",s1.cstring);
|
||||
|
||||
stripnewlines(&sa);
|
||||
s1 = concat_lines(sa);
|
||||
printf("\nconcat_lines(sa) = '%s'\n----\n",s1.cstring);
|
||||
|
||||
|
||||
}
|
||||
|
||||
};//end namespace ams
|
||||
|
||||
Reference in New Issue
Block a user