newconveniencefunctions1

This commit is contained in:
2026-04-29 21:00:24 -04:00
parent e8dc131f3c
commit 9d54f00969
8 changed files with 284 additions and 185 deletions

Binary file not shown.

Binary file not shown.

View File

@ -38,6 +38,9 @@ static const ams_chartype ams_char_cr = (ams_chartype) '\r'; //carriage return
static const ams_chartype ams_char_lf = (ams_chartype) '\n'; //newline static const ams_chartype ams_char_lf = (ams_chartype) '\n'; //newline
static const ams_chartype ams_char_tb = (ams_chartype) '\t'; //tab static const ams_chartype ams_char_tb = (ams_chartype) '\t'; //tab
static const ams_chartype ams_char_nt = (ams_chartype) '\0'; //null terminator static const ams_chartype ams_char_nt = (ams_chartype) '\0'; //null terminator
static const ams_chartype ams_char_dq = (ams_chartype) '"'; //doublequote
static const ams_chartype ams_char_sq = (ams_chartype) '\''; //singlequote
class amsstring class amsstring
{ {
@ -120,24 +123,26 @@ public:
bool isvalidnumber(); bool isvalidnumber();
double strtonum(); double strtonum();
//updated convenience functions as class members
}; };
//needs work //needs work
void splitlines(amsstring *s, std::vector<amsstring> *lns); //void splitlines(const amsstring *s, std::vector<amsstring> *lns);
void splitlines(amsstring *s, ams::amsarray<amsstring> *lns); void splitlines(const amsstring *s, ams::amsarray<amsstring> *lns);
void split(amsstring *s, const ams_chartype delimitchar, std::vector<amsstring> *lns); //void split(const amsstring *s, const ams_chartype delimitchar, std::vector<amsstring> *lns);
void split(amsstring *s, const ams_chartype delimitchar, ams::amsarray<amsstring> *lns); void split(const amsstring *s, const ams_chartype delimitchar, ams::amsarray<amsstring> *lns);
void split(amsstring *s, const ams_chartype *delimitstr, std::vector<amsstring> *lns); //void split(const amsstring *s, const ams_chartype *delimitstr, std::vector<amsstring> *lns);
void split(amsstring *s, const ams_chartype *delimitstr, ams::amsarray<amsstring> *lns); void split(const amsstring *s, const ams_chartype *delimitstr, ams::amsarray<amsstring> *lns);
void split(amsstring *s, amsstring *delimitstr, std::vector<amsstring> *lns); //void split(const amsstring *s, amsstring *delimitstr, std::vector<amsstring> *lns);
void split(amsstring *s, amsstring *delimitstr, ams::amsarray<amsstring> *lns); void split(const amsstring *s, amsstring *delimitstr, ams::amsarray<amsstring> *lns);
//splits a string, not counting whitespaces between non-whitespace characters //splits a string, not counting whitespaces between non-whitespace characters
void splitwhitespace(amsstring *s, std::vector<amsstring> *lns); //void splitwhitespace(const amsstring *s, std::vector<amsstring> *lns);
void splitwhitespace(amsstring *s, ams::amsarray<amsstring> *lns); void splitwhitespace(const amsstring *s, ams::amsarray<amsstring> *lns);
//removes all whitespace characters '\t','\r','\n' included //removes all whitespace characters '\t','\r','\n' included
//to the left and right of the string (but not in the middle) //to the left and right of the string (but not in the middle)
@ -146,6 +151,37 @@ void stripwhitespace(amsstring *s);
//completely removes all whitespace entirely //completely removes all whitespace entirely
void stripallwhitespace(amsstring *s); void stripallwhitespace(amsstring *s);
//Updated convenience functions: updated for a more python style
amsarray<amsstring> splitlines(const amsstring &s);
amsarray<amsstring> split(const amsstring &s, const ams_chartype delimitchar);
amsarray<amsstring> split(const amsstring &s, const ams_chartype *delimitstr);
//splits into two guaranteed pieces
//if a delimitchar or delimitstr is not encountered, the first piece will contain the entire string and the second will be ""
amsarray<amsstring> splitfirst(const amsstring &s, const ams_chartype delimitchar);
amsarray<amsstring> splitfirst(const amsstring &s, const ams_chartype *delimitstr);
//splits a string, not counting whitespaces between non-whitespace characters
amsarray<amsstring> splitwhitespace(const amsstring &s);
amsstring stripwhitespace(const amsstring &s);
amsstring stripallwhitespace(const amsstring &s);
//splits into two guaranteed pieces
//if an extension separator . is not encountered, the first piece will contain the entire string, the second ""
amsarray<amsstring> splitext(const amsstring &s);
//splits into two guaranteed pieces
//if a path separator is not encountered, the second piece will contain the entire string, and the first ""
amsarray<amsstring> splitpath(const amsstring &s);
//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 concatenate(const amsarray<amsstring> &slist);
void freadline(FILE *fp, amsstring *s); void freadline(FILE *fp, amsstring *s);
void freadlines(FILE *fp, std::vector<amsstring> *lines); void freadlines(FILE *fp, std::vector<amsstring> *lines);
void fwritelines(FILE *fp, amsstring *s); void fwritelines(FILE *fp, amsstring *s);

View File

@ -894,7 +894,7 @@ namespace ams
return; return;
} }
void splitlines(amsstring *s, ams::amsarray<amsstring> *lns) void splitlines(const amsstring *s, ams::amsarray<amsstring> *lns)
{ {
int I,I1,I2; int I,I1,I2;
amsstring q; amsstring q;
@ -1004,7 +1004,46 @@ namespace ams
} }
*/ */
void split(amsstring *s, const ams_chartype delimitchar, std::vector<amsstring> *lns) // void split(const amsstring *s, const ams_chartype delimitchar, std::vector<amsstring> *lns)
// {
// amsstring q;
// lns->resize(0);
// int I = 0;
// int ind1;
// int ind2;
// //int cs;
// ind1 = 0;
// ams_chartype c;
// while(I<s->length)
// {
// c = s->cstring[I];
// //cs = 0;
// if(c==delimitchar)
// {
// ind2 = I;
// ind2 = max<int>(0,ind2);
// s->substring(ind1,ind2,&q);
// lns->push_back(q);
// ind1 = I+1;
// ind2 = I+1;
// ind1 = min<int>(s->length,ind1);
// ind2 = min<int>(s->length,ind2);
// }
// I = I + 1;
// }
// if(ind1<s->length&&s->cstring[ind1]!=delimitchar)
// {
// s->substring(ind1,s->length,&q);
// lns->push_back(q);
// }
// return;
// }
void split(const amsstring *s, const ams_chartype delimitchar, ams::amsarray<amsstring> *lns)
{ {
amsstring q; amsstring q;
lns->resize(0); lns->resize(0);
@ -1043,46 +1082,85 @@ namespace ams
return; return;
} }
void split(amsstring *s, const ams_chartype delimitchar, ams::amsarray<amsstring> *lns) // void split(const amsstring *s, const ams_chartype *delimitstr, std::vector<amsstring> *lns)
{ // {
amsstring q; // amsstring q;
lns->resize(0); // int I;
// int ind1,ind2;
// int cs;
// int cnt;
// ams_chartype c;
// int N = amsstring_strlen(delimitstr);
// if(lns!=NULL && delimitstr!=NULL)
// {
// if(N>0)
// {
// lns->resize(0);
int I = 0; // ind1 = 0;
int ind1; // I = 0;
int ind2; // cs = 0;
//int cs; // cnt = 0;
ind1 = 0; // while(I<s->size())
ams_chartype c; // {
while(I<s->length) // c = s->at(I);
{ // if(cs==0 && c==delimitstr[cnt])
c = s->cstring[I]; // {
//cs = 0; // cs=1;
if(c==delimitchar) // ind2 = I;
{ // cnt = cnt+1;
ind2 = I; // }
ind2 = max<int>(0,ind2); // else if(cs==1 && c==delimitstr[cnt])
s->substring(ind1,ind2,&q); // {
lns->push_back(q); // cs=1;
ind1 = I+1; // cnt = cnt+1;
ind2 = I+1; // }
ind1 = min<int>(s->length,ind1); // else if(cs==1 && c!=delimitstr[cnt])
ind2 = min<int>(s->length,ind2); // {
} // cs = 0;
// ind2 = ind1;
// cnt = 0;
// I = I - 1; //step back so you can evaluate this char again
// }
I = I + 1; // if(cnt>=N)
} // {
if(ind1<s->length&&s->cstring[ind1]!=delimitchar) // //cut string from (ind1,ind2)
{ // cs = 0;
s->substring(ind1,s->length,&q);
lns->push_back(q);
}
return; // ind1 = min<int>(ind1,s->size());
} // ind1 = max<int>(ind1,0);
// ind2 = min<int>(ind2,s->size());
// ind2 = max<int>(ind2,0);
// ind2 = max<int>(ind2,ind1);
void split(amsstring *s, const ams_chartype *delimitstr, std::vector<amsstring> *lns) // s->substring(ind1,ind2,&q);
// lns->push_back(q);
// ind1 = I + 1;
// ind2 = I + 1;
// cnt = 0;
// }
// I = I + 1;
// }
// if(ind1<s->size())
// {
// s->substring(ind1,s->size(),&q);
// lns->push_back(q);
// }
// } //N>0
// else
// {
// lns->push_back(*s);
// }
// } //lns!=NULL
// return;
// }
void split(const amsstring *s, const ams_chartype *delimitstr, ams::amsarray<amsstring> *lns)
{ {
amsstring q; amsstring q;
int I; int I;
@ -1160,142 +1238,64 @@ namespace ams
return; return;
} }
void split(amsstring *s, const ams_chartype *delimitstr, ams::amsarray<amsstring> *lns) // void split(const amsstring *s, amsstring *delimitstr, std::vector<amsstring> *lns)
{ // {
amsstring q; // split(s,delimitstr->cstring,lns);
int I; // return;
int ind1,ind2; // }
int cs;
int cnt;
ams_chartype c;
int N = amsstring_strlen(delimitstr);
if(lns!=NULL && delimitstr!=NULL) void split(const amsstring *s, amsstring *delimitstr, ams::amsarray<amsstring> *lns)
{
if(N>0)
{
lns->resize(0);
ind1 = 0;
I = 0;
cs = 0;
cnt = 0;
while(I<s->size())
{
c = s->at(I);
if(cs==0 && c==delimitstr[cnt])
{
cs=1;
ind2 = I;
cnt = cnt+1;
}
else if(cs==1 && c==delimitstr[cnt])
{
cs=1;
cnt = cnt+1;
}
else if(cs==1 && c!=delimitstr[cnt])
{
cs = 0;
ind2 = ind1;
cnt = 0;
I = I - 1; //step back so you can evaluate this char again
}
if(cnt>=N)
{
//cut string from (ind1,ind2)
cs = 0;
ind1 = min<int>(ind1,s->size());
ind1 = max<int>(ind1,0);
ind2 = min<int>(ind2,s->size());
ind2 = max<int>(ind2,0);
ind2 = max<int>(ind2,ind1);
s->substring(ind1,ind2,&q);
lns->push_back(q);
ind1 = I + 1;
ind2 = I + 1;
cnt = 0;
}
I = I + 1;
}
if(ind1<s->size())
{
s->substring(ind1,s->size(),&q);
lns->push_back(q);
}
} //N>0
else
{
lns->push_back(*s);
}
} //lns!=NULL
return;
}
void split(amsstring *s, amsstring *delimitstr, std::vector<amsstring> *lns)
{
split(s,delimitstr->cstring,lns);
return;
}
void split(amsstring *s, amsstring *delimitstr, ams::amsarray<amsstring> *lns)
{ {
split(s,delimitstr->cstring,lns); split(s,delimitstr->cstring,lns);
return; return;
} }
void splitwhitespace(amsstring *s, std::vector<amsstring> *lns) // void splitwhitespace(const amsstring *s, std::vector<amsstring> *lns)
{ // {
int I; // int I;
int mode; // int mode;
ams_chartype c; // ams_chartype c;
int ind1 = 0; // int ind1 = 0;
int ind2 = 0; // int ind2 = 0;
I = 0; // I = 0;
mode = 0; // mode = 0;
amsstring q; // amsstring q;
bool qf = 0; // bool qf = 0;
if(s!=NULL && lns!=NULL) // if(s!=NULL && lns!=NULL)
{ // {
lns->resize(0); // lns->resize(0);
while(qf==0) // while(qf==0)
{ // {
c = s->cstring[I]; // c = s->cstring[I];
if(!(isspace(c)||c==' '||c=='\t')&&mode==0) // if(!(isspace(c)||c==' '||c=='\t')&&mode==0)
{ // {
mode = 1; // mode = 1;
ind1 = I; // ind1 = I;
ind1 = ams::max<int>(0,ind1); // 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)
{ // {
mode = 0; // mode = 0;
ind2 = I; // ind2 = I;
ind2 = ams::min<int>(ind2,s->length); // ind2 = ams::min<int>(ind2,s->length);
s->substring(ind1,ind2,&q); // s->substring(ind1,ind2,&q);
//printf("debug: %d, %s\n",I,q.cstring); // //printf("debug: %d, %s\n",I,q.cstring);
lns->push_back(q); // lns->push_back(q);
} // }
if(I>=s->length) // if(I>=s->length)
{ // {
qf = 1; // qf = 1;
} // }
I = I + 1; // I = I + 1;
} // }
} //test null pointers // } //test null pointers
return; // return;
} // }
void splitwhitespace(amsstring *s, ams::amsarray<amsstring> *lns) void splitwhitespace(const amsstring *s, ams::amsarray<amsstring> *lns)
{ {
int I; int I;
int mode; int mode;

View File

@ -0,0 +1,63 @@
#include <amsstring4/amsstring4.hpp>
namespace ams
{
amsarray<amsstring> splitlines(const amsstring &s)
{
amsarray<amsstring> ret;
splitlines(&s,&ret);
return ret;
}
amsarray<amsstring> split(const amsstring &s, const ams_chartype delimitchar)
{
amsarray<amsstring> ret;
return ret;
}
//splits a string, not counting whitespaces between non-whitespace characters
amsarray<amsstring> splitwhitespace(const amsstring &s)
{
amsarray<amsstring> ret;
return ret;
}
amsstring stripwhitespace(const amsstring &s)
{
amsstring ret;
return ret;
}
amsstring stripallwhitespace(const amsstring &s)
{
amsstring ret;
return ret;
}
amsarray<amsstring> splitext(const amsstring &s)
{
amsarray<amsstring> ret;
return ret;
}
amsarray<amsstring> splitpath(const amsstring &s)
{
amsarray<amsstring> ret;
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;
return ret;
}
};//end namespace ams

View File

@ -7,20 +7,20 @@ int main(int argc, char* argv[])
int ret = 0; int ret = 0;
printf("ams string4 library tests.\n"); printf("ams string4 library tests.\n");
amsstring4_basic_string_test1(); // amsstring4_basic_string_test1();
amsstring4_sscanf_test1(); // amsstring4_sscanf_test1();
amsstring4_basic_string_test2(); // amsstring4_basic_string_test2();
amsstring4_memoryleakcheck1(); // amsstring4_memoryleakcheck1();
amsstring4_memoryleakcheck2(); // amsstring4_memoryleakcheck2();
amsstring4_stringtests2(); // amsstring4_stringtests2();
amsstring4_test_find(); // amsstring4_test_find();
amsstring4_test_splitlines(); // amsstring4_test_splitlines();
amsstring4_test_split(); // amsstring4_test_split();
amsstring4_test_strip(); // amsstring4_test_strip();
amsstring4_test_freadwrite(); // amsstring4_test_freadwrite();
amsstring4_test_concatenation_operators(); // amsstring4_test_concatenation_operators();
return ret; return ret;
} }