Add string partitioning methods

This commit is contained in:
mniip 2018-05-02 23:42:34 +03:00
parent 6c9cb174fb
commit d1610c5127
2 changed files with 53 additions and 6 deletions

View File

@ -171,6 +171,57 @@ public:
inline Split SplitFromEndByAny(String const &str, size_t pos = npos) const { return Split(*this, super::find_last_of(str, pos), 1, true); }
inline Split SplitFromEndByNot(String const &str, size_t pos = npos) const { return Split(*this, super::find_last_not_of(str, pos), 1, true); }
inline std::vector<String> PartitionBy(value_type ch, bool includeEmpty = false) const
{
std::vector<String> result;
size_t at = 0;
while(true)
{
Split split = SplitBy(ch, at);
String part = split.Before();
if(includeEmpty || part.size())
result.push_back(part);
at = split.PositionAfter();
if(!split)
break;
}
return result;
}
inline std::vector<String> PartitionBy(String const &str, bool includeEmpty = false) const
{
std::vector<String> result;
size_t at = 0;
while(true)
{
Split split = SplitBy(str, at);
String part = split.Before();
if(includeEmpty || part.size())
result.push_back(part);
at = split.PositionAfter();
if(!split)
break;
}
return result;
}
inline std::vector<String> PartitionByAny(String const &str, bool includeEmpty = false) const
{
std::vector<String> result;
size_t at = 0;
while(true)
{
Split split = SplitByAny(str, at);
String part = split.Before();
if(includeEmpty || part.size())
result.push_back(part);
at = split.PositionAfter();
if(!split)
break;
}
return result;
}
inline String &Substitute(String const &needle, String const &replacement)
{
size_t needleSize = needle.size();

View File

@ -22,9 +22,7 @@ int TPTScriptInterface::Command(String command)
int retCode = -1;
//Split command into words, put them on the stack
String::Stream ss(command);
String word;
while(std::getline(ss, word, String::value_type(' ')))
for(String word : command.PartitionBy(' '))
words.push_back(word);
while(!words.empty())
{
@ -211,9 +209,7 @@ String TPTScriptInterface::FormatCommand(String command)
String outputData;
//Split command into words, put them on the stack
String::Stream ss(command);
String word;
while(std::getline(ss, word, String::value_type(' ')))
for(String word : command.PartitionBy(' '))
words.push_back(word);
while(!words.empty())
{