oj 3.9.1 → 3.16.11

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 (171) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1452 -0
  3. data/README.md +21 -6
  4. data/RELEASE_NOTES.md +61 -0
  5. data/ext/oj/buf.h +50 -68
  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 +9 -36
  10. data/ext/oj/circarray.c +38 -42
  11. data/ext/oj/circarray.h +12 -13
  12. data/ext/oj/code.c +158 -179
  13. data/ext/oj/code.h +20 -22
  14. data/ext/oj/compat.c +145 -205
  15. data/ext/oj/custom.c +740 -880
  16. data/ext/oj/debug.c +126 -0
  17. data/ext/oj/dump.c +1145 -844
  18. data/ext/oj/dump.h +71 -57
  19. data/ext/oj/dump_compat.c +575 -655
  20. data/ext/oj/dump_leaf.c +96 -186
  21. data/ext/oj/dump_object.c +533 -660
  22. data/ext/oj/dump_strict.c +306 -340
  23. data/ext/oj/encode.h +4 -33
  24. data/ext/oj/encoder.c +43 -0
  25. data/ext/oj/err.c +28 -28
  26. data/ext/oj/err.h +39 -42
  27. data/ext/oj/extconf.rb +28 -7
  28. data/ext/oj/fast.c +1052 -1113
  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 +471 -430
  34. data/ext/oj/object.c +532 -580
  35. data/ext/oj/odd.c +156 -142
  36. data/ext/oj/odd.h +25 -26
  37. data/ext/oj/oj.c +1346 -961
  38. data/ext/oj/oj.h +307 -290
  39. data/ext/oj/parse.c +954 -858
  40. data/ext/oj/parse.h +74 -72
  41. data/ext/oj/parser.c +1600 -0
  42. data/ext/oj/parser.h +103 -0
  43. data/ext/oj/rails.c +819 -836
  44. data/ext/oj/rails.h +8 -11
  45. data/ext/oj/reader.c +136 -147
  46. data/ext/oj/reader.h +69 -83
  47. data/ext/oj/resolve.c +41 -63
  48. data/ext/oj/resolve.h +4 -6
  49. data/ext/oj/rxclass.c +69 -72
  50. data/ext/oj/rxclass.h +12 -13
  51. data/ext/oj/saj.c +440 -485
  52. data/ext/oj/saj2.c +584 -0
  53. data/ext/oj/saj2.h +23 -0
  54. data/ext/oj/scp.c +79 -118
  55. data/ext/oj/simd.h +10 -0
  56. data/ext/oj/sparse.c +739 -709
  57. data/ext/oj/stream_writer.c +141 -175
  58. data/ext/oj/strict.c +103 -128
  59. data/ext/oj/string_writer.c +244 -261
  60. data/ext/oj/trace.c +34 -41
  61. data/ext/oj/trace.h +42 -15
  62. data/ext/oj/usual.c +1218 -0
  63. data/ext/oj/usual.h +69 -0
  64. data/ext/oj/util.c +107 -107
  65. data/ext/oj/util.h +4 -3
  66. data/ext/oj/val_stack.c +61 -78
  67. data/ext/oj/val_stack.h +80 -114
  68. data/ext/oj/validate.c +46 -0
  69. data/ext/oj/wab.c +316 -361
  70. data/lib/oj/active_support_helper.rb +1 -3
  71. data/lib/oj/bag.rb +8 -1
  72. data/lib/oj/easy_hash.rb +9 -9
  73. data/lib/oj/error.rb +1 -2
  74. data/lib/oj/json.rb +162 -150
  75. data/lib/oj/mimic.rb +54 -20
  76. data/lib/oj/saj.rb +20 -6
  77. data/lib/oj/schandler.rb +5 -4
  78. data/lib/oj/state.rb +12 -8
  79. data/lib/oj/version.rb +1 -2
  80. data/lib/oj.rb +2 -8
  81. data/pages/Compatibility.md +1 -1
  82. data/pages/Encoding.md +1 -1
  83. data/pages/InstallOptions.md +20 -0
  84. data/pages/JsonGem.md +15 -0
  85. data/pages/Modes.md +9 -3
  86. data/pages/Options.md +62 -12
  87. data/pages/Parser.md +309 -0
  88. data/pages/Rails.md +73 -22
  89. metadata +68 -192
  90. data/ext/oj/hash.c +0 -163
  91. data/ext/oj/hash.h +0 -46
  92. data/ext/oj/hash_test.c +0 -512
  93. data/test/_test_active.rb +0 -76
  94. data/test/_test_active_mimic.rb +0 -96
  95. data/test/_test_mimic_rails.rb +0 -126
  96. data/test/activerecord/result_test.rb +0 -27
  97. data/test/activesupport4/decoding_test.rb +0 -108
  98. data/test/activesupport4/encoding_test.rb +0 -531
  99. data/test/activesupport4/test_helper.rb +0 -41
  100. data/test/activesupport5/decoding_test.rb +0 -125
  101. data/test/activesupport5/encoding_test.rb +0 -485
  102. data/test/activesupport5/encoding_test_cases.rb +0 -90
  103. data/test/activesupport5/test_helper.rb +0 -50
  104. data/test/activesupport5/time_zone_test_helpers.rb +0 -24
  105. data/test/bar.rb +0 -25
  106. data/test/files.rb +0 -29
  107. data/test/foo.rb +0 -21
  108. data/test/helper.rb +0 -26
  109. data/test/isolated/shared.rb +0 -308
  110. data/test/isolated/test_mimic_after.rb +0 -13
  111. data/test/isolated/test_mimic_alone.rb +0 -12
  112. data/test/isolated/test_mimic_as_json.rb +0 -45
  113. data/test/isolated/test_mimic_before.rb +0 -13
  114. data/test/isolated/test_mimic_define.rb +0 -28
  115. data/test/isolated/test_mimic_rails_after.rb +0 -22
  116. data/test/isolated/test_mimic_rails_before.rb +0 -21
  117. data/test/isolated/test_mimic_redefine.rb +0 -15
  118. data/test/json_gem/json_addition_test.rb +0 -216
  119. data/test/json_gem/json_common_interface_test.rb +0 -148
  120. data/test/json_gem/json_encoding_test.rb +0 -107
  121. data/test/json_gem/json_ext_parser_test.rb +0 -20
  122. data/test/json_gem/json_fixtures_test.rb +0 -35
  123. data/test/json_gem/json_generator_test.rb +0 -383
  124. data/test/json_gem/json_generic_object_test.rb +0 -90
  125. data/test/json_gem/json_parser_test.rb +0 -470
  126. data/test/json_gem/json_string_matching_test.rb +0 -42
  127. data/test/json_gem/test_helper.rb +0 -18
  128. data/test/perf.rb +0 -107
  129. data/test/perf_compat.rb +0 -130
  130. data/test/perf_fast.rb +0 -164
  131. data/test/perf_file.rb +0 -64
  132. data/test/perf_object.rb +0 -138
  133. data/test/perf_saj.rb +0 -109
  134. data/test/perf_scp.rb +0 -151
  135. data/test/perf_simple.rb +0 -287
  136. data/test/perf_strict.rb +0 -145
  137. data/test/perf_wab.rb +0 -131
  138. data/test/sample/change.rb +0 -14
  139. data/test/sample/dir.rb +0 -19
  140. data/test/sample/doc.rb +0 -36
  141. data/test/sample/file.rb +0 -48
  142. data/test/sample/group.rb +0 -16
  143. data/test/sample/hasprops.rb +0 -16
  144. data/test/sample/layer.rb +0 -12
  145. data/test/sample/line.rb +0 -20
  146. data/test/sample/oval.rb +0 -10
  147. data/test/sample/rect.rb +0 -10
  148. data/test/sample/shape.rb +0 -35
  149. data/test/sample/text.rb +0 -20
  150. data/test/sample.rb +0 -54
  151. data/test/sample_json.rb +0 -37
  152. data/test/test_compat.rb +0 -509
  153. data/test/test_custom.rb +0 -503
  154. data/test/test_debian.rb +0 -53
  155. data/test/test_fast.rb +0 -470
  156. data/test/test_file.rb +0 -239
  157. data/test/test_gc.rb +0 -49
  158. data/test/test_hash.rb +0 -29
  159. data/test/test_integer_range.rb +0 -73
  160. data/test/test_null.rb +0 -376
  161. data/test/test_object.rb +0 -1018
  162. data/test/test_saj.rb +0 -186
  163. data/test/test_scp.rb +0 -433
  164. data/test/test_strict.rb +0 -410
  165. data/test/test_various.rb +0 -741
  166. data/test/test_wab.rb +0 -307
  167. data/test/test_writer.rb +0 -380
  168. data/test/tests.rb +0 -24
  169. data/test/tests_mimic.rb +0 -14
  170. data/test/tests_mimic_addition.rb +0 -7
  171. data/test/zoo.rb +0 -13
data/ext/oj/rails.h CHANGED
@@ -1,21 +1,18 @@
1
- /* rails.h
2
- * Copyright (c) 2017, Peter Ohler
3
- * All rights reserved.
4
- */
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.
5
3
 
6
4
  #ifndef OJ_RAILS_H
7
5
  #define OJ_RAILS_H
8
6
 
9
7
  #include "dump.h"
10
8
 
11
- extern void oj_mimic_rails_init();
12
- 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);
13
11
 
14
- extern bool oj_rails_hash_opt;
15
- extern bool oj_rails_array_opt;
16
- extern bool oj_rails_float_opt;
17
-
18
- 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;
19
15
 
16
+ extern VALUE oj_optimize_rails(VALUE self);
20
17
 
21
18
  #endif /* OJ_RAILS_H */
data/ext/oj/reader.c CHANGED
@@ -1,223 +1,212 @@
1
- /* reader.c
2
- * Copyright (c) 2011, Peter Ohler
3
- * All rights reserved.
4
- */
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.
5
3
 
6
- #include <stdlib.h>
7
4
  #include <errno.h>
8
5
  #include <stdio.h>
6
+ #include <stdlib.h>
9
7
  #include <strings.h>
10
8
  #include <sys/types.h>
9
+ #ifdef NEEDS_UIO
11
10
  #if NEEDS_UIO
12
- #include <sys/uio.h>
11
+ #include <sys/uio.h>
12
+ #endif
13
13
  #endif
14
- #include <unistd.h>
15
14
  #include <time.h>
15
+ #include <unistd.h>
16
16
 
17
- #include "ruby.h"
17
+ #include "mem.h"
18
18
  #include "oj.h"
19
19
  #include "reader.h"
20
+ #include "ruby.h"
20
21
 
21
- #define BUF_PAD 4
22
-
23
- static VALUE rescue_cb(VALUE rdr, VALUE err);
24
- static VALUE io_cb(VALUE rdr);
25
- static VALUE partial_io_cb(VALUE rdr);
26
- static int read_from_io(Reader reader);
27
- static int read_from_fd(Reader reader);
28
- static int read_from_io_partial(Reader reader);
29
- //static int read_from_str(Reader reader);
30
-
31
- void
32
- 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;
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;
48
48
 
49
49
  if (0 != fd) {
50
- reader->read_func = read_from_fd;
51
- reader->fd = fd;
50
+ reader->read_func = read_from_fd;
51
+ reader->fd = fd;
52
52
  } else if (rb_cString == io_class) {
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);
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);
58
58
  } else if (oj_stringio_class == io_class) {
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 &&
67
- Qnil != (stat = rb_funcall(io, oj_stat_id, 0)) &&
68
- Qnil != (ftype = rb_funcall(stat, oj_ftype_id, 0)) &&
69
- 0 == strcmp("file", StringValuePtr(ftype)) &&
70
- 0 == FIX2INT(rb_funcall(io, oj_pos_id, 0))) {
71
- reader->read_func = read_from_fd;
72
- 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));
73
71
  } else if (rb_respond_to(io, oj_readpartial_id)) {
74
- reader->read_func = read_from_io_partial;
75
- reader->io = io;
72
+ reader->read_func = read_from_io_partial;
73
+ reader->io = io;
76
74
  } else if (rb_respond_to(io, oj_read_id)) {
77
- reader->read_func = read_from_io;
78
- reader->io = io;
75
+ reader->read_func = read_from_io;
76
+ reader->io = io;
79
77
  } else if (to_s) {
80
- volatile VALUE rstr = rb_funcall(io, oj_to_s_id, 0);
81
-
82
- reader->read_func = 0;
83
- reader->in_str = StringValuePtr(rstr);
84
- reader->head = (char*)reader->in_str;
85
- reader->tail = reader->head;
86
- reader->read_end = reader->head + RSTRING_LEN(rstr);
87
- } else {
88
- rb_raise(rb_eArgError, "parser io argument must be a String or respond to readpartial() or read().\n");
78
+ volatile VALUE rstr = oj_safe_string_convert(io);
79
+
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);
85
+ } else {
86
+ rb_raise(rb_eArgError, "parser io argument must be a String or respond to readpartial() or read().\n");
89
87
  }
90
88
  }
91
89
 
92
- int
93
- oj_reader_read(Reader reader) {
94
- int err;
95
- size_t shift = 0;
90
+ int oj_reader_read(Reader reader) {
91
+ int err;
92
+ size_t shift = 0;
96
93
 
97
94
  if (0 == reader->read_func) {
98
- return -1;
95
+ return -1;
99
96
  }
100
97
  // if there is not much room to read into, shift or realloc a larger buffer.
101
98
  if (reader->head < reader->tail && 4096 > reader->end - reader->tail) {
102
- if (0 == reader->pro) {
103
- shift = reader->tail - reader->head;
104
- } else {
105
- shift = reader->pro - reader->head - 1; // leave one character so we can backup one
106
- }
107
- if (0 >= shift) { /* no space left so allocate more */
108
- const char *old = reader->head;
109
- size_t size = reader->end - reader->head + BUF_PAD;
110
-
111
- if (reader->head == reader->base) {
112
- reader->head = ALLOC_N(char, size * 2);
113
- memcpy((char*)reader->head, old, size);
114
- } else {
115
- REALLOC_N(reader->head, char, size * 2);
116
- }
117
- reader->free_head = 1;
118
- reader->end = reader->head + size * 2 - BUF_PAD;
119
- reader->tail = reader->head + (reader->tail - old);
120
- reader->read_end = reader->head + (reader->read_end - old);
121
- if (0 != reader->pro) {
122
- reader->pro = reader->head + (reader->pro - old);
123
- }
124
- if (0 != reader->str) {
125
- reader->str = reader->head + (reader->str - old);
126
- }
127
- } else {
128
- memmove((char*)reader->head, reader->head + shift, reader->read_end - (reader->head + shift));
129
- reader->tail -= shift;
130
- reader->read_end -= shift;
131
- if (0 != reader->pro) {
132
- reader->pro -= shift;
133
- }
134
- if (0 != reader->str) {
135
- reader->str -= shift;
136
- }
137
- }
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
+ }
138
135
  }
139
- err = reader->read_func(reader);
140
- *(char*)reader->read_end = '\0';
136
+ err = reader->read_func(reader);
137
+ *(char *)reader->read_end = '\0';
141
138
 
142
139
  return err;
143
140
  }
144
141
 
145
- static VALUE
146
- rescue_cb(VALUE rbuf, VALUE err) {
147
- VALUE clas = rb_obj_class(err);
142
+ static VALUE rescue_cb(VALUE rbuf, VALUE err) {
143
+ VALUE clas = rb_obj_class(err);
148
144
 
149
145
  if (rb_eTypeError != clas && rb_eEOFError != clas) {
150
- Reader reader = (Reader)rbuf;
146
+ Reader reader = (Reader)rbuf;
151
147
 
152
- 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);
153
149
  }
154
150
  return Qfalse;
155
151
  }
156
152
 
157
- static VALUE
158
- partial_io_cb(VALUE rbuf) {
159
- Reader reader = (Reader)rbuf;
160
- VALUE args[1];
161
- VALUE rstr;
162
- char *str;
163
- 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;
164
159
 
165
160
  args[0] = ULONG2NUM(reader->end - reader->tail);
166
- rstr = rb_funcall2(reader->io, oj_readpartial_id, 1, args);
161
+ rstr = rb_funcall2(reader->io, oj_readpartial_id, 1, args);
167
162
  if (Qnil == rstr) {
168
- return Qfalse;
163
+ return Qfalse;
169
164
  }
170
165
  str = StringValuePtr(rstr);
171
166
  cnt = RSTRING_LEN(rstr);
172
- //printf("*** partial read %lu bytes, str: '%s'\n", cnt, str);
173
167
  strcpy(reader->tail, str);
174
168
  reader->read_end = reader->tail + cnt;
175
169
 
176
170
  return Qtrue;
177
171
  }
178
172
 
179
- static VALUE
180
- io_cb(VALUE rbuf) {
181
- Reader reader = (Reader)rbuf;
182
- VALUE args[1];
183
- VALUE rstr;
184
- char *str;
185
- 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;
186
179
 
187
180
  args[0] = ULONG2NUM(reader->end - reader->tail);
188
- rstr = rb_funcall2(reader->io, oj_read_id, 1, args);
181
+ rstr = rb_funcall2(reader->io, oj_read_id, 1, args);
189
182
  if (Qnil == rstr) {
190
- return Qfalse;
183
+ return Qfalse;
191
184
  }
192
185
  str = StringValuePtr(rstr);
193
186
  cnt = RSTRING_LEN(rstr);
194
- //printf("*** read %lu bytes, str: '%s'\n", cnt, str);
195
187
  strcpy(reader->tail, str);
196
188
  reader->read_end = reader->tail + cnt;
197
189
 
198
190
  return Qtrue;
199
191
  }
200
192
 
201
- static int
202
- read_from_io_partial(Reader reader) {
193
+ static int read_from_io_partial(Reader reader) {
203
194
  return (Qfalse == rb_rescue(partial_io_cb, (VALUE)reader, rescue_cb, (VALUE)reader));
204
195
  }
205
196
 
206
- static int
207
- read_from_io(Reader reader) {
197
+ static int read_from_io(Reader reader) {
208
198
  return (Qfalse == rb_rescue(io_cb, (VALUE)reader, rescue_cb, (VALUE)reader));
209
199
  }
210
200
 
211
- static int
212
- read_from_fd(Reader reader) {
213
- ssize_t cnt;
214
- 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;
215
204
 
216
205
  cnt = read(reader->fd, reader->tail, max);
217
206
  if (cnt <= 0) {
218
- return -1;
207
+ return -1;
219
208
  } else if (0 != cnt) {
220
- reader->read_end = reader->tail + cnt;
209
+ reader->read_end = reader->tail + cnt;
221
210
  }
222
211
  return 0;
223
212
  }
data/ext/oj/reader.h CHANGED
@@ -1,92 +1,86 @@
1
- /* reader.h
2
- * Copyright (c) 2011, Peter Ohler
3
- * All rights reserved.
4
- */
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.
5
3
 
6
4
  #ifndef OJ_READER_H
7
5
  #define OJ_READER_H
8
6
 
7
+ #include "mem.h"
8
+
9
9
  typedef struct _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);
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);
22
22
  union {
23
- int fd;
24
- VALUE io;
25
- const char *in_str;
23
+ int fd;
24
+ VALUE io;
25
+ const char *in_str;
26
26
  };
27
27
  } *Reader;
28
28
 
29
- extern void oj_reader_init(Reader reader, VALUE io, int fd, bool to_s);
30
- 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);
31
31
 
32
- static inline char
33
- reader_get(Reader reader) {
34
- //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);
35
35
  if (reader->read_end <= reader->tail) {
36
- if (0 != oj_reader_read(reader)) {
37
- return '\0';
38
- }
36
+ if (0 != oj_reader_read(reader)) {
37
+ return '\0';
38
+ }
39
39
  }
40
40
  if ('\n' == *reader->tail) {
41
- reader->line++;
42
- reader->col = 0;
41
+ reader->line++;
42
+ reader->col = 0;
43
43
  }
44
44
  reader->col++;
45
45
  reader->pos++;
46
-
46
+
47
47
  return *reader->tail++;
48
48
  }
49
49
 
50
- static inline void
51
- reader_backup(Reader reader) {
50
+ static inline void reader_backup(Reader reader) {
52
51
  reader->tail--;
53
52
  reader->col--;
54
53
  reader->pos--;
55
54
  if (0 >= reader->col) {
56
- reader->line--;
57
- // 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
58
57
  }
59
58
  }
60
59
 
61
- static inline void
62
- reader_protect(Reader reader) {
60
+ static inline void reader_protect(Reader reader) {
63
61
  reader->pro = reader->tail;
64
- reader->str = reader->tail; // can't have str before pro
62
+ reader->str = reader->tail; // can't have str before pro
65
63
  }
66
64
 
67
- static inline void
68
- reader_release(Reader reader) {
65
+ static inline void reader_release(Reader reader) {
69
66
  reader->pro = 0;
70
67
  }
71
68
 
72
69
  /* Starts by reading a character so it is safe to use with an empty or
73
70
  * compacted buffer.
74
71
  */
75
- static inline char
76
- reader_next_non_white(Reader reader) {
77
- char c;
72
+ static inline char reader_next_non_white(Reader reader) {
73
+ char c;
78
74
 
79
75
  while ('\0' != (c = reader_get(reader))) {
80
- switch(c) {
81
- case ' ':
82
- case '\t':
83
- case '\f':
84
- case '\n':
85
- case '\r':
86
- break;
87
- default:
88
- return c;
89
- }
76
+ switch (c) {
77
+ case ' ':
78
+ case '\t':
79
+ case '\f':
80
+ case '\n':
81
+ case '\r': break;
82
+ default: return c;
83
+ }
90
84
  }
91
85
  return '\0';
92
86
  }
@@ -94,56 +88,48 @@ reader_next_non_white(Reader reader) {
94
88
  /* Starts by reading a character so it is safe to use with an empty or
95
89
  * compacted buffer.
96
90
  */
97
- static inline char
98
- reader_next_white(Reader reader) {
99
- char c;
91
+ static inline char reader_next_white(Reader reader) {
92
+ char c;
100
93
 
101
94
  while ('\0' != (c = reader_get(reader))) {
102
- switch(c) {
103
- case ' ':
104
- case '\t':
105
- case '\f':
106
- case '\n':
107
- case '\r':
108
- case '\0':
109
- return c;
110
- default:
111
- break;
112
- }
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
+ }
113
104
  }
114
105
  return '\0';
115
106
  }
116
107
 
117
- static inline int
118
- reader_expect(Reader reader, const char *s) {
108
+ static inline int reader_expect(Reader reader, const char *s) {
119
109
  for (; '\0' != *s; s++) {
120
- if (reader_get(reader) != *s) {
121
- return -1;
122
- }
110
+ if (reader_get(reader) != *s) {
111
+ return -1;
112
+ }
123
113
  }
124
114
  return 0;
125
115
  }
126
116
 
127
- static inline void
128
- reader_cleanup(Reader reader) {
117
+ static inline void reader_cleanup(Reader reader) {
129
118
  if (reader->free_head && 0 != reader->head) {
130
- xfree((char*)reader->head);
131
- reader->head = 0;
132
- reader->free_head = 0;
119
+ OJ_R_FREE((char *)reader->head);
120
+ reader->head = 0;
121
+ reader->free_head = 0;
133
122
  }
134
123
  }
135
124
 
136
- static inline int
137
- is_white(char c) {
138
- switch(c) {
125
+ static inline int is_white(char c) {
126
+ switch (c) {
139
127
  case ' ':
140
128
  case '\t':
141
129
  case '\f':
142
130
  case '\n':
143
- case '\r':
144
- return 1;
145
- default:
146
- break;
131
+ case '\r': return 1;
132
+ default: break;
147
133
  }
148
134
  return 0;
149
135
  }