diff options
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_ */ |