summaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
Diffstat (limited to 'src/core')
-rw-r--r--src/core/common/Utils.cpp25
-rw-r--r--src/core/common/Utils.hpp72
-rw-r--r--src/core/common/Whitespace.hpp62
-rw-r--r--src/core/common/WhitespaceHandler.hpp7
4 files changed, 102 insertions, 64 deletions
diff --git a/src/core/common/Utils.cpp b/src/core/common/Utils.cpp
index 4005143..3739c61 100644
--- a/src/core/common/Utils.cpp
+++ b/src/core/common/Utils.cpp
@@ -21,6 +21,7 @@
#include <string>
#include "Utils.hpp"
+#include "WhitespaceHandler.hpp"
namespace ousia {
@@ -87,5 +88,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;
+}
}
diff --git a/src/core/common/Utils.hpp b/src/core/common/Utils.hpp
index af7a773..16a9136 100644
--- a/src/core/common/Utils.hpp
+++ b/src/core/common/Utils.hpp
@@ -100,6 +100,60 @@ public:
static bool hasNonWhitepaceChar(const std::string &s);
/**
+ * Removes whitespace at the beginning and the end of the given string.
+ *
+ * @param s is the string that should be trimmed.
+ * @return a trimmed copy of s.
+ */
+ static std::string trim(const std::string &s);
+
+ /**
+ * Trims the given string or vector of chars by returning the start and end
+ * index.
+ *
+ * @param s is the container that should be trimmed.
+ * @param f is a function that returns true for values that should be
+ * removed.
+ * @return start and end index. Note that "end" points at the character
+ * beyond the end, thus "end" minus "start"
+ */
+ template <class T, class Filter>
+ static std::pair<size_t, size_t> trim(const T &s, Filter f)
+ {
+ size_t start = 0;
+ for (size_t i = 0; i < s.size(); i++) {
+ if (!f(s[i])) {
+ start = i;
+ break;
+ }
+ }
+
+ size_t end = 0;
+ for (ssize_t i = s.size() - 1; i >= static_cast<ssize_t>(start); i--) {
+ if (!f(s[i])) {
+ end = i + 1;
+ break;
+ }
+ }
+
+ if (end < start) {
+ start = 0;
+ end = 0;
+ }
+
+ return std::pair<size_t, size_t>{start, end};
+ }
+
+ /**
+ * Collapses the whitespaces in the given string (trims the string and
+ * replaces all whitespace characters by a single one).
+ *
+ * @param s is the string in which the whitespace should be collapsed.
+ * @return a copy of s with collapsed whitespace.
+ */
+ static std::string collapse(const std::string &s);
+
+ /**
* Turns the elements of a collection into a string separated by the
* given delimiter.
*
@@ -160,6 +214,24 @@ public:
static std::string extractFileExtension(const std::string &filename);
/**
+ * Checks whether the given string starts with the given prefix.
+ *
+ * @param s is the string.
+ * @param prefix is the string which should be checked for being a prefix of
+ * s.
+ */
+ static bool startsWith(const std::string &s, const std::string &prefix);
+
+ /**
+ * Checks whether the given string ends with the given suffix.
+ *
+ * @param s is the string.
+ * @param suffix is the string which should be checked for being a suffix of
+ * s.
+ */
+ static bool endsWith(const std::string &s, const std::string &suffix);
+
+ /**
* Hash functional to be used for enum classes.
* See http://stackoverflow.com/a/24847480/2188211
*/
diff --git a/src/core/common/Whitespace.hpp b/src/core/common/Whitespace.hpp
index 1e9f36a..72a2291 100644
--- a/src/core/common/Whitespace.hpp
+++ b/src/core/common/Whitespace.hpp
@@ -19,8 +19,7 @@
/**
* @file Whitespace.hpp
*
- * Contains the WhitespaceMode enum used in various places, as well es functions
- * for trimming and collapsing whitespaces.
+ * Contains the WhitespaceMode enum used in various places.
*
* @author Andreas Stöckel (astoecke@techfak.uni-bielefeld.de)
*/
@@ -55,65 +54,6 @@ enum class WhitespaceMode {
COLLAPSE
};
-/**
- * Collection of functions for trimming or collapsing whitespace.
- */
-class Whitespace {
- /**
- * Removes whitespace at the beginning and the end of the given string.
- *
- * @param s is the string that should be trimmed.
- * @return a trimmed copy of s.
- */
- static std::string trim(const std::string &s);
-
- /**
- * Trims the given string or vector of chars by returning the start and end
- * index.
- *
- * @param s is the container that should be trimmed.
- * @param f is a function that returns true for values that should be
- * removed.
- * @return start and end index. Note that "end" points at the character
- * beyond the end, thus "end" minus "start"
- */
- template <class T, class Filter>
- static std::pair<size_t, size_t> trim(const T &s, Filter f)
- {
- size_t start = 0;
- for (size_t i = 0; i < s.size(); i++) {
- if (!f(s[i])) {
- start = i;
- break;
- }
- }
-
- size_t end = 0;
- for (ssize_t i = s.size() - 1; i >= static_cast<ssize_t>(start); i--) {
- if (!f(s[i])) {
- end = i + 1;
- break;
- }
- }
-
- if (end < start) {
- start = 0;
- end = 0;
- }
-
- return std::pair<size_t, size_t>{start, end};
- }
-
- /**
- * Collapses the whitespaces in the given string (trims the string and
- * replaces all whitespace characters by a single one).
- *
- * @param s is the string in which the whitespace should be collapsed.
- * @return a copy of s with collapsed whitespace.
- */
- static std::string collapse(const std::string &s);
-};
-
}
#endif /* _OUSIA_WHITESPACE_HPP_ */
diff --git a/src/core/common/WhitespaceHandler.hpp b/src/core/common/WhitespaceHandler.hpp
index 1935c24..79e0518 100644
--- a/src/core/common/WhitespaceHandler.hpp
+++ b/src/core/common/WhitespaceHandler.hpp
@@ -32,7 +32,7 @@
#include <string>
#include <vector>
-#include "WhitespaceHandler.hpp"
+#include "Utils.hpp"
namespace ousia {
@@ -76,7 +76,7 @@ public:
/**
* Returns the content of the WhitespaceHandler as string.
*/
- std::string toString()
+ std::string toString() const
{
return std::string(textBuf.data(), textBuf.size());
}
@@ -214,7 +214,8 @@ inline void appendToWhitespaceHandler(WhitespaceHandler &handler, Buffer buf,
size_t start)
{
for (auto elem : buf) {
- handler.append(elem, start++);
+ handler.append(elem, start, start + 1);
+ start++;
}
}
}