X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=src%2Futil%2Fserialize.cpp;h=61d369bc487f3a5ae8099c228cb09568237bc47d;hb=984e063374c032ed17764931fd00c19afb92ebb9;hp=ced5fc7cf910d70dc384e00d91f204d89a93bc45;hpb=1a5b4b38f3457a8f1423305cef4e85d05da47d62;p=oweals%2Fminetest.git diff --git a/src/util/serialize.cpp b/src/util/serialize.cpp index ced5fc7cf..61d369bc4 100644 --- a/src/util/serialize.cpp +++ b/src/util/serialize.cpp @@ -229,7 +229,7 @@ std::string deSerializeLongString(std::istream &is) Buffer buf2(s_size); is.read(&buf2[0], s_size); - if (is.gcount() != s_size) + if ((u32)is.gcount() != s_size) throw SerializationError("deSerializeLongString: couldn't read all chars"); s.reserve(s_size); @@ -354,6 +354,55 @@ std::string deSerializeJsonString(std::istream &is) return os.str(); } +std::string serializeJsonStringIfNeeded(const std::string &s) +{ + for (size_t i = 0; i < s.size(); ++i) { + if (s[i] <= 0x1f || s[i] >= 0x7f || s[i] == ' ' || s[i] == '\"') + return serializeJsonString(s); + } + return s; +} + +std::string deSerializeJsonStringIfNeeded(std::istream &is) +{ + std::ostringstream tmp_os; + bool expect_initial_quote = true; + bool is_json = false; + bool was_backslash = false; + for (;;) { + char c = is.get(); + if (is.eof()) + break; + + if (expect_initial_quote && c == '"') { + tmp_os << c; + is_json = true; + } else if(is_json) { + tmp_os << c; + if (was_backslash) + was_backslash = false; + else if (c == '\\') + was_backslash = true; + else if (c == '"') + break; // Found end of string + } else { + if (c == ' ') { + // Found end of word + is.unget(); + break; + } else { + tmp_os << c; + } + } + expect_initial_quote = false; + } + if (is_json) { + std::istringstream tmp_is(tmp_os.str(), std::ios::binary); + return deSerializeJsonString(tmp_is); + } else + return tmp_os.str(); +} + //// //// String/Struct conversions ////