From ccaf4c0110067442ff8b141a73604ab2c5d5e715 Mon Sep 17 00:00:00 2001 From: Markus Mittendrein Date: Tue, 16 Feb 2016 15:36:54 +0100 Subject: Add missing Util --- Util.cpp | 191 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Util.hpp | 18 ++++++ 2 files changed, 209 insertions(+) create mode 100644 Util.cpp create mode 100644 Util.hpp 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 + +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; + } +} diff --git a/Util.hpp b/Util.hpp new file mode 100644 index 0000000..f90e108 --- /dev/null +++ b/Util.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include +#include + +namespace Util { + static const QMap unescapeChars { + {'n', '\n'}, + }; + + QString trimQuotes(QString string, bool& trimmed); + QString unescape(const QString& string, const QChar escapeChar = '\\'); + QString escape(const QString& string, const QChar escapeChar = '\\', const QString& escapeChars = ""); + QString joinEscape(const QStringList& list, const QChar joinChar, const QChar escapeChar = '\\'); + QStringList splitEscaped(const QString& joined, const QChar splitChar, const QChar escapeChar = '\\'); + QString& unescapeClonkString(QString&& string); + int indexOfEscaped(const QString& string, const QChar subject, int startPos = 0, const QChar escapeChar = '\\'); +} -- cgit v1.2.3-54-g00ecf