rapidjson 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/ext/rapidjson/json_escape.h +90 -0
- data/lib/rapidjson/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 17354e68c40c730955b333eacdc5232255f5c0e12605a2e35ea0a90dd84def19
|
4
|
+
data.tar.gz: c3d95c72fb9e4416f33007bf7076ed616e82bd138b2637e0a2e0a57a5b55faed
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5d3dc6b671e1cc4110c457a8551fdf05b62ef03ff355a93531406b32853fd0b9174bc6a9ae3b743f891fd1ff4fe8eb95506b36bff9753ca1ed8f60970e694ed7
|
7
|
+
data.tar.gz: 26195d47a4c2a68b190f0d46da8e09d2df7125b89ad53478758c9db74bf59e65c5a0a6b9fb10f47372da45fe25e269a0c6e48c44b36af10f3f22222739e70497
|
@@ -0,0 +1,90 @@
|
|
1
|
+
#include "ruby.h"
|
2
|
+
#include "ruby/encoding.h"
|
3
|
+
|
4
|
+
/* strlen("\\u2029") == 6 */
|
5
|
+
#define ESCAPE_JSON_MAX_LEN 6
|
6
|
+
|
7
|
+
static inline long
|
8
|
+
json_escaped_length(VALUE str)
|
9
|
+
{
|
10
|
+
const long len = RSTRING_LEN(str);
|
11
|
+
if (len >= LONG_MAX / ESCAPE_JSON_MAX_LEN) {
|
12
|
+
ruby_malloc_size_overflow(len, ESCAPE_JSON_MAX_LEN);
|
13
|
+
}
|
14
|
+
return len * ESCAPE_JSON_MAX_LEN;
|
15
|
+
}
|
16
|
+
|
17
|
+
static VALUE
|
18
|
+
escape_json(VALUE self, VALUE str)
|
19
|
+
{
|
20
|
+
if (!RB_TYPE_P(str, T_STRING)) {
|
21
|
+
str = rb_convert_type(str, T_STRING, "String", "to_s");
|
22
|
+
}
|
23
|
+
|
24
|
+
rb_encoding *enc = rb_enc_get(str);
|
25
|
+
if (enc != rb_utf8_encoding() && enc != rb_usascii_encoding()) {
|
26
|
+
rb_raise(rb_eEncCompatError, "input string must be UTF-8 or ASCII");
|
27
|
+
}
|
28
|
+
|
29
|
+
const char *cstr = RSTRING_PTR(str);
|
30
|
+
const unsigned long str_len = RSTRING_LEN(str);
|
31
|
+
const char *end = cstr + RSTRING_LEN(str);
|
32
|
+
|
33
|
+
size_t initial_match = strcspn(cstr, "&<>\xe2");
|
34
|
+
if (initial_match == str_len) {
|
35
|
+
return str;
|
36
|
+
}
|
37
|
+
|
38
|
+
VALUE escaped = rb_str_buf_new(json_escaped_length(str));
|
39
|
+
rb_str_resize(escaped, json_escaped_length(str));
|
40
|
+
char *buf = RSTRING_PTR(escaped);
|
41
|
+
char *dest = buf;
|
42
|
+
|
43
|
+
memcpy(dest, cstr, initial_match);
|
44
|
+
cstr += initial_match;
|
45
|
+
dest += initial_match;
|
46
|
+
|
47
|
+
while (cstr < end) {
|
48
|
+
const char c = *cstr++;
|
49
|
+
|
50
|
+
#define JSON_ESCAPE_CONCAT(s) do { \
|
51
|
+
memcpy(dest, ("\\u" s), strlen(s) + 2); \
|
52
|
+
dest += strlen(s) + 2; \
|
53
|
+
} while (0)
|
54
|
+
|
55
|
+
if (0) {
|
56
|
+
}
|
57
|
+
else if (c == '&') {
|
58
|
+
JSON_ESCAPE_CONCAT("0026");
|
59
|
+
}
|
60
|
+
else if (c == '>') {
|
61
|
+
JSON_ESCAPE_CONCAT("003e");
|
62
|
+
}
|
63
|
+
else if (c == '<') {
|
64
|
+
JSON_ESCAPE_CONCAT("003c");
|
65
|
+
}
|
66
|
+
else if (c == '\xe2' && cstr[0] == '\x80' && cstr[1] == '\xa8') {
|
67
|
+
JSON_ESCAPE_CONCAT("2028");
|
68
|
+
cstr += 2;
|
69
|
+
}
|
70
|
+
else if (c == '\xe2' && cstr[0] == '\x80' && cstr[1] == '\xa9') {
|
71
|
+
JSON_ESCAPE_CONCAT("2029");
|
72
|
+
cstr += 2;
|
73
|
+
}
|
74
|
+
else {
|
75
|
+
*dest++ = c;
|
76
|
+
}
|
77
|
+
|
78
|
+
initial_match = strcspn(cstr, "&<>\xe2");
|
79
|
+
memcpy(dest, cstr, initial_match);
|
80
|
+
cstr += initial_match;
|
81
|
+
dest += initial_match;
|
82
|
+
|
83
|
+
#undef JSON_ESCAPE_CONCAT
|
84
|
+
}
|
85
|
+
|
86
|
+
rb_str_resize(escaped, dest - buf);
|
87
|
+
rb_enc_associate(escaped, rb_enc_get(str));
|
88
|
+
|
89
|
+
return escaped;
|
90
|
+
}
|
data/lib/rapidjson/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rapidjson
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Hawthorn
|
@@ -27,6 +27,7 @@ files:
|
|
27
27
|
- ext/rapidjson/cext.hh
|
28
28
|
- ext/rapidjson/encoder.hh
|
29
29
|
- ext/rapidjson/extconf.rb
|
30
|
+
- ext/rapidjson/json_escape.h
|
30
31
|
- ext/rapidjson/parser.hh
|
31
32
|
- ext/rapidjson/rapidjson/include/rapidjson/allocators.h
|
32
33
|
- ext/rapidjson/rapidjson/include/rapidjson/cursorstreamwrapper.h
|
@@ -92,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
92
93
|
- !ruby/object:Gem::Version
|
93
94
|
version: '0'
|
94
95
|
requirements: []
|
95
|
-
rubygems_version: 3.
|
96
|
+
rubygems_version: 3.3.26
|
96
97
|
signing_key:
|
97
98
|
specification_version: 4
|
98
99
|
summary: Fast JSON encoder/decoder based using RapidJSON
|