json 1.8.3 → 1.8.5

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of json might be problematic. Click here for more details.

@@ -3,6 +3,28 @@
3
3
  #include "../fbuffer/fbuffer.h"
4
4
  #include "parser.h"
5
5
 
6
+ #if defined HAVE_RUBY_ENCODING_H
7
+ # define EXC_ENCODING UTF_8,
8
+ # ifndef HAVE_RB_ENC_RAISE
9
+ static void
10
+ enc_raise(rb_encoding *enc, VALUE exc, const char *fmt, ...)
11
+ {
12
+ va_list args;
13
+ VALUE mesg;
14
+
15
+ va_start(args, fmt);
16
+ mesg = rb_enc_vsprintf(enc, fmt, args);
17
+ va_end(args);
18
+
19
+ rb_exc_raise(rb_exc_new3(exc, mesg));
20
+ }
21
+ # define rb_enc_raise enc_raise
22
+ # endif
23
+ #else
24
+ # define EXC_ENCODING /* nothing */
25
+ # define rb_enc_raise rb_raise
26
+ #endif
27
+
6
28
  /* unicode */
7
29
 
8
30
  static const char digit_values[256] = {
@@ -68,9 +90,7 @@ static int convert_UTF32_to_UTF8(char *buf, UTF32 ch)
68
90
  }
69
91
 
70
92
  #ifdef HAVE_RUBY_ENCODING_H
71
- static VALUE CEncoding_ASCII_8BIT, CEncoding_UTF_8, CEncoding_UTF_16BE,
72
- CEncoding_UTF_16LE, CEncoding_UTF_32BE, CEncoding_UTF_32LE;
73
- static ID i_encoding, i_encode;
93
+ static rb_encoding *UTF_8, *UTF_16BE, *UTF_16LE, *UTF_32BE, *UTF_32LE;
74
94
  #else
75
95
  static ID i_iconv;
76
96
  #endif
@@ -84,19 +104,19 @@ static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions,
84
104
  i_match_string, i_aset, i_aref, i_leftshift;
85
105
 
86
106
 
87
- #line 110 "parser.rl"
107
+ #line 130 "parser.rl"
88
108
 
89
109
 
90
110
 
91
- #line 92 "parser.c"
92
- enum {JSON_object_start = 1};
93
- enum {JSON_object_first_final = 27};
94
- enum {JSON_object_error = 0};
111
+ #line 112 "parser.c"
112
+ static const int JSON_object_start = 1;
113
+ static const int JSON_object_first_final = 27;
114
+ static const int JSON_object_error = 0;
95
115
 
96
- enum {JSON_object_en_main = 1};
116
+ static const int JSON_object_en_main = 1;
97
117
 
98
118
 
99
- #line 151 "parser.rl"
119
+ #line 171 "parser.rl"
100
120
 
101
121
 
102
122
  static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -112,14 +132,14 @@ static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *resu
112
132
  *result = NIL_P(object_class) ? rb_hash_new() : rb_class_new_instance(0, 0, object_class);
113
133
 
114
134
 
115
- #line 116 "parser.c"
135
+ #line 136 "parser.c"
116
136
  {
117
137
  cs = JSON_object_start;
118
138
  }
119
139
 
120
- #line 166 "parser.rl"
140
+ #line 186 "parser.rl"
121
141
 
122
- #line 123 "parser.c"
142
+ #line 143 "parser.c"
123
143
  {
124
144
  if ( p == pe )
125
145
  goto _test_eof;
@@ -147,7 +167,7 @@ case 2:
147
167
  goto st2;
148
168
  goto st0;
149
169
  tr2:
150
- #line 133 "parser.rl"
170
+ #line 153 "parser.rl"
151
171
  {
152
172
  char *np;
153
173
  json->parsing_name = 1;
@@ -160,7 +180,7 @@ st3:
160
180
  if ( ++p == pe )
161
181
  goto _test_eof3;
162
182
  case 3:
163
- #line 164 "parser.c"
183
+ #line 184 "parser.c"
164
184
  switch( (*p) ) {
165
185
  case 13: goto st3;
166
186
  case 32: goto st3;
@@ -227,7 +247,7 @@ case 8:
227
247
  goto st8;
228
248
  goto st0;
229
249
  tr11:
230
- #line 118 "parser.rl"
250
+ #line 138 "parser.rl"
231
251
  {
232
252
  VALUE v = Qnil;
233
253
  char *np = JSON_parse_value(json, p, pe, &v);
@@ -247,7 +267,7 @@ st9:
247
267
  if ( ++p == pe )
248
268
  goto _test_eof9;
249
269
  case 9:
250
- #line 251 "parser.c"
270
+ #line 271 "parser.c"
251
271
  switch( (*p) ) {
252
272
  case 13: goto st9;
253
273
  case 32: goto st9;
@@ -336,14 +356,14 @@ case 18:
336
356
  goto st9;
337
357
  goto st18;
338
358
  tr4:
339
- #line 141 "parser.rl"
359
+ #line 161 "parser.rl"
340
360
  { p--; {p++; cs = 27; goto _out;} }
341
361
  goto st27;
342
362
  st27:
343
363
  if ( ++p == pe )
344
364
  goto _test_eof27;
345
365
  case 27:
346
- #line 347 "parser.c"
366
+ #line 367 "parser.c"
347
367
  goto st0;
348
368
  st19:
349
369
  if ( ++p == pe )
@@ -441,7 +461,7 @@ case 26:
441
461
  _out: {}
442
462
  }
443
463
 
444
- #line 167 "parser.rl"
464
+ #line 187 "parser.rl"
445
465
 
446
466
  if (cs >= JSON_object_first_final) {
447
467
  if (json->create_additions) {
@@ -466,15 +486,15 @@ case 26:
466
486
 
467
487
 
468
488
 
469
- #line 470 "parser.c"
470
- enum {JSON_value_start = 1};
471
- enum {JSON_value_first_final = 21};
472
- enum {JSON_value_error = 0};
489
+ #line 490 "parser.c"
490
+ static const int JSON_value_start = 1;
491
+ static const int JSON_value_first_final = 21;
492
+ static const int JSON_value_error = 0;
473
493
 
474
- enum {JSON_value_en_main = 1};
494
+ static const int JSON_value_en_main = 1;
475
495
 
476
496
 
477
- #line 271 "parser.rl"
497
+ #line 291 "parser.rl"
478
498
 
479
499
 
480
500
  static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -482,14 +502,14 @@ static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *resul
482
502
  int cs = EVIL;
483
503
 
484
504
 
485
- #line 486 "parser.c"
505
+ #line 506 "parser.c"
486
506
  {
487
507
  cs = JSON_value_start;
488
508
  }
489
509
 
490
- #line 278 "parser.rl"
510
+ #line 298 "parser.rl"
491
511
 
492
- #line 493 "parser.c"
512
+ #line 513 "parser.c"
493
513
  {
494
514
  if ( p == pe )
495
515
  goto _test_eof;
@@ -514,14 +534,14 @@ st0:
514
534
  cs = 0;
515
535
  goto _out;
516
536
  tr0:
517
- #line 219 "parser.rl"
537
+ #line 239 "parser.rl"
518
538
  {
519
539
  char *np = JSON_parse_string(json, p, pe, result);
520
540
  if (np == NULL) { p--; {p++; cs = 21; goto _out;} } else {p = (( np))-1;}
521
541
  }
522
542
  goto st21;
523
543
  tr2:
524
- #line 224 "parser.rl"
544
+ #line 244 "parser.rl"
525
545
  {
526
546
  char *np;
527
547
  if(pe > p + 9 - json->quirks_mode && !strncmp(MinusInfinity, p, 9)) {
@@ -530,7 +550,7 @@ tr2:
530
550
  {p = (( p + 10))-1;}
531
551
  p--; {p++; cs = 21; goto _out;}
532
552
  } else {
533
- rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p);
553
+ rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p);
534
554
  }
535
555
  }
536
556
  np = JSON_parse_float(json, p, pe, result);
@@ -541,7 +561,7 @@ tr2:
541
561
  }
542
562
  goto st21;
543
563
  tr5:
544
- #line 242 "parser.rl"
564
+ #line 262 "parser.rl"
545
565
  {
546
566
  char *np;
547
567
  json->current_nesting++;
@@ -551,7 +571,7 @@ tr5:
551
571
  }
552
572
  goto st21;
553
573
  tr9:
554
- #line 250 "parser.rl"
574
+ #line 270 "parser.rl"
555
575
  {
556
576
  char *np;
557
577
  json->current_nesting++;
@@ -561,39 +581,39 @@ tr9:
561
581
  }
562
582
  goto st21;
563
583
  tr16:
564
- #line 212 "parser.rl"
584
+ #line 232 "parser.rl"
565
585
  {
566
586
  if (json->allow_nan) {
567
587
  *result = CInfinity;
568
588
  } else {
569
- rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p - 8);
589
+ rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p - 8);
570
590
  }
571
591
  }
572
592
  goto st21;
573
593
  tr18:
574
- #line 205 "parser.rl"
594
+ #line 225 "parser.rl"
575
595
  {
576
596
  if (json->allow_nan) {
577
597
  *result = CNaN;
578
598
  } else {
579
- rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p - 2);
599
+ rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p - 2);
580
600
  }
581
601
  }
582
602
  goto st21;
583
603
  tr22:
584
- #line 199 "parser.rl"
604
+ #line 219 "parser.rl"
585
605
  {
586
606
  *result = Qfalse;
587
607
  }
588
608
  goto st21;
589
609
  tr25:
590
- #line 196 "parser.rl"
610
+ #line 216 "parser.rl"
591
611
  {
592
612
  *result = Qnil;
593
613
  }
594
614
  goto st21;
595
615
  tr28:
596
- #line 202 "parser.rl"
616
+ #line 222 "parser.rl"
597
617
  {
598
618
  *result = Qtrue;
599
619
  }
@@ -602,9 +622,9 @@ st21:
602
622
  if ( ++p == pe )
603
623
  goto _test_eof21;
604
624
  case 21:
605
- #line 258 "parser.rl"
625
+ #line 278 "parser.rl"
606
626
  { p--; {p++; cs = 21; goto _out;} }
607
- #line 608 "parser.c"
627
+ #line 628 "parser.c"
608
628
  goto st0;
609
629
  st2:
610
630
  if ( ++p == pe )
@@ -765,7 +785,7 @@ case 20:
765
785
  _out: {}
766
786
  }
767
787
 
768
- #line 279 "parser.rl"
788
+ #line 299 "parser.rl"
769
789
 
770
790
  if (cs >= JSON_value_first_final) {
771
791
  return p;
@@ -775,15 +795,15 @@ case 20:
775
795
  }
776
796
 
777
797
 
778
- #line 779 "parser.c"
779
- enum {JSON_integer_start = 1};
780
- enum {JSON_integer_first_final = 3};
781
- enum {JSON_integer_error = 0};
798
+ #line 799 "parser.c"
799
+ static const int JSON_integer_start = 1;
800
+ static const int JSON_integer_first_final = 3;
801
+ static const int JSON_integer_error = 0;
782
802
 
783
- enum {JSON_integer_en_main = 1};
803
+ static const int JSON_integer_en_main = 1;
784
804
 
785
805
 
786
- #line 295 "parser.rl"
806
+ #line 315 "parser.rl"
787
807
 
788
808
 
789
809
  static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -791,15 +811,15 @@ static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *res
791
811
  int cs = EVIL;
792
812
 
793
813
 
794
- #line 795 "parser.c"
814
+ #line 815 "parser.c"
795
815
  {
796
816
  cs = JSON_integer_start;
797
817
  }
798
818
 
799
- #line 302 "parser.rl"
819
+ #line 322 "parser.rl"
800
820
  json->memo = p;
801
821
 
802
- #line 803 "parser.c"
822
+ #line 823 "parser.c"
803
823
  {
804
824
  if ( p == pe )
805
825
  goto _test_eof;
@@ -833,14 +853,14 @@ case 3:
833
853
  goto st0;
834
854
  goto tr4;
835
855
  tr4:
836
- #line 292 "parser.rl"
856
+ #line 312 "parser.rl"
837
857
  { p--; {p++; cs = 4; goto _out;} }
838
858
  goto st4;
839
859
  st4:
840
860
  if ( ++p == pe )
841
861
  goto _test_eof4;
842
862
  case 4:
843
- #line 844 "parser.c"
863
+ #line 864 "parser.c"
844
864
  goto st0;
845
865
  st5:
846
866
  if ( ++p == pe )
@@ -859,7 +879,7 @@ case 5:
859
879
  _out: {}
860
880
  }
861
881
 
862
- #line 304 "parser.rl"
882
+ #line 324 "parser.rl"
863
883
 
864
884
  if (cs >= JSON_integer_first_final) {
865
885
  long len = p - json->memo;
@@ -874,15 +894,15 @@ case 5:
874
894
  }
875
895
 
876
896
 
877
- #line 878 "parser.c"
878
- enum {JSON_float_start = 1};
879
- enum {JSON_float_first_final = 8};
880
- enum {JSON_float_error = 0};
897
+ #line 898 "parser.c"
898
+ static const int JSON_float_start = 1;
899
+ static const int JSON_float_first_final = 8;
900
+ static const int JSON_float_error = 0;
881
901
 
882
- enum {JSON_float_en_main = 1};
902
+ static const int JSON_float_en_main = 1;
883
903
 
884
904
 
885
- #line 329 "parser.rl"
905
+ #line 349 "parser.rl"
886
906
 
887
907
 
888
908
  static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -890,15 +910,15 @@ static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *resul
890
910
  int cs = EVIL;
891
911
 
892
912
 
893
- #line 894 "parser.c"
913
+ #line 914 "parser.c"
894
914
  {
895
915
  cs = JSON_float_start;
896
916
  }
897
917
 
898
- #line 336 "parser.rl"
918
+ #line 356 "parser.rl"
899
919
  json->memo = p;
900
920
 
901
- #line 902 "parser.c"
921
+ #line 922 "parser.c"
902
922
  {
903
923
  if ( p == pe )
904
924
  goto _test_eof;
@@ -956,14 +976,14 @@ case 8:
956
976
  goto st0;
957
977
  goto tr9;
958
978
  tr9:
959
- #line 323 "parser.rl"
979
+ #line 343 "parser.rl"
960
980
  { p--; {p++; cs = 9; goto _out;} }
961
981
  goto st9;
962
982
  st9:
963
983
  if ( ++p == pe )
964
984
  goto _test_eof9;
965
985
  case 9:
966
- #line 967 "parser.c"
986
+ #line 987 "parser.c"
967
987
  goto st0;
968
988
  st5:
969
989
  if ( ++p == pe )
@@ -1024,7 +1044,7 @@ case 7:
1024
1044
  _out: {}
1025
1045
  }
1026
1046
 
1027
- #line 338 "parser.rl"
1047
+ #line 358 "parser.rl"
1028
1048
 
1029
1049
  if (cs >= JSON_float_first_final) {
1030
1050
  long len = p - json->memo;
@@ -1040,15 +1060,15 @@ case 7:
1040
1060
 
1041
1061
 
1042
1062
 
1043
- #line 1044 "parser.c"
1044
- enum {JSON_array_start = 1};
1045
- enum {JSON_array_first_final = 17};
1046
- enum {JSON_array_error = 0};
1063
+ #line 1064 "parser.c"
1064
+ static const int JSON_array_start = 1;
1065
+ static const int JSON_array_first_final = 17;
1066
+ static const int JSON_array_error = 0;
1047
1067
 
1048
- enum {JSON_array_en_main = 1};
1068
+ static const int JSON_array_en_main = 1;
1049
1069
 
1050
1070
 
1051
- #line 381 "parser.rl"
1071
+ #line 401 "parser.rl"
1052
1072
 
1053
1073
 
1054
1074
  static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -1062,14 +1082,14 @@ static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *resul
1062
1082
  *result = NIL_P(array_class) ? rb_ary_new() : rb_class_new_instance(0, 0, array_class);
1063
1083
 
1064
1084
 
1065
- #line 1066 "parser.c"
1085
+ #line 1086 "parser.c"
1066
1086
  {
1067
1087
  cs = JSON_array_start;
1068
1088
  }
1069
1089
 
1070
- #line 394 "parser.rl"
1090
+ #line 414 "parser.rl"
1071
1091
 
1072
- #line 1073 "parser.c"
1092
+ #line 1093 "parser.c"
1073
1093
  {
1074
1094
  if ( p == pe )
1075
1095
  goto _test_eof;
@@ -1108,7 +1128,7 @@ case 2:
1108
1128
  goto st2;
1109
1129
  goto st0;
1110
1130
  tr2:
1111
- #line 358 "parser.rl"
1131
+ #line 378 "parser.rl"
1112
1132
  {
1113
1133
  VALUE v = Qnil;
1114
1134
  char *np = JSON_parse_value(json, p, pe, &v);
@@ -1128,7 +1148,7 @@ st3:
1128
1148
  if ( ++p == pe )
1129
1149
  goto _test_eof3;
1130
1150
  case 3:
1131
- #line 1132 "parser.c"
1151
+ #line 1152 "parser.c"
1132
1152
  switch( (*p) ) {
1133
1153
  case 13: goto st3;
1134
1154
  case 32: goto st3;
@@ -1228,14 +1248,14 @@ case 12:
1228
1248
  goto st3;
1229
1249
  goto st12;
1230
1250
  tr4:
1231
- #line 373 "parser.rl"
1251
+ #line 393 "parser.rl"
1232
1252
  { p--; {p++; cs = 17; goto _out;} }
1233
1253
  goto st17;
1234
1254
  st17:
1235
1255
  if ( ++p == pe )
1236
1256
  goto _test_eof17;
1237
1257
  case 17:
1238
- #line 1239 "parser.c"
1258
+ #line 1259 "parser.c"
1239
1259
  goto st0;
1240
1260
  st13:
1241
1261
  if ( ++p == pe )
@@ -1291,12 +1311,12 @@ case 16:
1291
1311
  _out: {}
1292
1312
  }
1293
1313
 
1294
- #line 395 "parser.rl"
1314
+ #line 415 "parser.rl"
1295
1315
 
1296
1316
  if(cs >= JSON_array_first_final) {
1297
1317
  return p + 1;
1298
1318
  } else {
1299
- rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p);
1319
+ rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p);
1300
1320
  return NULL;
1301
1321
  }
1302
1322
  }
@@ -1372,15 +1392,15 @@ static VALUE json_string_unescape(VALUE result, char *string, char *stringEnd)
1372
1392
  }
1373
1393
 
1374
1394
 
1375
- #line 1376 "parser.c"
1376
- enum {JSON_string_start = 1};
1377
- enum {JSON_string_first_final = 8};
1378
- enum {JSON_string_error = 0};
1395
+ #line 1396 "parser.c"
1396
+ static const int JSON_string_start = 1;
1397
+ static const int JSON_string_first_final = 8;
1398
+ static const int JSON_string_error = 0;
1379
1399
 
1380
- enum {JSON_string_en_main = 1};
1400
+ static const int JSON_string_en_main = 1;
1381
1401
 
1382
1402
 
1383
- #line 494 "parser.rl"
1403
+ #line 514 "parser.rl"
1384
1404
 
1385
1405
 
1386
1406
  static int
@@ -1402,15 +1422,15 @@ static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *resu
1402
1422
 
1403
1423
  *result = rb_str_buf_new(0);
1404
1424
 
1405
- #line 1406 "parser.c"
1425
+ #line 1426 "parser.c"
1406
1426
  {
1407
1427
  cs = JSON_string_start;
1408
1428
  }
1409
1429
 
1410
- #line 515 "parser.rl"
1430
+ #line 535 "parser.rl"
1411
1431
  json->memo = p;
1412
1432
 
1413
- #line 1414 "parser.c"
1433
+ #line 1434 "parser.c"
1414
1434
  {
1415
1435
  if ( p == pe )
1416
1436
  goto _test_eof;
@@ -1435,7 +1455,7 @@ case 2:
1435
1455
  goto st0;
1436
1456
  goto st2;
1437
1457
  tr2:
1438
- #line 480 "parser.rl"
1458
+ #line 500 "parser.rl"
1439
1459
  {
1440
1460
  *result = json_string_unescape(*result, json->memo + 1, p);
1441
1461
  if (NIL_P(*result)) {
@@ -1446,14 +1466,14 @@ tr2:
1446
1466
  {p = (( p + 1))-1;}
1447
1467
  }
1448
1468
  }
1449
- #line 491 "parser.rl"
1469
+ #line 511 "parser.rl"
1450
1470
  { p--; {p++; cs = 8; goto _out;} }
1451
1471
  goto st8;
1452
1472
  st8:
1453
1473
  if ( ++p == pe )
1454
1474
  goto _test_eof8;
1455
1475
  case 8:
1456
- #line 1457 "parser.c"
1476
+ #line 1477 "parser.c"
1457
1477
  goto st0;
1458
1478
  st3:
1459
1479
  if ( ++p == pe )
@@ -1529,7 +1549,7 @@ case 7:
1529
1549
  _out: {}
1530
1550
  }
1531
1551
 
1532
- #line 517 "parser.rl"
1552
+ #line 537 "parser.rl"
1533
1553
 
1534
1554
  if (json->create_additions && RTEST(match_string = json->match_string)) {
1535
1555
  VALUE klass;
@@ -1566,29 +1586,29 @@ case 7:
1566
1586
 
1567
1587
  static VALUE convert_encoding(VALUE source)
1568
1588
  {
1569
- char *ptr = RSTRING_PTR(source);
1589
+ const char *ptr = RSTRING_PTR(source);
1570
1590
  long len = RSTRING_LEN(source);
1571
1591
  if (len < 2) {
1572
1592
  rb_raise(eParserError, "A JSON text must at least contain two octets!");
1573
1593
  }
1574
1594
  #ifdef HAVE_RUBY_ENCODING_H
1575
1595
  {
1576
- VALUE encoding = rb_funcall(source, i_encoding, 0);
1577
- if (encoding == CEncoding_ASCII_8BIT) {
1596
+ rb_encoding *enc = rb_enc_get(source);
1597
+ if (enc == rb_ascii8bit_encoding()) {
1578
1598
  if (len >= 4 && ptr[0] == 0 && ptr[1] == 0 && ptr[2] == 0) {
1579
- source = rb_funcall(source, i_encode, 2, CEncoding_UTF_8, CEncoding_UTF_32BE);
1599
+ source = rb_str_conv_enc(source, UTF_32BE, rb_utf8_encoding());
1580
1600
  } else if (len >= 4 && ptr[0] == 0 && ptr[2] == 0) {
1581
- source = rb_funcall(source, i_encode, 2, CEncoding_UTF_8, CEncoding_UTF_16BE);
1601
+ source = rb_str_conv_enc(source, UTF_16BE, rb_utf8_encoding());
1582
1602
  } else if (len >= 4 && ptr[1] == 0 && ptr[2] == 0 && ptr[3] == 0) {
1583
- source = rb_funcall(source, i_encode, 2, CEncoding_UTF_8, CEncoding_UTF_32LE);
1603
+ source = rb_str_conv_enc(source, UTF_32LE, rb_utf8_encoding());
1584
1604
  } else if (len >= 4 && ptr[1] == 0 && ptr[3] == 0) {
1585
- source = rb_funcall(source, i_encode, 2, CEncoding_UTF_8, CEncoding_UTF_16LE);
1605
+ source = rb_str_conv_enc(source, UTF_16LE, rb_utf8_encoding());
1586
1606
  } else {
1587
1607
  source = rb_str_dup(source);
1588
1608
  FORCE_UTF8(source);
1589
1609
  }
1590
1610
  } else {
1591
- source = rb_funcall(source, i_encode, 1, CEncoding_UTF_8);
1611
+ source = rb_str_conv_enc(source, rb_enc_get(source), rb_utf8_encoding());
1592
1612
  }
1593
1613
  }
1594
1614
  #else
@@ -1639,12 +1659,18 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
1639
1659
  if (json->Vsource) {
1640
1660
  rb_raise(rb_eTypeError, "already initialized instance");
1641
1661
  }
1662
+ #ifdef HAVE_RB_SCAN_ARGS_OPTIONAL_HASH
1663
+ rb_scan_args(argc, argv, "1:", &source, &opts);
1664
+ #else
1642
1665
  rb_scan_args(argc, argv, "11", &source, &opts);
1666
+ #endif
1643
1667
  if (!NIL_P(opts)) {
1668
+ #ifndef HAVE_RB_SCAN_ARGS_OPTIONAL_HASH
1644
1669
  opts = rb_convert_type(opts, T_HASH, "Hash", "to_hash");
1645
1670
  if (NIL_P(opts)) {
1646
1671
  rb_raise(rb_eArgError, "opts needs to be like a hash");
1647
1672
  } else {
1673
+ #endif
1648
1674
  VALUE tmp = ID2SYM(i_max_nesting);
1649
1675
  if (option_given_p(opts, tmp)) {
1650
1676
  VALUE max_nesting = rb_hash_aref(opts, tmp);
@@ -1707,7 +1733,9 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
1707
1733
  } else {
1708
1734
  json->match_string = Qnil;
1709
1735
  }
1736
+ #ifndef HAVE_RB_SCAN_ARGS_OPTIONAL_HASH
1710
1737
  }
1738
+ #endif
1711
1739
  } else {
1712
1740
  json->max_nesting = 100;
1713
1741
  json->allow_nan = 0;
@@ -1716,12 +1744,11 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
1716
1744
  json->object_class = Qnil;
1717
1745
  json->array_class = Qnil;
1718
1746
  }
1719
- source = rb_convert_type(source, T_STRING, "String", "to_str");
1747
+ StringValue(source);
1720
1748
  if (!json->quirks_mode) {
1721
- source = convert_encoding(StringValue(source));
1749
+ source = convert_encoding(source);
1722
1750
  }
1723
1751
  json->current_nesting = 0;
1724
- StringValue(source);
1725
1752
  json->len = RSTRING_LEN(source);
1726
1753
  json->source = RSTRING_PTR(source);;
1727
1754
  json->Vsource = source;
@@ -1729,15 +1756,15 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
1729
1756
  }
1730
1757
 
1731
1758
 
1732
- #line 1733 "parser.c"
1733
- enum {JSON_start = 1};
1734
- enum {JSON_first_final = 10};
1735
- enum {JSON_error = 0};
1759
+ #line 1760 "parser.c"
1760
+ static const int JSON_start = 1;
1761
+ static const int JSON_first_final = 10;
1762
+ static const int JSON_error = 0;
1736
1763
 
1737
- enum {JSON_en_main = 1};
1764
+ static const int JSON_en_main = 1;
1738
1765
 
1739
1766
 
1740
- #line 740 "parser.rl"
1767
+ #line 767 "parser.rl"
1741
1768
 
1742
1769
 
1743
1770
  static VALUE cParser_parse_strict(VALUE self)
@@ -1748,16 +1775,16 @@ static VALUE cParser_parse_strict(VALUE self)
1748
1775
  GET_PARSER;
1749
1776
 
1750
1777
 
1751
- #line 1752 "parser.c"
1778
+ #line 1779 "parser.c"
1752
1779
  {
1753
1780
  cs = JSON_start;
1754
1781
  }
1755
1782
 
1756
- #line 750 "parser.rl"
1783
+ #line 777 "parser.rl"
1757
1784
  p = json->source;
1758
1785
  pe = p + json->len;
1759
1786
 
1760
- #line 1761 "parser.c"
1787
+ #line 1788 "parser.c"
1761
1788
  {
1762
1789
  if ( p == pe )
1763
1790
  goto _test_eof;
@@ -1813,7 +1840,7 @@ case 5:
1813
1840
  goto st1;
1814
1841
  goto st5;
1815
1842
  tr3:
1816
- #line 729 "parser.rl"
1843
+ #line 756 "parser.rl"
1817
1844
  {
1818
1845
  char *np;
1819
1846
  json->current_nesting = 1;
@@ -1822,7 +1849,7 @@ tr3:
1822
1849
  }
1823
1850
  goto st10;
1824
1851
  tr4:
1825
- #line 722 "parser.rl"
1852
+ #line 749 "parser.rl"
1826
1853
  {
1827
1854
  char *np;
1828
1855
  json->current_nesting = 1;
@@ -1834,7 +1861,7 @@ st10:
1834
1861
  if ( ++p == pe )
1835
1862
  goto _test_eof10;
1836
1863
  case 10:
1837
- #line 1838 "parser.c"
1864
+ #line 1865 "parser.c"
1838
1865
  switch( (*p) ) {
1839
1866
  case 13: goto st10;
1840
1867
  case 32: goto st10;
@@ -1891,27 +1918,27 @@ case 9:
1891
1918
  _out: {}
1892
1919
  }
1893
1920
 
1894
- #line 753 "parser.rl"
1921
+ #line 780 "parser.rl"
1895
1922
 
1896
1923
  if (cs >= JSON_first_final && p == pe) {
1897
1924
  return result;
1898
1925
  } else {
1899
- rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p);
1926
+ rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p);
1900
1927
  return Qnil;
1901
1928
  }
1902
1929
  }
1903
1930
 
1904
1931
 
1905
1932
 
1906
- #line 1907 "parser.c"
1907
- enum {JSON_quirks_mode_start = 1};
1908
- enum {JSON_quirks_mode_first_final = 10};
1909
- enum {JSON_quirks_mode_error = 0};
1933
+ #line 1934 "parser.c"
1934
+ static const int JSON_quirks_mode_start = 1;
1935
+ static const int JSON_quirks_mode_first_final = 10;
1936
+ static const int JSON_quirks_mode_error = 0;
1910
1937
 
1911
- enum {JSON_quirks_mode_en_main = 1};
1938
+ static const int JSON_quirks_mode_en_main = 1;
1912
1939
 
1913
1940
 
1914
- #line 778 "parser.rl"
1941
+ #line 805 "parser.rl"
1915
1942
 
1916
1943
 
1917
1944
  static VALUE cParser_parse_quirks_mode(VALUE self)
@@ -1922,16 +1949,16 @@ static VALUE cParser_parse_quirks_mode(VALUE self)
1922
1949
  GET_PARSER;
1923
1950
 
1924
1951
 
1925
- #line 1926 "parser.c"
1952
+ #line 1953 "parser.c"
1926
1953
  {
1927
1954
  cs = JSON_quirks_mode_start;
1928
1955
  }
1929
1956
 
1930
- #line 788 "parser.rl"
1957
+ #line 815 "parser.rl"
1931
1958
  p = json->source;
1932
1959
  pe = p + json->len;
1933
1960
 
1934
- #line 1935 "parser.c"
1961
+ #line 1962 "parser.c"
1935
1962
  {
1936
1963
  if ( p == pe )
1937
1964
  goto _test_eof;
@@ -1965,7 +1992,7 @@ st0:
1965
1992
  cs = 0;
1966
1993
  goto _out;
1967
1994
  tr2:
1968
- #line 770 "parser.rl"
1995
+ #line 797 "parser.rl"
1969
1996
  {
1970
1997
  char *np = JSON_parse_value(json, p, pe, &result);
1971
1998
  if (np == NULL) { p--; {p++; cs = 10; goto _out;} } else {p = (( np))-1;}
@@ -1975,7 +2002,7 @@ st10:
1975
2002
  if ( ++p == pe )
1976
2003
  goto _test_eof10;
1977
2004
  case 10:
1978
- #line 1979 "parser.c"
2005
+ #line 2006 "parser.c"
1979
2006
  switch( (*p) ) {
1980
2007
  case 13: goto st10;
1981
2008
  case 32: goto st10;
@@ -2064,12 +2091,12 @@ case 9:
2064
2091
  _out: {}
2065
2092
  }
2066
2093
 
2067
- #line 791 "parser.rl"
2094
+ #line 818 "parser.rl"
2068
2095
 
2069
2096
  if (cs >= JSON_quirks_mode_first_final && p == pe) {
2070
2097
  return result;
2071
2098
  } else {
2072
- rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p);
2099
+ rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p);
2073
2100
  return Qnil;
2074
2101
  }
2075
2102
  }
@@ -2194,14 +2221,11 @@ void Init_parser(void)
2194
2221
  i_aref = rb_intern("[]");
2195
2222
  i_leftshift = rb_intern("<<");
2196
2223
  #ifdef HAVE_RUBY_ENCODING_H
2197
- CEncoding_UTF_8 = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-8"));
2198
- CEncoding_UTF_16BE = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-16be"));
2199
- CEncoding_UTF_16LE = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-16le"));
2200
- CEncoding_UTF_32BE = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-32be"));
2201
- CEncoding_UTF_32LE = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-32le"));
2202
- CEncoding_ASCII_8BIT = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("ascii-8bit"));
2203
- i_encoding = rb_intern("encoding");
2204
- i_encode = rb_intern("encode");
2224
+ UTF_8 = rb_utf8_encoding();
2225
+ UTF_16BE = rb_enc_find("utf-16be");
2226
+ UTF_16LE = rb_enc_find("utf-16le");
2227
+ UTF_32BE = rb_enc_find("utf-32be");
2228
+ UTF_32LE = rb_enc_find("utf-32le");
2205
2229
  #else
2206
2230
  i_iconv = rb_intern("iconv");
2207
2231
  #endif