Add string partitioning methods
This commit is contained in:
parent
6c9cb174fb
commit
d1610c5127
@ -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();
|
||||
|
@ -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())
|
||||
{
|
||||
|
Reference in New Issue
Block a user