-
-
Notifications
You must be signed in to change notification settings - Fork 441
/
Helpers.cpp
280 lines (242 loc) · 7.27 KB
/
Helpers.cpp
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include "Helpers.hpp"
#include "providers/twitch/TwitchCommon.hpp"
#include <QDirIterator>
#include <QLocale>
#include <QRegularExpression>
#include <QUuid>
namespace chatterino {
namespace _helpers_internal {
int skipSpace(const QStringRef &view, int startPos)
{
while (startPos < view.length() && view.at(startPos).isSpace())
{
startPos++;
}
return startPos - 1;
}
bool matchesIgnorePlural(const QStringRef &word, const QString &singular)
{
if (!word.startsWith(singular))
{
return false;
}
if (word.length() == singular.length())
{
return true;
}
return word.length() == singular.length() + 1 &&
word.at(word.length() - 1).toLatin1() == 's';
}
std::pair<uint64_t, bool> findUnitMultiplierToSec(const QStringRef &view,
int &pos)
{
// Step 1. find end of unit
int startIdx = pos;
int endIdx = view.length();
for (; pos < view.length(); pos++)
{
auto c = view.at(pos);
if (c.isSpace() || c.isDigit())
{
endIdx = pos;
break;
}
}
pos--;
// TODO(QT6): use sliced (more readable)
auto unit = view.mid(startIdx, endIdx - startIdx);
if (unit.isEmpty())
{
return std::make_pair(0, false);
}
auto first = unit.at(0).toLatin1();
switch (first)
{
case 's': {
if (unit.length() == 1 ||
matchesIgnorePlural(unit, QStringLiteral("second")))
{
return std::make_pair(1, true);
}
}
break;
case 'm': {
if (unit.length() == 1 ||
matchesIgnorePlural(unit, QStringLiteral("minute")))
{
return std::make_pair(60, true);
}
if ((unit.length() == 2 && unit.at(1).toLatin1() == 'o') ||
matchesIgnorePlural(unit, QStringLiteral("month")))
{
return std::make_pair(60 * 60 * 24 * 30, true);
}
}
break;
case 'h': {
if (unit.length() == 1 ||
matchesIgnorePlural(unit, QStringLiteral("hour")))
{
return std::make_pair(60 * 60, true);
}
}
break;
case 'd': {
if (unit.length() == 1 ||
matchesIgnorePlural(unit, QStringLiteral("day")))
{
return std::make_pair(60 * 60 * 24, true);
}
}
break;
case 'w': {
if (unit.length() == 1 ||
matchesIgnorePlural(unit, QStringLiteral("week")))
{
return std::make_pair(60 * 60 * 24 * 7, true);
}
}
break;
}
return std::make_pair(0, false);
}
} // namespace _helpers_internal
using namespace _helpers_internal;
bool startsWithOrContains(const QString &str1, const QString &str2,
Qt::CaseSensitivity caseSensitivity, bool startsWith)
{
if (startsWith)
{
return str1.startsWith(str2, caseSensitivity);
}
return str1.contains(str2, caseSensitivity);
}
bool isNeutral(const QString &s)
{
static const QRegularExpression re("\\p{L}");
const QRegularExpressionMatch match = re.match(s);
return !match.hasMatch();
}
QString generateUuid()
{
auto uuid = QUuid::createUuid();
return uuid.toString();
}
QString formatRichLink(const QString &url, bool file)
{
return QString("<a href=\"") + (file ? "file:///" : "") + url + "\">" +
url + "</a>";
}
QString formatRichNamedLink(const QString &url, const QString &name, bool file)
{
return QString("<a href=\"") + (file ? "file:///" : "") + url + "\">" +
name + "</a>";
}
QString shortenString(const QString &str, unsigned maxWidth)
{
auto shortened = QString(str);
if (str.size() > int(maxWidth))
{
shortened.resize(int(maxWidth));
shortened += "...";
}
return shortened;
}
QString localizeNumbers(const int &number)
{
QLocale locale;
return locale.toString(number);
}
QString kFormatNumbers(const int &number)
{
return QString("%1K").arg(number / 1000);
}
QColor getRandomColor(const QString &userId)
{
bool ok = true;
int colorSeed = userId.toInt(&ok);
if (!ok)
{
// We were unable to convert the user ID to an integer, this means Twitch started to use non-integer user IDs (or we're on IRC)
// Use sum of unicode values of all characters in id / IRC nick
colorSeed = 0;
for (const auto &c : userId)
{
colorSeed += c.digitValue();
}
}
const auto colorIndex = colorSeed % TWITCH_USERNAME_COLORS.size();
return TWITCH_USERNAME_COLORS[colorIndex];
}
QString formatUserMention(const QString &userName, bool isFirstWord,
bool mentionUsersWithComma)
{
QString result = userName;
if (isFirstWord && mentionUsersWithComma)
{
result += ",";
}
return result;
}
int64_t parseDurationToSeconds(const QString &inputString,
uint64_t noUnitMultiplier)
{
if (inputString.length() == 0)
{
return -1;
}
// TODO(QT6): use QStringView
QStringRef input(&inputString);
input = input.trimmed();
uint64_t currentValue = 0;
bool visitingNumber = true; // input must start with a number
int numberStartIdx = 0;
for (int pos = 0; pos < input.length(); pos++)
{
QChar c = input.at(pos);
if (visitingNumber && !c.isDigit())
{
uint64_t parsed =
(uint64_t)input.mid(numberStartIdx, pos - numberStartIdx)
.toUInt();
if (c.isSpace())
{
pos = skipSpace(input, pos) + 1;
if (pos >= input.length())
{
// input like "40 ", this shouldn't happen
// since we trimmed the view
return -1;
}
c = input.at(pos);
}
auto result = findUnitMultiplierToSec(input, pos);
if (!result.second)
{
return -1; // invalid unit or leading spaces (shouldn't happen)
}
currentValue += parsed * result.first;
visitingNumber = false;
}
else if (!visitingNumber && !c.isSpace())
{
if (!c.isDigit())
{
return -1; // expected a digit
}
visitingNumber = true;
numberStartIdx = pos;
}
// else: visitingNumber && isDigit || !visitingNumber && isSpace
}
if (visitingNumber)
{
if (numberStartIdx != 0)
{
return -1; // input like "1w 3s 70", 70 what? apples?
}
currentValue += input.toUInt() * noUnitMultiplier;
}
return (int64_t)currentValue;
}
} // namespace chatterino