Sese Framework  2.3.0
A cross-platform framework
Loading...
Searching...
No Matches
SString.h
Go to the documentation of this file.
1// Copyright 2024 libsese
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
20
21#pragma once
22
23#include <sese/Config.h>
24
25#include <vector>
26
27namespace sstr {
28
30struct SChar final {
31 uint32_t code = 0;
32
33 explicit SChar(uint32_t code) noexcept;
34
35 bool operator==(const SChar &ch) const;
36 bool operator<(const SChar &ch) const;
37
38 bool operator<=(const SChar &ch) const;
39 bool operator>(const SChar &ch) const;
40 bool operator>=(const SChar &ch) const;
41 bool operator!=(const SChar &ch) const;
42
43 SChar operator+(const SChar &ch) const;
44 SChar operator-(const SChar &ch) const;
45
46 explicit operator uint32_t() const;
47};
48
52 SChar getUnicodeFromUTF8Char(const char *u8char);
53
57 size_t getStringLengthFromUTF8String(const char *str);
58
62 size_t getByteLengthFromUTF8String(const char *str);
63
67 char getSizeFromUTF8Char(char ch);
68
73
78 SChar getUnicodeCharFromUTF8Char(char size, const char *ch);
79
80class SString;
81
84public:
85 SStringView() noexcept = default;
86 explicit SStringView(const char *u8str) noexcept;
87 virtual ~SStringView() = default;
88
89public:
91 class Iterator {
92 public:
95 using pointer = const SChar *;
96 using reference = const SChar &;
97 using iterator_category = std::forward_iterator_tag;
98
99 Iterator(const char *ref, size_t size, size_t pos = 0);
100
101 Iterator operator++();
102 // Iterator operator++(int c);
103
104 bool operator==(const Iterator &other) const;
105 bool operator!=(const Iterator &other) const;
106 SChar operator*();
107
108 private:
109 Iterator() = default;
110
111 const char *_ref = nullptr;
112 size_t _pos = 0;
113 size_t _size = 0;
114 SChar _ch = SChar(0);
115 };
116
117public:
118 Iterator begin();
119 Iterator end();
120
124 [[nodiscard]] bool null() const;
125
129 [[nodiscard]] bool empty() const;
130
133 [[nodiscard]] size_t len() const;
134
137 [[nodiscard]] virtual size_t size() const;
138
141 [[nodiscard]] const char *data() const;
142
146 [[nodiscard]] int32_t find(const SStringView &str) const;
147
151 int32_t find(const char *u8str) const;
152
156 int32_t findByBytes(const char *bytes) const;
157
161 [[nodiscard]] SString trim() const;
162
165 [[nodiscard]] SString reverse() const;
166
170 [[nodiscard]] SString append(const SStringView &str) const;
171
176 SString append(const char *u8str) const;
177
181 [[nodiscard]] std::vector<SString> split(const SStringView &str) const;
182
186 std::vector<SString> split(const char *str) const;
187
191 [[nodiscard]] SString substring(size_t begin) const;
196 [[nodiscard]] SString substring(size_t begin, size_t len) const;
197
201 [[nodiscard]] bool endsWith(const SStringView &suffix) const;
202
206 [[nodiscard]] bool endsWith(const std::string_view &suffix) const;
207
211 [[nodiscard]] bool startsWith(const SStringView &prefix) const;
212
216 [[nodiscard]] bool startsWith(const std::string_view &prefix) const;
217
219 [[nodiscard]] bool isLower() const;
221 [[nodiscard]] bool isUpper() const;
222
224 [[nodiscard]] SString toLower() const;
226 [[nodiscard]] SString toUpper() const;
227
228 [[nodiscard]] SChar at(size_t index) const;
229 [[nodiscard]] std::vector<SChar> toChars() const;
230 [[nodiscard]] std::string toString() const;
231 [[nodiscard]] std::wstring toWString() const;
232 [[nodiscard]] std::unique_ptr<wchar_t[]> toCWString() const;
233
234public:
235 SChar operator[](size_t index) const;
236 bool operator!=(const SStringView &str) const;
237 bool operator!=(const char *u8str) const;
238 bool operator==(const SStringView &str) const;
239 bool operator==(const char *u8str) const;
240 SString operator+(const SStringView &str) const;
241 SString operator+(const char *u8str) const;
242
243protected:
244 char *_data = nullptr;
245 size_t _size = 0;
246};
247
249class SString final : public SStringView {
250public:
251 friend class SStringView;
252
253 explicit SString() noexcept;
254 SString(const char *str, size_t size);
255 SString(const SString &s_string) noexcept;
256 SString(SString &&s_string) noexcept;
257 ~SString() noexcept override;
258
259 static SString fromSChars(SChar ch[], size_t size);
260 static SString fromSChars(std::vector<SChar> &chars);
261 static SString fromUTF8(const char *str);
262 static SString fromUCS2LE(const wchar_t *str);
263
264public:
267 [[nodiscard]] size_t cap() const;
270 [[nodiscard]] size_t size() const override;
271
273 void toLower();
275 void toUpper();
276
279 char *data();
280
281public:
282 void operator+=(const SStringView &str);
283 void operator+=(const char *u8str);
284
285protected:
286 size_t _capacity = 0;
287};
288} // namespace sstr