23 #include "irrString.h"
32 #include <unordered_map>
36 #define STRINGIFY(x) #x
37 #define TOSTRING(x) STRINGIFY(x)
40 #define IS_ASCII_PRINTABLE_CHAR(x) \
41 (((unsigned int)(x) >= 0x20) && \
42 ( (unsigned int)(x) <= 0x7e))
45 #define IS_UTF8_MULTB_INNER(x) \
46 (((unsigned char)(x) >= 0x80) && \
47 ( (unsigned char)(x) <= 0xbf))
50 #define IS_UTF8_MULTB_START(x) \
51 (((unsigned char)(x) >= 0xc2) && \
52 ( (unsigned char)(x) <= 0xf4))
56 #define UTF8_MULTB_START_LEN(x) \
57 (((unsigned char)(x) < 0xe0) ? 2 : \
58 (((unsigned char)(x) < 0xf0) ? 3 : 4))
60 typedef std::unordered_map<std::string, std::string>
StringMap;
76 std::string
urlencode(
const std::string &str);
77 std::string
urldecode(
const std::string &str);
80 size_t mystrlcpy(
char *dst,
const char *src,
size_t size);
81 char *
mystrtok_r(
char *s,
const char *sep,
char **lasts);
83 bool parseColorString(
const std::string &value, video::SColor &color,
bool quiet,
84 unsigned char default_alpha = 0xff);
95 str.insert(str.end(), len - str.size(),
' ');
114 const char **
p = ends;
116 for (; *
p && (*p)[0] !=
'\0';
p++) {
117 std::string end = *
p;
118 if (str.size() < end.size())
120 if (str.compare(str.size() - end.size(), end.size(), end) == 0)
121 return str.substr(0, str.size() - end.size());
137 template <
typename T>
139 const std::basic_string<T> &s2,
140 bool case_insensitive =
false)
142 if (!case_insensitive)
145 if (s1.size() != s2.size())
148 for (
size_t i = 0; i < s1.size(); ++i)
149 if(tolower(s1[i]) != tolower(s2[i]))
166 template <
typename T>
168 const std::basic_string<T> &prefix,
169 bool case_insensitive =
false)
171 if (str.size() < prefix.size())
174 if (!case_insensitive)
175 return str.compare(0, prefix.size(), prefix) == 0;
177 for (
size_t i = 0; i < prefix.size(); ++i)
178 if (tolower(str[i]) != tolower(prefix[i]))
193 template <
typename T>
196 bool case_insensitive =
false)
213 template <
typename T>
215 const std::basic_string<T> &suffix,
216 bool case_insensitive =
false)
218 if (str.size() < suffix.size())
221 size_t start = str.size() - suffix.size();
222 if (!case_insensitive)
223 return str.compare(start, suffix.size(), suffix) == 0;
225 for (
size_t i = 0; i < suffix.size(); ++i)
226 if (tolower(str[start + i]) != tolower(suffix[i]))
242 template <
typename T>
245 bool case_insensitive =
false)
258 template <
typename T>
260 const std::basic_string<T> &str,
263 std::vector<std::basic_string<T> > parts;
264 std::basic_stringstream<T> sstr(str);
265 std::basic_string<T> part;
267 while (std::getline(sstr, part, delimiter))
268 parts.push_back(part);
282 s2.reserve(str.size());
295 inline std::string
trim(
const std::string &str)
299 while (std::isspace(str[front]))
302 size_t back = str.size();
303 while (back > front && std::isspace(str[back - 1]))
306 return str.substr(front, back - front);
316 inline bool is_yes(
const std::string &str)
320 return s2 ==
"y" || s2 ==
"yes" || s2 ==
"true" || atoi(s2.c_str()) != 0;
336 inline s32
mystoi(
const std::string &str, s32 min, s32 max)
338 s32 i = atoi(str.c_str());
352 inline s32
mystoi(
const std::string &str)
354 return atoi(str.c_str());
361 inline float mystof(
const std::string &str)
363 return atof(str.c_str());
370 template <
typename T>
373 std::stringstream tmp(str);
380 inline s64
stoi64(
const std::string &str) {
return from_string<s64>(str); }
382 #if __cplusplus < 201103L
386 template <
typename T>
393 #define DEFINE_STD_TOSTRING_FLOATINGPOINT(T) \
395 inline string to_string<T>(T val) \
399 << std::setprecision(6) \
407 #undef DEFINE_STD_TOSTRING_FLOATINGPOINT
410 template <
typename T>
419 inline std::string
itos(s32 i) {
return std::to_string(i); }
421 inline std::string
i64tos(s64 i) {
return std::to_string(i); }
427 inline std::string
ftos(
float f)
429 std::ostringstream oss;
442 inline void str_replace(std::string &str,
const std::string &pattern,
443 const std::string &replacement)
445 std::string::size_type start = str.find(pattern, 0);
446 while (start != str.npos) {
447 str.replace(start, pattern.size(), replacement);
448 start = str.find(pattern, start + replacement.size());
471 void str_replace(std::string &str,
char from,
char to);
484 inline bool string_allowed(
const std::string &str,
const std::string &allowed_chars)
486 return str.find_first_not_of(allowed_chars) == str.npos;
501 const std::string &blacklisted_chars)
503 return str.find_first_of(blacklisted_chars) == str.npos;
527 size_t character_idx = 0;
528 for (
size_t i = 0; i < from.size(); i++) {
531 if (character_idx > 0 && character_idx % row_len == 0)
545 template <
typename T>
548 std::basic_string<T> res;
550 for (
size_t i = 0; i < s.length(); i++) {
568 template <
typename T>
571 std::basic_string<T> output;
573 while (i < s.length()) {
574 if (s[i] ==
'\x1b') {
576 if (i == s.length())
continue;
579 while (i < s.length() && s[i] !=
')') {
597 template <
typename T>
598 std::vector<std::basic_string<T> >
split(
const std::basic_string<T> &s, T delim)
600 std::vector<std::basic_string<T> > tokens;
602 std::basic_string<T> current;
603 bool last_was_escape =
false;
604 for (
size_t i = 0; i < s.length(); i++) {
606 if (last_was_escape) {
609 last_was_escape =
false;
612 tokens.push_back(current);
613 current = std::basic_string<T>();
614 last_was_escape =
false;
615 }
else if (si ==
'\\') {
616 last_was_escape =
true;
619 last_was_escape =
false;
624 tokens.push_back(current);
646 for (
char i : to_check)
647 if (!std::isdigit(i))
650 return !to_check.empty();
661 return val ?
"true" :
"false";
671 std::stringstream ss;
692 inline std::string
str_join(
const std::vector<std::string> &list,
693 const std::string &delimiter)
695 std::ostringstream oss;
697 for (
const auto &part : list) {
711 std::wstring str(input.c_str());
721 return irr::core::stringw(str.c_str());
730 std::string
sanitizeDirName(
const std::string &str,
const std::string &optional_prefix);
Definition: translation.h:31
s64 stoi64(const std::string &str)
Returns a 64-bit signed value represented by the string str (decimal).
Definition: string.h:380
std::string writeFlagString(u32 flags, const FlagDesc *flagdesc, u32 flagmask)
Definition: string.cpp:251
bool string_allowed(const std::string &str, const std::string &allowed_chars)
Check that a string only contains whitelisted characters.
Definition: string.h:484
bool str_starts_with(const std::basic_string< T > &str, const std::basic_string< T > &prefix, bool case_insensitive=false)
Check whether str begins with the string prefix.
Definition: string.h:167
std::string urldecode(const std::string &str)
Definition: string.cpp:197
std::string lowercase(const std::string &str)
Definition: string.h:278
bool is_yes(const std::string &str)
Returns whether str should be regarded as (bool) true.
Definition: string.h:316
#define DEFINE_STD_TOSTRING_FLOATINGPOINT(T)
Definition: string.h:393
u32 readFlagString(std::string str, const FlagDesc *flagdesc, u32 *flagmask)
Definition: string.cpp:215
std::basic_string< T > unescape_string(const std::basic_string< T > &s)
Removes backslashes from an escaped string (FormSpec strings)
Definition: string.h:546
#define IS_UTF8_MULTB_INNER(x)
Definition: string.h:45
const std::string duration_to_string(int sec)
Definition: string.h:664
std::string removeStringEnd(const std::string &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:111
void str_replace(std::string &str, const std::string &pattern, const std::string &replacement)
Replace all occurrences of pattern in str with replacement.
Definition: string.h:442
irr::core::stringw utf8_to_stringw(const std::string &input)
Create a irr::core:stringw from a UTF8 std::string.
Definition: string.h:718
std::wstring utf8_to_wide(const std::string &input)
Definition: string.cpp:90
std::string trim(const std::string &str)
Definition: string.h:295
bool parseColorString(const std::string &value, video::SColor &color, bool quiet, unsigned char default_alpha=0xff)
Definition: string.cpp:327
T from_string(const std::string &str)
Returns a value represented by the string val.
Definition: string.h:371
char * mystrtok_r(char *s, const char *sep, char **lasts)
Definition: string.cpp:285
std::wstring translate_string(const std::wstring &s, Translations *translations)
Definition: string.cpp:812
std::unordered_map< std::string, std::string > StringMap
Definition: string.h:60
std::string stringw_to_utf8(const irr::core::stringw &input)
Create a UTF8 std::string from a irr::core::stringw.
Definition: string.h:709
std::string urlencode(const std::string &str)
Definition: string.cpp:179
std::string str_join(const std::vector< std::string > &list, const std::string &delimiter)
Joins a vector of strings by the string delimiter.
Definition: string.h:692
wstring to_wstring(T val)
Returns a wide string representing the value val.
Definition: string.h:411
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:92
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:259
std::wstring unescape_translate(const std::wstring &s)
Definition: string.h:633
wchar_t * utf8_to_wide_c(const char *str)
Definition: string.cpp:169
float mystof(const std::string &str)
Returns a float reprensented by the string str (decimal).
Definition: string.h:361
std::string wide_to_utf8(const std::wstring &input)
Definition: string.cpp:118
std::string itos(s32 i)
Returns a string representing the decimal value of the 32-bit value i.
Definition: string.h:419
std::basic_string< T > unescape_enriched(const std::basic_string< T > &s)
Remove all escape sequences in s.
Definition: string.h:569
void str_formspec_escape(std::string &str)
Escapes characters [ ] \ , ; that can not be used in formspecs.
Definition: string.h:455
std::string i64tos(s64 i)
Returns a string representing the decimal value of the 64-bit value i.
Definition: string.h:421
bool str_equal(const std::basic_string< T > &s1, const std::basic_string< T > &s2, bool case_insensitive=false)
Check two strings for equivalence.
Definition: string.h:138
bool is_number(const std::string &to_check)
Checks that all characters in to_check are a decimal digits.
Definition: string.h:644
std::string ftos(float f)
Returns a string representing the decimal value of the float value f.
Definition: string.h:427
bool string_allowed_blacklist(const std::string &str, const std::string &blacklisted_chars)
Check that a string contains no blacklisted characters.
Definition: string.h:500
string to_string(T val)
Returns a string representing the value val.
Definition: string.h:387
std::string wrap_rows(const std::string &from, unsigned row_len)
Create a string based on from where a newline is forcefully inserted every row_len characters.
Definition: string.h:522
std::string sanitizeDirName(const std::string &str, const std::string &optional_prefix)
Sanitize the name of a new directory.
Definition: string.cpp:868
std::vector< std::basic_string< T > > split(const std::basic_string< T > &s, T delim)
Definition: string.h:598
const char * bool_to_cstr(bool val)
Returns a C-string, either "true" or "false", corresponding to val.
Definition: string.h:659
bool str_ends_with(const std::basic_string< T > &str, const std::basic_string< T > &suffix, bool case_insensitive=false)
Check whether str ends with the string suffix.
Definition: string.h:214
u64 read_seed(const char *str)
Definition: string.cpp:311
size_t mystrlcpy(char *dst, const char *src, size_t size)
Definition: string.cpp:272
s32 mystoi(const std::string &str, s32 min, s32 max)
Converts the string str to a signed 32-bit integer.
Definition: string.h:336
u32 flag
Definition: string.h:64
const char * name
Definition: string.h:63
std::string p(std::string path)
Definition: test_filepath.cpp:59