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.hpp | |
parent | 314e97ac5307f5053fc0c31ec23c39ba9c9a0aac (diff) | |
parent | ed79df8f263dcd973c8ceb016b516644d87d8aa8 (diff) |
Merge branch 'master' of somweyr.de:ousia
Diffstat (limited to 'src/core/Utils.hpp')
-rw-r--r-- | src/core/Utils.hpp | 52 |
1 files changed, 44 insertions, 8 deletions
diff --git a/src/core/Utils.hpp b/src/core/Utils.hpp index 2fcd794..14bd7b4 100644 --- a/src/core/Utils.hpp +++ b/src/core/Utils.hpp @@ -16,18 +16,16 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ - #ifndef _OUSIA_UTILS_H_ #define _OUSIA_UTILS_H_ +#include <sstream> #include <string> namespace ousia { class Utils { - public: - /** * Returns true if the given character is in [A-Za-z] */ @@ -39,10 +37,7 @@ public: /** * Returns true if the given character is in [0-9] */ - static bool isNumeric(const char c) - { - return (c >= '0') && (c <= '9'); - } + static bool isNumeric(const char c) { return (c >= '0') && (c <= '9'); } /** * Returns true if the given character is in [A-Za-z0-9] @@ -57,8 +52,49 @@ public: */ static bool isIdentifier(const std::string &name); -}; + /** + * Returns true if the given character is a whitespace character. + */ + static bool isWhitespace(const char c) + { + return (c == ' ') || (c == '\t') || (c == '\n') || (c == '\r'); + } + /** + * Removes whitespace at the beginning and the end of the given string. + */ + static std::string trim(const std::string &s); + + /** + * Turns the elements of a collection into a string separated by the + * given delimiter. + * + * @param es is an iterable container of elements that can be appended to an + * output stream (the << operator must be implemented). + * @param delim is the delimiter that should be used to separate the items. + * @param start is a character sequence that should be prepended to the + * result. + * @param end is a character sequence that should be appended to the result. + */ + template <class T> + static std::string join(T es, const std::string &delim, + const std::string &start = "", + const std::string &end = "") + { + std::stringstream res; + bool first = true; + res << start; + for (const auto &e : es) { + if (!first) { + res << delim; + } + res << e; + first = false; + } + res << end; + return res.str(); + } +}; } #endif /* _OUSIA_UTILS_H_ */ |