diff options
author | Andreas Stöckel <astoecke@techfak.uni-bielefeld.de> | 2014-12-03 00:01:03 +0100 |
---|---|---|
committer | Andreas Stöckel <astoecke@techfak.uni-bielefeld.de> | 2014-12-03 00:01:03 +0100 |
commit | b143fe4e0df319a88df9cba22c5dd707000810d4 (patch) | |
tree | 112c2b95fc55de0629403e9dd37e274cc41bd3dd /src/core/Utils.cpp | |
parent | 92a31ed239fe41af07bb13d572203a7753091a5b (diff) |
added trim, join and isWhitespace function to Utils
Diffstat (limited to 'src/core/Utils.cpp')
-rw-r--r-- | src/core/Utils.cpp | 22 |
1 files changed, 21 insertions, 1 deletions
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 <http://www.gnu.org/licenses/>. */ +#include <algorithm> +#include <limits> + #include "Utils.hpp" namespace ousia { +std::string Utils::trim(const std::string &s) +{ + size_t firstNonWhitespace = std::numeric_limits<size_t>::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; } - } |