oj 3.11.0 → 3.16.5

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.
Files changed (173) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1421 -0
  3. data/README.md +20 -5
  4. data/RELEASE_NOTES.md +61 -0
  5. data/ext/oj/buf.h +48 -38
  6. data/ext/oj/cache.c +329 -0
  7. data/ext/oj/cache.h +22 -0
  8. data/ext/oj/cache8.c +60 -62
  9. data/ext/oj/cache8.h +8 -7
  10. data/ext/oj/circarray.c +35 -35
  11. data/ext/oj/circarray.h +11 -9
  12. data/ext/oj/code.c +156 -174
  13. data/ext/oj/code.h +19 -18
  14. data/ext/oj/compat.c +140 -197
  15. data/ext/oj/custom.c +737 -879
  16. data/ext/oj/debug.c +126 -0
  17. data/ext/oj/dump.c +830 -835
  18. data/ext/oj/dump.h +65 -53
  19. data/ext/oj/dump_compat.c +566 -642
  20. data/ext/oj/dump_leaf.c +95 -182
  21. data/ext/oj/dump_object.c +518 -659
  22. data/ext/oj/dump_strict.c +301 -334
  23. data/ext/oj/encode.h +3 -4
  24. data/ext/oj/encoder.c +43 -0
  25. data/ext/oj/err.c +27 -24
  26. data/ext/oj/err.h +38 -13
  27. data/ext/oj/extconf.rb +23 -7
  28. data/ext/oj/fast.c +1043 -1073
  29. data/ext/oj/intern.c +313 -0
  30. data/ext/oj/intern.h +22 -0
  31. data/ext/oj/mem.c +318 -0
  32. data/ext/oj/mem.h +53 -0
  33. data/ext/oj/mimic_json.c +449 -423
  34. data/ext/oj/object.c +530 -576
  35. data/ext/oj/odd.c +155 -138
  36. data/ext/oj/odd.h +24 -22
  37. data/ext/oj/oj.c +1331 -993
  38. data/ext/oj/oj.h +306 -292
  39. data/ext/oj/parse.c +934 -938
  40. data/ext/oj/parse.h +73 -70
  41. data/ext/oj/parser.c +1600 -0
  42. data/ext/oj/parser.h +101 -0
  43. data/ext/oj/rails.c +795 -845
  44. data/ext/oj/rails.h +7 -7
  45. data/ext/oj/reader.c +132 -140
  46. data/ext/oj/reader.h +67 -78
  47. data/ext/oj/resolve.c +40 -59
  48. data/ext/oj/resolve.h +3 -2
  49. data/ext/oj/rxclass.c +67 -67
  50. data/ext/oj/rxclass.h +11 -9
  51. data/ext/oj/saj.c +441 -480
  52. data/ext/oj/saj2.c +584 -0
  53. data/ext/oj/saj2.h +23 -0
  54. data/ext/oj/scp.c +78 -111
  55. data/ext/oj/sparse.c +726 -730
  56. data/ext/oj/stream_writer.c +146 -165
  57. data/ext/oj/strict.c +103 -123
  58. data/ext/oj/string_writer.c +241 -253
  59. data/ext/oj/trace.c +29 -33
  60. data/ext/oj/trace.h +41 -11
  61. data/ext/oj/usual.c +1218 -0
  62. data/ext/oj/usual.h +69 -0
  63. data/ext/oj/util.c +103 -103
  64. data/ext/oj/util.h +3 -2
  65. data/ext/oj/val_stack.c +60 -49
  66. data/ext/oj/val_stack.h +79 -85
  67. data/ext/oj/validate.c +46 -0
  68. data/ext/oj/wab.c +307 -350
  69. data/lib/oj/active_support_helper.rb +1 -3
  70. data/lib/oj/bag.rb +8 -1
  71. data/lib/oj/easy_hash.rb +9 -9
  72. data/lib/oj/error.rb +1 -2
  73. data/lib/oj/json.rb +162 -150
  74. data/lib/oj/mimic.rb +9 -19
  75. data/lib/oj/saj.rb +20 -6
  76. data/lib/oj/schandler.rb +5 -4
  77. data/lib/oj/state.rb +12 -8
  78. data/lib/oj/version.rb +1 -2
  79. data/lib/oj.rb +2 -0
  80. data/pages/Compatibility.md +1 -1
  81. data/pages/InstallOptions.md +20 -0
  82. data/pages/JsonGem.md +15 -0
  83. data/pages/Modes.md +8 -3
  84. data/pages/Options.md +43 -5
  85. data/pages/Parser.md +309 -0
  86. data/pages/Rails.md +14 -2
  87. data/test/_test_active.rb +8 -9
  88. data/test/_test_active_mimic.rb +7 -8
  89. data/test/_test_mimic_rails.rb +17 -20
  90. data/test/activerecord/result_test.rb +12 -8
  91. data/test/activesupport6/encoding_test.rb +63 -28
  92. data/test/{activesupport5 → activesupport7}/abstract_unit.rb +16 -12
  93. data/test/{activesupport5 → activesupport7}/decoding_test.rb +2 -10
  94. data/test/{activesupport5 → activesupport7}/encoding_test.rb +86 -50
  95. data/test/{activesupport5 → activesupport7}/encoding_test_cases.rb +6 -0
  96. data/test/{activesupport5 → activesupport7}/time_zone_test_helpers.rb +8 -0
  97. data/test/files.rb +15 -15
  98. data/test/foo.rb +17 -43
  99. data/test/helper.rb +16 -3
  100. data/test/isolated/shared.rb +3 -2
  101. data/test/json_gem/json_addition_test.rb +2 -2
  102. data/test/json_gem/json_common_interface_test.rb +8 -6
  103. data/test/json_gem/json_encoding_test.rb +0 -0
  104. data/test/json_gem/json_ext_parser_test.rb +1 -0
  105. data/test/json_gem/json_fixtures_test.rb +3 -2
  106. data/test/json_gem/json_generator_test.rb +71 -41
  107. data/test/json_gem/json_generic_object_test.rb +11 -11
  108. data/test/json_gem/json_parser_test.rb +54 -47
  109. data/test/json_gem/json_string_matching_test.rb +9 -9
  110. data/test/json_gem/test_helper.rb +12 -0
  111. data/test/mem.rb +34 -0
  112. data/test/perf.rb +22 -27
  113. data/test/perf_compat.rb +31 -33
  114. data/test/perf_dump.rb +50 -0
  115. data/test/perf_fast.rb +80 -82
  116. data/test/perf_file.rb +27 -29
  117. data/test/perf_object.rb +65 -69
  118. data/test/perf_once.rb +59 -0
  119. data/test/perf_parser.rb +183 -0
  120. data/test/perf_saj.rb +46 -54
  121. data/test/perf_scp.rb +58 -69
  122. data/test/perf_simple.rb +41 -39
  123. data/test/perf_strict.rb +74 -82
  124. data/test/perf_wab.rb +67 -69
  125. data/test/prec.rb +5 -5
  126. data/test/sample/change.rb +0 -1
  127. data/test/sample/dir.rb +0 -1
  128. data/test/sample/doc.rb +0 -1
  129. data/test/sample/file.rb +0 -1
  130. data/test/sample/group.rb +0 -1
  131. data/test/sample/hasprops.rb +0 -1
  132. data/test/sample/layer.rb +0 -1
  133. data/test/sample/rect.rb +0 -1
  134. data/test/sample/shape.rb +0 -1
  135. data/test/sample/text.rb +0 -1
  136. data/test/sample.rb +16 -16
  137. data/test/sample_json.rb +8 -8
  138. data/test/test_compat.rb +97 -45
  139. data/test/test_custom.rb +73 -51
  140. data/test/test_debian.rb +7 -10
  141. data/test/test_fast.rb +135 -79
  142. data/test/test_file.rb +41 -30
  143. data/test/test_gc.rb +16 -5
  144. data/test/test_generate.rb +21 -0
  145. data/test/test_hash.rb +15 -5
  146. data/test/test_integer_range.rb +9 -9
  147. data/test/test_null.rb +20 -20
  148. data/test/test_object.rb +99 -96
  149. data/test/test_parser.rb +11 -0
  150. data/test/test_parser_debug.rb +27 -0
  151. data/test/test_parser_saj.rb +337 -0
  152. data/test/test_parser_usual.rb +251 -0
  153. data/test/test_rails.rb +2 -2
  154. data/test/test_saj.rb +10 -8
  155. data/test/test_scp.rb +38 -40
  156. data/test/test_strict.rb +40 -32
  157. data/test/test_various.rb +165 -84
  158. data/test/test_wab.rb +48 -44
  159. data/test/test_writer.rb +47 -47
  160. data/test/tests.rb +13 -5
  161. data/test/tests_mimic.rb +12 -3
  162. data/test/tests_mimic_addition.rb +12 -3
  163. metadata +75 -127
  164. data/ext/oj/hash.c +0 -135
  165. data/ext/oj/hash.h +0 -18
  166. data/ext/oj/hash_test.c +0 -484
  167. data/test/activesupport4/decoding_test.rb +0 -108
  168. data/test/activesupport4/encoding_test.rb +0 -531
  169. data/test/activesupport4/test_helper.rb +0 -41
  170. data/test/activesupport5/test_helper.rb +0 -72
  171. data/test/bar.rb +0 -35
  172. data/test/baz.rb +0 -16
  173. data/test/zoo.rb +0 -13
data/ext/oj/rails.h CHANGED
@@ -1,18 +1,18 @@
1
1
  // Copyright (c) 2017 Peter Ohler. All rights reserved.
2
+ // Licensed under the MIT License. See LICENSE file in the project root for license details.
2
3
 
3
4
  #ifndef OJ_RAILS_H
4
5
  #define OJ_RAILS_H
5
6
 
6
7
  #include "dump.h"
7
8
 
8
- extern void oj_mimic_rails_init();
9
- extern ROpt oj_rails_get_opt(ROptTable rot, VALUE clas);
9
+ extern void oj_mimic_rails_init(void);
10
+ extern ROpt oj_rails_get_opt(ROptTable rot, VALUE clas);
10
11
 
11
- extern bool oj_rails_hash_opt;
12
- extern bool oj_rails_array_opt;
13
- extern bool oj_rails_float_opt;
14
-
15
- extern VALUE oj_optimize_rails(VALUE self);
12
+ extern bool oj_rails_hash_opt;
13
+ extern bool oj_rails_array_opt;
14
+ extern bool oj_rails_float_opt;
16
15
 
16
+ extern VALUE oj_optimize_rails(VALUE self);
17
17
 
18
18
  #endif /* OJ_RAILS_H */
data/ext/oj/reader.c CHANGED
@@ -1,220 +1,212 @@
1
1
  // Copyright (c) 2011 Peter Ohler. All rights reserved.
2
+ // Licensed under the MIT License. See LICENSE file in the project root for license details.
2
3
 
3
- #include <stdlib.h>
4
4
  #include <errno.h>
5
5
  #include <stdio.h>
6
+ #include <stdlib.h>
6
7
  #include <strings.h>
7
8
  #include <sys/types.h>
9
+ #ifdef NEEDS_UIO
8
10
  #if NEEDS_UIO
9
11
  #include <sys/uio.h>
10
12
  #endif
11
- #include <unistd.h>
13
+ #endif
12
14
  #include <time.h>
15
+ #include <unistd.h>
13
16
 
14
- #include "ruby.h"
17
+ #include "mem.h"
15
18
  #include "oj.h"
16
19
  #include "reader.h"
20
+ #include "ruby.h"
17
21
 
18
- #define BUF_PAD 4
19
-
20
- static VALUE rescue_cb(VALUE rdr, VALUE err);
21
- static VALUE io_cb(VALUE rdr);
22
- static VALUE partial_io_cb(VALUE rdr);
23
- static int read_from_io(Reader reader);
24
- static int read_from_fd(Reader reader);
25
- static int read_from_io_partial(Reader reader);
26
- //static int read_from_str(Reader reader);
27
-
28
- void
29
- oj_reader_init(Reader reader, VALUE io, int fd, bool to_s) {
30
- VALUE io_class = rb_obj_class(io);
31
- VALUE stat;
32
- VALUE ftype;
33
-
34
- reader->head = reader->base;
35
- *((char*)reader->head) = '\0';
36
- reader->end = reader->head + sizeof(reader->base) - BUF_PAD;
37
- reader->tail = reader->head;
38
- reader->read_end = reader->head;
39
- reader->pro = 0;
40
- reader->str = 0;
41
- reader->pos = 0;
42
- reader->line = 1;
43
- reader->col = 0;
44
- reader->free_head = 0;
22
+ #define BUF_PAD 4
23
+
24
+ static VALUE rescue_cb(VALUE rdr, VALUE err);
25
+ static VALUE io_cb(VALUE rdr);
26
+ static VALUE partial_io_cb(VALUE rdr);
27
+ static int read_from_io(Reader reader);
28
+ static int read_from_fd(Reader reader);
29
+ static int read_from_io_partial(Reader reader);
30
+ // static int read_from_str(Reader reader);
31
+
32
+ void oj_reader_init(Reader reader, VALUE io, int fd, bool to_s) {
33
+ VALUE io_class = rb_obj_class(io);
34
+ VALUE stat;
35
+ VALUE ftype;
36
+
37
+ reader->head = reader->base;
38
+ *((char *)reader->head) = '\0';
39
+ reader->end = reader->head + sizeof(reader->base) - BUF_PAD;
40
+ reader->tail = reader->head;
41
+ reader->read_end = reader->head;
42
+ reader->pro = 0;
43
+ reader->str = 0;
44
+ reader->pos = 0;
45
+ reader->line = 1;
46
+ reader->col = 0;
47
+ reader->free_head = 0;
45
48
 
46
49
  if (0 != fd) {
47
- reader->read_func = read_from_fd;
48
- reader->fd = fd;
50
+ reader->read_func = read_from_fd;
51
+ reader->fd = fd;
49
52
  } else if (rb_cString == io_class) {
50
- reader->read_func = 0;
51
- reader->in_str = StringValuePtr(io);
52
- reader->head = (char*)reader->in_str;
53
- reader->tail = reader->head;
54
- reader->read_end = reader->head + RSTRING_LEN(io);
53
+ reader->read_func = 0;
54
+ reader->in_str = StringValuePtr(io);
55
+ reader->head = (char *)reader->in_str;
56
+ reader->tail = reader->head;
57
+ reader->read_end = reader->head + RSTRING_LEN(io);
55
58
  } else if (oj_stringio_class == io_class) {
56
- VALUE s = rb_funcall2(io, oj_string_id, 0, 0);
57
-
58
- reader->read_func = 0;
59
- reader->in_str = StringValuePtr(s);
60
- reader->head = (char*)reader->in_str;
61
- reader->tail = reader->head;
62
- reader->read_end = reader->head + RSTRING_LEN(s);
63
- } else if (rb_cFile == io_class &&
64
- Qnil != (stat = rb_funcall(io, oj_stat_id, 0)) &&
65
- Qnil != (ftype = rb_funcall(stat, oj_ftype_id, 0)) &&
66
- 0 == strcmp("file", StringValuePtr(ftype)) &&
67
- 0 == FIX2INT(rb_funcall(io, oj_pos_id, 0))) {
68
- reader->read_func = read_from_fd;
69
- reader->fd = FIX2INT(rb_funcall(io, oj_fileno_id, 0));
59
+ VALUE s = rb_funcall2(io, oj_string_id, 0, 0);
60
+
61
+ reader->read_func = 0;
62
+ reader->in_str = StringValuePtr(s);
63
+ reader->head = (char *)reader->in_str;
64
+ reader->tail = reader->head;
65
+ reader->read_end = reader->head + RSTRING_LEN(s);
66
+ } else if (rb_cFile == io_class && Qnil != (stat = rb_funcall(io, oj_stat_id, 0)) &&
67
+ Qnil != (ftype = rb_funcall(stat, oj_ftype_id, 0)) && 0 == strcmp("file", StringValuePtr(ftype)) &&
68
+ 0 == FIX2INT(rb_funcall(io, oj_pos_id, 0))) {
69
+ reader->read_func = read_from_fd;
70
+ reader->fd = FIX2INT(rb_funcall(io, oj_fileno_id, 0));
70
71
  } else if (rb_respond_to(io, oj_readpartial_id)) {
71
- reader->read_func = read_from_io_partial;
72
- reader->io = io;
72
+ reader->read_func = read_from_io_partial;
73
+ reader->io = io;
73
74
  } else if (rb_respond_to(io, oj_read_id)) {
74
- reader->read_func = read_from_io;
75
- reader->io = io;
75
+ reader->read_func = read_from_io;
76
+ reader->io = io;
76
77
  } else if (to_s) {
77
- volatile VALUE rstr = rb_funcall(io, oj_to_s_id, 0);
78
+ volatile VALUE rstr = oj_safe_string_convert(io);
78
79
 
79
- reader->read_func = 0;
80
- reader->in_str = StringValuePtr(rstr);
81
- reader->head = (char*)reader->in_str;
82
- reader->tail = reader->head;
83
- reader->read_end = reader->head + RSTRING_LEN(rstr);
80
+ reader->read_func = 0;
81
+ reader->in_str = StringValuePtr(rstr);
82
+ reader->head = (char *)reader->in_str;
83
+ reader->tail = reader->head;
84
+ reader->read_end = reader->head + RSTRING_LEN(rstr);
84
85
  } else {
85
- rb_raise(rb_eArgError, "parser io argument must be a String or respond to readpartial() or read().\n");
86
+ rb_raise(rb_eArgError, "parser io argument must be a String or respond to readpartial() or read().\n");
86
87
  }
87
88
  }
88
89
 
89
- int
90
- oj_reader_read(Reader reader) {
91
- int err;
92
- size_t shift = 0;
90
+ int oj_reader_read(Reader reader) {
91
+ int err;
92
+ size_t shift = 0;
93
93
 
94
94
  if (0 == reader->read_func) {
95
- return -1;
95
+ return -1;
96
96
  }
97
97
  // if there is not much room to read into, shift or realloc a larger buffer.
98
98
  if (reader->head < reader->tail && 4096 > reader->end - reader->tail) {
99
- if (0 == reader->pro) {
100
- shift = reader->tail - reader->head;
101
- } else {
102
- shift = reader->pro - reader->head - 1; // leave one character so we can backup one
103
- }
104
- if (0 >= shift) { /* no space left so allocate more */
105
- const char *old = reader->head;
106
- size_t size = reader->end - reader->head + BUF_PAD;
107
-
108
- if (reader->head == reader->base) {
109
- reader->head = ALLOC_N(char, size * 2);
110
- memcpy((char*)reader->head, old, size);
111
- } else {
112
- REALLOC_N(reader->head, char, size * 2);
113
- }
114
- reader->free_head = 1;
115
- reader->end = reader->head + size * 2 - BUF_PAD;
116
- reader->tail = reader->head + (reader->tail - old);
117
- reader->read_end = reader->head + (reader->read_end - old);
118
- if (0 != reader->pro) {
119
- reader->pro = reader->head + (reader->pro - old);
120
- }
121
- if (0 != reader->str) {
122
- reader->str = reader->head + (reader->str - old);
123
- }
124
- } else {
125
- memmove((char*)reader->head, reader->head + shift, reader->read_end - (reader->head + shift));
126
- reader->tail -= shift;
127
- reader->read_end -= shift;
128
- if (0 != reader->pro) {
129
- reader->pro -= shift;
130
- }
131
- if (0 != reader->str) {
132
- reader->str -= shift;
133
- }
134
- }
99
+ if (0 == reader->pro) {
100
+ shift = reader->tail - reader->head;
101
+ } else {
102
+ shift = reader->pro - reader->head - 1; // leave one character so we can backup one
103
+ }
104
+ if (0 >= shift) { /* no space left so allocate more */
105
+ const char *old = reader->head;
106
+ size_t size = reader->end - reader->head + BUF_PAD;
107
+
108
+ if (reader->head == reader->base) {
109
+ reader->head = OJ_R_ALLOC_N(char, size * 2);
110
+ memcpy((char *)reader->head, old, size);
111
+ } else {
112
+ OJ_R_REALLOC_N(reader->head, char, size * 2);
113
+ }
114
+ reader->free_head = 1;
115
+ reader->end = reader->head + size * 2 - BUF_PAD;
116
+ reader->tail = reader->head + (reader->tail - old);
117
+ reader->read_end = reader->head + (reader->read_end - old);
118
+ if (0 != reader->pro) {
119
+ reader->pro = reader->head + (reader->pro - old);
120
+ }
121
+ if (0 != reader->str) {
122
+ reader->str = reader->head + (reader->str - old);
123
+ }
124
+ } else {
125
+ memmove((char *)reader->head, reader->head + shift, reader->read_end - (reader->head + shift));
126
+ reader->tail -= shift;
127
+ reader->read_end -= shift;
128
+ if (0 != reader->pro) {
129
+ reader->pro -= shift;
130
+ }
131
+ if (0 != reader->str) {
132
+ reader->str -= shift;
133
+ }
134
+ }
135
135
  }
136
- err = reader->read_func(reader);
137
- *(char*)reader->read_end = '\0';
136
+ err = reader->read_func(reader);
137
+ *(char *)reader->read_end = '\0';
138
138
 
139
139
  return err;
140
140
  }
141
141
 
142
- static VALUE
143
- rescue_cb(VALUE rbuf, VALUE err) {
144
- VALUE clas = rb_obj_class(err);
142
+ static VALUE rescue_cb(VALUE rbuf, VALUE err) {
143
+ VALUE clas = rb_obj_class(err);
145
144
 
146
145
  if (rb_eTypeError != clas && rb_eEOFError != clas) {
147
- Reader reader = (Reader)rbuf;
146
+ Reader reader = (Reader)rbuf;
148
147
 
149
- rb_raise(clas, "at line %d, column %d\n", reader->line, reader->col);
148
+ rb_raise(clas, "at line %d, column %d\n", reader->line, reader->col);
150
149
  }
151
150
  return Qfalse;
152
151
  }
153
152
 
154
- static VALUE
155
- partial_io_cb(VALUE rbuf) {
156
- Reader reader = (Reader)rbuf;
157
- VALUE args[1];
158
- VALUE rstr;
159
- char *str;
160
- size_t cnt;
153
+ static VALUE partial_io_cb(VALUE rbuf) {
154
+ Reader reader = (Reader)rbuf;
155
+ VALUE args[1];
156
+ VALUE rstr;
157
+ char *str;
158
+ size_t cnt;
161
159
 
162
160
  args[0] = ULONG2NUM(reader->end - reader->tail);
163
- rstr = rb_funcall2(reader->io, oj_readpartial_id, 1, args);
161
+ rstr = rb_funcall2(reader->io, oj_readpartial_id, 1, args);
164
162
  if (Qnil == rstr) {
165
- return Qfalse;
163
+ return Qfalse;
166
164
  }
167
165
  str = StringValuePtr(rstr);
168
166
  cnt = RSTRING_LEN(rstr);
169
- //printf("*** partial read %lu bytes, str: '%s'\n", cnt, str);
170
167
  strcpy(reader->tail, str);
171
168
  reader->read_end = reader->tail + cnt;
172
169
 
173
170
  return Qtrue;
174
171
  }
175
172
 
176
- static VALUE
177
- io_cb(VALUE rbuf) {
178
- Reader reader = (Reader)rbuf;
179
- VALUE args[1];
180
- VALUE rstr;
181
- char *str;
182
- size_t cnt;
173
+ static VALUE io_cb(VALUE rbuf) {
174
+ Reader reader = (Reader)rbuf;
175
+ VALUE args[1];
176
+ VALUE rstr;
177
+ char *str;
178
+ size_t cnt;
183
179
 
184
180
  args[0] = ULONG2NUM(reader->end - reader->tail);
185
- rstr = rb_funcall2(reader->io, oj_read_id, 1, args);
181
+ rstr = rb_funcall2(reader->io, oj_read_id, 1, args);
186
182
  if (Qnil == rstr) {
187
- return Qfalse;
183
+ return Qfalse;
188
184
  }
189
185
  str = StringValuePtr(rstr);
190
186
  cnt = RSTRING_LEN(rstr);
191
- //printf("*** read %lu bytes, str: '%s'\n", cnt, str);
192
187
  strcpy(reader->tail, str);
193
188
  reader->read_end = reader->tail + cnt;
194
189
 
195
190
  return Qtrue;
196
191
  }
197
192
 
198
- static int
199
- read_from_io_partial(Reader reader) {
193
+ static int read_from_io_partial(Reader reader) {
200
194
  return (Qfalse == rb_rescue(partial_io_cb, (VALUE)reader, rescue_cb, (VALUE)reader));
201
195
  }
202
196
 
203
- static int
204
- read_from_io(Reader reader) {
197
+ static int read_from_io(Reader reader) {
205
198
  return (Qfalse == rb_rescue(io_cb, (VALUE)reader, rescue_cb, (VALUE)reader));
206
199
  }
207
200
 
208
- static int
209
- read_from_fd(Reader reader) {
210
- ssize_t cnt;
211
- size_t max = reader->end - reader->tail;
201
+ static int read_from_fd(Reader reader) {
202
+ ssize_t cnt;
203
+ size_t max = reader->end - reader->tail;
212
204
 
213
205
  cnt = read(reader->fd, reader->tail, max);
214
206
  if (cnt <= 0) {
215
- return -1;
207
+ return -1;
216
208
  } else if (0 != cnt) {
217
- reader->read_end = reader->tail + cnt;
209
+ reader->read_end = reader->tail + cnt;
218
210
  }
219
211
  return 0;
220
212
  }
data/ext/oj/reader.h CHANGED
@@ -1,42 +1,45 @@
1
1
  // Copyright (c) 2011 Peter Ohler. All rights reserved.
2
+ // Licensed under the MIT License. See LICENSE file in the project root for license details.
2
3
 
3
4
  #ifndef OJ_READER_H
4
5
  #define OJ_READER_H
5
6
 
7
+ #include "mem.h"
8
+
6
9
  typedef struct _reader {
7
- char base[0x00001000];
8
- char *head;
9
- char *end;
10
- char *tail;
11
- char *read_end; /* one past last character read */
12
- char *pro; /* protection start, buffer can not slide past this point */
13
- char *str; /* start of current string being read */
14
- long pos;
15
- int line;
16
- int col;
17
- int free_head;
18
- int (*read_func)(struct _reader *reader);
10
+ char base[0x00001000];
11
+ char *head;
12
+ char *end;
13
+ char *tail;
14
+ char *read_end; /* one past last character read */
15
+ char *pro; /* protection start, buffer can not slide past this point */
16
+ char *str; /* start of current string being read */
17
+ long pos;
18
+ int line;
19
+ int col;
20
+ int free_head;
21
+ int (*read_func)(struct _reader *reader);
19
22
  union {
20
- int fd;
21
- VALUE io;
22
- const char *in_str;
23
+ int fd;
24
+ VALUE io;
25
+ const char *in_str;
23
26
  };
24
27
  } *Reader;
25
28
 
26
- extern void oj_reader_init(Reader reader, VALUE io, int fd, bool to_s);
27
- extern int oj_reader_read(Reader reader);
29
+ extern void oj_reader_init(Reader reader, VALUE io, int fd, bool to_s);
30
+ extern int oj_reader_read(Reader reader);
28
31
 
29
- static inline char
30
- reader_get(Reader reader) {
31
- //printf("*** drive get from '%s' from start: %ld buf: %p from read_end: %ld\n", reader->tail, reader->tail - reader->head, reader->head, reader->read_end - reader->tail);
32
+ static inline char reader_get(Reader reader) {
33
+ // printf("*** drive get from '%s' from start: %ld buf: %p from read_end: %ld\n",
34
+ // reader->tail, reader->tail - reader->head, reader->head, reader->read_end - reader->tail);
32
35
  if (reader->read_end <= reader->tail) {
33
- if (0 != oj_reader_read(reader)) {
34
- return '\0';
35
- }
36
+ if (0 != oj_reader_read(reader)) {
37
+ return '\0';
38
+ }
36
39
  }
37
40
  if ('\n' == *reader->tail) {
38
- reader->line++;
39
- reader->col = 0;
41
+ reader->line++;
42
+ reader->col = 0;
40
43
  }
41
44
  reader->col++;
42
45
  reader->pos++;
@@ -44,46 +47,40 @@ reader_get(Reader reader) {
44
47
  return *reader->tail++;
45
48
  }
46
49
 
47
- static inline void
48
- reader_backup(Reader reader) {
50
+ static inline void reader_backup(Reader reader) {
49
51
  reader->tail--;
50
52
  reader->col--;
51
53
  reader->pos--;
52
54
  if (0 >= reader->col) {
53
- reader->line--;
54
- // allow col to be negative since we never backup twice in a row
55
+ reader->line--;
56
+ // allow col to be negative since we never backup twice in a row
55
57
  }
56
58
  }
57
59
 
58
- static inline void
59
- reader_protect(Reader reader) {
60
+ static inline void reader_protect(Reader reader) {
60
61
  reader->pro = reader->tail;
61
- reader->str = reader->tail; // can't have str before pro
62
+ reader->str = reader->tail; // can't have str before pro
62
63
  }
63
64
 
64
- static inline void
65
- reader_release(Reader reader) {
65
+ static inline void reader_release(Reader reader) {
66
66
  reader->pro = 0;
67
67
  }
68
68
 
69
69
  /* Starts by reading a character so it is safe to use with an empty or
70
70
  * compacted buffer.
71
71
  */
72
- static inline char
73
- reader_next_non_white(Reader reader) {
74
- char c;
72
+ static inline char reader_next_non_white(Reader reader) {
73
+ char c;
75
74
 
76
75
  while ('\0' != (c = reader_get(reader))) {
77
- switch(c) {
78
- case ' ':
79
- case '\t':
80
- case '\f':
81
- case '\n':
82
- case '\r':
83
- break;
84
- default:
85
- return c;
86
- }
76
+ switch (c) {
77
+ case ' ':
78
+ case '\t':
79
+ case '\f':
80
+ case '\n':
81
+ case '\r': break;
82
+ default: return c;
83
+ }
87
84
  }
88
85
  return '\0';
89
86
  }
@@ -91,56 +88,48 @@ reader_next_non_white(Reader reader) {
91
88
  /* Starts by reading a character so it is safe to use with an empty or
92
89
  * compacted buffer.
93
90
  */
94
- static inline char
95
- reader_next_white(Reader reader) {
96
- char c;
91
+ static inline char reader_next_white(Reader reader) {
92
+ char c;
97
93
 
98
94
  while ('\0' != (c = reader_get(reader))) {
99
- switch(c) {
100
- case ' ':
101
- case '\t':
102
- case '\f':
103
- case '\n':
104
- case '\r':
105
- case '\0':
106
- return c;
107
- default:
108
- break;
109
- }
95
+ switch (c) {
96
+ case ' ':
97
+ case '\t':
98
+ case '\f':
99
+ case '\n':
100
+ case '\r':
101
+ case '\0': return c;
102
+ default: break;
103
+ }
110
104
  }
111
105
  return '\0';
112
106
  }
113
107
 
114
- static inline int
115
- reader_expect(Reader reader, const char *s) {
108
+ static inline int reader_expect(Reader reader, const char *s) {
116
109
  for (; '\0' != *s; s++) {
117
- if (reader_get(reader) != *s) {
118
- return -1;
119
- }
110
+ if (reader_get(reader) != *s) {
111
+ return -1;
112
+ }
120
113
  }
121
114
  return 0;
122
115
  }
123
116
 
124
- static inline void
125
- reader_cleanup(Reader reader) {
117
+ static inline void reader_cleanup(Reader reader) {
126
118
  if (reader->free_head && 0 != reader->head) {
127
- xfree((char*)reader->head);
128
- reader->head = 0;
129
- reader->free_head = 0;
119
+ OJ_R_FREE((char *)reader->head);
120
+ reader->head = 0;
121
+ reader->free_head = 0;
130
122
  }
131
123
  }
132
124
 
133
- static inline int
134
- is_white(char c) {
135
- switch(c) {
125
+ static inline int is_white(char c) {
126
+ switch (c) {
136
127
  case ' ':
137
128
  case '\t':
138
129
  case '\f':
139
130
  case '\n':
140
- case '\r':
141
- return 1;
142
- default:
143
- break;
131
+ case '\r': return 1;
132
+ default: break;
144
133
  }
145
134
  return 0;
146
135
  }