ext/flydata/json/json_ext.cpp in flydata-0.5.15 vs ext/flydata/json/json_ext.cpp in flydata-0.5.16
- old
+ new
@@ -8,10 +8,20 @@
_declspec(dllexport)
#endif
void Init_json_ext();
}
+inline void write_escape_str(const char*& buf, long& buf_len, const char* escape_str, long escape_str_len, std::stringstream& ss)
+{
+ if (buf_len > 0)
+ {
+ ss.write(buf, buf_len);
+ }
+ ss.write(escape_str, escape_str_len);
+ buf = 0; buf_len = 0;
+}
+
inline void add_json_value(const char* buf, long buf_len, bool quote, std::stringstream& ss)
{
if (quote)
{
ss.put('\"');
@@ -19,34 +29,165 @@
const char* ptr = 0;
long len = 0;
const char* esc_str = 0;
for (long i = 0; i < buf_len; i++) {
if (!ptr) { ptr = buf + i; len = 0; }
+
+ if ((buf[i] >= 0x5d && buf[i] < 0x7f) || // small letter alphabet
+ (buf[i] >= 0x30 && buf[i] < 0x5c) || // digit and capital letter alphabet
+ (buf[i] >= 0x80) || // multi-byte utf-8
+ (buf[i] >= 0x23 && buf[i] < 0x2f) || // signs
+ (buf[i] == 0x21) // exclamation mark
+ ) {
+ len++;
+ continue;
+ }
+ // Handle JSON special characters. See http://www.json.org/
switch (buf[i])
{
case '\"':
esc_str = "\\\"";
+ write_escape_str(ptr, len, esc_str, 2, ss);
+ break;
case '\\':
esc_str = "\\\\";
+ write_escape_str(ptr, len, esc_str, 2, ss);
+ break;
case '/':
esc_str = "\\/";
+ write_escape_str(ptr, len, esc_str, 2, ss);
+ break;
+ case '\t':
+ esc_str = "\\t";
+ write_escape_str(ptr, len, esc_str, 2, ss);
+ break;
+ case '\n':
+ esc_str = "\\n";
+ write_escape_str(ptr, len, esc_str, 2, ss);
+ break;
+ case 0x00:
+ esc_str = "\\u0000";
+ write_escape_str(ptr, len, esc_str, 6, ss);
+ break;
+ case 0x01:
+ esc_str = "\\u0001";
+ write_escape_str(ptr, len, esc_str, 6, ss);
+ break;
+ case 0x02:
+ esc_str = "\\u0002";
+ write_escape_str(ptr, len, esc_str, 6, ss);
+ break;
+ case 0x03:
+ esc_str = "\\u0003";
+ write_escape_str(ptr, len, esc_str, 6, ss);
+ break;
+ case 0x04:
+ esc_str = "\\u0004";
+ write_escape_str(ptr, len, esc_str, 6, ss);
+ break;
+ case 0x05:
+ esc_str = "\\u0005";
+ write_escape_str(ptr, len, esc_str, 6, ss);
+ break;
+ case 0x06:
+ esc_str = "\\u0006";
+ write_escape_str(ptr, len, esc_str, 6, ss);
+ break;
+ case '\a':
+ esc_str = "\\u0007";
+ write_escape_str(ptr, len, esc_str, 6, ss);
+ break;
case '\b':
esc_str = "\\b";
+ write_escape_str(ptr, len, esc_str, 2, ss);
+ break;
+ case 0x0b:
+ esc_str = "\\u000b";
+ write_escape_str(ptr, len, esc_str, 6, ss);
+ break;
case '\f':
esc_str = "\\f";
- case '\n':
- esc_str = "\\n";
+ write_escape_str(ptr, len, esc_str, 2, ss);
+ break;
case '\r':
esc_str = "\\r";
- case '\t':
- esc_str = "\\t";
-
- if (len > 0)
- {
- ss.write(ptr, len);
- }
- ss.write(esc_str, 2);
- ptr = 0; len = 0;
+ write_escape_str(ptr, len, esc_str, 2, ss);
+ break;
+ case 0x0e:
+ esc_str = "\\u000e";
+ write_escape_str(ptr, len, esc_str, 6, ss);
+ break;
+ case 0x0f:
+ esc_str = "\\u000f";
+ write_escape_str(ptr, len, esc_str, 6, ss);
+ break;
+ case 0x10:
+ esc_str = "\\u0010";
+ write_escape_str(ptr, len, esc_str, 6, ss);
+ break;
+ case 0x11:
+ esc_str = "\\u0011";
+ write_escape_str(ptr, len, esc_str, 6, ss);
+ break;
+ case 0x12:
+ esc_str = "\\u0012";
+ write_escape_str(ptr, len, esc_str, 6, ss);
+ break;
+ case 0x13:
+ esc_str = "\\u0013";
+ write_escape_str(ptr, len, esc_str, 6, ss);
+ break;
+ case 0x14:
+ esc_str = "\\u0014";
+ write_escape_str(ptr, len, esc_str, 6, ss);
+ break;
+ case 0x15:
+ esc_str = "\\u0015";
+ write_escape_str(ptr, len, esc_str, 6, ss);
+ break;
+ case 0x16:
+ esc_str = "\\u0016";
+ write_escape_str(ptr, len, esc_str, 6, ss);
+ break;
+ case 0x17:
+ esc_str = "\\u0017";
+ write_escape_str(ptr, len, esc_str, 6, ss);
+ break;
+ case 0x18:
+ esc_str = "\\u0018";
+ write_escape_str(ptr, len, esc_str, 6, ss);
+ break;
+ case 0x19:
+ esc_str = "\\u0019";
+ write_escape_str(ptr, len, esc_str, 6, ss);
+ break;
+ case 0x1a:
+ esc_str = "\\u001a";
+ write_escape_str(ptr, len, esc_str, 6, ss);
+ break;
+ case 0x1b:
+ esc_str = "\\u001b";
+ write_escape_str(ptr, len, esc_str, 6, ss);
+ break;
+ case 0x1c:
+ esc_str = "\\u001c";
+ write_escape_str(ptr, len, esc_str, 6, ss);
+ break;
+ case 0x1d:
+ esc_str = "\\u001d";
+ write_escape_str(ptr, len, esc_str, 6, ss);
+ break;
+ case 0x1e:
+ esc_str = "\\u001e";
+ write_escape_str(ptr, len, esc_str, 6, ss);
+ break;
+ case 0x1f:
+ esc_str = "\\u001f";
+ write_escape_str(ptr, len, esc_str, 6, ss);
+ break;
+ case 0x7f:
+ esc_str = "\\u007f";
+ write_escape_str(ptr, len, esc_str, 6, ss);
break;
default:
len++;
break;
}