1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #pragma once
27 
28 #include "utils_global.h"
29 
30 #include "smallstring.h"
31 
32 #include <vector>
33 
34 #include <QStringList>
35 
36 namespace Utils {
37 
38 template<typename String>
39 class BasicSmallStringVector : public std::vector<String>
40 {
41     using Base = std::vector<String>;
42 
43 public:
44     BasicSmallStringVector() = default;
45 
46     using Base::Base;
47 
BasicSmallStringVector(const Base & stringVector)48     explicit BasicSmallStringVector(const Base &stringVector)
49         : Base(stringVector.begin(), stringVector.end())
50     {
51     }
52 
BasicSmallStringVector(std::initializer_list<String> list)53     BasicSmallStringVector(std::initializer_list<String> list)
54     {
55         Base::reserve(list.size());
56 
57         for (auto &&entry : list)
58             Base::push_back(std::move(entry));
59     }
60 
BasicSmallStringVector(const QStringList & stringList)61     explicit BasicSmallStringVector(const QStringList &stringList)
62     {
63         std::vector<SmallString>::reserve(std::size_t(stringList.count()));
64 
65         for (const QString &string : stringList)
66             Base::push_back(SmallString::fromQString(string));
67     }
68 
BasicSmallStringVector(const std::vector<std::string> & stringVector)69     explicit BasicSmallStringVector(const std::vector<std::string> &stringVector)
70     {
71         Base::reserve(std::size_t(stringVector.size()));
72 
73         for (const std::string &string : stringVector)
74            Base::emplace_back(string);
75     }
76 
77     BasicSmallStringVector(const BasicSmallStringVector &) = default;
78     BasicSmallStringVector &operator=(const BasicSmallStringVector &) = default;
79 
80     BasicSmallStringVector(BasicSmallStringVector &&) noexcept = default;
81     BasicSmallStringVector &operator=(BasicSmallStringVector &&)
82         noexcept(std::is_nothrow_move_assignable<Base>::value) = default;
83 
join(SmallStringView separator)84     SmallString join(SmallStringView separator) const
85     {
86         SmallString joinedString;
87 
88         joinedString.reserve(totalByteSize() + separator.size() * std::size_t(Base::size()));
89 
90         for (auto stringIterator = Base::begin(); stringIterator != Base::end(); ++stringIterator) {
91             joinedString.append(*stringIterator);
92             if (std::next(stringIterator) != Base::end())
93                 joinedString.append(separator);
94         }
95 
96         return joinedString;
97     }
98 
contains(SmallStringView string)99     bool contains(SmallStringView string) const noexcept
100     {
101         return std::find(Base::begin(), Base::end(), string) != Base::end();
102     }
103 
removeFast(SmallStringView valueToBeRemoved)104     bool removeFast(SmallStringView valueToBeRemoved)
105     {
106         auto position = std::remove(Base::begin(), Base::end(), valueToBeRemoved);
107 
108         const bool hasEntry = position != Base::end();
109 
110         erase(position, Base::end());
111 
112         return hasEntry;
113     }
114 
append(String && string)115     void append(String &&string)
116     {
117         push_back(std::move(string));
118     }
119 
clone()120     BasicSmallStringVector clone() const { return *this; }
121 
122     operator std::vector<std::string>() const
123     {
124         return std::vector<std::string>(Base::begin(), Base::end());
125     }
126 
QStringList()127     operator QStringList() const
128     {
129         QStringList qStringList;
130         qStringList.reserve(int(Base::size()));
131 
132         for (const auto &entry : *this)
133             qStringList.push_back(QString(entry));
134 
135         return qStringList;
136     }
137 
138 private:
totalByteSize()139     std::size_t totalByteSize() const
140     {
141         std::size_t totalSize = 0;
142 
143         for (auto &&string : *this)
144             totalSize += string.size();
145 
146         return totalSize;
147     }
148 };
149 
150 
151 
152 using SmallStringVector = BasicSmallStringVector<BasicSmallString<31>>;
153 using PathStringVector = BasicSmallStringVector<BasicSmallString<190>>;
154 using StringViewVector = BasicSmallStringVector<SmallStringView>;
155 } // namespace Utils;
156