2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2016-02-17 16:05:41 +01:00
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include "utils_global.h"
|
|
|
|
|
|
|
|
|
|
#include "smallstring.h"
|
|
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
2016-08-08 16:27:07 +02:00
|
|
|
#include <QStringList>
|
|
|
|
|
|
2016-02-17 16:05:41 +01:00
|
|
|
namespace Utils {
|
|
|
|
|
|
2017-07-31 17:26:46 +02:00
|
|
|
template<typename String>
|
|
|
|
|
class BasicSmallStringVector : public std::vector<String>
|
2016-02-17 16:05:41 +01:00
|
|
|
{
|
2017-07-31 17:26:46 +02:00
|
|
|
using Base = std::vector<String>;
|
|
|
|
|
|
2016-02-17 16:05:41 +01:00
|
|
|
public:
|
2016-11-01 17:15:17 +01:00
|
|
|
BasicSmallStringVector() = default;
|
2016-02-17 16:05:41 +01:00
|
|
|
|
2017-07-31 17:26:46 +02:00
|
|
|
using Base::Base;
|
|
|
|
|
|
2017-01-05 13:22:58 +01:00
|
|
|
explicit BasicSmallStringVector(const Base &stringVector)
|
|
|
|
|
: Base(stringVector.begin(), stringVector.end())
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-31 17:26:46 +02:00
|
|
|
BasicSmallStringVector(std::initializer_list<String> list)
|
2016-02-17 16:05:41 +01:00
|
|
|
{
|
2016-11-01 17:15:17 +01:00
|
|
|
Base::reserve(list.size());
|
2016-02-17 16:05:41 +01:00
|
|
|
|
|
|
|
|
for (auto &&entry : list)
|
2017-07-31 17:26:46 +02:00
|
|
|
Base::push_back(std::move(entry));
|
2016-02-17 16:05:41 +01:00
|
|
|
}
|
|
|
|
|
|
2016-11-01 17:15:17 +01:00
|
|
|
explicit BasicSmallStringVector(const QStringList &stringList)
|
2016-02-17 16:05:41 +01:00
|
|
|
{
|
2016-11-01 17:15:17 +01:00
|
|
|
std::vector<SmallString>::reserve(std::size_t(stringList.count()));
|
2016-02-17 16:05:41 +01:00
|
|
|
|
|
|
|
|
for (const QString &string : stringList)
|
2016-11-01 17:15:17 +01:00
|
|
|
Base::push_back(SmallString::fromQString(string));
|
2016-02-17 16:05:41 +01:00
|
|
|
}
|
|
|
|
|
|
2016-11-01 17:15:17 +01:00
|
|
|
explicit BasicSmallStringVector(const std::vector<std::string> &stringVector)
|
2016-10-18 18:23:05 +02:00
|
|
|
{
|
2016-11-01 17:15:17 +01:00
|
|
|
Base::reserve(std::size_t(stringVector.size()));
|
2016-10-18 18:23:05 +02:00
|
|
|
|
|
|
|
|
for (const std::string &string : stringVector)
|
2016-11-01 17:15:17 +01:00
|
|
|
Base::emplace_back(string);
|
2016-10-18 18:23:05 +02:00
|
|
|
}
|
|
|
|
|
|
2016-11-01 17:15:17 +01:00
|
|
|
BasicSmallStringVector(const BasicSmallStringVector &) = default;
|
|
|
|
|
BasicSmallStringVector &operator=(const BasicSmallStringVector &) = default;
|
2016-02-17 16:05:41 +01:00
|
|
|
|
2016-11-01 17:15:17 +01:00
|
|
|
BasicSmallStringVector(BasicSmallStringVector &&) noexcept = default;
|
2017-01-24 13:21:05 +01:00
|
|
|
BasicSmallStringVector &operator=(BasicSmallStringVector &&)
|
|
|
|
|
noexcept(std::is_nothrow_move_assignable<Base>::value) = default;
|
2016-02-17 16:05:41 +01:00
|
|
|
|
2017-07-31 17:26:46 +02:00
|
|
|
SmallString join(SmallStringView separator) const
|
2016-02-17 16:05:41 +01:00
|
|
|
{
|
2016-11-01 17:15:17 +01:00
|
|
|
SmallString joinedString;
|
2016-02-17 16:05:41 +01:00
|
|
|
|
2016-11-01 17:15:17 +01:00
|
|
|
joinedString.reserve(totalByteSize() + separator.size() * std::size_t(Base::size()));
|
2016-02-17 16:05:41 +01:00
|
|
|
|
2016-11-01 17:15:17 +01:00
|
|
|
for (auto stringIterator = Base::begin(); stringIterator != Base::end(); ++stringIterator) {
|
2016-02-17 16:05:41 +01:00
|
|
|
joinedString.append(*stringIterator);
|
2016-11-01 17:15:17 +01:00
|
|
|
if (std::next(stringIterator) != Base::end())
|
2016-02-17 16:05:41 +01:00
|
|
|
joinedString.append(separator);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return joinedString;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-01 17:15:17 +01:00
|
|
|
bool contains(SmallStringView string) const noexcept
|
2016-02-17 16:05:41 +01:00
|
|
|
{
|
2016-11-01 17:15:17 +01:00
|
|
|
return std::find(Base::begin(), Base::end(), string) != Base::end();
|
2016-02-17 16:05:41 +01:00
|
|
|
}
|
|
|
|
|
|
2016-11-01 17:15:17 +01:00
|
|
|
bool removeFast(SmallStringView valueToBeRemoved)
|
2016-02-17 16:05:41 +01:00
|
|
|
{
|
2016-11-01 17:15:17 +01:00
|
|
|
auto position = std::remove(Base::begin(), Base::end(), valueToBeRemoved);
|
2016-02-17 16:05:41 +01:00
|
|
|
|
2016-11-01 17:15:17 +01:00
|
|
|
const bool hasEntry = position != Base::end();
|
2016-02-17 16:05:41 +01:00
|
|
|
|
2016-11-01 17:15:17 +01:00
|
|
|
erase(position, Base::end());
|
2016-02-17 16:05:41 +01:00
|
|
|
|
|
|
|
|
return hasEntry;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-31 17:26:46 +02:00
|
|
|
void append(String &&string)
|
2016-02-17 16:05:41 +01:00
|
|
|
{
|
|
|
|
|
push_back(std::move(string));
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-31 16:54:39 +02:00
|
|
|
BasicSmallStringVector clone() const { return *this; }
|
2016-02-17 16:05:41 +01:00
|
|
|
|
2016-08-01 17:21:07 +02:00
|
|
|
operator std::vector<std::string>() const
|
|
|
|
|
{
|
2016-11-01 17:15:17 +01:00
|
|
|
return std::vector<std::string>(Base::begin(), Base::end());
|
2016-08-01 17:21:07 +02:00
|
|
|
}
|
|
|
|
|
|
2017-01-05 13:24:16 +01:00
|
|
|
operator QStringList() const
|
|
|
|
|
{
|
|
|
|
|
QStringList qStringList;
|
|
|
|
|
qStringList.reserve(int(Base::size()));
|
|
|
|
|
|
2017-08-29 12:54:10 +02:00
|
|
|
for (const auto &entry : *this)
|
|
|
|
|
qStringList.push_back(QString(entry));
|
2017-01-05 13:24:16 +01:00
|
|
|
|
|
|
|
|
return qStringList;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 16:05:41 +01:00
|
|
|
private:
|
|
|
|
|
std::size_t totalByteSize() const
|
|
|
|
|
{
|
|
|
|
|
std::size_t totalSize = 0;
|
|
|
|
|
|
|
|
|
|
for (auto &&string : *this)
|
|
|
|
|
totalSize += string.size();
|
|
|
|
|
|
|
|
|
|
return totalSize;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2017-07-31 17:26:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
using SmallStringVector = BasicSmallStringVector<BasicSmallString<31>>;
|
|
|
|
|
using PathStringVector = BasicSmallStringVector<BasicSmallString<190>>;
|
|
|
|
|
using StringViewVector = BasicSmallStringVector<SmallStringView>;
|
2016-02-17 16:05:41 +01:00
|
|
|
} // namespace Utils;
|