sptk2 logo
SPTK Home Page
Strings.h
1/*
2╔══════════════════════════════════════════════════════════════════════════════╗
3║ SIMPLY POWERFUL TOOLKIT (SPTK) ║
4╟──────────────────────────────────────────────────────────────────────────────╢
5║ copyright © 1999-2022 Alexey Parshin. All rights reserved. ║
6║ email alexeyp@gmail.com ║
7╚══════════════════════════════════════════════════════════════════════════════╝
8┌──────────────────────────────────────────────────────────────────────────────┐
9│ This library is free software; you can redistribute it and/or modify it │
10│ under the terms of the GNU Library General Public License as published by │
11│ the Free Software Foundation; either version 2 of the License, or (at your │
12│ option) any later version. │
13│ │
14│ This library is distributed in the hope that it will be useful, but │
15│ WITHOUT ANY WARRANTY; without even the implied warranty of │
16│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library │
17│ General Public License for more details. │
18│ │
19│ You should have received a copy of the GNU Library General Public License │
20│ along with this library; if not, write to the Free Software Foundation, │
21│ Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. │
22│ │
23│ Please report all bugs and problems to alexeyp@gmail.com. │
24└──────────────────────────────────────────────────────────────────────────────┘
25*/
26
27#pragma once
28
29#include <sptk5/String.h>
30
31#ifdef _WIN32
32#include <iterator>
33#endif
34
35namespace sptk {
36
37class Exception;
38
50class SP_EXPORT Strings
51 : public std::vector<String>
52{
53public:
57 enum class SortOrder : uint8_t
58 {
59 UNSORTED,
60 ASCENDING,
61 DESCENDING
62 };
63
67 enum class SplitMode : uint8_t
68 {
72 DELIMITER,
73
77 ANYCHAR,
78
82 REGEXP
83
84 };
85
89 Strings() = default;
90
94 Strings(const Strings& other) = default;
95
99 Strings(Strings&& other) = default;
100
105 Strings(std::initializer_list<String> list)
106 {
107 std::copy(list.begin(), list.end(), back_inserter(*this));
108 }
109
116 Strings(const String& src, const char* delimiter, SplitMode mode = SplitMode::DELIMITER) noexcept;
117
121 virtual ~Strings() = default;
122
129 void fromString(const String& src, const char* delimiter, SplitMode mode);
130
138 virtual int indexOf(const String& needle) const;
139
144 void saveToFile(const fs::path& fileName) const;
145
150 void loadFromFile(const fs::path& fileName);
151
155 int64_t argument() const
156 {
157 return (int) m_userData;
158 }
159
164 void argument(int64_t d)
165 {
166 m_userData = d;
167 }
168
173 iterator remove(size_t i)
174 {
175 return StringVector::erase(begin() + i);
176 }
177
182 iterator remove(const String& str)
183 {
184 if (auto itor = std::find(begin(), end(), str); itor != end())
185 {
186 return StringVector::erase(itor);
187 }
188 return end();
189 }
190
191 Strings& operator=(const Strings& other) = default;
192 Strings& operator=(Strings&& other) = default;
193
198 String join(const String& delimiter) const;
199
204 Strings grep(const String& pattern) const;
205
209 void sort(bool ascending = true);
210
214 void clear()
215 {
216 m_sorted = SortOrder::UNSORTED;
217 StringVector::clear();
218 m_userData = 0;
219 }
220
225 void resize(size_t size)
226 {
227 if (size > this->size())
228 {
229 m_sorted = SortOrder::UNSORTED;
230 }
231 StringVector::resize(size);
232 }
233
237 void push_back(const String& str)
238 {
239 m_sorted = SortOrder::UNSORTED;
240 StringVector::push_back(str);
241 }
242
246 void push_back(String&& str)
247 {
248 m_sorted = SortOrder::UNSORTED;
249 StringVector::push_back(std::move(str));
250 }
251
255 template<typename... Args>
256 void emplace_back(Args&&... args)
257 {
258 m_sorted = SortOrder::UNSORTED;
259 StringVector::emplace_back(args...);
260 }
261
267 String& operator[](size_t index)
268 {
269 m_sorted = SortOrder::UNSORTED;
270 return StringVector::operator[](index);
271 }
272
278 const String& operator[](size_t index) const
279 {
280 return StringVector::operator[](index);
281 }
282
283private:
284 using StringVector = std::vector<String>;
285
289 int64_t m_userData {0};
290
294 SortOrder m_sorted {SortOrder::UNSORTED};
295};
296
300} // namespace sptk
Definition: String.h:49
Definition: Strings.h:52
iterator remove(const String &str)
Definition: Strings.h:182
Strings(const String &src, const char *delimiter, SplitMode mode=SplitMode::DELIMITER) noexcept
void fromString(const String &src, const char *delimiter, SplitMode mode)
String & operator[](size_t index)
Definition: Strings.h:267
Strings grep(const String &pattern) const
virtual int indexOf(const String &needle) const
void clear()
Definition: Strings.h:214
virtual ~Strings()=default
void push_back(const String &str)
Definition: Strings.h:237
Strings(Strings &&other)=default
void saveToFile(const fs::path &fileName) const
String join(const String &delimiter) const
void argument(int64_t d)
Definition: Strings.h:164
void push_back(String &&str)
Definition: Strings.h:246
Strings(std::initializer_list< String > list)
Definition: Strings.h:105
void loadFromFile(const fs::path &fileName)
SplitMode
Definition: Strings.h:68
Strings(const Strings &other)=default
int64_t argument() const
Definition: Strings.h:155
void resize(size_t size)
Definition: Strings.h:225
Strings()=default
void emplace_back(Args &&... args)
Definition: Strings.h:256
SortOrder
Definition: Strings.h:58
void sort(bool ascending=true)
const String & operator[](size_t index) const
Definition: Strings.h:278
iterator remove(size_t i)
Definition: Strings.h:173

Fri Oct 14 2022 09:58:32: SPTK 5.4.1