X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Futil%2Fstring.h;h=54a5a458eaa5124ebfe7aaf1f6e03a1234235b00;hb=61ed56f916f71a708b56260d7cba2160e8166529;hp=6c48adeb3d70b0368f4f62b5a7a13d4ebcd03081;hpb=ba78194636a9a498f6979cc21cd39399f23d658a;p=minetest.git diff --git a/src/util/string.h b/src/util/string.h index 6c48adeb3..54a5a458e 100644 --- a/src/util/string.h +++ b/src/util/string.h @@ -21,18 +21,24 @@ with this program; if not, write to the Free Software Foundation, Inc., #define UTIL_STRING_HEADER #include "../irrlichttypes.h" -#include "../strfnd.h" // For trim() -#include "pointer.h" +#include #include #include #include #include +#include + +#define STRINGIFY(x) #x +#define TOSTRING(x) STRINGIFY(x) struct FlagDesc { const char *name; u32 flag; }; +std::wstring narrow_to_wide(const std::string& mbs); +std::string wide_to_narrow(const std::wstring& wcs); + static inline std::string padStringRight(std::string s, size_t len) { if(len > s.size()) @@ -95,29 +101,6 @@ inline bool str_starts_with(const std::wstring& str, const std::wstring& prefix, return true; } -inline std::wstring narrow_to_wide(const std::string& mbs) -{ - size_t wcl = mbs.size(); - Buffer wcs(wcl+1); - size_t l = mbstowcs(*wcs, mbs.c_str(), wcl); - if(l == (size_t)(-1)) - return L""; - wcs[l] = 0; - return *wcs; -} - -inline std::string wide_to_narrow(const std::wstring& wcs) -{ - size_t mbl = wcs.size()*4; - SharedBuffer mbs(mbl+1); - size_t l = wcstombs(*mbs, wcs.c_str(), mbl); - if(l == (size_t)(-1)) - mbs[0] = 0; - else - mbs[l] = 0; - return *mbs; -} - // Split a string using the given delimiter. Returns a vector containing // the component parts. inline std::vector str_split(const std::wstring &str, wchar_t delimiter) @@ -130,6 +113,16 @@ inline std::vector str_split(const std::wstring &str, wchar_t deli return parts; } +inline std::vector str_split(const std::string &str, char delimiter) { + + std::vector parts; + std::stringstream sstr(str); + std::string part; + while(std::getline(sstr, part, delimiter)) + parts.push_back(part); + return parts; +} + inline std::string lowercase(const std::string &s) { std::string s2; @@ -143,10 +136,33 @@ inline std::string lowercase(const std::string &s) return s2; } +inline std::string trim(const std::string &s) +{ + size_t front = 0; + while(s[front] == ' ' || + s[front] == '\t' || + s[front] == '\r' || + s[front] == '\n' + ) + ++front; + + size_t back = s.size(); + while(back > front && + (s[back-1] == ' ' || + s[back-1] == '\t' || + s[back-1] == '\r' || + s[back-1] == '\n' + ) + ) + --back; + + return s.substr(front, back - front); +} + inline bool is_yes(const std::string &s) { std::string s2 = lowercase(trim(s)); - if(s2 == "y" || s2 == "yes" || s2 == "true" || s2 == "1") + if(s2 == "y" || s2 == "yes" || s2 == "true" || atoi(s2.c_str()) != 0) return true; return false; } @@ -161,6 +177,12 @@ inline s32 mystoi(const std::string &s, s32 min, s32 max) return i; } +inline s64 stoi64(const std::string &s) { + std::stringstream tmp(s); + s64 t; + tmp >> t; + return t; +} // MSVC2010 includes it's own versions of these //#if !defined(_MSC_VER) || _MSC_VER < 1600 @@ -198,6 +220,12 @@ inline std::string itos(s32 i) return o.str(); } +inline std::string i64tos(s64 i) { + std::ostringstream o; + o<