summaryrefslogtreecommitdiffstats
path: root/src/Util.cpp
blob: 36588023ba92e773284b749093003b2f4777ca19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#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;
    }
}