diff options
author | Benjamin Paassen <bpaassen@techfak.uni-bielefeld.de> | 2014-12-03 00:57:57 +0100 |
---|---|---|
committer | Benjamin Paassen <bpaassen@techfak.uni-bielefeld.de> | 2014-12-03 00:57:57 +0100 |
commit | 41366eb61e5b85524b8ee07ae183df4f9f8a1f6d (patch) | |
tree | ac9468e4adc6cfcb63b4adc324770dc07de0e5aa /src/core/Utils.cpp | |
parent | 314e97ac5307f5053fc0c31ec23c39ba9c9a0aac (diff) | |
parent | ed79df8f263dcd973c8ceb016b516644d87d8aa8 (diff) |
Merge branch 'master' of somweyr.de:ousia
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; } - } |