From b143fe4e0df319a88df9cba22c5dd707000810d4 Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Wed, 3 Dec 2014 00:01:03 +0100 Subject: added trim, join and isWhitespace function to Utils --- src/core/Utils.cpp | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'src/core/Utils.cpp') diff --git a/src/core/Utils.cpp b/src/core/Utils.cpp index 184fdd0..c460ed4 100644 --- a/src/core/Utils.cpp +++ b/src/core/Utils.cpp @@ -16,10 +16,31 @@ along with this program. If not, see . */ +#include +#include + #include "Utils.hpp" namespace ousia { +std::string Utils::trim(const std::string &s) +{ + size_t firstNonWhitespace = std::numeric_limits::max(); + size_t lastNonWhitespace = 0; + for (size_t i = 0; i < s.size(); i++) { + if (!isWhitespace(s[i])) { + firstNonWhitespace = std::min(i, firstNonWhitespace); + lastNonWhitespace = std::max(i, lastNonWhitespace); + } + } + + if (firstNonWhitespace < lastNonWhitespace) { + return s.substr(firstNonWhitespace, + lastNonWhitespace - firstNonWhitespace + 1); + } + return std::string{}; +} + bool Utils::isIdentifier(const std::string &name) { bool first = true; @@ -34,6 +55,5 @@ bool Utils::isIdentifier(const std::string &name) } return true; } - } -- cgit v1.2.3