From: TimePath Date: Fri, 6 Apr 2018 10:43:30 +0000 (+1000) Subject: Chat: use full-fledged JSON for communication between client/menu X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=864a942f97f5fc9bb38483c857329a8673743191;p=xonotic%2Fxonotic-data.pk3dir.git Chat: use full-fledged JSON for communication between client/menu --- diff --git a/qcsrc/lib/cvar.qh b/qcsrc/lib/cvar.qh index 19b48ee3e..cdd1b23a8 100644 --- a/qcsrc/lib/cvar.qh +++ b/qcsrc/lib/cvar.qh @@ -1,5 +1,6 @@ #pragma once +#include "json.qh" #include "nil.qh" #include "progname.qh" #include "static.qh" @@ -33,24 +34,13 @@ string MakeConsoleSafe(string input) ERASEABLE string console_encode(string input) { - input = strreplace("$", "$$", input); - input = strreplace("\\", "\\\\", input); - input = strreplace("\r", "\\r", input); - input = strreplace("\n", "\\n", input); - input = strreplace("\"", "\\\"", input); - input = sprintf("\"%s\"", input); - return input; + return json_stringify_string(strreplace("$", "$$", input)); } ERASEABLE string console_decode(string input) { - input = substring(input, 1, -2); - input = strreplace("\\r", "\r", input); - input = strreplace("\\n", "\n", input); - input = strreplace("\\\"", "\"", input); - input = strreplace("\\\\", "\\", input); - return input; + return json_parse_string(input); } ERASEABLE diff --git a/qcsrc/lib/json.qc b/qcsrc/lib/json.qc index b477fe15e..c0199264c 100644 --- a/qcsrc/lib/json.qc +++ b/qcsrc/lib/json.qc @@ -299,6 +299,67 @@ void json_dump(int buf) #undef JSON_FAIL #undef JSON_END +ERASEABLE +string json_stringify_string(string s) +{ + string buf = "\""; + FOREACH_CHAR(s, true, { + switch (it) { + default: + buf = strcat(buf, chr2str(it)); + break; + case '\b': + case '\f': + case '\n': + case '\r': + case '\t': + case '\"': + case '\\': + buf = strcat(buf, chr2str('\\', it)); + break; + } + }); + return strcat(buf, "\""); +} + +ERASEABLE +string json_parse_string(string s) +{ + s = substring(s, 1, -2); + string buf = ""; + bool parseEscape = false; + FOREACH_CHAR(s, true, { + switch (it) { + default: + if (!parseEscape) { + buf = strcat(buf, chr2str(it)); + } else { + switch (it) { + // do something sane for invalid escape sequences + default: buf = strcat(buf, "\\", chr2str(it)); break; + case 'b': buf = strcat(buf, "\b"); break; + case 'f': buf = strcat(buf, "\f"); break; + case 'n': buf = strcat(buf, "\n"); break; + case 'r': buf = strcat(buf, "\r"); break; + case 't': buf = strcat(buf, "\t"); break; + case '"': buf = strcat(buf, "\""); break; + } + parseEscape = false; + } + break; + case '\\': + if (!parseEscape) { + parseEscape = true; + } else { + buf = strcat(buf, "\\"); + parseEscape = false; + } + break; + } + }); + return buf; +} + TEST(json, Parse) { string s = "{\n\ diff --git a/qcsrc/lib/json.qh b/qcsrc/lib/json.qh index 6f70f09be..c1f33b1c0 100644 --- a/qcsrc/lib/json.qh +++ b/qcsrc/lib/json.qh @@ -1 +1,4 @@ #pragma once + +string json_stringify_string(string s); +string json_parse_string(string s);