2016-02-17 16:05:41 +01:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
|
|
|
|
**
|
|
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#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 {
|
|
|
|
|
|
2016-11-01 17:15:17 +01:00
|
|
|
template<uint SmallStringSize>
|
|
|
|
|
class BasicSmallStringVector : public std::vector<BasicSmallString<SmallStringSize>>
|
2016-02-17 16:05:41 +01:00
|
|
|
{
|
2017-01-24 13:11:52 +01:00
|
|
|
using SmallString = BasicSmallString<SmallStringSize>;
|
2016-11-01 17:15:17 +01:00
|
|
|
using Base = std::vector<SmallString>;
|
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-01-05 13:22:58 +01:00
|
|
|
explicit BasicSmallStringVector(const Base &stringVector)
|
|
|
|
|
: Base(stringVector.begin(), stringVector.end())
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-01 17:15:17 +01:00
|
|
|
BasicSmallStringVector(std::initializer_list<SmallString> 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)
|
2016-11-01 17:15:17 +01:00
|
|
|
Base::push_back(entry.clone());
|
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
|
|
|
|
2016-11-01 17:15:17 +01:00
|
|
|
SmallString join(SmallString &&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;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-01 17:15:17 +01:00
|
|
|
void append(SmallString &&string)
|
2016-02-17 16:05:41 +01:00
|
|
|
{
|
|
|
|
|
push_back(std::move(string));
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-01 17:15:17 +01:00
|
|
|
BasicSmallStringVector clone() const
|
2016-02-17 16:05:41 +01:00
|
|
|
{
|
2016-11-01 17:15:17 +01:00
|
|
|
BasicSmallStringVector clonedVector;
|
|
|
|
|
clonedVector.reserve(Base::size());
|
2016-02-17 16:05:41 +01:00
|
|
|
|
|
|
|
|
for (auto &&entry : *this)
|
|
|
|
|
clonedVector.push_back(entry.clone());
|
|
|
|
|
|
|
|
|
|
return clonedVector;
|
|
|
|
|
}
|
|
|
|
|
|
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()));
|
|
|
|
|
|
|
|
|
|
std::copy(Base::begin(), Base::end(), std::back_inserter(qStringList));
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2016-11-01 17:15:17 +01:00
|
|
|
using SmallStringVector = BasicSmallStringVector<31>;
|
2017-01-05 13:30:10 +01:00
|
|
|
using PathStringVector = BasicSmallStringVector<191>;
|
2016-02-17 16:05:41 +01:00
|
|
|
} // namespace Utils;
|