rapidjson 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4824f0636d8f801de840ebf32bd639258ce1e21a063228beaabd2b4ad6e1760a
4
- data.tar.gz: 04bdbf36fdfd8b2ef289d8ba6e68b9d34c731cd1ffae61c154dd276ed56dc35e
3
+ metadata.gz: 17354e68c40c730955b333eacdc5232255f5c0e12605a2e35ea0a90dd84def19
4
+ data.tar.gz: c3d95c72fb9e4416f33007bf7076ed616e82bd138b2637e0a2e0a57a5b55faed
5
5
  SHA512:
6
- metadata.gz: 74f3d0a620d505b0aa55601830f294794e8a938ee087d9fcc228fc6a83452c2b255b1ddf9c0b3aea1d086808259e6f75744fcd6fb9528361090d2bea9d35a612
7
- data.tar.gz: cff30c2bafab60f467ee59cf8a63691f93dd2de1fb5f07573e54a1303f3f279f9100ec9e1c6991cf5c9e874c25df3f6dd52b3e549aef3e510d0d068ce50f9877
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
+ }
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RapidJSON
4
- VERSION = "0.2.0"
4
+ VERSION = "0.2.1"
5
5
  end
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.0
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.4.10
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