summaryrefslogtreecommitdiffstats
path: root/Util.cpp
diff options
context:
space:
mode:
authorMarkus Mittendrein <git@maxmitti.tk>2016-02-16 15:36:54 +0100
committerMarkus Mittendrein <git@maxmitti.tk>2016-02-16 15:36:59 +0100
commitccaf4c0110067442ff8b141a73604ab2c5d5e715 (patch)
tree22419806e626dd552b9b675397cc9013082137c4 /Util.cpp
parent31262330b0ab05a975cf2d616e2386384aa13b00 (diff)
downloadqt-config-ccaf4c0110067442ff8b141a73604ab2c5d5e715.tar.gz
qt-config-ccaf4c0110067442ff8b141a73604ab2c5d5e715.zip
Add missing Util
Diffstat (limited to 'Util.cpp')
-rw-r--r--Util.cpp191
1 files changed, 191 insertions, 0 deletions
diff --git a/Util.cpp b/Util.cpp
new file mode 100644
index 0000000..41500da
--- /dev/null
+++ b/Util.cpp
@@ -0,0 +1,191 @@
+#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;
+ }
+}