oj 2.18.5 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +33 -226
- data/ext/oj/circarray.c +0 -25
- data/ext/oj/circarray.h +0 -25
- data/ext/oj/code.c +227 -0
- data/ext/oj/code.h +40 -0
- data/ext/oj/compat.c +126 -38
- data/ext/oj/custom.c +1097 -0
- data/ext/oj/dump.c +658 -2376
- data/ext/oj/dump.h +92 -0
- data/ext/oj/dump_compat.c +937 -0
- data/ext/oj/dump_leaf.c +254 -0
- data/ext/oj/dump_object.c +810 -0
- data/ext/oj/dump_rails.c +329 -0
- data/ext/oj/dump_strict.c +416 -0
- data/ext/oj/err.c +0 -25
- data/ext/oj/err.h +8 -2
- data/ext/oj/fast.c +24 -24
- data/ext/oj/mimic_json.c +817 -0
- data/ext/oj/mimic_rails.c +806 -0
- data/ext/oj/mimic_rails.h +17 -0
- data/ext/oj/object.c +18 -72
- data/ext/oj/odd.c +0 -25
- data/ext/oj/odd.h +2 -27
- data/ext/oj/oj.c +655 -1503
- data/ext/oj/oj.h +93 -40
- data/ext/oj/parse.c +99 -46
- data/ext/oj/parse.h +12 -26
- data/ext/oj/reader.c +1 -25
- data/ext/oj/reader.h +3 -25
- data/ext/oj/resolve.c +9 -11
- data/ext/oj/resolve.h +2 -2
- data/ext/oj/rxclass.c +133 -0
- data/ext/oj/rxclass.h +27 -0
- data/ext/oj/saj.c +4 -25
- data/ext/oj/scp.c +3 -25
- data/ext/oj/sparse.c +89 -13
- data/ext/oj/stream_writer.c +301 -0
- data/ext/oj/strict.c +4 -27
- data/ext/oj/string_writer.c +480 -0
- data/ext/oj/val_stack.h +6 -2
- data/lib/oj.rb +1 -23
- data/lib/oj/easy_hash.rb +12 -4
- data/lib/oj/json.rb +172 -0
- data/lib/oj/mimic.rb +123 -18
- data/lib/oj/state.rb +131 -0
- data/lib/oj/version.rb +1 -1
- data/pages/Advanced.md +22 -0
- data/pages/Compatibility.md +25 -0
- data/pages/Custom.md +23 -0
- data/pages/Encoding.md +65 -0
- data/pages/JsonGem.md +79 -0
- data/pages/Modes.md +140 -0
- data/pages/Options.md +250 -0
- data/pages/Rails.md +60 -0
- data/pages/Security.md +20 -0
- data/test/activesupport4/decoding_test.rb +105 -0
- data/test/activesupport4/encoding_test.rb +531 -0
- data/test/activesupport4/test_helper.rb +41 -0
- data/test/activesupport5/decoding_test.rb +125 -0
- data/test/activesupport5/encoding_test.rb +483 -0
- data/test/activesupport5/encoding_test_cases.rb +90 -0
- data/test/activesupport5/test_helper.rb +50 -0
- data/test/activesupport5/time_zone_test_helpers.rb +24 -0
- data/test/json_gem/json_addition_test.rb +216 -0
- data/test/json_gem/json_common_interface_test.rb +143 -0
- data/test/json_gem/json_encoding_test.rb +109 -0
- data/test/json_gem/json_ext_parser_test.rb +20 -0
- data/test/json_gem/json_fixtures_test.rb +35 -0
- data/test/json_gem/json_generator_test.rb +383 -0
- data/test/json_gem/json_generic_object_test.rb +90 -0
- data/test/json_gem/json_parser_test.rb +470 -0
- data/test/json_gem/json_string_matching_test.rb +42 -0
- data/test/json_gem/test_helper.rb +18 -0
- data/test/perf_compat.rb +30 -28
- data/test/perf_object.rb +1 -1
- data/test/perf_strict.rb +18 -1
- data/test/sample.rb +0 -1
- data/test/test_compat.rb +169 -93
- data/test/test_custom.rb +355 -0
- data/test/test_file.rb +0 -8
- data/test/test_null.rb +376 -0
- data/test/test_object.rb +268 -3
- data/test/test_scp.rb +22 -1
- data/test/test_strict.rb +160 -4
- data/test/test_various.rb +52 -620
- data/test/tests.rb +14 -0
- data/test/tests_mimic.rb +14 -0
- data/test/tests_mimic_addition.rb +7 -0
- metadata +89 -47
- data/test/activesupport_datetime_test.rb +0 -23
- data/test/bug.rb +0 -51
- data/test/bug2.rb +0 -10
- data/test/bug3.rb +0 -46
- data/test/bug_fast.rb +0 -32
- data/test/bug_load.rb +0 -24
- data/test/crash.rb +0 -111
- data/test/curl/curl_oj.rb +0 -46
- data/test/curl/get_oj.rb +0 -24
- data/test/curl/just_curl.rb +0 -31
- data/test/curl/just_oj.rb +0 -51
- data/test/example.rb +0 -11
- data/test/foo.rb +0 -24
- data/test/io.rb +0 -48
- data/test/isolated/test_mimic_rails_datetime.rb +0 -27
- data/test/mod.rb +0 -16
- data/test/rails.rb +0 -50
- data/test/russian.rb +0 -18
- data/test/struct.rb +0 -29
- data/test/test_serializer.rb +0 -59
- data/test/write_timebars.rb +0 -31
data/ext/oj/parse.h
CHANGED
@@ -1,31 +1,6 @@
|
|
1
1
|
/* parse.h
|
2
2
|
* Copyright (c) 2011, Peter Ohler
|
3
3
|
* All rights reserved.
|
4
|
-
*
|
5
|
-
* Redistribution and use in source and binary forms, with or without
|
6
|
-
* modification, are permitted provided that the following conditions are met:
|
7
|
-
*
|
8
|
-
* - Redistributions of source code must retain the above copyright notice, this
|
9
|
-
* list of conditions and the following disclaimer.
|
10
|
-
*
|
11
|
-
* - Redistributions in binary form must reproduce the above copyright notice,
|
12
|
-
* this list of conditions and the following disclaimer in the documentation
|
13
|
-
* and/or other materials provided with the distribution.
|
14
|
-
*
|
15
|
-
* - Neither the name of Peter Ohler nor the names of its contributors may be
|
16
|
-
* used to endorse or promote products derived from this software without
|
17
|
-
* specific prior written permission.
|
18
|
-
*
|
19
|
-
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
20
|
-
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
21
|
-
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
22
|
-
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
23
|
-
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
24
|
-
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
25
|
-
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
26
|
-
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
27
|
-
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
28
|
-
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
29
4
|
*/
|
30
5
|
|
31
6
|
#ifndef __OJ_PARSE_H__
|
@@ -33,12 +8,16 @@
|
|
33
8
|
|
34
9
|
#include <stdarg.h>
|
35
10
|
#include <stdio.h>
|
11
|
+
#include <string.h>
|
36
12
|
|
37
13
|
#include "ruby.h"
|
38
14
|
#include "oj.h"
|
39
15
|
#include "val_stack.h"
|
40
16
|
#include "circarray.h"
|
41
17
|
#include "reader.h"
|
18
|
+
#include "rxclass.h"
|
19
|
+
|
20
|
+
struct _RxClass;
|
42
21
|
|
43
22
|
typedef struct _NumInfo {
|
44
23
|
int64_t i;
|
@@ -48,7 +27,6 @@ typedef struct _NumInfo {
|
|
48
27
|
const char *str;
|
49
28
|
size_t len;
|
50
29
|
long exp;
|
51
|
-
//int dec_cnt;
|
52
30
|
int big;
|
53
31
|
int infinity;
|
54
32
|
int nan;
|
@@ -70,7 +48,9 @@ typedef struct _ParseInfo {
|
|
70
48
|
VALUE handler;
|
71
49
|
struct _ValStack stack;
|
72
50
|
CircArray circ_array;
|
51
|
+
struct _RxClass str_rx;
|
73
52
|
int expect_value;
|
53
|
+
int max_depth; // just for the json gem
|
74
54
|
VALUE proc;
|
75
55
|
VALUE (*start_hash)(struct _ParseInfo *pi);
|
76
56
|
void (*end_hash)(struct _ParseInfo *pi);
|
@@ -89,6 +69,7 @@ typedef struct _ParseInfo {
|
|
89
69
|
void (*add_num)(struct _ParseInfo *pi, NumInfo ni);
|
90
70
|
void (*add_value)(struct _ParseInfo *pi, VALUE val);
|
91
71
|
VALUE err_class;
|
72
|
+
bool has_callbacks;
|
92
73
|
} *ParseInfo;
|
93
74
|
|
94
75
|
extern void oj_parse2(ParseInfo pi);
|
@@ -103,4 +84,9 @@ extern void oj_set_compat_callbacks(ParseInfo pi);
|
|
103
84
|
extern void oj_sparse2(ParseInfo pi);
|
104
85
|
extern VALUE oj_pi_sparse(int argc, VALUE *argv, ParseInfo pi, int fd);
|
105
86
|
|
87
|
+
static inline void
|
88
|
+
parse_info_init(ParseInfo pi) {
|
89
|
+
memset(pi, 0, sizeof(struct _ParseInfo));
|
90
|
+
}
|
91
|
+
|
106
92
|
#endif /* __OJ_PARSE_H__ */
|
data/ext/oj/reader.c
CHANGED
@@ -1,31 +1,6 @@
|
|
1
1
|
/* reader.c
|
2
2
|
* Copyright (c) 2011, Peter Ohler
|
3
3
|
* All rights reserved.
|
4
|
-
*
|
5
|
-
* Redistribution and use in source and binary forms, with or without
|
6
|
-
* modification, are permitted provided that the following conditions are met:
|
7
|
-
*
|
8
|
-
* - Redistributions of source code must retain the above copyright notice, this
|
9
|
-
* list of conditions and the following disclaimer.
|
10
|
-
*
|
11
|
-
* - Redistributions in binary form must reproduce the above copyright notice,
|
12
|
-
* this list of conditions and the following disclaimer in the documentation
|
13
|
-
* and/or other materials provided with the distribution.
|
14
|
-
*
|
15
|
-
* - Neither the name of Peter Ohler nor the names of its contributors may be
|
16
|
-
* used to endorse or promote products derived from this software without
|
17
|
-
* specific prior written permission.
|
18
|
-
*
|
19
|
-
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
20
|
-
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
21
|
-
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
22
|
-
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
23
|
-
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
24
|
-
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
25
|
-
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
26
|
-
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
27
|
-
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
28
|
-
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
29
4
|
*/
|
30
5
|
|
31
6
|
#include <stdlib.h>
|
@@ -66,6 +41,7 @@ oj_reader_init(Reader reader, VALUE io, int fd) {
|
|
66
41
|
reader->read_end = reader->head;
|
67
42
|
reader->pro = 0;
|
68
43
|
reader->str = 0;
|
44
|
+
reader->pos = 0;
|
69
45
|
reader->line = 1;
|
70
46
|
reader->col = 0;
|
71
47
|
reader->free_head = 0;
|
data/ext/oj/reader.h
CHANGED
@@ -1,31 +1,6 @@
|
|
1
1
|
/* reader.h
|
2
2
|
* Copyright (c) 2011, Peter Ohler
|
3
3
|
* All rights reserved.
|
4
|
-
*
|
5
|
-
* Redistribution and use in source and binary forms, with or without
|
6
|
-
* modification, are permitted provided that the following conditions are met:
|
7
|
-
*
|
8
|
-
* - Redistributions of source code must retain the above copyright notice, this
|
9
|
-
* list of conditions and the following disclaimer.
|
10
|
-
*
|
11
|
-
* - Redistributions in binary form must reproduce the above copyright notice,
|
12
|
-
* this list of conditions and the following disclaimer in the documentation
|
13
|
-
* and/or other materials provided with the distribution.
|
14
|
-
*
|
15
|
-
* - Neither the name of Peter Ohler nor the names of its contributors may be
|
16
|
-
* used to endorse or promote products derived from this software without
|
17
|
-
* specific prior written permission.
|
18
|
-
*
|
19
|
-
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
20
|
-
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
21
|
-
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
22
|
-
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
23
|
-
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
24
|
-
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
25
|
-
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
26
|
-
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
27
|
-
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
28
|
-
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
29
4
|
*/
|
30
5
|
|
31
6
|
#ifndef __OJ_READER_H__
|
@@ -39,6 +14,7 @@ typedef struct _Reader {
|
|
39
14
|
char *read_end; /* one past last character read */
|
40
15
|
char *pro; /* protection start, buffer can not slide past this point */
|
41
16
|
char *str; /* start of current string being read */
|
17
|
+
long pos;
|
42
18
|
int line;
|
43
19
|
int col;
|
44
20
|
int free_head;
|
@@ -66,6 +42,7 @@ reader_get(Reader reader) {
|
|
66
42
|
reader->col = 0;
|
67
43
|
}
|
68
44
|
reader->col++;
|
45
|
+
reader->pos++;
|
69
46
|
|
70
47
|
return *reader->tail++;
|
71
48
|
}
|
@@ -74,6 +51,7 @@ static inline void
|
|
74
51
|
reader_backup(Reader reader) {
|
75
52
|
reader->tail--;
|
76
53
|
reader->col--;
|
54
|
+
reader->pos--;
|
77
55
|
if (0 >= reader->col) {
|
78
56
|
reader->line--;
|
79
57
|
// allow col to be negative since we never backup twice in a row
|
data/ext/oj/resolve.c
CHANGED
@@ -56,7 +56,7 @@ resolve_classname(VALUE mod, const char *classname, int auto_define) {
|
|
56
56
|
}
|
57
57
|
|
58
58
|
static VALUE
|
59
|
-
resolve_classpath(ParseInfo pi, const char *name, size_t len, int auto_define) {
|
59
|
+
resolve_classpath(ParseInfo pi, const char *name, size_t len, int auto_define, VALUE error_class) {
|
60
60
|
char class_name[1024];
|
61
61
|
VALUE clas;
|
62
62
|
char *end = class_name + sizeof(class_name) - 1;
|
@@ -84,23 +84,21 @@ resolve_classpath(ParseInfo pi, const char *name, size_t len, int auto_define) {
|
|
84
84
|
}
|
85
85
|
*s = '\0';
|
86
86
|
if (Qundef == (clas = resolve_classname(clas, class_name, auto_define))) {
|
87
|
-
|
88
|
-
|
87
|
+
oj_set_error_at(pi, error_class, __FILE__, __LINE__, "class %s is not defined", name);
|
88
|
+
if (Qnil != error_class) {
|
89
|
+
pi->err_class = error_class;
|
89
90
|
}
|
90
|
-
memcpy(class_name, name, len);
|
91
|
-
class_name[len] = '\0';
|
92
|
-
oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "class %s is not defined", class_name);
|
93
91
|
}
|
94
92
|
return clas;
|
95
93
|
}
|
96
94
|
|
97
95
|
VALUE
|
98
|
-
oj_name2class(ParseInfo pi, const char *name, size_t len, int auto_define) {
|
96
|
+
oj_name2class(ParseInfo pi, const char *name, size_t len, int auto_define, VALUE error_class) {
|
99
97
|
VALUE clas;
|
100
98
|
VALUE *slot;
|
101
99
|
|
102
100
|
if (No == pi->options.class_cache) {
|
103
|
-
return resolve_classpath(pi, name, len, auto_define);
|
101
|
+
return resolve_classpath(pi, name, len, auto_define, error_class);
|
104
102
|
}
|
105
103
|
#if USE_PTHREAD_MUTEX
|
106
104
|
pthread_mutex_lock(&oj_cache_mutex);
|
@@ -108,7 +106,7 @@ oj_name2class(ParseInfo pi, const char *name, size_t len, int auto_define) {
|
|
108
106
|
rb_mutex_lock(oj_cache_mutex);
|
109
107
|
#endif
|
110
108
|
if (Qnil == (clas = oj_class_hash_get(name, len, &slot))) {
|
111
|
-
if (Qundef != (clas = resolve_classpath(pi, name, len, auto_define))) {
|
109
|
+
if (Qundef != (clas = resolve_classpath(pi, name, len, auto_define, error_class))) {
|
112
110
|
*slot = clas;
|
113
111
|
}
|
114
112
|
}
|
@@ -121,9 +119,9 @@ oj_name2class(ParseInfo pi, const char *name, size_t len, int auto_define) {
|
|
121
119
|
}
|
122
120
|
|
123
121
|
VALUE
|
124
|
-
oj_name2struct(ParseInfo pi, VALUE nameVal) {
|
122
|
+
oj_name2struct(ParseInfo pi, VALUE nameVal, VALUE error_class) {
|
125
123
|
size_t len = RSTRING_LEN(nameVal);
|
126
124
|
const char *str = StringValuePtr(nameVal);
|
127
125
|
|
128
|
-
return resolve_classpath(pi, str, len, 0);
|
126
|
+
return resolve_classpath(pi, str, len, 0, error_class);
|
129
127
|
}
|
data/ext/oj/resolve.h
CHANGED
@@ -33,7 +33,7 @@
|
|
33
33
|
|
34
34
|
#include "ruby.h"
|
35
35
|
|
36
|
-
extern VALUE oj_name2class(ParseInfo pi, const char *name, size_t len, int auto_define);
|
37
|
-
extern VALUE oj_name2struct(ParseInfo pi, VALUE nameVal);
|
36
|
+
extern VALUE oj_name2class(ParseInfo pi, const char *name, size_t len, int auto_define, VALUE error_class);
|
37
|
+
extern VALUE oj_name2struct(ParseInfo pi, VALUE nameVal, VALUE error_class);
|
38
38
|
|
39
39
|
#endif /* __OJ_RESOLVE_H__ */
|
data/ext/oj/rxclass.c
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
/* rxclass.c
|
2
|
+
* Copyright (c) 2017, Peter Ohler
|
3
|
+
* All rights reserved.
|
4
|
+
*/
|
5
|
+
|
6
|
+
#include <sys/types.h>
|
7
|
+
#include <stdlib.h>
|
8
|
+
#include <errno.h>
|
9
|
+
#include <regex.h>
|
10
|
+
#include <string.h>
|
11
|
+
#include <stdio.h>
|
12
|
+
|
13
|
+
#include "rxclass.h"
|
14
|
+
|
15
|
+
typedef struct _RxC {
|
16
|
+
struct _RxC *next;
|
17
|
+
VALUE rrx;
|
18
|
+
regex_t rx;
|
19
|
+
VALUE clas;
|
20
|
+
char src[256];
|
21
|
+
} *RxC;
|
22
|
+
|
23
|
+
void
|
24
|
+
oj_rxclass_init(RxClass rc) {
|
25
|
+
*rc->err = '\0';
|
26
|
+
rc->head = NULL;
|
27
|
+
rc->tail = NULL;
|
28
|
+
}
|
29
|
+
|
30
|
+
void
|
31
|
+
oj_rxclass_cleanup(RxClass rc) {
|
32
|
+
RxC rxc;
|
33
|
+
|
34
|
+
while (NULL != (rxc = rc->head)) {
|
35
|
+
rc->head = rc->head->next;
|
36
|
+
if (Qnil == rxc->rrx) {
|
37
|
+
regfree(&rxc->rx);
|
38
|
+
}
|
39
|
+
xfree(rxc);
|
40
|
+
}
|
41
|
+
}
|
42
|
+
|
43
|
+
void
|
44
|
+
oj_rxclass_rappend(RxClass rc, VALUE rx, VALUE clas) {
|
45
|
+
RxC rxc = ALLOC_N(struct _RxC, 1);
|
46
|
+
|
47
|
+
memset(rxc, 0, sizeof(struct _RxC));
|
48
|
+
rxc->rrx = rx;
|
49
|
+
rxc->clas = clas;
|
50
|
+
if (NULL == rc->tail) {
|
51
|
+
rc->head = rxc;
|
52
|
+
} else {
|
53
|
+
rc->tail->next = rxc;
|
54
|
+
}
|
55
|
+
rc->tail = rxc;
|
56
|
+
}
|
57
|
+
|
58
|
+
// Attempt to compile the expression. If it fails populate the error code..
|
59
|
+
int
|
60
|
+
oj_rxclass_append(RxClass rc, const char *expr, VALUE clas) {
|
61
|
+
// Use mallow and not ALLOC_N to avoid pulliing ruby.h which conflicts
|
62
|
+
// with regex_t.
|
63
|
+
RxC rxc;
|
64
|
+
int err;
|
65
|
+
int flags = 0;
|
66
|
+
|
67
|
+
if (sizeof(rxc->src) <= strlen(expr)) {
|
68
|
+
snprintf(rc->err, sizeof(rc->err), "expressions must be less than %lu chracter", sizeof(rxc->src));
|
69
|
+
return EINVAL;
|
70
|
+
}
|
71
|
+
rxc = ALLOC_N(struct _RxC, 1);
|
72
|
+
rxc->next = 0;
|
73
|
+
rxc->rrx = Qnil;
|
74
|
+
rxc->clas = clas;
|
75
|
+
if (0 != (err = regcomp(&rxc->rx, expr, flags))) {
|
76
|
+
regerror(err, &rxc->rx, rc->err, sizeof(rc->err));
|
77
|
+
free(rxc);
|
78
|
+
return err;
|
79
|
+
}
|
80
|
+
if (NULL == rc->tail) {
|
81
|
+
rc->head = rxc;
|
82
|
+
} else {
|
83
|
+
rc->tail->next = rxc;
|
84
|
+
}
|
85
|
+
rc->tail = rxc;
|
86
|
+
|
87
|
+
return 0;
|
88
|
+
}
|
89
|
+
|
90
|
+
VALUE
|
91
|
+
oj_rxclass_match(RxClass rc, const char *str, int len) {
|
92
|
+
RxC rxc;
|
93
|
+
char buf[4096];
|
94
|
+
|
95
|
+
for (rxc = rc->head; NULL != rxc; rxc = rxc->next) {
|
96
|
+
if (Qnil != rxc->rrx) {
|
97
|
+
// Must use a valiabel for this to work.
|
98
|
+
volatile VALUE rstr = rb_str_new(str, len);
|
99
|
+
|
100
|
+
//if (Qtrue == rb_funcall(rxc->rrx, rb_intern("match?"), 1, rstr)) {
|
101
|
+
if (Qnil != rb_funcall(rxc->rrx, rb_intern("match"), 1, rstr)) {
|
102
|
+
return rxc->clas;
|
103
|
+
}
|
104
|
+
} else if (len < (int)sizeof(buf)) {
|
105
|
+
// string is not \0 terminated so copy and atempt a match
|
106
|
+
memcpy(buf, str, len);
|
107
|
+
buf[len] = '\0';
|
108
|
+
if (0 == regexec(&rxc->rx, buf, 0, NULL, 0)) { // match
|
109
|
+
return rxc->clas;
|
110
|
+
}
|
111
|
+
} else {
|
112
|
+
// TBD allocate a larger buffer and attempt
|
113
|
+
}
|
114
|
+
}
|
115
|
+
return Qnil;
|
116
|
+
}
|
117
|
+
|
118
|
+
void
|
119
|
+
oj_rxclass_copy(RxClass src, RxClass dest) {
|
120
|
+
dest->head = NULL;
|
121
|
+
dest->tail = NULL;
|
122
|
+
if (NULL != src->head) {
|
123
|
+
RxC rxc;
|
124
|
+
|
125
|
+
for (rxc = src->head; NULL != rxc; rxc = rxc->next) {
|
126
|
+
if (Qnil != rxc->rrx) {
|
127
|
+
oj_rxclass_rappend(dest, rxc->rrx, rxc->clas);
|
128
|
+
} else {
|
129
|
+
oj_rxclass_append(dest, rxc->src, rxc->clas);
|
130
|
+
}
|
131
|
+
}
|
132
|
+
}
|
133
|
+
}
|
data/ext/oj/rxclass.h
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
/* rxclass.h
|
2
|
+
* Copyright (c) 2017, Peter Ohler
|
3
|
+
* All rights reserved.
|
4
|
+
*/
|
5
|
+
|
6
|
+
#ifndef __OJ_RXCLASS_H__
|
7
|
+
#define __OJ_RXCLASS_H__
|
8
|
+
|
9
|
+
#include <stdbool.h>
|
10
|
+
#include "ruby.h"
|
11
|
+
|
12
|
+
struct _RxC;
|
13
|
+
|
14
|
+
typedef struct _RxClass {
|
15
|
+
struct _RxC *head;
|
16
|
+
struct _RxC *tail;
|
17
|
+
char err[128];
|
18
|
+
} *RxClass;
|
19
|
+
|
20
|
+
extern void oj_rxclass_init(RxClass rc);
|
21
|
+
extern void oj_rxclass_cleanup(RxClass rc);
|
22
|
+
extern int oj_rxclass_append(RxClass rc, const char *expr, VALUE clas);
|
23
|
+
extern VALUE oj_rxclass_match(RxClass rc, const char *str, int len);
|
24
|
+
extern void oj_rxclass_copy(RxClass src, RxClass dest);
|
25
|
+
extern void oj_rxclass_rappend(RxClass rc, VALUE rx, VALUE clas);
|
26
|
+
|
27
|
+
#endif /* __OJ_RXCLASS_H__ */
|
data/ext/oj/saj.c
CHANGED
@@ -1,31 +1,6 @@
|
|
1
1
|
/* saj.c
|
2
2
|
* Copyright (c) 2012, Peter Ohler
|
3
3
|
* All rights reserved.
|
4
|
-
*
|
5
|
-
* Redistribution and use in source and binary forms, with or without
|
6
|
-
* modification, are permitted provided that the following conditions are met:
|
7
|
-
*
|
8
|
-
* - Redistributions of source code must retain the above copyright notice, this
|
9
|
-
* list of conditions and the following disclaimer.
|
10
|
-
*
|
11
|
-
* - Redistributions in binary form must reproduce the above copyright notice,
|
12
|
-
* this list of conditions and the following disclaimer in the documentation
|
13
|
-
* and/or other materials provided with the distribution.
|
14
|
-
*
|
15
|
-
* - Neither the name of Peter Ohler nor the names of its contributors may be
|
16
|
-
* used to endorse or promote products derived from this software without
|
17
|
-
* specific prior written permission.
|
18
|
-
*
|
19
|
-
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
20
|
-
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
21
|
-
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
22
|
-
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
23
|
-
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
24
|
-
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
25
|
-
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
26
|
-
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
27
|
-
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
28
|
-
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
29
4
|
*/
|
30
5
|
|
31
6
|
#if !IS_WINDOWS
|
@@ -682,6 +657,10 @@ saj_parse(VALUE handler, char *json) {
|
|
682
657
|
* if the JSON is malformed.
|
683
658
|
* @param [Oj::Saj] handler Saj (responds to Oj::Saj methods) like handler
|
684
659
|
* @param [IO|String] io IO Object to read from
|
660
|
+
* @deprecated The sc_parse() method along with the ScHandler is the preferred
|
661
|
+
* callback parser. It is slightly faster and handles streams while the
|
662
|
+
* saj_parse() methos requires a complete read before parsing.
|
663
|
+
* @see sc_parse
|
685
664
|
*/
|
686
665
|
VALUE
|
687
666
|
oj_saj_parse(int argc, VALUE *argv, VALUE self) {
|