diff --git a/src/common/String.h b/src/common/String.h index 79eb3d145..ee3206b8b 100644 --- a/src/common/String.h +++ b/src/common/String.h @@ -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 PartitionBy(value_type ch, bool includeEmpty = false) const + { + std::vector 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 PartitionBy(String const &str, bool includeEmpty = false) const + { + std::vector 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 PartitionByAny(String const &str, bool includeEmpty = false) const + { + std::vector 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(); diff --git a/src/lua/TPTScriptInterface.cpp b/src/lua/TPTScriptInterface.cpp index 22ee72d2d..aa5f9de6f 100644 --- a/src/lua/TPTScriptInterface.cpp +++ b/src/lua/TPTScriptInterface.cpp @@ -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()) {