Skip to content

Commit 0aabbc5

Browse files
committed
Rename JSON_Parser in JSON_ParserConfig
1 parent 83f6dc7 commit 0aabbc5

File tree

1 file changed

+81
-81
lines changed

1 file changed

+81
-81
lines changed

ext/json/ext/parser/parser.c

Lines changed: 81 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -394,10 +394,10 @@ typedef struct JSON_ParserStruct {
394394
bool freeze;
395395
bool create_additions;
396396
bool deprecated_create_additions;
397-
} JSON_Parser;
397+
} JSON_ParserConfig;
398398

399399
typedef struct JSON_ParserStateStruct {
400-
JSON_Parser *json;
400+
JSON_ParserConfig *config;
401401
VALUE stack_handle;
402402
const char *cursor;
403403
const char *end;
@@ -408,11 +408,11 @@ typedef struct JSON_ParserStateStruct {
408408
int current_nesting;
409409
} JSON_ParserState;
410410

411-
#define GET_PARSER \
412-
JSON_Parser *json; \
413-
TypedData_Get_Struct(self, JSON_Parser, &JSON_Parser_type, json)
411+
#define GET_PARSER_CONFIG \
412+
JSON_ParserConfig *config; \
413+
TypedData_Get_Struct(self, JSON_ParserConfig, &JSON_ParserConfig_type, config)
414414

415-
static const rb_data_type_t JSON_Parser_type;
415+
static const rb_data_type_t JSON_ParserConfig_type;
416416

417417
#ifndef HAVE_STRNLEN
418418
static size_t strnlen(const char *s, size_t maxlen)
@@ -703,17 +703,17 @@ static VALUE json_decode_float(JSON_ParserState *state, const char *start, const
703703
{
704704
VALUE mod = Qnil;
705705
ID method_id = 0;
706-
JSON_Parser *json = state->json;
707-
if (json->decimal_class) {
706+
JSON_ParserConfig *config = state->config;
707+
if (config->decimal_class) {
708708
// TODO: we should move this to the constructor
709-
if (rb_respond_to(json->decimal_class, i_try_convert)) {
710-
mod = json->decimal_class;
709+
if (rb_respond_to(config->decimal_class, i_try_convert)) {
710+
mod = config->decimal_class;
711711
method_id = i_try_convert;
712-
} else if (rb_respond_to(json->decimal_class, i_new)) {
713-
mod = json->decimal_class;
712+
} else if (rb_respond_to(config->decimal_class, i_new)) {
713+
mod = config->decimal_class;
714714
method_id = i_new;
715-
} else if (RB_TYPE_P(json->decimal_class, T_CLASS)) {
716-
VALUE name = rb_class_name(json->decimal_class);
715+
} else if (RB_TYPE_P(config->decimal_class, T_CLASS)) {
716+
VALUE name = rb_class_name(config->decimal_class);
717717
const char *name_cstr = RSTRING_PTR(name);
718718
const char *last_colon = strrchr(name_cstr, ':');
719719
if (last_colon) {
@@ -749,8 +749,8 @@ static VALUE json_decode_float(JSON_ParserState *state, const char *start, const
749749
static inline VALUE json_decode_array(JSON_ParserState *state, long count)
750750
{
751751
VALUE array;
752-
if (RB_UNLIKELY(state->json->array_class)) {
753-
array = rb_class_new_instance(0, 0, state->json->array_class);
752+
if (RB_UNLIKELY(state->config->array_class)) {
753+
array = rb_class_new_instance(0, 0, state->config->array_class);
754754
VALUE *items = rvalue_stack_peek(state->stack, count);
755755
long index;
756756
for (index = 0; index < count; index++) {
@@ -762,7 +762,7 @@ static inline VALUE json_decode_array(JSON_ParserState *state, long count)
762762

763763
rvalue_stack_pop(state->stack, count);
764764

765-
if (state->json->freeze) {
765+
if (state->config->freeze) {
766766
RB_OBJ_FREEZE(array);
767767
}
768768

@@ -772,8 +772,8 @@ static inline VALUE json_decode_array(JSON_ParserState *state, long count)
772772
static inline VALUE json_decode_object(JSON_ParserState *state, long count)
773773
{
774774
VALUE object;
775-
if (RB_UNLIKELY(state->json->object_class)) {
776-
object = rb_class_new_instance(0, 0, state->json->object_class);
775+
if (RB_UNLIKELY(state->config->object_class)) {
776+
object = rb_class_new_instance(0, 0, state->config->object_class);
777777
long index = 0;
778778
VALUE *items = rvalue_stack_peek(state->stack, count);
779779
while (index < count) {
@@ -788,25 +788,25 @@ static inline VALUE json_decode_object(JSON_ParserState *state, long count)
788788

789789
rvalue_stack_pop(state->stack, count);
790790

791-
if (RB_UNLIKELY(state->json->create_additions)) {
791+
if (RB_UNLIKELY(state->config->create_additions)) {
792792
VALUE klassname;
793-
if (state->json->object_class) {
794-
klassname = rb_funcall(object, i_aref, 1, state->json->create_id);
793+
if (state->config->object_class) {
794+
klassname = rb_funcall(object, i_aref, 1, state->config->create_id);
795795
} else {
796-
klassname = rb_hash_aref(object, state->json->create_id);
796+
klassname = rb_hash_aref(object, state->config->create_id);
797797
}
798798
if (!NIL_P(klassname)) {
799799
VALUE klass = rb_funcall(mJSON, i_deep_const_get, 1, klassname);
800800
if (RTEST(rb_funcall(klass, i_json_creatable_p, 0))) {
801-
if (state->json->deprecated_create_additions) {
801+
if (state->config->deprecated_create_additions) {
802802
json_deprecated(deprecated_create_additions_warning);
803803
}
804804
object = rb_funcall(klass, i_json_create, 1, object);
805805
}
806806
}
807807
}
808808

809-
if (state->json->freeze) {
809+
if (state->config->freeze) {
810810
RB_OBJ_FREEZE(object);
811811
}
812812

@@ -827,19 +827,19 @@ static int match_i(VALUE regexp, VALUE klass, VALUE memo)
827827
static inline VALUE json_decode_string(JSON_ParserState *state, const char *start, const char *end, bool escaped, bool is_name)
828828
{
829829
VALUE string;
830-
bool intern = is_name || state->json->freeze;
831-
bool symbolize = is_name && state->json->symbolize_names;
830+
bool intern = is_name || state->config->freeze;
831+
bool symbolize = is_name && state->config->symbolize_names;
832832
if (escaped) {
833833
string = json_string_unescape(state, start, end, is_name, intern, symbolize);
834834
} else {
835835
string = json_string_fastpath(state, start, end, is_name, intern, symbolize);
836836
}
837837

838-
if (RB_UNLIKELY(state->json->create_additions && RTEST(state->json->match_string))) {
838+
if (RB_UNLIKELY(state->config->create_additions && RTEST(state->config->match_string))) {
839839
VALUE klass;
840840
VALUE memo = rb_ary_new2(2);
841841
rb_ary_push(memo, string);
842-
rb_hash_foreach(state->json->match_string, match_i, memo);
842+
rb_hash_foreach(state->config->match_string, match_i, memo);
843843
klass = rb_ary_entry(memo, 1);
844844
if (RTEST(klass)) {
845845
string = rb_funcall(klass, i_json_create, 1, string);
@@ -912,15 +912,15 @@ static VALUE json_parse_any(JSON_ParserState *state)
912912
break;
913913
case 'N':
914914
// Note: memcmp with a small power of two compile to an integer comparison
915-
if (state->json->allow_nan && (state->end - state->cursor >= 3) && (memcmp(state->cursor + 1, "aN", 2) == 0)) {
915+
if (state->config->allow_nan && (state->end - state->cursor >= 3) && (memcmp(state->cursor + 1, "aN", 2) == 0)) {
916916
state->cursor += 3;
917917
return PUSH(CNaN);
918918
}
919919

920920
raise_parse_error("unexpected character: %s", state->cursor);
921921
break;
922922
case 'I':
923-
if (state->json->allow_nan && (state->end - state->cursor >= 8) && (memcmp(state->cursor, "Infinity", 8) == 0)) {
923+
if (state->config->allow_nan && (state->end - state->cursor >= 8) && (memcmp(state->cursor, "Infinity", 8) == 0)) {
924924
state->cursor += 8;
925925
return PUSH(CInfinity);
926926
}
@@ -929,7 +929,7 @@ static VALUE json_parse_any(JSON_ParserState *state)
929929
break;
930930
case '-':
931931
// Note: memcmp with a small power of two compile to an integer comparison
932-
if (state->json->allow_nan && (state->end - state->cursor >= 9) && (memcmp(state->cursor + 1, "Infinity", 8) == 0)) {
932+
if (state->config->allow_nan && (state->end - state->cursor >= 9) && (memcmp(state->cursor + 1, "Infinity", 8) == 0)) {
933933
state->cursor += 9;
934934
return PUSH(CMinusInfinity);
935935
}
@@ -1002,7 +1002,7 @@ static VALUE json_parse_any(JSON_ParserState *state)
10021002
return PUSH(json_decode_array(state, 0));
10031003
} else {
10041004
state->current_nesting++;
1005-
if (RB_UNLIKELY(state->json->max_nesting && (state->json->max_nesting < state->current_nesting))) {
1005+
if (RB_UNLIKELY(state->config->max_nesting && (state->config->max_nesting < state->current_nesting))) {
10061006
rb_raise(eNestingError, "nesting of %d is too deep", state->current_nesting);
10071007
}
10081008
state->in_array++;
@@ -1023,7 +1023,7 @@ static VALUE json_parse_any(JSON_ParserState *state)
10231023

10241024
if (*state->cursor == ',') {
10251025
state->cursor++;
1026-
if (state->json->allow_trailing_comma) {
1026+
if (state->config->allow_trailing_comma) {
10271027
json_eat_whitespace(state);
10281028
if ((state->cursor < state->end) && (*state->cursor == ']')) {
10291029
continue;
@@ -1048,7 +1048,7 @@ static VALUE json_parse_any(JSON_ParserState *state)
10481048
return PUSH(json_decode_object(state, 0));
10491049
} else {
10501050
state->current_nesting++;
1051-
if (RB_UNLIKELY(state->json->max_nesting && (state->json->max_nesting < state->current_nesting))) {
1051+
if (RB_UNLIKELY(state->config->max_nesting && (state->config->max_nesting < state->current_nesting))) {
10521052
rb_raise(eNestingError, "nesting of %d is too deep", state->current_nesting);
10531053
}
10541054

@@ -1081,7 +1081,7 @@ static VALUE json_parse_any(JSON_ParserState *state)
10811081
state->cursor++;
10821082
json_eat_whitespace(state);
10831083

1084-
if (state->json->allow_trailing_comma) {
1084+
if (state->config->allow_trailing_comma) {
10851085
if ((state->cursor < state->end) && (*state->cursor == '}')) {
10861086
continue;
10871087
}
@@ -1155,50 +1155,50 @@ static VALUE convert_encoding(VALUE source)
11551155

11561156
static int configure_parser_i(VALUE key, VALUE val, VALUE data)
11571157
{
1158-
JSON_Parser *json = (JSON_Parser *)data;
1159-
1160-
if (key == sym_max_nesting) { json->max_nesting = RTEST(val) ? FIX2INT(val) : 0; }
1161-
else if (key == sym_allow_nan) { json->allow_nan = RTEST(val); }
1162-
else if (key == sym_allow_trailing_comma) { json->allow_trailing_comma = RTEST(val); }
1163-
else if (key == sym_symbolize_names) { json->symbolize_names = RTEST(val); }
1164-
else if (key == sym_freeze) { json->freeze = RTEST(val); }
1165-
else if (key == sym_create_id) { json->create_id = RTEST(val) ? val : Qfalse; }
1166-
else if (key == sym_object_class) { json->object_class = RTEST(val) ? val : Qfalse; }
1167-
else if (key == sym_array_class) { json->array_class = RTEST(val) ? val : Qfalse; }
1168-
else if (key == sym_decimal_class) { json->decimal_class = RTEST(val) ? val : Qfalse; }
1169-
else if (key == sym_match_string) { json->match_string = RTEST(val) ? val : Qfalse; }
1158+
JSON_ParserConfig *config = (JSON_ParserConfig *)data;
1159+
1160+
if (key == sym_max_nesting) { config->max_nesting = RTEST(val) ? FIX2INT(val) : 0; }
1161+
else if (key == sym_allow_nan) { config->allow_nan = RTEST(val); }
1162+
else if (key == sym_allow_trailing_comma) { config->allow_trailing_comma = RTEST(val); }
1163+
else if (key == sym_symbolize_names) { config->symbolize_names = RTEST(val); }
1164+
else if (key == sym_freeze) { config->freeze = RTEST(val); }
1165+
else if (key == sym_create_id) { config->create_id = RTEST(val) ? val : Qfalse; }
1166+
else if (key == sym_object_class) { config->object_class = RTEST(val) ? val : Qfalse; }
1167+
else if (key == sym_array_class) { config->array_class = RTEST(val) ? val : Qfalse; }
1168+
else if (key == sym_decimal_class) { config->decimal_class = RTEST(val) ? val : Qfalse; }
1169+
else if (key == sym_match_string) { config->match_string = RTEST(val) ? val : Qfalse; }
11701170
else if (key == sym_create_additions) {
11711171
if (NIL_P(val)) {
1172-
json->create_additions = true;
1173-
json->deprecated_create_additions = true;
1172+
config->create_additions = true;
1173+
config->deprecated_create_additions = true;
11741174
} else {
1175-
json->create_additions = RTEST(val);
1176-
json->deprecated_create_additions = false;
1175+
config->create_additions = RTEST(val);
1176+
config->deprecated_create_additions = false;
11771177
}
11781178
}
11791179

11801180
return ST_CONTINUE;
11811181
}
11821182

1183-
static void parser_init(JSON_Parser *json, VALUE opts)
1183+
static void parser_config_init(JSON_ParserConfig *config, VALUE opts)
11841184
{
1185-
json->max_nesting = 100;
1185+
config->max_nesting = 100;
11861186

11871187
if (!NIL_P(opts)) {
11881188
Check_Type(opts, T_HASH);
11891189
if (RHASH_SIZE(opts) > 0) {
11901190
// We assume in most cases few keys are set so it's faster to go over
11911191
// the provided keys than to check all possible keys.
1192-
rb_hash_foreach(opts, configure_parser_i, (VALUE)json);
1192+
rb_hash_foreach(opts, configure_parser_i, (VALUE)config);
11931193

1194-
if (json->symbolize_names && json->create_additions) {
1194+
if (config->symbolize_names && config->create_additions) {
11951195
rb_raise(rb_eArgError,
11961196
"options :symbolize_names and :create_additions cannot be "
11971197
" used in conjunction");
11981198
}
11991199

1200-
if (json->create_additions && !json->create_id) {
1201-
json->create_id = rb_funcall(mJSON, i_create_id, 0);
1200+
if (config->create_additions && !config->create_id) {
1201+
config->create_id = rb_funcall(mJSON, i_create_id, 0);
12021202
}
12031203
}
12041204

@@ -1239,9 +1239,9 @@ static void parser_init(JSON_Parser *json, VALUE opts)
12391239
*/
12401240
static VALUE cParserConfig_initialize(VALUE self, VALUE opts)
12411241
{
1242-
GET_PARSER;
1242+
GET_PARSER_CONFIG;
12431243

1244-
parser_init(json, opts);
1244+
parser_config_init(config, opts);
12451245
return self;
12461246
}
12471247

@@ -1253,7 +1253,7 @@ static VALUE cParser_parse_safe(VALUE vstate)
12531253
return result;
12541254
}
12551255

1256-
static VALUE cParser_parse(JSON_Parser *json, VALUE Vsource)
1256+
static VALUE cParser_parse(JSON_ParserConfig *config, VALUE Vsource)
12571257
{
12581258
Vsource = convert_encoding(StringValue(Vsource));
12591259
StringValue(Vsource);
@@ -1266,7 +1266,7 @@ static VALUE cParser_parse(JSON_Parser *json, VALUE Vsource)
12661266
};
12671267

12681268
JSON_ParserState _state = {
1269-
.json = json,
1269+
.config = config,
12701270
.cursor = RSTRING_PTR(Vsource),
12711271
.end = RSTRING_PTR(Vsource) + RSTRING_LEN(Vsource),
12721272
.stack = &stack,
@@ -1297,54 +1297,54 @@ static VALUE cParser_parse(JSON_Parser *json, VALUE Vsource)
12971297
*/
12981298
static VALUE cParserConfig_parse(VALUE self, VALUE Vsource)
12991299
{
1300-
GET_PARSER;
1301-
return cParser_parse(json, Vsource);
1300+
GET_PARSER_CONFIG;
1301+
return cParser_parse(config, Vsource);
13021302
}
13031303

13041304
static VALUE cParser_m_parse(VALUE klass, VALUE Vsource, VALUE opts)
13051305
{
13061306
Vsource = convert_encoding(StringValue(Vsource));
13071307
StringValue(Vsource);
13081308

1309-
JSON_Parser _parser = {0};
1310-
JSON_Parser *json = &_parser;
1311-
parser_init(json, opts);
1309+
JSON_ParserConfig _config = {0};
1310+
JSON_ParserConfig *config = &_config;
1311+
parser_config_init(config, opts);
13121312

1313-
return cParser_parse(json, Vsource);
1313+
return cParser_parse(config, Vsource);
13141314
}
13151315

13161316
static void JSON_mark(void *ptr)
13171317
{
1318-
JSON_Parser *json = ptr;
1319-
rb_gc_mark(json->create_id);
1320-
rb_gc_mark(json->object_class);
1321-
rb_gc_mark(json->array_class);
1322-
rb_gc_mark(json->decimal_class);
1323-
rb_gc_mark(json->match_string);
1318+
JSON_ParserConfig *config = ptr;
1319+
rb_gc_mark(config->create_id);
1320+
rb_gc_mark(config->object_class);
1321+
rb_gc_mark(config->array_class);
1322+
rb_gc_mark(config->decimal_class);
1323+
rb_gc_mark(config->match_string);
13241324
}
13251325

13261326
static void JSON_free(void *ptr)
13271327
{
1328-
JSON_Parser *json = ptr;
1329-
ruby_xfree(json);
1328+
JSON_ParserConfig *config = ptr;
1329+
ruby_xfree(config);
13301330
}
13311331

13321332
static size_t JSON_memsize(const void *ptr)
13331333
{
1334-
return sizeof(JSON_Parser);
1334+
return sizeof(JSON_ParserConfig);
13351335
}
13361336

1337-
static const rb_data_type_t JSON_Parser_type = {
1338-
"JSON/Parser",
1337+
static const rb_data_type_t JSON_ParserConfig_type = {
1338+
"JSON/ParserConfig",
13391339
{JSON_mark, JSON_free, JSON_memsize,},
13401340
0, 0,
13411341
RUBY_TYPED_FREE_IMMEDIATELY,
13421342
};
13431343

13441344
static VALUE cJSON_parser_s_allocate(VALUE klass)
13451345
{
1346-
JSON_Parser *json;
1347-
return TypedData_Make_Struct(klass, JSON_Parser, &JSON_Parser_type, json);
1346+
JSON_ParserConfig *config;
1347+
return TypedData_Make_Struct(klass, JSON_ParserConfig, &JSON_ParserConfig_type, config);
13481348
}
13491349

13501350
void Init_parser(void)

0 commit comments

Comments
 (0)