I think Arduino should include built-in the quickest easiest least variable declaring requirement parsing functions in the wild west!
void TrimStripLeft(String *text, String strip) {
while (text->startsWith(strip)) text->remove(0,strip.length());
}
String TrimStripLeft(String text, String strip) {
TrimStripLeft(&text, strip);
return text;
}
void TrimStripLeft(String *text, char strip) {
TrimStripLeft(text, String(strip));
}
String TrimStripLeft(String text, char strip) {
TrimStripLeft(&text, String(strip));
return text;
}
void TrimStripRight(String *text, String strip) {
while (text->endsWith(strip)) text->remove((text->length()-strip.length())-1);
}
String TrimStripRight(String text, String strip) {
TrimStripRight(&text, strip);
return text;
}
void TrimStripRight(String *text, char strip) {
TrimStripRight(text, String(strip));
}
String TrimStripRight(String text, char strip) {
TrimStripRight(&text, String(strip));
return text;
}
void TrimStrip(String *text, String strip) {
if (text->startsWith(strip)) TrimStripLeft(text,strip);
if (text->endsWith(strip)) TrimStripRight(text,strip);
}
String TrimStrip(String text, String strip) {
TrimStrip(&text, strip);
return text;
}
void TrimStrip(String *text, char strip) {
TrimStrip(text, String(strip));
}
String TrimStrip(String text, char strip) {
TrimStrip(&text, String(strip));
return text;
}
String RemoveNextArg(String *args, String delim, bool strip) {
//returns the next argument seperated by delim
//from args, modifying args to reflect the removal
String ret=String(*args);
if (ret.indexOf(delim)>-1) {
ret.remove(ret.indexOf(delim));
args->remove(0,(args->indexOf(delim))+delim.length());
} else {
*args = String();
}
if (strip) TrimStrip(args, delim);
return ret;
}
String RemoveNextArg(String *args, String delim) {
return RemoveNextArg(args, delim, true);
}
String RemoveNextArg(String *args) {
return RemoveNextArg(args, " ", true);
}
String RemoveNextArg(String *args, char delim) {
return RemoveNextArg(args, String(delim), true);
}
String RemoveNextArg(String *args, bool strip) {
return RemoveNextArg(args, " ", strip);
}
String RemoveNextArg(String *args, char delim, bool strip) {
return RemoveNextArg(args, String(delim), strip);
}
String NextArg(String text, String delim, bool strip) {
//returns the next arg from text seperated by delim, but does not change text
if (strip)
return ((text.indexOf(delim)>-1)?text.substring(0,text.indexOf(delim)):text);
else
return ((text.indexOf(delim)>-1)?TrimStrip(text.substring(0,text.indexOf(delim)),delim):text);
}
String NextArg(String text, String delim) {
return NextArg(text,delim, true);
}
String NextArg(String text) {
return NextArg(text," ",true);
}
String NextArg(String text, char delim) {
return NextArg(text,String(delim),true);
}
String NextArg(String text, bool strip) {
return NextArg(text," ", strip);
}
String NextArg(String text, char delim, bool strip) {
return NextArg(text,String(delim),strip);
}
String RemoveArg(String text, String delim, bool strip) {
//returns the text following the next arg seperated by delim, but does not change text
if (strip)
return ((text.indexOf(delim)>-1)?text.substring(text.indexOf(delim)+delim.length()):"");
else
return ((text.indexOf(delim)>-1)?TrimStrip(text.substring(text.indexOf(delim)+delim.length()),delim):"");
}
String RemoveArg(String text, String delim) {
return RemoveArg(text,delim, true);
}
String RemoveArg(String text) {
return RemoveArg(text," ",true);
}
String RemoveArg(String text, char delim) {
return RemoveArg(text,String(delim),true);
}
String RemoveArg(String text, bool strip) {
return RemoveArg(text," ", strip);
}
String RemoveArg(String text, char delim, bool strip) {
return RemoveArg(text,String(delim),strip);
}
For instance:
do while (str.length>0) {
Serial.print("Line! ");
Serial.println(RemoveNextArg(str, '\n'));
}
If you ever dreaded the grunt work, of strings, or more variables, I know I have, and I'm talking about Visual Basic, let alone C, you need these! They should be core don't forget! Personally, INI files, oh man, I had a template two functions I would grab to do another, and it was annoying. Check this out, say you have your INI text, all you need is one more variable, line:
do while (text.length>0) {
String line = RemoveNextArg(text, '\n');
switch (RemoveNextArg(line, '=') {
case "propname":
///set some prop = line
break;
case "propname2":
///set some prop2 = line
break;
}