Gamedev Framework (gf) 1.2.0
A C++17 framework for 2D games
StringUtils.h
1/*
2 * Gamedev Framework (gf)
3 * Copyright (C) 2016-2022 Julien Bernard
4 *
5 * This software is provided 'as-is', without any express or implied
6 * warranty. In no event will the authors be held liable for any damages
7 * arising from the use of this software.
8 *
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, subject to the following restrictions:
12 *
13 * 1. The origin of this software must not be misrepresented; you must not
14 * claim that you wrote the original software. If you use this software
15 * in a product, an acknowledgment in the product documentation would be
16 * appreciated but is not required.
17 * 2. Altered source versions must be plainly marked as such, and must not be
18 * misrepresented as being the original software.
19 * 3. This notice may not be removed or altered from any source distribution.
20 */
21#ifndef GF_STRING_UTILS_H
22#define GF_STRING_UTILS_H
23
24#include <cassert>
25#include <cstdarg>
26
27#include <vector>
28#include <string>
29#include <string_view>
30
31#include "CoreApi.h"
32#include "Portability.h"
33
34namespace gf {
35#ifndef DOXYGEN_SHOULD_SKIP_THIS
36inline namespace v1 {
37#endif
38
49 GF_CORE_API std::string niceNum(float num, float precision);
50
57 GF_CORE_API std::string formatString(const char *fmt, ...) GF_FORMAT(1, 2);
58
66 GF_CORE_API std::string formatString(const char *fmt, va_list ap);
67
79 GF_CORE_API std::string escapeString(std::string_view str);
80
90 GF_CORE_API std::vector<std::string_view> splitInParagraphs(std::string_view str);
91
101 GF_CORE_API std::vector<std::string_view> splitInWords(std::string_view str);
102
103
110 struct GF_CORE_API CodepointRange {
111 std::string_view ref;
112
117 struct Iterator {
118 using difference_type = std::ptrdiff_t;
119 using value_type = char32_t;
122 using iterator_category = std::forward_iterator_tag;
123
127 void swap(Iterator& other) noexcept {
128 std::swap(current, other.current);
129 }
130
136 constexpr reference operator*() const noexcept {
137 return decode();
138 }
139
145 constexpr pointer operator->() const noexcept {
146 return decode();
147 }
148
154 constexpr Iterator& operator++() noexcept {
155 step();
156 return *this;
157 }
158
164 constexpr Iterator operator++(int) noexcept {
165 Iterator copy = *this;
166 step();
167 return copy;
168 }
169
176 constexpr bool operator!=(const Iterator& other) const noexcept {
177 return current != other.current;
178 }
179
186 constexpr bool operator==(const Iterator& other) const noexcept {
187 return current == other.current;
188 }
189
190 const char *current;
191
192 private:
193 constexpr char32_t decode() const noexcept {
194 char32_t codepoint = 0;
195 uint8_t c = current[0];
196
197 if ((c & 0b10000000) == 0b00000000) {
198 codepoint = c & 0b01111111;
199 } else if ((c & 0b11100000) == 0b11000000) {
200 codepoint = c & 0b00011111;
201 codepoint = (codepoint << 6) + (current[1] & 0b00111111);
202 } else if ((c & 0b11110000) == 0b11100000) {
203 codepoint = c & 0b00001111;
204 codepoint = (codepoint << 6) + (current[1] & 0b00111111);
205 codepoint = (codepoint << 6) + (current[2] & 0b00111111);
206 } else {
207 assert((c & 0b11111000) == 0b11110000);
208 codepoint = c & 0b00000111;
209 codepoint = (codepoint << 6) + (current[1] & 0b00111111);
210 codepoint = (codepoint << 6) + (current[2] & 0b00111111);
211 codepoint = (codepoint << 6) + (current[3] & 0b00111111);
212 }
213
214 return codepoint;
215 }
216
217 constexpr void step() noexcept {
218 uint8_t c = current[0];
219
220 if ((c & 0b10000000) == 0b00000000) {
221 current += 1;
222 } else if ((c & 0b11100000) == 0b11000000) {
223 current += 2;
224 } else if ((c & 0b11110000) == 0b11100000) {
225 current += 3;
226 } else {
227 assert((c & 0b11111000) == 0b11110000);
228 current += 4;
229 }
230 }
231 };
232
233 Iterator begin() const noexcept {
234 return Iterator{ ref.data() };
235 }
236
237 Iterator end() const noexcept {
238 return Iterator{ ref.data() + ref.size() };
239 }
240
241 };
242
247 inline
248 constexpr CodepointRange codepoints(std::string_view ref) {
249 return CodepointRange{ ref };
250 }
251
252#ifndef DOXYGEN_SHOULD_SKIP_THIS
253}
254#endif
255}
256
257#endif // GF_STRING_UTILS_H
GF_CORE_API std::string niceNum(float num, float precision)
Create a string representation of a floating point number.
GF_CORE_API std::vector< std::string_view > splitInWords(std::string_view str)
Split a string in multiples words.
GF_CORE_API std::string escapeString(std::string_view str)
Escape a string.
GF_CORE_API std::vector< std::string_view > splitInParagraphs(std::string_view str)
Split a string in multiples paragraphs.
GF_CORE_API std::string formatString(const char *fmt,...)
Format a string like printf.
The namespace for gf classes.
Iterator for a range of codepoints.
Definition: StringUtils.h:117
constexpr pointer operator->() const noexcept
Pointer operator.
Definition: StringUtils.h:145
value_type reference
Definition: StringUtils.h:121
char32_t value_type
Definition: StringUtils.h:119
constexpr bool operator!=(const Iterator &other) const noexcept
Inequality operator.
Definition: StringUtils.h:176
const char * current
Definition: StringUtils.h:190
std::ptrdiff_t difference_type
Definition: StringUtils.h:118
constexpr Iterator & operator++() noexcept
Increment operator (prefix)
Definition: StringUtils.h:154
std::forward_iterator_tag iterator_category
Definition: StringUtils.h:122
constexpr Iterator operator++(int) noexcept
Increment operator (postfix)
Definition: StringUtils.h:164
void swap(Iterator &other) noexcept
Swap the iterator with another iterator.
Definition: StringUtils.h:127
constexpr bool operator==(const Iterator &other) const noexcept
Equality operator.
Definition: StringUtils.h:186
constexpr reference operator*() const noexcept
Dereference operator.
Definition: StringUtils.h:136
value_type pointer
Definition: StringUtils.h:120
A range over a sequence of codepoints in UTF-8.
Definition: StringUtils.h:110
Iterator begin() const noexcept
Definition: StringUtils.h:233
constexpr CodepointRange codepoints(std::string_view ref)
Create a range over codepoints from a string.
Definition: StringUtils.h:248
std::string_view ref
Definition: StringUtils.h:111
Iterator end() const noexcept
Definition: StringUtils.h:237