MythTV master
stringutil.h
Go to the documentation of this file.
1#ifndef STRINGUTIL_H_
2#define STRINGUTIL_H_
3
4// The following distributions have a C++20 compiler but don't
5// support std::format:
6//
7// Centos/RHEL 9, Debian 11&12, NetBSD10, Ubuntu 22.04
8#if __has_include(<format>) // C++20
9#include <format>
10#endif
11
12#include <string_view>
13#include <vector>
14
15#include <QByteArray>
16#include <QString>
17
18#include "mythbaseexp.h"
19
20namespace StringUtil
21{
22MBASE_PUBLIC bool isValidUTF8(const QByteArray &data);
23
31inline QString intToPaddedString(int n, int width = 2)
32{
33#ifdef __cpp_lib_format // C++20 with <format>
34 return QString::fromStdString(std::format("{:0{}d}", n, width));
35#else
36 return QString("%1").arg(n, width, 10, QChar('0'));
37#endif // __cpp_lib_format
38}
39
40inline QString indentSpaces(unsigned int level, unsigned int size = 4)
41{
42 return {static_cast<int>(level * size), QChar(' ')};
43}
44
48inline QString bool_to_string(bool val)
49{
50 return val ? QStringLiteral("true") : QStringLiteral("false");
51}
52
54std::strong_ordering naturalCompare(const QString &_a, const QString &_b,
55 Qt::CaseSensitivity caseSensitivity = Qt::CaseSensitive);
56
61inline bool naturalSortCompare(const QString &a, const QString &b,
62 Qt::CaseSensitivity caseSensitivity = Qt::CaseSensitive)
63{
64 // NOLINTNEXTLINE(modernize-use-nullptr)
65 return naturalCompare(a, b, caseSensitivity) < 0;
66}
67
68MBASE_PUBLIC QString formatKBytes(int64_t sizeKB, int prec=1);
69MBASE_PUBLIC QString formatBytes(int64_t sizeB, int prec=1);
70
79inline std::vector<std::string_view> split_sv(const std::string_view s, const std::string_view delimiter)
80{
81 // There are infinitely many empty strings at each position, avoid infinite loop
82 if (delimiter.empty())
83 return {s};
84 std::vector<std::string_view> tokens;
85 size_t last_pos = 0;
86 size_t pos = s.find(delimiter);
87 while (pos != std::string_view::npos)
88 {
89 tokens.emplace_back(s.substr(last_pos, pos - last_pos));
90 last_pos = pos + delimiter.size();
91 pos = s.find(delimiter, last_pos);
92 }
93 tokens.emplace_back(s.substr(last_pos));
94 return tokens;
95}
96
97} // namespace StringUtil
98
99#endif // STRINGUTIL_H_
#define MBASE_PUBLIC
Definition: mythbaseexp.h:8
MBASE_PUBLIC bool isValidUTF8(const QByteArray &data)
Definition: stringutil.cpp:9
std::vector< std::string_view > split_sv(const std::string_view s, const std::string_view delimiter)
Split a std::string_view into a std::vector of std::string_views.
Definition: stringutil.h:79
QString bool_to_string(bool val)
This is equivalent to QVariant(bool).toString()
Definition: stringutil.h:48
MBASE_PUBLIC std::strong_ordering naturalCompare(const QString &_a, const QString &_b, Qt::CaseSensitivity caseSensitivity=Qt::CaseSensitive)
This method chops the input a and b into pieces of digits and non-digits (a1.05 becomes a | 1 | .
Definition: stringutil.cpp:122
bool naturalSortCompare(const QString &a, const QString &b, Qt::CaseSensitivity caseSensitivity=Qt::CaseSensitive)
naturalCompare as a std::sort compatible function (ignoring the third parameter, which is never used)...
Definition: stringutil.h:61
MBASE_PUBLIC QString formatBytes(int64_t sizeB, int prec=1)
Definition: stringutil.cpp:302
QString intToPaddedString(int n, int width=2)
Creates a zero padded string representation of an integer.
Definition: stringutil.h:31
QString indentSpaces(unsigned int level, unsigned int size=4)
Definition: stringutil.h:40
MBASE_PUBLIC QString formatKBytes(int64_t sizeKB, int prec=1)
Definition: stringutil.cpp:281