2012-06-17 00:29:13 +02:00
|
|
|
/*
|
2013-02-24 18:40:43 +01:00
|
|
|
Minetest
|
2013-02-24 19:38:45 +01:00
|
|
|
Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
2012-06-17 00:29:13 +02:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2017-08-17 22:19:39 +02:00
|
|
|
#pragma once
|
2012-06-17 00:29:13 +02:00
|
|
|
|
2014-10-03 06:11:21 +02:00
|
|
|
#include "irrlichttypes_bloated.h"
|
2017-08-20 13:30:50 +02:00
|
|
|
#include <cstdlib>
|
2012-06-17 00:29:13 +02:00
|
|
|
#include <string>
|
|
|
|
#include <cstring>
|
|
|
|
#include <vector>
|
2015-05-19 04:30:25 +02:00
|
|
|
#include <map>
|
2012-06-17 00:29:13 +02:00
|
|
|
#include <sstream>
|
2016-08-11 19:22:40 +02:00
|
|
|
#include <iomanip>
|
2014-06-29 16:57:50 +02:00
|
|
|
#include <cctype>
|
2017-06-04 21:00:04 +02:00
|
|
|
#include <unordered_map>
|
2012-06-17 00:29:13 +02:00
|
|
|
|
2014-06-25 19:04:47 +02:00
|
|
|
#define STRINGIFY(x) #x
|
|
|
|
#define TOSTRING(x) STRINGIFY(x)
|
|
|
|
|
2015-07-25 07:43:32 +02:00
|
|
|
// Checks whether a value is an ASCII printable character
|
|
|
|
#define IS_ASCII_PRINTABLE_CHAR(x) \
|
|
|
|
(((unsigned int)(x) >= 0x20) && \
|
|
|
|
( (unsigned int)(x) <= 0x7e))
|
|
|
|
|
2015-06-17 22:10:22 +02:00
|
|
|
// Checks whether a byte is an inner byte for an utf-8 multibyte sequence
|
2015-07-25 07:43:32 +02:00
|
|
|
#define IS_UTF8_MULTB_INNER(x) \
|
|
|
|
(((unsigned char)(x) >= 0x80) && \
|
|
|
|
( (unsigned char)(x) <= 0xbf))
|
|
|
|
|
|
|
|
// Checks whether a byte is a start byte for an utf-8 multibyte sequence
|
|
|
|
#define IS_UTF8_MULTB_START(x) \
|
|
|
|
(((unsigned char)(x) >= 0xc2) && \
|
|
|
|
( (unsigned char)(x) <= 0xf4))
|
|
|
|
|
|
|
|
// Given a start byte x for an utf-8 multibyte sequence
|
|
|
|
// it gives the length of the whole sequence in bytes.
|
|
|
|
#define UTF8_MULTB_START_LEN(x) \
|
|
|
|
(((unsigned char)(x) < 0xe0) ? 2 : \
|
|
|
|
(((unsigned char)(x) < 0xf0) ? 3 : 4))
|
2015-06-17 22:10:22 +02:00
|
|
|
|
2017-06-04 21:00:04 +02:00
|
|
|
typedef std::unordered_map<std::string, std::string> StringMap;
|
2015-05-19 04:30:25 +02:00
|
|
|
|
2013-02-05 21:01:33 +01:00
|
|
|
struct FlagDesc {
|
|
|
|
const char *name;
|
|
|
|
u32 flag;
|
|
|
|
};
|
|
|
|
|
2015-06-10 00:35:21 +02:00
|
|
|
// try not to convert between wide/utf8 encodings; this can result in data loss
|
|
|
|
// try to only convert between them when you need to input/output stuff via Irrlicht
|
|
|
|
std::wstring utf8_to_wide(const std::string &input);
|
|
|
|
std::string wide_to_utf8(const std::wstring &input);
|
|
|
|
|
2015-07-07 05:55:07 +02:00
|
|
|
wchar_t *utf8_to_wide_c(const char *str);
|
|
|
|
|
2015-06-10 00:35:21 +02:00
|
|
|
// NEVER use those two functions unless you have a VERY GOOD reason to
|
|
|
|
// they just convert between wide and multibyte encoding
|
|
|
|
// multibyte encoding depends on current locale, this is no good, especially on Windows
|
|
|
|
|
2015-02-01 23:59:23 +01:00
|
|
|
// You must free the returned string!
|
2015-03-07 06:09:27 +01:00
|
|
|
// The returned string is allocated using new
|
|
|
|
wchar_t *narrow_to_wide_c(const char *str);
|
|
|
|
std::wstring narrow_to_wide(const std::string &mbs);
|
|
|
|
std::string wide_to_narrow(const std::wstring &wcs);
|
2015-04-12 04:49:13 +02:00
|
|
|
|
2016-06-11 09:23:53 +02:00
|
|
|
std::string urlencode(const std::string &str);
|
|
|
|
std::string urldecode(const std::string &str);
|
2014-11-02 15:17:20 +01:00
|
|
|
u32 readFlagString(std::string str, const FlagDesc *flagdesc, u32 *flagmask);
|
|
|
|
std::string writeFlagString(u32 flags, const FlagDesc *flagdesc, u32 flagmask);
|
|
|
|
size_t mystrlcpy(char *dst, const char *src, size_t size);
|
|
|
|
char *mystrtok_r(char *s, const char *sep, char **lasts);
|
|
|
|
u64 read_seed(const char *str);
|
2018-04-23 19:50:50 +02:00
|
|
|
bool parseColorString(const std::string &value, video::SColor &color, bool quiet,
|
|
|
|
unsigned char default_alpha = 0xff);
|
2013-08-11 04:09:45 +02:00
|
|
|
|
2014-11-02 15:17:20 +01:00
|
|
|
|
|
|
|
/**
|
2014-12-11 23:58:50 +01:00
|
|
|
* Returns a copy of \p str with spaces inserted at the right hand side to ensure
|
|
|
|
* that the string is \p len characters in length. If \p str is <= \p len then the
|
|
|
|
* returned string will be identical to str.
|
2014-11-02 15:17:20 +01:00
|
|
|
*/
|
2014-12-11 23:58:50 +01:00
|
|
|
inline std::string padStringRight(std::string str, size_t len)
|
2012-06-17 00:29:13 +02:00
|
|
|
{
|
2014-12-11 23:58:50 +01:00
|
|
|
if (len > str.size())
|
|
|
|
str.insert(str.end(), len - str.size(), ' ');
|
2014-11-02 15:17:20 +01:00
|
|
|
|
2014-12-11 23:58:50 +01:00
|
|
|
return str;
|
2012-06-17 00:29:13 +02:00
|
|
|
}
|
|
|
|
|
2014-11-02 15:17:20 +01:00
|
|
|
/**
|
2014-12-11 23:58:50 +01:00
|
|
|
* Returns a version of \p str with the first occurrence of a string
|
2014-11-02 15:17:20 +01:00
|
|
|
* contained within ends[] removed from the end of the string.
|
|
|
|
*
|
2014-12-11 23:58:50 +01:00
|
|
|
* @param str
|
2014-11-02 15:17:20 +01:00
|
|
|
* @param ends A NULL- or ""- terminated array of strings to remove from s in
|
2014-12-11 23:58:50 +01:00
|
|
|
* the copy produced. Note that once one of these strings is removed
|
|
|
|
* that no further postfixes contained within this array are removed.
|
2014-11-02 15:17:20 +01:00
|
|
|
*
|
2014-12-11 23:58:50 +01:00
|
|
|
* @return If no end could be removed then "" is returned.
|
2014-11-02 15:17:20 +01:00
|
|
|
*/
|
2014-12-11 23:58:50 +01:00
|
|
|
inline std::string removeStringEnd(const std::string &str,
|
|
|
|
const char *ends[])
|
2012-06-17 00:29:13 +02:00
|
|
|
{
|
|
|
|
const char **p = ends;
|
2014-11-02 15:17:20 +01:00
|
|
|
|
|
|
|
for (; *p && (*p)[0] != '\0'; p++) {
|
2012-06-17 00:29:13 +02:00
|
|
|
std::string end = *p;
|
2014-12-11 23:58:50 +01:00
|
|
|
if (str.size() < end.size())
|
2012-06-17 00:29:13 +02:00
|
|
|
continue;
|
2014-12-11 23:58:50 +01:00
|
|
|
if (str.compare(str.size() - end.size(), end.size(), end) == 0)
|
|
|
|
return str.substr(0, str.size() - end.size());
|
2012-06-17 00:29:13 +02:00
|
|
|
}
|
2014-11-02 15:17:20 +01:00
|
|
|
|
2012-06-17 00:29:13 +02:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2014-11-02 15:17:20 +01:00
|
|
|
|
|
|
|
/**
|
2014-12-11 23:58:50 +01:00
|
|
|
* Check two strings for equivalence. If \p case_insensitive is true
|
|
|
|
* then the case of the strings is ignored (default is false).
|
2014-11-02 15:17:20 +01:00
|
|
|
*
|
|
|
|
* @param s1
|
|
|
|
* @param s2
|
|
|
|
* @param case_insensitive
|
|
|
|
* @return true if the strings match
|
|
|
|
*/
|
2014-12-11 23:58:50 +01:00
|
|
|
template <typename T>
|
|
|
|
inline bool str_equal(const std::basic_string<T> &s1,
|
|
|
|
const std::basic_string<T> &s2,
|
2012-06-17 00:29:13 +02:00
|
|
|
bool case_insensitive = false)
|
|
|
|
{
|
2014-12-11 23:58:50 +01:00
|
|
|
if (!case_insensitive)
|
|
|
|
return s1 == s2;
|
2014-11-02 15:17:20 +01:00
|
|
|
|
2014-12-11 23:58:50 +01:00
|
|
|
if (s1.size() != s2.size())
|
|
|
|
return false;
|
2014-11-02 15:17:20 +01:00
|
|
|
|
2014-12-11 23:58:50 +01:00
|
|
|
for (size_t i = 0; i < s1.size(); ++i)
|
|
|
|
if(tolower(s1[i]) != tolower(s2[i]))
|
|
|
|
return false;
|
2014-11-02 15:17:20 +01:00
|
|
|
|
2014-12-11 23:58:50 +01:00
|
|
|
return true;
|
2012-06-17 00:29:13 +02:00
|
|
|
}
|
|
|
|
|
2014-11-02 15:17:20 +01:00
|
|
|
|
|
|
|
/**
|
2014-12-11 23:58:50 +01:00
|
|
|
* Check whether \p str begins with the string prefix. If \p case_insensitive
|
|
|
|
* is true then the check is case insensitve (default is false; i.e. case is
|
|
|
|
* significant).
|
2014-11-02 15:17:20 +01:00
|
|
|
*
|
|
|
|
* @param str
|
|
|
|
* @param prefix
|
|
|
|
* @param case_insensitive
|
2014-12-11 23:58:50 +01:00
|
|
|
* @return true if the str begins with prefix
|
2014-11-02 15:17:20 +01:00
|
|
|
*/
|
2014-12-11 23:58:50 +01:00
|
|
|
template <typename T>
|
|
|
|
inline bool str_starts_with(const std::basic_string<T> &str,
|
|
|
|
const std::basic_string<T> &prefix,
|
2012-06-17 00:29:13 +02:00
|
|
|
bool case_insensitive = false)
|
|
|
|
{
|
2014-11-02 15:17:20 +01:00
|
|
|
if (str.size() < prefix.size())
|
2012-06-17 00:29:13 +02:00
|
|
|
return false;
|
2014-11-02 15:17:20 +01:00
|
|
|
|
2014-12-11 23:58:50 +01:00
|
|
|
if (!case_insensitive)
|
|
|
|
return str.compare(0, prefix.size(), prefix) == 0;
|
2014-11-02 15:17:20 +01:00
|
|
|
|
2014-12-11 23:58:50 +01:00
|
|
|
for (size_t i = 0; i < prefix.size(); ++i)
|
|
|
|
if (tolower(str[i]) != tolower(prefix[i]))
|
|
|
|
return false;
|
2012-06-17 00:29:13 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-01 14:26:57 +02:00
|
|
|
/**
|
|
|
|
* Check whether \p str begins with the string prefix. If \p case_insensitive
|
|
|
|
* is true then the check is case insensitve (default is false; i.e. case is
|
|
|
|
* significant).
|
|
|
|
*
|
|
|
|
* @param str
|
|
|
|
* @param prefix
|
|
|
|
* @param case_insensitive
|
|
|
|
* @return true if the str begins with prefix
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
inline bool str_starts_with(const std::basic_string<T> &str,
|
|
|
|
const T *prefix,
|
|
|
|
bool case_insensitive = false)
|
|
|
|
{
|
|
|
|
return str_starts_with(str, std::basic_string<T>(prefix),
|
|
|
|
case_insensitive);
|
|
|
|
}
|
2014-11-02 15:17:20 +01:00
|
|
|
|
2017-01-31 18:05:03 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether \p str ends with the string suffix. If \p case_insensitive
|
|
|
|
* is true then the check is case insensitve (default is false; i.e. case is
|
|
|
|
* significant).
|
|
|
|
*
|
|
|
|
* @param str
|
|
|
|
* @param suffix
|
|
|
|
* @param case_insensitive
|
|
|
|
* @return true if the str begins with suffix
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
inline bool str_ends_with(const std::basic_string<T> &str,
|
|
|
|
const std::basic_string<T> &suffix,
|
|
|
|
bool case_insensitive = false)
|
|
|
|
{
|
|
|
|
if (str.size() < suffix.size())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
size_t start = str.size() - suffix.size();
|
|
|
|
if (!case_insensitive)
|
|
|
|
return str.compare(start, suffix.size(), suffix) == 0;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < suffix.size(); ++i)
|
|
|
|
if (tolower(str[start + i]) != tolower(suffix[i]))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether \p str ends with the string suffix. If \p case_insensitive
|
|
|
|
* is true then the check is case insensitve (default is false; i.e. case is
|
|
|
|
* significant).
|
|
|
|
*
|
|
|
|
* @param str
|
|
|
|
* @param suffix
|
|
|
|
* @param case_insensitive
|
|
|
|
* @return true if the str begins with suffix
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
inline bool str_ends_with(const std::basic_string<T> &str,
|
|
|
|
const T *suffix,
|
|
|
|
bool case_insensitive = false)
|
|
|
|
{
|
|
|
|
return str_ends_with(str, std::basic_string<T>(suffix),
|
|
|
|
case_insensitive);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-02 15:17:20 +01:00
|
|
|
/**
|
|
|
|
* Splits a string into its component parts separated by the character
|
2014-12-11 23:58:50 +01:00
|
|
|
* \p delimiter.
|
2014-11-02 15:17:20 +01:00
|
|
|
*
|
2014-12-11 23:58:50 +01:00
|
|
|
* @return An std::vector<std::basic_string<T> > of the component parts
|
2014-11-02 15:17:20 +01:00
|
|
|
*/
|
2014-12-11 23:58:50 +01:00
|
|
|
template <typename T>
|
|
|
|
inline std::vector<std::basic_string<T> > str_split(
|
|
|
|
const std::basic_string<T> &str,
|
|
|
|
T delimiter)
|
|
|
|
{
|
|
|
|
std::vector<std::basic_string<T> > parts;
|
|
|
|
std::basic_stringstream<T> sstr(str);
|
|
|
|
std::basic_string<T> part;
|
2014-11-02 15:17:20 +01:00
|
|
|
|
|
|
|
while (std::getline(sstr, part, delimiter))
|
2014-09-28 10:10:43 +02:00
|
|
|
parts.push_back(part);
|
2014-11-02 15:17:20 +01:00
|
|
|
|
2014-09-28 10:10:43 +02:00
|
|
|
return parts;
|
|
|
|
}
|
|
|
|
|
2014-11-02 15:17:20 +01:00
|
|
|
|
|
|
|
/**
|
2014-12-11 23:58:50 +01:00
|
|
|
* @param str
|
|
|
|
* @return A copy of \p str converted to all lowercase characters.
|
2014-11-02 15:17:20 +01:00
|
|
|
*/
|
2014-12-11 23:58:50 +01:00
|
|
|
inline std::string lowercase(const std::string &str)
|
2012-06-17 00:29:13 +02:00
|
|
|
{
|
2017-08-20 13:30:50 +02:00
|
|
|
std::string s2;
|
2014-11-02 15:17:20 +01:00
|
|
|
|
2014-12-11 23:58:50 +01:00
|
|
|
s2.reserve(str.size());
|
2014-11-02 15:17:20 +01:00
|
|
|
|
2017-08-20 13:30:50 +02:00
|
|
|
for (char i : str)
|
|
|
|
s2 += tolower(i);
|
2014-11-02 15:17:20 +01:00
|
|
|
|
2012-06-17 00:29:13 +02:00
|
|
|
return s2;
|
|
|
|
}
|
|
|
|
|
2014-11-02 15:17:20 +01:00
|
|
|
|
|
|
|
/**
|
2014-12-11 23:58:50 +01:00
|
|
|
* @param str
|
|
|
|
* @return A copy of \p str with leading and trailing whitespace removed.
|
2014-11-02 15:17:20 +01:00
|
|
|
*/
|
2014-12-11 23:58:50 +01:00
|
|
|
inline std::string trim(const std::string &str)
|
2013-08-11 04:09:45 +02:00
|
|
|
{
|
|
|
|
size_t front = 0;
|
2014-11-02 15:17:20 +01:00
|
|
|
|
2014-12-11 23:58:50 +01:00
|
|
|
while (std::isspace(str[front]))
|
2013-08-11 04:09:45 +02:00
|
|
|
++front;
|
|
|
|
|
2014-12-11 23:58:50 +01:00
|
|
|
size_t back = str.size();
|
|
|
|
while (back > front && std::isspace(str[back - 1]))
|
2013-08-11 04:09:45 +02:00
|
|
|
--back;
|
|
|
|
|
2014-12-11 23:58:50 +01:00
|
|
|
return str.substr(front, back - front);
|
2013-08-11 04:09:45 +02:00
|
|
|
}
|
|
|
|
|
2014-11-02 15:17:20 +01:00
|
|
|
|
|
|
|
/**
|
2014-12-11 23:58:50 +01:00
|
|
|
* Returns whether \p str should be regarded as (bool) true. Case and leading
|
|
|
|
* and trailing whitespace are ignored. Values that will return
|
|
|
|
* true are "y", "yes", "true" and any number that is not 0.
|
|
|
|
* @param str
|
2014-11-02 15:17:20 +01:00
|
|
|
*/
|
2014-12-11 23:58:50 +01:00
|
|
|
inline bool is_yes(const std::string &str)
|
2012-06-17 00:29:13 +02:00
|
|
|
{
|
2014-12-11 23:58:50 +01:00
|
|
|
std::string s2 = lowercase(trim(str));
|
2014-11-02 15:17:20 +01:00
|
|
|
|
2017-01-07 11:05:05 +01:00
|
|
|
return s2 == "y" || s2 == "yes" || s2 == "true" || atoi(s2.c_str()) != 0;
|
2012-06-17 00:29:13 +02:00
|
|
|
}
|
|
|
|
|
2014-11-02 15:17:20 +01:00
|
|
|
|
|
|
|
/**
|
2014-12-11 23:58:50 +01:00
|
|
|
* Converts the string \p str to a signed 32-bit integer. The converted value
|
|
|
|
* is constrained so that min <= value <= max.
|
2014-11-02 15:17:20 +01:00
|
|
|
*
|
|
|
|
* @see atoi(3) for limitations
|
|
|
|
*
|
2014-12-11 23:58:50 +01:00
|
|
|
* @param str
|
2014-11-02 15:17:20 +01:00
|
|
|
* @param min Range minimum
|
|
|
|
* @param max Range maximum
|
|
|
|
* @return The value converted to a signed 32-bit integer and constrained
|
2014-12-11 23:58:50 +01:00
|
|
|
* within the range defined by min and max (inclusive)
|
2014-11-02 15:17:20 +01:00
|
|
|
*/
|
2014-12-11 23:58:50 +01:00
|
|
|
inline s32 mystoi(const std::string &str, s32 min, s32 max)
|
2012-06-17 00:29:13 +02:00
|
|
|
{
|
2014-12-11 23:58:50 +01:00
|
|
|
s32 i = atoi(str.c_str());
|
2014-11-02 15:17:20 +01:00
|
|
|
|
|
|
|
if (i < min)
|
2012-06-17 00:29:13 +02:00
|
|
|
i = min;
|
2014-11-02 15:17:20 +01:00
|
|
|
if (i > max)
|
2012-06-17 00:29:13 +02:00
|
|
|
i = max;
|
2014-11-02 15:17:20 +01:00
|
|
|
|
2012-06-17 00:29:13 +02:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2014-11-02 15:17:20 +01:00
|
|
|
|
2012-06-17 00:29:13 +02:00
|
|
|
// MSVC2010 includes it's own versions of these
|
|
|
|
//#if !defined(_MSC_VER) || _MSC_VER < 1600
|
|
|
|
|
2014-11-02 15:17:20 +01:00
|
|
|
|
|
|
|
/**
|
2014-12-11 23:58:50 +01:00
|
|
|
* Returns a 32-bit value reprensented by the string \p str (decimal).
|
2014-11-02 15:17:20 +01:00
|
|
|
* @see atoi(3) for further limitations
|
|
|
|
*/
|
2014-12-11 23:58:50 +01:00
|
|
|
inline s32 mystoi(const std::string &str)
|
2014-02-05 06:05:58 +01:00
|
|
|
{
|
2014-12-11 23:58:50 +01:00
|
|
|
return atoi(str.c_str());
|
2014-02-05 06:05:58 +01:00
|
|
|
}
|
|
|
|
|
2014-11-02 15:17:20 +01:00
|
|
|
|
|
|
|
/**
|
2014-12-11 23:58:50 +01:00
|
|
|
* Returns s 32-bit value represented by the wide string \p str (decimal).
|
2014-11-02 15:17:20 +01:00
|
|
|
* @see atoi(3) for further limitations
|
|
|
|
*/
|
2014-12-11 23:58:50 +01:00
|
|
|
inline s32 mystoi(const std::wstring &str)
|
2012-06-17 00:29:13 +02:00
|
|
|
{
|
2014-12-11 23:58:50 +01:00
|
|
|
return mystoi(wide_to_narrow(str));
|
2012-06-17 00:29:13 +02:00
|
|
|
}
|
|
|
|
|
2014-11-02 15:17:20 +01:00
|
|
|
|
|
|
|
/**
|
2014-12-11 23:58:50 +01:00
|
|
|
* Returns a float reprensented by the string \p str (decimal).
|
2014-11-02 15:17:20 +01:00
|
|
|
* @see atof(3)
|
|
|
|
*/
|
2014-12-11 23:58:50 +01:00
|
|
|
inline float mystof(const std::string &str)
|
2012-06-17 00:29:13 +02:00
|
|
|
{
|
2014-12-11 23:58:50 +01:00
|
|
|
return atof(str.c_str());
|
2012-06-17 00:29:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//#endif
|
|
|
|
|
|
|
|
#define stoi mystoi
|
|
|
|
#define stof mystof
|
|
|
|
|
2016-02-09 07:08:31 +01:00
|
|
|
/// Returns a value represented by the string \p val.
|
|
|
|
template <typename T>
|
|
|
|
inline T from_string(const std::string &str)
|
|
|
|
{
|
|
|
|
std::stringstream tmp(str);
|
|
|
|
T t;
|
|
|
|
tmp >> t;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a 64-bit signed value represented by the string \p str (decimal).
|
|
|
|
inline s64 stoi64(const std::string &str) { return from_string<s64>(str); }
|
|
|
|
|
2016-08-11 19:22:40 +02:00
|
|
|
#if __cplusplus < 201103L
|
|
|
|
namespace std {
|
2014-11-02 15:17:20 +01:00
|
|
|
|
2016-02-09 07:08:31 +01:00
|
|
|
/// Returns a string representing the value \p val.
|
2014-12-11 23:58:50 +01:00
|
|
|
template <typename T>
|
2016-08-11 19:22:40 +02:00
|
|
|
inline string to_string(T val)
|
2012-06-17 00:29:13 +02:00
|
|
|
{
|
2016-08-11 19:22:40 +02:00
|
|
|
ostringstream oss;
|
2014-12-11 23:58:50 +01:00
|
|
|
oss << val;
|
|
|
|
return oss.str();
|
2012-06-17 00:29:13 +02:00
|
|
|
}
|
2016-08-11 19:22:40 +02:00
|
|
|
#define DEFINE_STD_TOSTRING_FLOATINGPOINT(T) \
|
|
|
|
template <> \
|
|
|
|
inline string to_string<T>(T val) \
|
|
|
|
{ \
|
|
|
|
ostringstream oss; \
|
|
|
|
oss << std::fixed \
|
|
|
|
<< std::setprecision(6) \
|
|
|
|
<< val; \
|
|
|
|
return oss.str(); \
|
|
|
|
}
|
|
|
|
DEFINE_STD_TOSTRING_FLOATINGPOINT(float)
|
|
|
|
DEFINE_STD_TOSTRING_FLOATINGPOINT(double)
|
|
|
|
DEFINE_STD_TOSTRING_FLOATINGPOINT(long double)
|
|
|
|
|
|
|
|
#undef DEFINE_STD_TOSTRING_FLOATINGPOINT
|
|
|
|
|
|
|
|
/// Returns a wide string representing the value \p val
|
|
|
|
template <typename T>
|
|
|
|
inline wstring to_wstring(T val)
|
|
|
|
{
|
|
|
|
return utf8_to_wide(to_string(val));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2012-06-17 00:29:13 +02:00
|
|
|
|
2014-12-11 23:58:50 +01:00
|
|
|
/// Returns a string representing the decimal value of the 32-bit value \p i.
|
2016-08-11 19:22:40 +02:00
|
|
|
inline std::string itos(s32 i) { return std::to_string(i); }
|
2014-12-11 23:58:50 +01:00
|
|
|
/// Returns a string representing the decimal value of the 64-bit value \p i.
|
2016-08-11 19:22:40 +02:00
|
|
|
inline std::string i64tos(s64 i) { return std::to_string(i); }
|
|
|
|
|
|
|
|
// std::to_string uses the '%.6f' conversion, which is inconsistent with
|
|
|
|
// std::ostream::operator<<() and impractical too. ftos() uses the
|
|
|
|
// more generic and std::ostream::operator<<()-compatible '%G' format.
|
2014-12-11 23:58:50 +01:00
|
|
|
/// Returns a string representing the decimal value of the float value \p f.
|
2016-08-11 19:22:40 +02:00
|
|
|
inline std::string ftos(float f)
|
|
|
|
{
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << f;
|
|
|
|
return oss.str();
|
|
|
|
}
|
2012-10-22 23:18:44 +02:00
|
|
|
|
2014-11-02 15:17:20 +01:00
|
|
|
|
|
|
|
/**
|
2014-12-11 23:58:50 +01:00
|
|
|
* Replace all occurrences of \p pattern in \p str with \p replacement.
|
2014-11-02 15:17:20 +01:00
|
|
|
*
|
2014-12-11 23:58:50 +01:00
|
|
|
* @param str String to replace pattern with replacement within.
|
|
|
|
* @param pattern The pattern to replace.
|
|
|
|
* @param replacement What to replace the pattern with.
|
2014-11-02 15:17:20 +01:00
|
|
|
*/
|
2014-12-11 23:58:50 +01:00
|
|
|
inline void str_replace(std::string &str, const std::string &pattern,
|
|
|
|
const std::string &replacement)
|
2012-06-17 00:29:13 +02:00
|
|
|
{
|
|
|
|
std::string::size_type start = str.find(pattern, 0);
|
2014-11-02 15:17:20 +01:00
|
|
|
while (start != str.npos) {
|
2012-06-17 00:29:13 +02:00
|
|
|
str.replace(start, pattern.size(), replacement);
|
2014-12-11 23:58:50 +01:00
|
|
|
start = str.find(pattern, start + replacement.size());
|
2012-06-17 00:29:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-11 10:39:37 +02:00
|
|
|
/**
|
|
|
|
* Escapes characters [ ] \ , ; that can not be used in formspecs
|
|
|
|
*/
|
|
|
|
inline void str_formspec_escape(std::string &str)
|
|
|
|
{
|
|
|
|
str_replace(str, "\\", "\\\\");
|
|
|
|
str_replace(str, "]", "\\]");
|
|
|
|
str_replace(str, "[", "\\[");
|
|
|
|
str_replace(str, ";", "\\;");
|
|
|
|
str_replace(str, ",", "\\,");
|
|
|
|
}
|
|
|
|
|
2014-11-02 15:17:20 +01:00
|
|
|
/**
|
2014-12-11 23:58:50 +01:00
|
|
|
* Replace all occurrences of the character \p from in \p str with \p to.
|
2014-11-02 15:17:20 +01:00
|
|
|
*
|
2014-12-11 23:58:50 +01:00
|
|
|
* @param str The string to (potentially) modify.
|
|
|
|
* @param from The character in str to replace.
|
|
|
|
* @param to The replacement character.
|
2014-11-02 15:17:20 +01:00
|
|
|
*/
|
2014-12-11 23:58:50 +01:00
|
|
|
void str_replace(std::string &str, char from, char to);
|
2012-06-17 00:29:13 +02:00
|
|
|
|
2014-11-02 15:17:20 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check that a string only contains whitelisted characters. This is the
|
|
|
|
* opposite of string_allowed_blacklist().
|
|
|
|
*
|
2014-12-11 23:58:50 +01:00
|
|
|
* @param str The string to be checked.
|
2014-11-02 15:17:20 +01:00
|
|
|
* @param allowed_chars A string containing permitted characters.
|
|
|
|
* @return true if the string is allowed, otherwise false.
|
|
|
|
*
|
|
|
|
* @see string_allowed_blacklist()
|
|
|
|
*/
|
2014-12-11 23:58:50 +01:00
|
|
|
inline bool string_allowed(const std::string &str, const std::string &allowed_chars)
|
2012-06-17 00:29:13 +02:00
|
|
|
{
|
2014-12-11 23:58:50 +01:00
|
|
|
return str.find_first_not_of(allowed_chars) == str.npos;
|
2012-06-17 00:29:13 +02:00
|
|
|
}
|
|
|
|
|
2014-11-02 15:17:20 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check that a string contains no blacklisted characters. This is the
|
|
|
|
* opposite of string_allowed().
|
|
|
|
*
|
2014-12-11 23:58:50 +01:00
|
|
|
* @param str The string to be checked.
|
2014-11-02 15:17:20 +01:00
|
|
|
* @param blacklisted_chars A string containing prohibited characters.
|
|
|
|
* @return true if the string is allowed, otherwise false.
|
|
|
|
|
|
|
|
* @see string_allowed()
|
|
|
|
*/
|
2014-12-11 23:58:50 +01:00
|
|
|
inline bool string_allowed_blacklist(const std::string &str,
|
2014-11-02 15:17:20 +01:00
|
|
|
const std::string &blacklisted_chars)
|
2012-09-02 22:51:17 +02:00
|
|
|
{
|
2014-12-11 23:58:50 +01:00
|
|
|
return str.find_first_of(blacklisted_chars) == str.npos;
|
2012-09-02 22:51:17 +02:00
|
|
|
}
|
|
|
|
|
2014-11-02 15:17:20 +01:00
|
|
|
|
|
|
|
/**
|
2014-12-11 23:58:50 +01:00
|
|
|
* Create a string based on \p from where a newline is forcefully inserted
|
|
|
|
* every \p row_len characters.
|
2014-11-02 15:17:20 +01:00
|
|
|
*
|
|
|
|
* @note This function does not honour word wraps and blindy inserts a newline
|
2014-12-11 23:58:50 +01:00
|
|
|
* every \p row_len characters whether it breaks a word or not. It is
|
|
|
|
* intended to be used for, for example, showing paths in the GUI.
|
2014-11-02 15:17:20 +01:00
|
|
|
*
|
2015-06-17 22:10:22 +02:00
|
|
|
* @note This function doesn't wrap inside utf-8 multibyte sequences and also
|
|
|
|
* counts multibyte sequences correcly as single characters.
|
|
|
|
*
|
|
|
|
* @param from The (utf-8) string to be wrapped into rows.
|
2014-12-11 23:58:50 +01:00
|
|
|
* @param row_len The row length (in characters).
|
2014-11-02 15:17:20 +01:00
|
|
|
* @return A new string with the wrapping applied.
|
|
|
|
*/
|
2014-12-11 23:58:50 +01:00
|
|
|
inline std::string wrap_rows(const std::string &from,
|
|
|
|
unsigned row_len)
|
2012-06-17 00:29:13 +02:00
|
|
|
{
|
|
|
|
std::string to;
|
2014-11-02 15:17:20 +01:00
|
|
|
|
2015-06-17 22:10:22 +02:00
|
|
|
size_t character_idx = 0;
|
2014-11-02 15:17:20 +01:00
|
|
|
for (size_t i = 0; i < from.size(); i++) {
|
2015-06-18 20:34:17 +02:00
|
|
|
if (!IS_UTF8_MULTB_INNER(from[i])) {
|
2015-06-19 18:04:11 +02:00
|
|
|
// Wrap string after last inner byte of char
|
|
|
|
if (character_idx > 0 && character_idx % row_len == 0)
|
2015-06-18 20:34:17 +02:00
|
|
|
to += '\n';
|
2015-06-17 22:10:22 +02:00
|
|
|
character_idx++;
|
2015-06-18 20:34:17 +02:00
|
|
|
}
|
2012-06-17 00:29:13 +02:00
|
|
|
to += from[i];
|
|
|
|
}
|
2014-11-02 15:17:20 +01:00
|
|
|
|
2012-06-17 00:29:13 +02:00
|
|
|
return to;
|
|
|
|
}
|
|
|
|
|
2014-11-02 15:17:20 +01:00
|
|
|
|
|
|
|
/**
|
2015-01-15 22:14:40 +01:00
|
|
|
* Removes backslashes from an escaped string (FormSpec strings)
|
2014-11-02 15:17:20 +01:00
|
|
|
*/
|
2014-12-11 23:58:50 +01:00
|
|
|
template <typename T>
|
2016-04-04 18:31:00 +02:00
|
|
|
inline std::basic_string<T> unescape_string(const std::basic_string<T> &s)
|
2013-02-24 22:00:35 +01:00
|
|
|
{
|
2014-12-11 23:58:50 +01:00
|
|
|
std::basic_string<T> res;
|
|
|
|
|
2013-08-20 22:38:14 +02:00
|
|
|
for (size_t i = 0; i < s.length(); i++) {
|
2015-01-16 12:05:44 +01:00
|
|
|
if (s[i] == '\\') {
|
2015-01-15 22:14:40 +01:00
|
|
|
i++;
|
2015-01-16 12:05:44 +01:00
|
|
|
if (i >= s.length())
|
|
|
|
break;
|
|
|
|
}
|
2015-01-15 22:14:40 +01:00
|
|
|
res += s[i];
|
2013-02-24 22:00:35 +01:00
|
|
|
}
|
2014-12-11 23:58:50 +01:00
|
|
|
|
2013-02-24 22:00:35 +01:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2016-04-04 18:31:00 +02:00
|
|
|
/**
|
|
|
|
* Remove all escape sequences in \p s.
|
|
|
|
*
|
|
|
|
* @param s The string in which to remove escape sequences.
|
|
|
|
* @return \p s, with escape sequences removed.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
std::basic_string<T> unescape_enriched(const std::basic_string<T> &s)
|
|
|
|
{
|
|
|
|
std::basic_string<T> output;
|
|
|
|
size_t i = 0;
|
|
|
|
while (i < s.length()) {
|
|
|
|
if (s[i] == '\x1b') {
|
|
|
|
++i;
|
|
|
|
if (i == s.length()) continue;
|
|
|
|
if (s[i] == '(') {
|
|
|
|
++i;
|
|
|
|
while (i < s.length() && s[i] != ')') {
|
|
|
|
if (s[i] == '\\') {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
output += s[i];
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
2014-11-02 15:17:20 +01:00
|
|
|
|
2016-05-31 17:30:11 +02:00
|
|
|
template <typename T>
|
|
|
|
std::vector<std::basic_string<T> > split(const std::basic_string<T> &s, T delim)
|
|
|
|
{
|
|
|
|
std::vector<std::basic_string<T> > tokens;
|
|
|
|
|
|
|
|
std::basic_string<T> current;
|
|
|
|
bool last_was_escape = false;
|
|
|
|
for (size_t i = 0; i < s.length(); i++) {
|
|
|
|
T si = s[i];
|
|
|
|
if (last_was_escape) {
|
|
|
|
current += '\\';
|
|
|
|
current += si;
|
|
|
|
last_was_escape = false;
|
|
|
|
} else {
|
|
|
|
if (si == delim) {
|
|
|
|
tokens.push_back(current);
|
|
|
|
current = std::basic_string<T>();
|
|
|
|
last_was_escape = false;
|
|
|
|
} else if (si == '\\') {
|
|
|
|
last_was_escape = true;
|
|
|
|
} else {
|
|
|
|
current += si;
|
|
|
|
last_was_escape = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//push last element
|
|
|
|
tokens.push_back(current);
|
|
|
|
|
|
|
|
return tokens;
|
|
|
|
}
|
|
|
|
|
2017-01-31 18:05:03 +01:00
|
|
|
std::wstring translate_string(const std::wstring &s);
|
|
|
|
|
|
|
|
inline std::wstring unescape_translate(const std::wstring &s) {
|
|
|
|
return unescape_enriched(translate_string(s));
|
|
|
|
}
|
|
|
|
|
2014-11-02 15:17:20 +01:00
|
|
|
/**
|
2014-12-11 23:58:50 +01:00
|
|
|
* Checks that all characters in \p to_check are a decimal digits.
|
2014-11-02 15:17:20 +01:00
|
|
|
*
|
2014-12-11 23:58:50 +01:00
|
|
|
* @param to_check
|
|
|
|
* @return true if to_check is not empty and all characters in to_check are
|
|
|
|
* decimal digits, otherwise false
|
2014-11-02 15:17:20 +01:00
|
|
|
*/
|
2014-12-11 23:58:50 +01:00
|
|
|
inline bool is_number(const std::string &to_check)
|
2014-06-25 19:04:47 +02:00
|
|
|
{
|
2017-08-20 13:30:50 +02:00
|
|
|
for (char i : to_check)
|
|
|
|
if (!std::isdigit(i))
|
2014-12-11 23:58:50 +01:00
|
|
|
return false;
|
2014-06-25 19:04:47 +02:00
|
|
|
|
2014-12-11 23:58:50 +01:00
|
|
|
return !to_check.empty();
|
2014-11-02 15:17:20 +01:00
|
|
|
}
|
2014-06-25 19:04:47 +02:00
|
|
|
|
2014-11-02 15:17:20 +01:00
|
|
|
|
|
|
|
/**
|
2014-12-11 23:58:50 +01:00
|
|
|
* Returns a C-string, either "true" or "false", corresponding to \p val.
|
2014-11-02 15:17:20 +01:00
|
|
|
*
|
2014-12-11 23:58:50 +01:00
|
|
|
* @return If \p val is true, then "true" is returned, otherwise "false".
|
2014-11-02 15:17:20 +01:00
|
|
|
*/
|
2014-12-11 23:58:50 +01:00
|
|
|
inline const char *bool_to_cstr(bool val)
|
2014-11-02 15:17:20 +01:00
|
|
|
{
|
2014-12-11 23:58:50 +01:00
|
|
|
return val ? "true" : "false";
|
2014-06-25 19:04:47 +02:00
|
|
|
}
|
|
|
|
|
2017-04-15 23:19:18 +02:00
|
|
|
inline const std::string duration_to_string(int sec)
|
|
|
|
{
|
2017-04-17 13:49:48 +02:00
|
|
|
int min = sec / 60;
|
2017-04-15 23:19:18 +02:00
|
|
|
sec %= 60;
|
2017-04-17 13:49:48 +02:00
|
|
|
int hour = min / 60;
|
2017-04-15 23:19:18 +02:00
|
|
|
min %= 60;
|
|
|
|
|
|
|
|
std::stringstream ss;
|
|
|
|
if (hour > 0) {
|
|
|
|
ss << hour << "h ";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (min > 0) {
|
|
|
|
ss << min << "m ";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sec > 0) {
|
|
|
|
ss << sec << "s ";
|
|
|
|
}
|
|
|
|
|
|
|
|
return ss.str();
|
|
|
|
}
|