summaryrefslogtreecommitdiff
path: root/src/core/common/Utils.hpp
diff options
context:
space:
mode:
authorAndreas Stöckel <astoecke@techfak.uni-bielefeld.de>2015-02-15 21:32:54 +0100
committerAndreas Stöckel <astoecke@techfak.uni-bielefeld.de>2015-02-15 21:32:54 +0100
commit8e5e08c4f293434585d2a88f7f331f8ce49b67b9 (patch)
treefa82a937b1ea80f45d7955938c333f68f8a0f3f6 /src/core/common/Utils.hpp
parent2544749215bc2465bfeca431e271110ca86d8a83 (diff)
parent40f4666c43211d9071a827ad8a2524688e7f678f (diff)
Merge branch 'astoecke_parser_stack_new'
Conflicts: application/src/core/parser/stack/DocumentHandler.cpp application/src/core/parser/stack/DocumentHandler.hpp
Diffstat (limited to 'src/core/common/Utils.hpp')
-rw-r--r--src/core/common/Utils.hpp86
1 files changed, 77 insertions, 9 deletions
diff --git a/src/core/common/Utils.hpp b/src/core/common/Utils.hpp
index 2c8a5b3..b5a54fc 100644
--- a/src/core/common/Utils.hpp
+++ b/src/core/common/Utils.hpp
@@ -74,16 +74,45 @@ public:
}
/**
- * Returns true if the given character is in [A-Za-z][A-Za-z0-9_-]*
+ * Returns true if the given string is in
+ * \code{.txt}
+ * [A-Za-z][A-Za-z0-9_-]*
+ * \endCode
+ *
+ * @param name is the string that should be tested.
+ * @return true if the string matches the regular expression given above,
+ * false otherwise.
*/
static bool isIdentifier(const std::string &name);
/**
+ * Returns true if the given string is an identifier or an empty string.
+ */
+ static bool isIdentifierOrEmpty(const std::string &name);
+
+ /**
+ * Returns true if the given string is in
+ * \code{.txt}
+ * ([A-Za-z][A-Za-z0-9_-]*)(:[A-Za-z][A-Za-z0-9_-]*)*
+ * \endCode
+ *
+ * @param name is the string that should be tested.
+ * @return true if the string matches the regular expression given above,
+ * false otherwise.
+ */
+ static bool isNamespacedIdentifier(const std::string &name);
+
+ /**
+ * Returns true if the given character is a linebreak character.
+ */
+ static bool isLinebreak(const char c) { return (c == '\n') || (c == '\r'); }
+
+ /**
* Returns true if the given character is a whitespace character.
*/
static bool isWhitespace(const char c)
{
- return (c == ' ') || (c == '\t') || (c == '\n') || (c == '\r');
+ return (c == ' ') || (c == '\t') || isLinebreak(c);
}
/**
@@ -95,11 +124,6 @@ public:
static bool hasNonWhitepaceChar(const std::string &s);
/**
- * Returns true if the given character is a whitespace character.
- */
- static bool isLinebreak(const char c) { return (c == '\n') || (c == '\r'); }
-
- /**
* Removes whitespace at the beginning and the end of the given string.
*
* @param s is the string that should be trimmed.
@@ -120,8 +144,25 @@ public:
template <class T, class Filter>
static std::pair<size_t, size_t> trim(const T &s, Filter f)
{
+ return trim(s, s.size(), f);
+ }
+
+ /**
+ * 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 len is the number of elements in the container.
+ * @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, size_t len, Filter f)
+ {
size_t start = 0;
- for (size_t i = 0; i < s.size(); i++) {
+ for (size_t i = 0; i < len; i++) {
if (!f(s[i])) {
start = i;
break;
@@ -129,7 +170,7 @@ public:
}
size_t end = 0;
- for (ssize_t i = s.size() - 1; i >= static_cast<ssize_t>(start); i--) {
+ for (ssize_t i = len - 1; i >= static_cast<ssize_t>(start); i--) {
if (!f(s[i])) {
end = i + 1;
break;
@@ -145,6 +186,15 @@ public:
}
/**
+ * 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.
*
@@ -205,6 +255,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
*/