Skip to content

Commit

Permalink
Merge pull request qminer#624 from gregorleban/ER.v7.3
Browse files Browse the repository at this point in the history
- updated TJsonVal::AddEscapeChAFromStr method as suggested by Janez …
  • Loading branch information
blazf authored Jun 22, 2018
2 parents 007afe7 + 714099d commit 6ac3300
Showing 1 changed file with 36 additions and 57 deletions.
93 changes: 36 additions & 57 deletions src/glib/base/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,69 +403,48 @@ PJsonVal TJsonVal::GetValFromStr(const TStr& JsonStr){
}

void TJsonVal::AddEscapeChAFromStr(const TStr& Str, TChA& ChA){
if (TUnicodeDef::IsDef()) {
static TUniCodec UniCodec(uehIgnore, false, TUniCodec::DefaultReplacementChar, false);
// parse the UTF8 string
TIntV UStr; TUnicodeDef::GetDef()->DecodeUtf8(Str, UStr);
TIntV UStr; UniCodec.DecodeUtf8(Str, UStr, false);
// escape the string
for (int ChN = 0; ChN < UStr.Len(); ChN++) {
const int UCh = UStr[ChN];
if (UCh < 0x80) {
// 7-bit ascii
const char Ch = (char)UCh;
switch (Ch) {
case '"' : ChA.AddCh('\\'); ChA.AddCh('"'); break;
case '\\' : ChA.AddCh('\\'); ChA.AddCh('\\'); break;
case '/' : ChA.AddCh('\\'); ChA.AddCh('/'); break;
case '\b' : ChA.AddCh('\\'); ChA.AddCh('b'); break;
case '\f' : ChA.AddCh('\\'); ChA.AddCh('f'); break;
case '\n' : ChA.AddCh('\\'); ChA.AddCh('n'); break;
case '\r' : ChA.AddCh('\\'); ChA.AddCh('r'); break;
case '\t' : ChA.AddCh('\\'); ChA.AddCh('t'); break;
default :
ChA.AddCh(Ch);
const int UCh = UStr[ChN];
if (UCh < 0x80) {
// 7-bit ascii
const char Ch = (char) UCh;
switch (Ch) {
case '"': ChA.AddCh('\\'); ChA.AddCh('"'); break;
case '\\': ChA.AddCh('\\'); ChA.AddCh('\\'); break;
case '/': ChA.AddCh('\\'); ChA.AddCh('/'); break;
case '\b': ChA.AddCh('\\'); ChA.AddCh('b'); break;
case '\f': ChA.AddCh('\\'); ChA.AddCh('f'); break;
case '\n': ChA.AddCh('\\'); ChA.AddCh('n'); break;
case '\r': ChA.AddCh('\\'); ChA.AddCh('r'); break;
case '\t': ChA.AddCh('\\'); ChA.AddCh('t'); break;
default:
if (UCh < 32) { ChA += "\\u"; ChA += TStr::Fmt("%04x", UCh); }
else ChA.AddCh(Ch);
}
}
} else {
// escape
EAssertR(UCh <= 0x10FFFF, "Unable to JSON encode character U+" + TStr(UCh));
if (UCh <= 0xFFFF) {
ChA += "\\u";
ChA += TStr::Fmt("%04x", UCh);
} else {
// U+10000 .. U+10FFFF
int UChH = 0xD800 + ((UCh - 0x010000) >> 10);
int UChL = 0xDC00 + ((UCh - 0x010000) & 0x3FF);
ChA += "\\u";
ChA += TStr::Fmt("%04x", UChH);
ChA += "\\u";
ChA += TStr::Fmt("%04x", UChL);
}
}
}
} else {
// escape the string
for (int ChN = 0; ChN < Str.Len(); ChN++) {
const char Ch = Str[ChN];
if ((Ch & 0x80) == 0) {
// 7-bit ascii
switch (Ch) {
case '"' : ChA.AddCh('\\'); ChA.AddCh('"'); break;
case '\\' : ChA.AddCh('\\'); ChA.AddCh('\\'); break;
case '/' : ChA.AddCh('\\'); ChA.AddCh('/'); break;
case '\b' : ChA.AddCh('\\'); ChA.AddCh('b'); break;
case '\f' : ChA.AddCh('\\'); ChA.AddCh('f'); break;
case '\n' : ChA.AddCh('\\'); ChA.AddCh('n'); break;
case '\r' : ChA.AddCh('\\'); ChA.AddCh('r'); break;
case '\t' : ChA.AddCh('\\'); ChA.AddCh('t'); break;
default : ChA.AddCh(Ch);
else {
// escape
EAssertR(UCh <= 0x10FFFF, "Unable to JSON encode character U+" +
TStr(UCh));
if (UCh <= 0xFFFF) {
ChA += "\\u";
ChA += TStr::Fmt("%04x", UCh);
}
else {
// U+10000 .. U+10FFFF
int UChH = 0xD800 + ((UCh - 0x010000) >> 10);
int UChL = 0xDC00 + ((UCh - 0x010000) & 0x3FF);
ChA += "\\u";
ChA += TStr::Fmt("%04x", UChH);
ChA += "\\u";
ChA += TStr::Fmt("%04x", UChL);
}
}
} else {
printf("Warning: no TUnicodeDef, possible errors when escaping unicode characters!");
// escape
ChA += "\\u";
ChA += TStr::Fmt("%02x", (int)Ch);
}
}
}
}

void TJsonVal::AddQChAFromStr(const TStr& Str, TChA& ChA){
Expand Down

0 comments on commit 6ac3300

Please sign in to comment.