X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Futil%2Fstring.h;h=122262af85348d227c10ba94c3fd5fb902df9f89;hb=1c1c97cbd1d7913ac12bf550ec02c97f843a0fd3;hp=572c371502c5965167c764d3110cd72488034b2d;hpb=ce106a4113de048718ab9f396db3d368c35218fc;p=minetest.git diff --git a/src/util/string.h b/src/util/string.h index 572c37150..122262af8 100644 --- a/src/util/string.h +++ b/src/util/string.h @@ -17,12 +17,10 @@ with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#ifndef UTIL_STRING_HEADER -#define UTIL_STRING_HEADER +#pragma once #include "irrlichttypes_bloated.h" -#include "cpp11_container.h" -#include +#include #include #include #include @@ -30,6 +28,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include #include +#include #define STRINGIFY(x) #x #define TOSTRING(x) STRINGIFY(x) @@ -55,7 +54,7 @@ with this program; if not, write to the Free Software Foundation, Inc., (((unsigned char)(x) < 0xe0) ? 2 : \ (((unsigned char)(x) < 0xf0) ? 3 : 4)) -typedef UNORDERED_MAP StringMap; +typedef std::unordered_map StringMap; struct FlagDesc { const char *name; @@ -236,8 +235,8 @@ inline std::string lowercase(const std::string &str) s2.reserve(str.size()); - for (size_t i = 0; i < str.size(); i++) - s2 += tolower(str[i]); + for (char i : str) + s2 += tolower(i); return s2; } @@ -422,6 +421,18 @@ inline void str_replace(std::string &str, const std::string &pattern, } } +/** + * 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, ",", "\\,"); +} + /** * Replace all occurrences of the character \p from in \p str with \p to. * @@ -596,8 +607,8 @@ std::vector > split(const std::basic_string &s, T delim) */ inline bool is_number(const std::string &to_check) { - for (size_t i = 0; i < to_check.size(); i++) - if (!std::isdigit(to_check[i])) + for (char i : to_check) + if (!std::isdigit(i)) return false; return !to_check.empty(); @@ -614,4 +625,25 @@ inline const char *bool_to_cstr(bool val) return val ? "true" : "false"; } -#endif +inline const std::string duration_to_string(int sec) +{ + int min = sec / 60; + sec %= 60; + int hour = min / 60; + 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(); +}