summaryrefslogtreecommitdiffstats
path: root/src/Util.cpp
diff options
context:
space:
mode:
authorMarkus Mittendrein <git@maxmitti.tk>2016-03-31 19:38:49 +0200
committerMarkus Mittendrein <git@maxmitti.tk>2016-03-31 19:46:33 +0200
commit135eeef23be2372bd1821d19d17bfcd187b2da05 (patch)
tree1fb178138e8f39d6684abf6268ce3c6e5610790f /src/Util.cpp
parentbb5431ba4d88018275d459b16dfdc129ab133b45 (diff)
downloadmanager-135eeef23be2372bd1821d19d17bfcd187b2da05.tar.gz
manager-135eeef23be2372bd1821d19d17bfcd187b2da05.zip
Move ConfigBase and Util into a separate qt-config repo for reuse
Diffstat (limited to 'src/Util.cpp')
l---------[-rw-r--r--]src/Util.cpp192
1 files changed, 1 insertions, 191 deletions
diff --git a/src/Util.cpp b/src/Util.cpp
index 41500da..bbc718a 100644..120000
--- a/src/Util.cpp
+++ b/src/Util.cpp
@@ -1,191 +1 @@
-#include "Util.hpp"
-
-#include <QRegularExpression>
-
-namespace Util {
- QString trimQuotes(QString string, bool& trimmed)
- {
- if(string.isEmpty()) return string;
-
- trimmed = false;
- if(string.length() >= 2 && string.at(0) == '"' && string.at(string.length() - 1) == '"')
- {
- string.remove(0, 1);
- string.remove(string.length() - 1, 1);
- trimmed = true;
- }
- return string;
- }
-
- QString unescape(const QString &string, const QChar escapeChar)
- {
- if(string.isEmpty()) return string;
- QString ret;
- bool escaped = false;
- QChar c = string.at(0);
- for(int i = 0; i < string.length(); ++i)
- {
- c = string.at(i);
- if(!escaped)
- {
- if(c == escapeChar)
- {
- escaped = true;
- continue;
- }
- else
- {
- ret.append(c);
- }
- }
- else
- {
- if(unescapeChars.contains(c))
- {
- ret.append(unescapeChars.value(c));
- }
- else
- {
- ret.append(c);
- }
- escaped = false;
- }
- }
- if(escaped)
- {
- ret.append(escapeChar);
- }
- return ret;
- }
-
- QString escape(const QString &string, const QChar escapeChar, const QString &escapeChars)
- {
- QString ret;
-
- for(int i = 0; i < string.length(); ++i)
- {
- QChar c = string.at(i);
-
- if(c == escapeChar)
- {
- ret.append(escapeChar);
- ret.append(escapeChar);
- }
- else if(escapeChars.contains(c))
- {
- ret.append(escapeChar);
- ret.append(c);
- }
- else if(unescapeChars.values().contains(c))
- {
- ret.append(unescapeChars.key(c));
- }
- else
- {
- ret.append(c);
- }
- }
-
- return ret;
- }
-
- QString joinEscape(const QStringList &list, const QChar joinChar, const QChar escapeChar)
- {
- QString ret;
- bool first = true;
- foreach(const QString& part, list)
- {
- if(!first)
- {
- ret.append(joinChar);
- }
- else
- {
- first = false;
- }
- ret.append(escape(part, escapeChar, joinChar));
- }
- return ret;
- }
-
- QStringList splitEscaped(const QString &joined, const QChar splitChar, const QChar escapeChar)
- {
- QStringList ret;
-
- int partPos = 0;
- bool escaped = false;
- for(int i = 0; i < joined.length(); ++i)
- {
- QChar c = joined.at(i);
- if(!escaped)
- {
- if(c == escapeChar)
- {
- escaped = true;
- }
- else if(c == splitChar)
- {
- ret.append(Util::unescape(joined.mid(partPos, i - partPos), escapeChar));
- partPos = i + 1;
- }
- }
- else
- {
- escaped = false;
- }
- }
- ret.append(Util::unescape(joined.mid(partPos), escapeChar));
-
- return ret;
- }
-
- QString& unescapeClonkString(QString&& string)
- {
- static QRegularExpression escapeExp(R"(\\[0-7]+)");
- QRegularExpressionMatch match;
- while((match = escapeExp.match(string)).hasMatch())
- {
- unsigned char c = (unsigned char)match.capturedRef(0).mid(1).toInt(0, 8);
-
- string.replace(match.capturedStart(), match.capturedLength(), c);
- }
- return string;
- }
-
- int indexOfEscaped(const QString& string, const QChar subject, int startPos, const QChar escapeChar)
- {
- if(string.isEmpty())
- {
- return -1;
- }
- if(startPos < 0)
- {
- startPos += string.length();
- }
- bool escaped = false;
- for(int pos = startPos; pos < string.length(); ++pos)
- {
- QChar c = string.at(pos);
- if(!escaped)
- {
- if(c == escapeChar)
- {
- escaped = true;
- continue;
- }
- else
- {
- if(c == subject)
- {
- return pos;
- }
- }
- }
- else
- {
- escaped = false;
- }
- }
- return -1;
- }
-}
+qt-config/Util.cpp \ No newline at end of file