18#include <QCoreApplication>
19#include <QApplication>
27 for (
int i = 0;
i < len;
i++) {
28 list << QString(arr[
i]);
37err(QString *str,
const QString &errmsg)
52 for (
int i = 0;
i < str.length();
i++) {
54 if (validChars.indexOf(c) < 0) {
65 QString scrubbed = email;
66 scrubbed = scrubbed.replace(
"@",
" at ");
67 scrubbed = scrubbed.replace(
".",
" dot ");
76 const QString &sep,
const QString &le)
79 int pos, nextsep, wordlen, n;
80 int seplen = sep.length();
82 if (str.length() < width) {
88 while (pos < str.length()) {
90 nextsep = str.indexOf(sep, pos);
92 nextsep = str.length();
94 wordlen = nextsep-pos;
104 wrapped.append(str.mid(pos, wordlen+seplen));
105 n = n - wordlen - seplen;
106 pos += wordlen + seplen;
108 return wrapped.trimmed();
118 for (
int i = 0;
i < buf.size();
i++) {
119 hex +=
"0123456789ABCDEF"[((quint8)buf[
i]) >> 4];
120 hex +=
"0123456789ABCDEF"[((quint8)buf[
i]) & 0xf];
137 in = str.toLocal8Bit();
139 for (
int i = 0;
i < in.length();
i++) {
158 if (QChar(c).isPrint() && c < 127) {
162 out.append(QString::number(c, 8).toAscii());
167 return QString::fromAscii(out);
183 if (str.length() < 2)
185 if (! str.startsWith(
"\"") || ! str.endsWith(
"\""))
187 if (str.endsWith(
"\\\"") && ! str.endsWith(
"\\\\\""))
191 while (
i < str.length()-1) {
192 if (str[
i] == QLatin1Char(
'\\')) {
194 if (c == QLatin1Char(
'n')) {
196 }
else if (c == QLatin1Char(
'r')) {
198 }
else if (c == QLatin1Char(
't')) {
200 }
else if (c == QLatin1Char(
'x')) {
201 if (
i + 2 >= str.length())
204 char val =
static_cast<char>(str.mid(
i+1, 2).toUInt(&isHex, 16));
209 }
else if (c.isDigit()) {
210 if (
i + 2 >= str.length())
213 uint val = str.mid(
i, 3).toUInt(&isOctal, 8);
214 if (! isOctal || val > 255)
216 out.append(
static_cast<char>(val));
219 out.append(str[
i].toLatin1());
221 }
else if (str[
i] == QLatin1Char(
'\"')) {
226 out.append(str[
i].toLatin1());
232 return QString::fromLocal8Bit(out.data());
243QHash<QString,QString>
248 QHash<QString,QString> keyvals;
252 while (
i < len && str[
i].isSpace())
257 while (
i < len && !str[
i].isSpace() && str[
i] !=
'=')
258 key.append(str[
i++]);
260 if (
i < len && str[
i] ==
'=') {
261 if (++
i < len && str[
i] ==
'\"') {
266 if (str[
i] ==
'\\') {
270 }
else if (str[
i] ==
'\"') {
278 keyvals.insert(key, val);
281 while (
i < len && !str[
i].isSpace())
282 val.append(str[
i++]);
283 keyvals.insert(key, val);
287 keyvals.insert(key, QString(
""));
289 while (
i < len && str[
i].isSpace())
299 return QHash<QString,QString>();
313 while (
i < len && str[
i].isSpace())
318 if (str[
i] ==
'\"') {
323 if (str[
i] ==
'\\') {
327 }
else if (str[
i] ==
'\"') {
338 while (
i < len && !str[
i].isSpace())
339 arg.append(str[
i++]);
342 while (
i < len && str[
i].isSpace())
353 return QStringList();
363 foreach (QString arg, args) {
364 if (arg.contains(
"\"") || arg.contains(
"\\") || arg.contains(
" "))
369 return out.join(
" ");
377 for (
int i = 0;
i < str.length();
i++) {
378 char c = str[
i].toUpper().toAscii();
379 if ((c < 'A' || c >
'F') && (c < '0' || c >
'9'))
391 int secs = (seconds % 60);
392 int mins = (seconds / 60 % 60);
393 int hours = (seconds / 3600 % 24);
394 int days = (seconds / 86400);
397 uptime += qApp->translate(
"stringutil.h",
"%1 days ").arg(days);
399 uptime += qApp->translate(
"stringutil.h",
"%1 hours ").arg(hours);
401 uptime += qApp->translate(
"stringutil.h",
"%1 mins ").arg(mins);
403 uptime += qApp->translate(
"stringutil.h",
"%1 secs").arg(secs);
413 return date.toString(
"yyyy-MM-dd HH:mm:ss");
422 return qApp->translate(
"stringutil.h",
"%1 B/s").arg(bytes);
424 return qApp->translate(
"stringutil.h",
"%1 KB/s").arg(bytes/1024.0, 0,
'f', 2);
425 if (bytes < 1073741824)
426 return qApp->translate(
"stringutil.h",
"%1 MB/s").arg(bytes/1048576.0, 0,
'f', 2);
428 return qApp->translate(
"stringutil.h",
"%1 GB/s").arg(bytes/1073741824.0, 0,
'f', 2);
DebugMessage error(const QString &fmt)
QString string_escape(const QString &str)
QString string_format_bandwidth(quint64 bytes)
QString ensure_valid_chars(const QString &str, const QString &validChars)
bool string_is_hex(const QString &str)
QString string_format_datetime(const QDateTime &date)
QString base16_encode(const QByteArray &buf)
QString string_unescape(const QString &str, bool *ok)
QString string_wrap(const QString &str, int width, const QString &sep, const QString &le)
bool err(QString *str, const QString &errmsg)
QHash< QString, QString > string_parse_keyvals(const QString &str, bool *ok)
QString string_format_arguments(const QStringList &args)
QString scrub_email_addr(const QString &email)
QString string_format_uptime(quint64 seconds)
QStringList char_array_to_stringlist(char **arr, int len)
QStringList string_parse_arguments(const QString &str, bool *ok)