Fixed JSON lib errors with characters >= 0x80
This commit is contained in:
parent
bfb2019c3a
commit
bd5cce8d6a
1 changed files with 19 additions and 15 deletions
34
lib/json.cpp
34
lib/json.cpp
|
@ -8,14 +8,14 @@
|
||||||
#include <string.h> //for memcpy
|
#include <string.h> //for memcpy
|
||||||
#include <arpa/inet.h> //for htonl
|
#include <arpa/inet.h> //for htonl
|
||||||
|
|
||||||
static int c2hex(char c){
|
static inline char c2hex(char c){
|
||||||
if (c >= '0' && c <= '9') return c - '0';
|
if (c >= '0' && c <= '9') return c - '0';
|
||||||
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
|
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
|
||||||
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
|
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char hex2c(char c){
|
static inline char hex2c(char c){
|
||||||
if (c < 10){return '0' + c;}
|
if (c < 10){return '0' + c;}
|
||||||
if (c < 16){return 'A' + (c - 10);}
|
if (c < 16){return 'A' + (c - 10);}
|
||||||
return '0';
|
return '0';
|
||||||
|
@ -25,7 +25,8 @@ static std::string read_string(int separator, std::istream & fromstream){
|
||||||
std::string out;
|
std::string out;
|
||||||
bool escaped = false;
|
bool escaped = false;
|
||||||
while (fromstream.good()){
|
while (fromstream.good()){
|
||||||
int c = fromstream.get();
|
char c;
|
||||||
|
fromstream.get(c);
|
||||||
if (c == '\\'){
|
if (c == '\\'){
|
||||||
escaped = true;
|
escaped = true;
|
||||||
continue;
|
continue;
|
||||||
|
@ -48,15 +49,18 @@ static std::string read_string(int separator, std::istream & fromstream){
|
||||||
out += '\t';
|
out += '\t';
|
||||||
break;
|
break;
|
||||||
case 'u': {
|
case 'u': {
|
||||||
int d1 = fromstream.get();
|
char d1, d2, d3, d4;
|
||||||
int d2 = fromstream.get();
|
fromstream.get(d1);
|
||||||
int d3 = fromstream.get();
|
fromstream.get(d2);
|
||||||
int d4 = fromstream.get();
|
fromstream.get(d3);
|
||||||
c = c2hex(d4) + (c2hex(d3) << 4) + (c2hex(d2) << 8) + (c2hex(d1) << 16);
|
fromstream.get(d4);
|
||||||
|
out.append(1, (c2hex(d4) + (c2hex(d3) << 4)));
|
||||||
|
//We ignore the upper two characters.
|
||||||
|
// + (c2hex(d2) << 8) + (c2hex(d1) << 16)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
out += (char)c;
|
out.append(1, c);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
escaped = false;
|
escaped = false;
|
||||||
|
@ -64,7 +68,7 @@ static std::string read_string(int separator, std::istream & fromstream){
|
||||||
if (c == separator){
|
if (c == separator){
|
||||||
return out;
|
return out;
|
||||||
}else{
|
}else{
|
||||||
out += (char)c;
|
out.append(1, c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -74,7 +78,7 @@ static std::string read_string(int separator, std::istream & fromstream){
|
||||||
static std::string string_escape(const std::string val){
|
static std::string string_escape(const std::string val){
|
||||||
std::string out = "\"";
|
std::string out = "\"";
|
||||||
for (unsigned int i = 0; i < val.size(); ++i){
|
for (unsigned int i = 0; i < val.size(); ++i){
|
||||||
switch (val[i]){
|
switch (val.data()[i]){
|
||||||
case '"':
|
case '"':
|
||||||
out += "\\\"";
|
out += "\\\"";
|
||||||
break;
|
break;
|
||||||
|
@ -97,12 +101,12 @@ static std::string string_escape(const std::string val){
|
||||||
out += "\\t";
|
out += "\\t";
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if (val[i] < 32 || val[i] > 126){
|
if (val.data()[i] < 32 || val.data()[i] > 126){
|
||||||
out += "\\u00";
|
out += "\\u00";
|
||||||
out += hex2c((val[i] >> 4) & 0xf);
|
out += hex2c((val.data()[i] >> 4) & 0xf);
|
||||||
out += hex2c(val[i] & 0xf);
|
out += hex2c(val.data()[i] & 0xf);
|
||||||
}else{
|
}else{
|
||||||
out += val[i];
|
out += val.data()[i];
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue