summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/core/Utils.cpp22
-rw-r--r--src/core/Utils.hpp52
2 files changed, 65 insertions, 9 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;
}
-
}
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_ */