strings.hpp
1 //------------------------------------------------------------------------------
2 // strings.hpp
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301 USA
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2010 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #ifndef __STRINGS_HPP_
24 #define __STRINGS_HPP_
25 // Why hpp in this case? Glibc 2.26 changed string.h to include its strings.h
26 // so this file would shadow that header and cause errors.
27 
28 #include "global.h"
29 
30 #include <QtContainerFwd>
31 
32 class QString;
33 
34 // Keep compatibility with older Qt releases while using identifiers Qt 6 requires
35 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
36 #include <QString>
37 namespace Qt
38 {
39  constexpr auto KeepEmptyParts = QString::KeepEmptyParts;
40  constexpr auto SkipEmptyParts = QString::SkipEmptyParts;
41 }
42 #endif
43 
44 // \c = '\034'
45 #define ESCAPE_COLOR_CHAR '\034'
46 
50 class MAIN_EXPORT Strings
51 {
52  public:
57  static QString colorizeString(const QString &str, int def=4);
58 
65  static QStringList combineManyPaths(const QStringList &fronts, const QString &pathEnd);
66 
84  static QString combinePaths(QString pathFront, QString pathEnd);
85 
91  static QString createRandomAlphaNumericString(unsigned numChars);
92  static QString createRandomAlphaNumericStringWithNewLines(unsigned numCharsPerLine, unsigned numLines);
93 
97  static const QString &escape(QString &str);
98 
105  static QString formatDataAmount(qint64 bytes);
106 
116  static QString formatDataSpeed(float speedInBytesPerSecond);
117 
129  static QString formatTime(float seconds);
130 
131  static bool isCharOnCharList(char c, const QString& charList);
132 
140  static QString normalizePath(QString path);
141 
145  static QByteArray readUntilByte(QDataStream& stream, unsigned char stopByte);
146 
156  static void translateServerAddress(const QString& addressString, QString& hostname, unsigned short& port, const QString& defaultAddress);
157 
158  static const QString &unescape(QString &str);
159 
167  static bool isUrlSafe(const QString& url);
168 
169  static QString timestamp(const QString& format);
170  static QString& trim(QString& str, const QString& charList) { return trimr(triml(str, charList), charList); }
171  static QString& trimr(QString& str, const QString& charList);
172  static QString& triml(QString& str, const QString& charList);
173 
185  static QString wrapUrlsWithHtmlATags(const QString& str);
186 
187  protected:
188  enum DataUnit
189  {
190  Byte = 0,
191  Kilobyte = 1,
192  Megabyte = 2,
193  Gigabyte = 3
194  };
195 
196  static const unsigned RANDOM_CHAR_POOL_SIZE = 36;
197  static const char RANDOM_CHAR_POOL[RANDOM_CHAR_POOL_SIZE];
198 
199 
200  static float scaleDataUnit(float bytes, DataUnit& outUnit);
201 };
202 
203 #endif