23 #include "strings.hpp"
27 #include "datastreamoperatorwrapper.h"
28 #include "plugins/engineplugin.h"
29 #include "plugins/pluginloader.h"
34 #include <QDataStream>
37 #include <QStringList>
40 const char Strings::RANDOM_CHAR_POOL[RANDOM_CHAR_POOL_SIZE] =
42 'a',
'b',
'c',
'd',
'e',
'f',
'g',
43 'h',
'i',
'j',
'k',
'l',
'm',
'n',
44 'o',
'p',
'q',
'r',
's',
't',
'u',
45 'v',
'w',
'x',
'y',
'z',
'0',
'1',
46 '2',
'3',
'4',
'5',
'6',
'7',
'8',
52 static const char colorChart[22][7] =
80 for (
int i = 0; i < str.length(); i++)
82 if (str[i] == ESCAPE_COLOR_CHAR)
85 if (i >= str.length())
87 QChar colorChar = str[i].toLower();
88 int color = colorChar.toLatin1() - 97;
92 color = current == 0 ? 19 : current - 1;
93 else if (colorChar ==
'*')
95 else if (colorChar ==
'!')
97 else if (colorChar ==
'[')
99 int end = str.indexOf(
']', i);
102 QString colorName = str.mid(i + 1, end - i - 1);
103 if (colorName.indexOf(
'"') == -1)
104 ret += QString(
"<span style=\"color: " + colorName +
"\">");
105 i += colorName.length() + 1;
109 else if (colorChar ==
'-')
123 if (color >= 0 && color < 22)
125 ret += QString(
"<span style=\"color: #") + colorChart[color] +
"\">";
140 for (
const QString &s : fronts)
149 QString combinedPath;
152 if (pathFront.isEmpty())
157 if (pathEnd.isEmpty())
162 pathFront = Strings::trimr(pathFront,
"/\\");
163 pathEnd = Strings::triml(pathEnd,
"/\\");
165 combinedPath = pathFront +
"/" + pathEnd;
173 QString generatedString =
"";
174 for (
unsigned i = 0; i < numChars; ++i)
177 generatedString += RANDOM_CHAR_POOL[index];
180 return generatedString;
183 QString Strings::createRandomAlphaNumericStringWithNewLines(
unsigned numCharsPerLine,
unsigned numLines)
185 QString generatedString =
"";
186 for (
unsigned i = 0; i < numLines; ++i)
191 return generatedString;
195 static char escapeCharacters[] = {
'\\',
'"', 0};
198 for (
unsigned int i = 0; escapeCharacters[i] != 0; i++)
201 for (
int p = 0; p < str.length() && (p = str.indexOf(escapeCharacters[i], p)) != -1; p += 2)
208 const QString &Strings::unescape(QString &str)
210 for (
unsigned int i = 0; escapeCharacters[i] != 0; i++)
212 QString sequence =
"\\" + QString(escapeCharacters[i]);
213 for (
int p = 0; p < str.length() && (p = str.indexOf(sequence, p)) != -1; p++)
214 str.replace(str.indexOf(sequence, p), 2, escapeCharacters[i]);
223 auto fBytes = (float)bytes;
224 fBytes = scaleDataUnit(fBytes, dataUnit);
226 QString formattedString = QString(
"%1 ").arg(fBytes, 0,
'f', 2);
230 formattedString +=
"B";
234 formattedString +=
"kB";
238 formattedString +=
"MB";
242 formattedString +=
"GB";
247 return "#ERR: Formatting data amount error.";
250 return formattedString;
257 speedInBytesPerSecond = scaleDataUnit(speedInBytesPerSecond, dataUnit);
259 QString formattedString = QString(
"%1 ").arg(speedInBytesPerSecond, 0,
'f', 2);
263 formattedString +=
"B/s";
267 formattedString +=
"kB/s";
271 formattedString +=
"MB/s";
275 formattedString +=
"GB/s";
280 return "#ERR: Formatting speed error.";
283 return formattedString;
290 return "#ERR: Formatting time error.";
293 seconds = ceil(seconds);
300 int remainingSeconds = 0;
302 if (seconds >= 3600.0f)
305 hours = seconds / 3600.0f;
306 seconds -= hours * 3600.0f;
309 if (seconds >= 60.0f)
312 minutes = seconds / 60.0f;
313 seconds -= minutes * 60.0f;
316 remainingSeconds = (int)seconds;
318 QString formattedString;
321 formattedString += QString(
"%1h ").arg(hours);
324 if (hours > 0 || minutes > 0)
326 formattedString += QString(
"%1min. ").arg(minutes);
329 formattedString += QString(
"%1s").arg(remainingSeconds);
331 return formattedString;
334 bool Strings::isCharOnCharList(
char c,
const QString &charList)
336 for (
const auto &candidate : charList)
347 QUrl urlObject = url;
349 QString scheme = urlObject.scheme();
351 bool bIsSafe1 = scheme.isEmpty();
352 bool bIsSafe2 = (scheme.compare(
"http", Qt::CaseInsensitive) == 0);
353 bool bIsSafe3 = (scheme.compare(
"ftp", Qt::CaseInsensitive) == 0);
354 bool bIsSafe4 = (scheme.compare(
"https", Qt::CaseInsensitive) == 0);
357 return bIsSafe1 || bIsSafe2 || bIsSafe3 || bIsSafe4;
362 path = QDir::fromNativeSeparators(path);
363 path = QDir::cleanPath(path);
374 float Strings::scaleDataUnit(
float bytes, DataUnit &outUnit)
376 const static float UPPER_BOUNDARY = 900.0f;
379 while (bytes > UPPER_BOUNDARY && outUnit != Gigabyte)
382 outUnit = (DataUnit)((
int)outUnit + 1);
388 QString Strings::timestamp(
const QString &format)
390 return QDateTime::currentDateTime().toString(format);
396 QStringList addressAndPort = addressString.split(
":");
397 QStringList defaultAddressAndPort = defaultAddress.split(
":");
399 if (addressAndPort.size() >= 1 && addressAndPort.size() <= 2)
401 hostname = addressAndPort[0];
402 if (addressAndPort.size() == 2)
404 port = addressAndPort[1].toUShort();
410 if (defaultAddressAndPort.size() >= 1)
412 hostname = defaultAddressAndPort[0];
416 if (port == 0 && defaultAddressAndPort.size() >= 2)
418 port = defaultAddressAndPort[1].toUShort();
422 QString &Strings::trimr(QString &str,
const QString &charList)
425 for (i = str.length() - 1; i >= 0; --i)
427 if (!isCharOnCharList(str[i].toLatin1(), charList))
432 return str.remove(i, str.length() - i);
435 QString &Strings::triml(QString &str,
const QString &charList)
438 for (i = 0; i < str.length(); ++i)
440 if (!isCharOnCharList(str[i].toLatin1(), charList))
444 return str.remove(0, i);
449 static QString pluginSchemes;
450 if (pluginSchemes.isEmpty())
452 pluginSchemes =
"zds";
454 for (
unsigned int i = 0; i < gPlugins->numPlugins(); ++i)
455 pluginSchemes = QString(
"%1|%2").arg(pluginSchemes)
456 .arg(gPlugins->plugin(i)->info()->data()->scheme);
459 QRegExp pattern(QString(
"("
461 "(http|https|ftp|%1)://"
463 ")[\\w\\-\\.,@?^=%&:/~\\+#\\(\\)]+"
464 ")").arg(pluginSchemes), Qt::CaseInsensitive);
465 QString newString = str;
469 while ((index = pattern.indexIn(newString, offset)) >= 0)
471 QString cap = pattern.cap(1);
472 int capLength = cap.length();
474 QString replacement = cap;
475 if (cap.startsWith(
"www.", Qt::CaseInsensitive))
477 replacement =
"http://" + cap;
480 replacement = QString(
"<a href=\"%1\">%2</a>").arg(replacement, cap);
482 newString.replace(index, capLength, replacement);
483 offset = index + replacement.length();