diff options
author | Benjamin Paassen <bpaassen@techfak.uni-bielefeld.de> | 2015-02-15 21:56:04 +0100 |
---|---|---|
committer | Benjamin Paassen <bpaassen@techfak.uni-bielefeld.de> | 2015-02-15 21:56:04 +0100 |
commit | d2f99e4b43ed93ef0fa8e138e0c3afc79775b77c (patch) | |
tree | 8e7cdb894b7036b3ca01499ee9432d2e62930477 /src/core/common/Utils.cpp | |
parent | 40f7df390f00f85c17bd0e6527ec4ba19cbce4fc (diff) | |
parent | 4f2872d9968aec93bebff90d1238347c8a364949 (diff) |
Merge branch 'master' of somweyr.de:ousia
Diffstat (limited to 'src/core/common/Utils.cpp')
-rw-r--r-- | src/core/common/Utils.cpp | 54 |
1 files changed, 46 insertions, 8 deletions
diff --git a/src/core/common/Utils.cpp b/src/core/common/Utils.cpp index 563fe2a..f8b53c6 100644 --- a/src/core/common/Utils.cpp +++ b/src/core/common/Utils.cpp @@ -18,19 +18,13 @@ #include <algorithm> #include <cctype> -#include <limits> #include <string> #include "Utils.hpp" +#include "WhitespaceHandler.hpp" namespace ousia { -std::string Utils::trim(const std::string &s) -{ - std::pair<size_t, size_t> bounds = trim(s, Utils::isWhitespace); - return s.substr(bounds.first, bounds.second - bounds.first); -} - bool Utils::isIdentifier(const std::string &name) { bool first = true; @@ -43,7 +37,27 @@ bool Utils::isIdentifier(const std::string &name) } first = false; } - return true; + return !first; +} + +bool Utils::isIdentifierOrEmpty(const std::string &name) +{ + return name.empty() || isIdentifier(name); +} + +bool Utils::isNamespacedIdentifier(const std::string &name) +{ + bool first = true; + for (char c : name) { + if (first && !isIdentifierStartCharacter(c)) { + return false; + } + if (!first && (!isIdentifierCharacter(c) && c != ':')) { + return false; + } + first = (c == ':'); + } + return !first; } bool Utils::hasNonWhitepaceChar(const std::string &s) @@ -94,5 +108,29 @@ std::string Utils::extractFileExtension(const std::string &filename) } return std::string{}; } + +std::string Utils::trim(const std::string &s) +{ + std::pair<size_t, size_t> bounds = trim(s, Utils::isWhitespace); + return s.substr(bounds.first, bounds.second - bounds.first); +} + +std::string Utils::collapse(const std::string &s) +{ + CollapsingWhitespaceHandler h; + appendToWhitespaceHandler(h, s, 0); + return h.toString(); +} + +bool Utils::startsWith(const std::string &s, const std::string &prefix) +{ + return prefix.size() <= s.size() && s.substr(0, prefix.size()) == prefix; +} + +bool Utils::endsWith(const std::string &s, const std::string &suffix) +{ + return suffix.size() <= s.size() && + s.substr(s.size() - suffix.size(), suffix.size()) == suffix; +} } |