diff options
Diffstat (limited to 'runtime/utils.cc')
| -rw-r--r-- | runtime/utils.cc | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/runtime/utils.cc b/runtime/utils.cc index 9796b99635..e039581b05 100644 --- a/runtime/utils.cc +++ b/runtime/utils.cc @@ -889,6 +889,35 @@ void Split(const std::string& s, char separator, std::vector<std::string>& resul } } +std::string Trim(std::string s) { + std::string result; + unsigned int start_index = 0; + unsigned int end_index = s.size() - 1; + + // Skip initial whitespace. + while (start_index < s.size()) { + if (!isspace(s[start_index])) { + break; + } + start_index++; + } + + // Skip terminating whitespace. + while (end_index >= start_index) { + if (!isspace(s[end_index])) { + break; + } + end_index--; + } + + // All spaces, no beef. + if (end_index < start_index) { + return ""; + } + // Start_index is the first non-space, end_index is the last one. + return s.substr(start_index, end_index - start_index + 1); +} + template <typename StringT> std::string Join(std::vector<StringT>& strings, char separator) { if (strings.empty()) { |