Luanti 5.11.0-dev
 
Loading...
Searching...
No Matches
string.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
8#include "config.h" // IS_CLIENT_BUILD
9#if IS_CLIENT_BUILD
10#include "irrString.h"
11#endif
12#include <cstdlib>
13#include <string>
14#include <string_view>
15#include <cstring>
16#include <vector>
17#include <limits>
18#include <sstream>
19#include <iomanip>
20#include <cctype>
21#include <cwctype>
22#include <unordered_map>
23#include <optional>
24
25class Translations;
26
27#define STRINGIFY(x) #x
28#define TOSTRING(x) STRINGIFY(x)
29
30// Checks whether a value is an ASCII printable character
31#define IS_ASCII_PRINTABLE_CHAR(x) \
32 (((unsigned int)(x) >= 0x20) && \
33 ( (unsigned int)(x) <= 0x7e))
34
35// Checks whether a value is in a Unicode private use area
36#define IS_PRIVATE_USE_CHAR16(x) \
37 ((wchar_t)(x) >= 0xE000 && \
38 (wchar_t)(x) <= 0xF8FF)
39#define IS_PRIVATE_USE_CHAR32(x) \
40 (((wchar_t)(x) >= 0xF0000 && \
41 (wchar_t)(x) <= 0xFFFFD) || \
42 ((wchar_t)(x) >= 0x100000 && \
43 (wchar_t)(x) <= 0x10FFFD))
44#if WCHAR_MAX > 0xFFFF
45#define IS_PRIVATE_USE_CHAR(x) (IS_PRIVATE_USE_CHAR16(x) || IS_PRIVATE_USE_CHAR32(x))
46#else
47#define IS_PRIVATE_USE_CHAR(x) IS_PRIVATE_USE_CHAR16(x)
48#endif
49
50// Checks whether a byte is an inner byte for an utf-8 multibyte sequence
51#define IS_UTF8_MULTB_INNER(x) \
52 (((unsigned char)(x) >= 0x80) && \
53 ( (unsigned char)(x) <= 0xbf))
54
55// Checks whether a byte is a start byte for an utf-8 multibyte sequence
56#define IS_UTF8_MULTB_START(x) \
57 (((unsigned char)(x) >= 0xc2) && \
58 ( (unsigned char)(x) <= 0xf4))
59
60// Given a start byte x for an utf-8 multibyte sequence
61// it gives the length of the whole sequence in bytes.
62#define UTF8_MULTB_START_LEN(x) \
63 (((unsigned char)(x) < 0xe0) ? 2 : \
64 (((unsigned char)(x) < 0xf0) ? 3 : 4))
65
66typedef std::unordered_map<std::string, std::string> StringMap;
67
68struct FlagDesc {
69 const char *name;
70 u32 flag;
71};
72
73// Try to avoid converting between wide and UTF-8 unless you need to
74// input/output stuff via Irrlicht
75std::wstring utf8_to_wide(std::string_view input);
76std::string wide_to_utf8(std::wstring_view input);
77
78void wide_add_codepoint(std::wstring &result, char32_t codepoint);
79
80std::string urlencode(std::string_view str);
81std::string urldecode(std::string_view str);
82
83u32 readFlagString(std::string str, const FlagDesc *flagdesc, u32 *flagmask);
84std::string writeFlagString(u32 flags, const FlagDesc *flagdesc, u32 flagmask);
85
86size_t mystrlcpy(char *dst, const char *src, size_t size) noexcept;
87char *mystrtok_r(char *s, const char *sep, char **lasts) noexcept;
88
89u64 read_seed(const char *str);
90bool parseColorString(const std::string &value, video::SColor &color, bool quiet,
91 unsigned char default_alpha = 0xff);
92std::string encodeHexColorString(video::SColor color);
93
99inline char my_tolower(char c)
100{
101 // By design this function cannot handle any Unicode (codepoints don't fit into char),
102 // but make sure to pass it through unchanged.
103 // tolower() can mangle it if the POSIX locale is not UTF-8.
104 if (static_cast<unsigned char>(c) > 0x7f)
105 return c;
106 // toupper(3): "If the argument c is of type char, it must be cast to unsigned char"
107 return tolower(static_cast<unsigned char>(c));
108}
109
115inline std::string padStringRight(std::string str, size_t len)
116{
117 if (len > str.size())
118 str.insert(str.end(), len - str.size(), ' ');
119
120 return str;
121}
122
134inline std::string_view removeStringEnd(std::string_view str,
135 const char *ends[])
136{
137 const char **p = ends;
138
139 for (; *p && (*p)[0] != '\0'; p++) {
140 std::string_view end(*p);
141 if (str.size() < end.size())
142 continue;
143 if (str.compare(str.size() - end.size(), end.size(), end) == 0)
144 return str.substr(0, str.size() - end.size());
145 }
146
147 return std::string_view();
148}
149
150
151#define MAKE_VARIANT(_name, _t0, _t1) \
152 template <typename T, typename... Args> \
153 inline auto _name(_t0 arg1, _t1 arg2, Args&&... args) \
154 { \
155 return (_name)(std::basic_string_view<T>(arg1), std::basic_string_view<T>(arg2), \
156 std::forward<Args>(args)...); \
157 }
158
159
169template <typename T>
170inline bool str_equal(std::basic_string_view<T> s1,
171 std::basic_string_view<T> s2,
172 bool case_insensitive = false)
173{
174 if (!case_insensitive)
175 return s1 == s2;
176
177 if (s1.size() != s2.size())
178 return false;
179
180 for (size_t i = 0; i < s1.size(); ++i)
181 if (my_tolower(s1[i]) != my_tolower(s2[i]))
182 return false;
183
184 return true;
185}
186
187// For some reason an std::string will not implicitly get converted
188// to an std::basic_string_view<char> in the template case above, so we need
189// these three wrappers. It works if you take out the template parameters.
190// see also <https://stackoverflow.com/questions/68380141/>
191MAKE_VARIANT(str_equal, const std::basic_string<T> &, const std::basic_string<T> &)
192
193MAKE_VARIANT(str_equal, std::basic_string_view<T>, const std::basic_string<T> &)
194
195MAKE_VARIANT(str_equal, const std::basic_string<T> &, std::basic_string_view<T>)
196
197
198
208template <typename T>
209inline bool str_starts_with(std::basic_string_view<T> str,
210 std::basic_string_view<T> prefix,
211 bool case_insensitive = false)
212{
213 if (str.size() < prefix.size())
214 return false;
215
216 if (!case_insensitive)
217 return str.compare(0, prefix.size(), prefix) == 0;
218
219 for (size_t i = 0; i < prefix.size(); ++i)
220 if (my_tolower(str[i]) != my_tolower(prefix[i]))
221 return false;
222 return true;
223}
224
225// (same conversion issue here)
226MAKE_VARIANT(str_starts_with, const std::basic_string<T> &, const std::basic_string<T> &)
227
228MAKE_VARIANT(str_starts_with, std::basic_string_view<T>, const std::basic_string<T> &)
229
230MAKE_VARIANT(str_starts_with, const std::basic_string<T> &, std::basic_string_view<T>)
231
232// (the same but with char pointers, only for the prefix argument)
233MAKE_VARIANT(str_starts_with, const std::basic_string<T> &, const T*)
234
235MAKE_VARIANT(str_starts_with, std::basic_string_view<T>, const T*)
236
237
238
248template <typename T>
249inline bool str_ends_with(std::basic_string_view<T> str,
250 std::basic_string_view<T> suffix,
251 bool case_insensitive = false)
252{
253 if (str.size() < suffix.size())
254 return false;
255
256 size_t start = str.size() - suffix.size();
257 if (!case_insensitive)
258 return str.compare(start, suffix.size(), suffix) == 0;
259
260 for (size_t i = 0; i < suffix.size(); ++i)
261 if (my_tolower(str[start + i]) != my_tolower(suffix[i]))
262 return false;
263 return true;
264}
265
266// (same conversion issue here)
267MAKE_VARIANT(str_ends_with, const std::basic_string<T> &, const std::basic_string<T> &)
268
269MAKE_VARIANT(str_ends_with, std::basic_string_view<T>, const std::basic_string<T> &)
270
271MAKE_VARIANT(str_ends_with, const std::basic_string<T> &, std::basic_string_view<T>)
272
273// (the same but with char pointers, only for the suffix argument)
274MAKE_VARIANT(str_ends_with, const std::basic_string<T> &, const T*)
275
276MAKE_VARIANT(str_ends_with, std::basic_string_view<T>, const T*)
277
278
279#undef MAKE_VARIANT
280
281
288template <typename T>
289inline std::vector<std::basic_string<T> > str_split(
290 const std::basic_string<T> &str,
291 T delimiter)
292{
293 std::vector<std::basic_string<T> > parts;
294 std::basic_stringstream<T> sstr(str);
295 std::basic_string<T> part;
296
297 while (std::getline(sstr, part, delimiter))
298 parts.push_back(part);
299
300 return parts;
301}
302
303
308inline std::string lowercase(std::string_view str)
309{
310 std::string s2;
311 s2.resize(str.size());
312 for (size_t i = 0; i < str.size(); i++)
313 s2[i] = my_tolower(str[i]);
314 return s2;
315}
316
317
318inline bool my_isspace(const char c)
319{
320 return std::isspace(c);
321}
322
323inline bool my_isspace(const wchar_t c)
324{
325 return std::iswspace(c);
326}
327
332template<typename T>
333inline std::basic_string_view<T> trim(const std::basic_string_view<T> &str)
334{
335 size_t front = 0;
336 size_t back = str.size();
337
338 while (front < back && my_isspace(str[front]))
339 ++front;
340
341 while (back > front && my_isspace(str[back - 1]))
342 --back;
343
344 return str.substr(front, back - front);
345}
346
347// If input was a temporary string keep it one to make sure patterns like
348// trim(func_that_returns_str()) are predictable regarding memory allocation
349// and don't lead to UAF. ↓ ↓ ↓
350
355template<typename T>
356inline std::basic_string<T> trim(std::basic_string<T> &&str)
357{
358 std::basic_string<T> ret(trim(std::basic_string_view<T>(str)));
359 return ret;
360}
361
362template<typename T>
363inline std::basic_string_view<T> trim(const std::basic_string<T> &str)
364{
365 return trim(std::basic_string_view<T>(str));
366}
367
368// The above declaration causes ambiguity with char pointers so we have to fix that:
369template<typename T>
370inline std::basic_string_view<T> trim(const T *str)
371{
372 return trim(std::basic_string_view<T>(str));
373}
374
375
382inline bool is_yes(std::string_view str)
383{
384 std::string s2 = lowercase(trim(str));
385
386 return s2 == "y" || s2 == "yes" || s2 == "true" || atoi(s2.c_str()) != 0;
387}
388
389
402inline s32 mystoi(const std::string &str, s32 min, s32 max)
403{
404 s32 i = atoi(str.c_str());
405
406 if (i < min)
407 i = min;
408 if (i > max)
409 i = max;
410
411 return i;
412}
413
418inline s32 mystoi(const std::string &str)
419{
420 return atoi(str.c_str());
421}
422
427inline float mystof(const std::string &str)
428{
429 return atof(str.c_str());
430}
431
432#define stoi mystoi
433#define stof mystof
434
436template <typename T>
437inline T from_string(const std::string &str)
438{
439 std::istringstream tmp(str);
440 T t;
441 tmp >> t;
442 return t;
443}
444
446inline s64 stoi64(const std::string &str) { return from_string<s64>(str); }
447
449inline std::string itos(s32 i) { return std::to_string(i); }
451inline std::string i64tos(s64 i) { return std::to_string(i); }
452
454inline std::string ftos(float f)
455{
456 std::ostringstream oss;
457 oss << std::setprecision(std::numeric_limits<float>::max_digits10) << f;
458 return oss.str();
459}
460
461
469inline void str_replace(std::string &str, std::string_view pattern,
470 std::string_view replacement)
471{
472 std::string::size_type start = str.find(pattern, 0);
473 while (start != str.npos) {
474 str.replace(start, pattern.size(), replacement);
475 start = str.find(pattern, start + replacement.size());
476 }
477}
478
482inline void str_formspec_escape(std::string &str)
483{
484 str_replace(str, "\\", "\\\\");
485 str_replace(str, "]", "\\]");
486 str_replace(str, "[", "\\[");
487 str_replace(str, ";", "\\;");
488 str_replace(str, ",", "\\,");
489 str_replace(str, "$", "\\$");
490}
491
499void str_replace(std::string &str, char from, char to);
500
501
512inline bool string_allowed(std::string_view str, std::string_view allowed_chars)
513{
514 return str.find_first_not_of(allowed_chars) == str.npos;
515}
516
517
528inline bool string_allowed_blacklist(std::string_view str,
529 std::string_view blacklisted_chars)
530{
531 return str.find_first_of(blacklisted_chars) == str.npos;
532}
533
534
551std::string wrap_rows(std::string_view from, unsigned row_len, bool has_color_codes = false);
552
553
557template <typename T>
558inline std::basic_string<T> unescape_string(const std::basic_string<T> &s)
559{
560 std::basic_string<T> res;
561 res.reserve(s.size());
562
563 for (size_t i = 0; i < s.length(); i++) {
564 if (s[i] == '\\') {
565 i++;
566 if (i >= s.length())
567 break;
568 }
569 res += s[i];
570 }
571
572 return res;
573}
574
581template <typename T>
582std::basic_string<T> unescape_enriched(const std::basic_string<T> &s)
583{
584 std::basic_string<T> output;
585 output.reserve(s.size());
586 size_t i = 0;
587 while (i < s.length()) {
588 if (s[i] == '\x1b') {
589 ++i;
590 if (i == s.length()) continue;
591 if (s[i] == '(') {
592 ++i;
593 while (i < s.length() && s[i] != ')') {
594 if (s[i] == '\\') {
595 ++i;
596 }
597 ++i;
598 }
599 ++i;
600 } else {
601 ++i;
602 }
603 continue;
604 }
605 output += s[i];
606 ++i;
607 }
608 return output;
609}
610
611template <typename T>
612std::vector<std::basic_string<T> > split(const std::basic_string<T> &s, T delim)
613{
614 std::vector<std::basic_string<T> > tokens;
615
616 std::basic_string<T> current;
617 bool last_was_escape = false;
618 for (size_t i = 0; i < s.length(); i++) {
619 T si = s[i];
620 if (last_was_escape) {
621 current += '\\';
622 current += si;
623 last_was_escape = false;
624 } else {
625 if (si == delim) {
626 tokens.push_back(current);
627 current.clear();
628 last_was_escape = false;
629 } else if (si == '\\') {
630 last_was_escape = true;
631 } else {
632 current += si;
633 last_was_escape = false;
634 }
635 }
636 }
637 //push last element
638 tokens.push_back(current);
639
640 return tokens;
641}
642
643std::wstring translate_string(std::wstring_view s, Translations *translations);
644
645std::wstring translate_string(std::wstring_view s);
646
647inline std::wstring unescape_translate(std::wstring_view s)
648{
650}
651
659inline bool is_number(std::string_view to_check)
660{
661 for (char i : to_check)
662 if (!std::isdigit(i))
663 return false;
664
665 return !to_check.empty();
666}
667
668
674inline const char *bool_to_cstr(bool val)
675{
676 return val ? "true" : "false";
677}
678
686inline const std::string duration_to_string(int sec)
687{
688 std::ostringstream ss;
689 const char *neg = "";
690 if (sec < 0) {
691 sec = -sec;
692 neg = "-";
693 }
694 int total_sec = sec;
695 int min = sec / 60;
696 sec %= 60;
697 int hour = min / 60;
698 min %= 60;
699 int day = hour / 24;
700 hour %= 24;
701
702 if (day > 0) {
703 ss << neg << day << "d";
704 if (hour > 0 || min > 0 || sec > 0)
705 ss << " ";
706 }
707
708 if (hour > 0) {
709 ss << neg << hour << "h";
710 if (min > 0 || sec > 0)
711 ss << " ";
712 }
713
714 if (min > 0) {
715 ss << neg << min << "min";
716 if (sec > 0)
717 ss << " ";
718 }
719
720 if (sec > 0 || total_sec == 0) {
721 ss << neg << sec << "s";
722 }
723
724 return ss.str();
725}
726
732inline std::string str_join(const std::vector<std::string> &list,
733 std::string_view delimiter)
734{
735 std::ostringstream oss;
736 bool first = true;
737 for (const auto &part : list) {
738 if (!first)
739 oss << delimiter;
740 oss << part;
741 first = false;
742 }
743 return oss.str();
744}
745
746#if IS_CLIENT_BUILD
750inline std::string stringw_to_utf8(const irr::core::stringw &input)
751{
752 std::wstring_view sv(input.c_str(), input.size());
753 return wide_to_utf8(sv);
754}
755
759inline irr::core::stringw utf8_to_stringw(std::string_view input)
760{
761 std::wstring str = utf8_to_wide(input);
762 return irr::core::stringw(str.c_str(), str.size());
763}
764#endif
765
772std::string sanitizeDirName(std::string_view str, std::string_view optional_prefix);
773
781[[nodiscard]]
782std::string sanitize_untrusted(std::string_view str, bool keep_escapes = true);
783
790void safe_print_string(std::ostream &os, std::string_view str);
791
798std::optional<v3f> str_to_v3f(std::string_view str);
Definition translation.h:19
std::optional< v3f > str_to_v3f(std::string_view str)
Parses a string of form (1, 2, 3) or 1, 2, 4 to a v3f.
Definition string.cpp:1071
bool my_isspace(const char c)
Definition string.h:318
s64 stoi64(const std::string &str)
Returns a 64-bit signed value represented by the string str (decimal).
Definition string.h:446
std::string writeFlagString(u32 flags, const FlagDesc *flagdesc, u32 flagmask)
Definition string.cpp:276
bool string_allowed(std::string_view str, std::string_view allowed_chars)
Check that a string only contains whitelisted characters.
Definition string.h:512
std::string wide_to_utf8(std::wstring_view input)
Definition string.cpp:115
bool str_equal(std::basic_string_view< T > s1, std::basic_string_view< T > s2, bool case_insensitive=false)
Check two strings for equivalence.
Definition string.h:170
std::string sanitize_untrusted(std::string_view str, bool keep_escapes=true)
Sanitize an untrusted string (e.g.
Definition string.cpp:1021
char * mystrtok_r(char *s, const char *sep, char **lasts) noexcept
Definition string.cpp:310
u32 readFlagString(std::string str, const FlagDesc *flagdesc, u32 *flagmask)
Definition string.cpp:240
std::wstring unescape_translate(std::wstring_view s)
Definition string.h:647
void wide_add_codepoint(std::wstring &result, char32_t codepoint)
Definition string.cpp:142
const std::string duration_to_string(int sec)
Converts a duration in seconds to a pretty-printed duration in days, hours, minutes and seconds.
Definition string.h:686
const char * bool_to_cstr(bool val)
Returns a C-string, either "true" or "false", corresponding to val.
Definition string.h:674
std::wstring translate_string(std::wstring_view s, Translations *translations)
Definition string.cpp:912
char my_tolower(char c)
Converts a letter to lowercase, with safe handling of the char type and non-ASCII.
Definition string.h:99
std::vector< std::basic_string< T > > split(const std::basic_string< T > &s, T delim)
Definition string.h:612
std::string wrap_rows(std::string_view from, unsigned row_len, bool has_color_codes=false)
Create a string based on from where a newline is forcefully inserted every row_len characters.
Definition string.cpp:635
bool parseColorString(const std::string &value, video::SColor &color, bool quiet, unsigned char default_alpha=0xff)
Definition string.cpp:600
void str_replace(std::string &str, std::string_view pattern, std::string_view replacement)
Replace all occurrences of pattern in str with replacement.
Definition string.h:469
T from_string(const std::string &str)
Returns a value represented by the string val.
Definition string.h:437
std::vector< std::basic_string< T > > str_split(const std::basic_string< T > &str, T delimiter)
Splits a string into its component parts separated by the character delimiter.
Definition string.h:289
std::string sanitizeDirName(std::string_view str, std::string_view optional_prefix)
Sanitize the name of a new directory.
Definition string.cpp:968
std::unordered_map< std::string, std::string > StringMap
Definition string.h:66
bool str_ends_with(std::basic_string_view< T > str, std::basic_string_view< T > suffix, bool case_insensitive=false)
Check whether str ends with the string suffix.
Definition string.h:249
std::string urlencode(std::string_view str)
Definition string.cpp:204
bool string_allowed_blacklist(std::string_view str, std::string_view blacklisted_chars)
Check that a string contains no blacklisted characters.
Definition string.h:528
size_t mystrlcpy(char *dst, const char *src, size_t size) noexcept
Definition string.cpp:297
std::string str_join(const std::vector< std::string > &list, std::string_view delimiter)
Joins a vector of strings by the string delimiter.
Definition string.h:732
std::string urldecode(std::string_view str)
Definition string.cpp:222
std::string padStringRight(std::string str, size_t len)
Returns a copy of str with spaces inserted at the right hand side to ensure that the string is len ch...
Definition string.h:115
bool str_starts_with(std::basic_string_view< T > str, std::basic_string_view< T > prefix, bool case_insensitive=false)
Check whether str begins with the string prefix.
Definition string.h:209
float mystof(const std::string &str)
Returns a float reprensented by the string str (decimal).
Definition string.h:427
std::string itos(s32 i)
Returns a string representing the decimal value of the 32-bit value i.
Definition string.h:449
void str_formspec_escape(std::string &str)
Escapes characters that cannot be used in formspecs.
Definition string.h:482
void safe_print_string(std::ostream &os, std::string_view str)
Prints a sanitized version of a string without control characters.
Definition string.cpp:1056
std::basic_string< T > unescape_enriched(const std::basic_string< T > &s)
Remove all escape sequences in s.
Definition string.h:582
std::string i64tos(s64 i)
Returns a string representing the decimal value of the 64-bit value i.
Definition string.h:451
std::basic_string_view< T > trim(const std::basic_string_view< T > &str)
Definition string.h:333
std::string lowercase(std::string_view str)
Definition string.h:308
std::string ftos(float f)
Returns a string representing the exact decimal value of the float value f.
Definition string.h:454
std::string_view removeStringEnd(std::string_view str, const char *ends[])
Returns a version of str with the first occurrence of a string contained within ends[] removed from t...
Definition string.h:134
std::string encodeHexColorString(video::SColor color)
Definition string.cpp:616
#define MAKE_VARIANT(_name, _t0, _t1)
Definition string.h:151
std::wstring utf8_to_wide(std::string_view input)
Definition string.cpp:87
bool is_yes(std::string_view str)
Returns whether str should be regarded as (bool) true.
Definition string.h:382
u64 read_seed(const char *str)
Definition string.cpp:336
bool is_number(std::string_view to_check)
Checks that all characters in to_check are a decimal digits.
Definition string.h:659
std::basic_string< T > unescape_string(const std::basic_string< T > &s)
Removes backslashes from an escaped string (FormSpec strings)
Definition string.h:558
s32 mystoi(const std::string &str, s32 min, s32 max)
Converts the string str to a signed 32-bit integer.
Definition string.h:402
Definition string.h:68
u32 flag
Definition string.h:70
const char * name
Definition string.h:69
static std::string p(std::string path)
Definition test_filesys.cpp:55