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 <stdlib.h>
+#include <cstdlib>
#include <string>
#include <cstring>
#include <vector>
#include <sstream>
#include <iomanip>
#include <cctype>
+#include <unordered_map>
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
(((unsigned char)(x) < 0xe0) ? 2 : \
(((unsigned char)(x) < 0xf0) ? 3 : 4))
-typedef UNORDERED_MAP<std::string, std::string> StringMap;
+typedef std::unordered_map<std::string, std::string> StringMap;
struct FlagDesc {
const char *name;
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;
}
}
}
+/**
+ * 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.
*
*/
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();
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();
+}