@@ -1450,6 +1450,43 @@ case 16:
1450
1450
}
1451
1451
}
1452
1452
1453
+ static inline VALUE build_string (const char * buffer , const char * bufferStart , bool intern , bool symbolize )
1454
+ {
1455
+ if (symbolize ) {
1456
+ intern = true;
1457
+ }
1458
+ VALUE result ;
1459
+ # ifdef HAVE_RB_ENC_INTERNED_STR
1460
+ if (intern ) {
1461
+ result = rb_enc_interned_str (bufferStart , (long )(buffer - bufferStart ), rb_utf8_encoding ());
1462
+ } else {
1463
+ result = rb_utf8_str_new (bufferStart , (long )(buffer - bufferStart ));
1464
+ }
1465
+ # else
1466
+ result = rb_utf8_str_new (bufferStart , (long )(buffer - bufferStart ));
1467
+ if (intern ) {
1468
+ # if STR_UMINUS_DEDUPE_FROZEN
1469
+ // Starting from MRI 3.0 it is preferable to freeze the string
1470
+ // before deduplication so that it can be interned directly
1471
+ // otherwise it would be duplicated first which is wasteful.
1472
+ result = rb_funcall (rb_str_freeze (result ), i_uminus , 0 );
1473
+ # elif STR_UMINUS_DEDUPE
1474
+ // MRI 2.5 and older do not deduplicate strings that are already
1475
+ // frozen.
1476
+ result = rb_funcall (result , i_uminus , 0 );
1477
+ # else
1478
+ result = rb_str_freeze (result );
1479
+ # endif
1480
+ }
1481
+ # endif
1482
+
1483
+ if (symbolize ) {
1484
+ result = rb_str_intern (result );
1485
+ }
1486
+
1487
+ return result ;
1488
+ }
1489
+
1453
1490
static const size_t MAX_STACK_BUFFER_SIZE = 128 ;
1454
1491
static VALUE json_string_unescape (char * string , char * stringEnd , int intern , int symbolize )
1455
1492
{
@@ -1561,55 +1598,25 @@ static VALUE json_string_unescape(char *string, char *stringEnd, int intern, int
1561
1598
buffer += pe - p ;
1562
1599
}
1563
1600
1564
- # ifdef HAVE_RB_ENC_INTERNED_STR
1565
- if (intern ) {
1566
- result = rb_enc_interned_str (bufferStart , (long )(buffer - bufferStart ), rb_utf8_encoding ());
1567
- } else {
1568
- result = rb_utf8_str_new (bufferStart , (long )(buffer - bufferStart ));
1569
- }
1570
- if (bufferSize > MAX_STACK_BUFFER_SIZE ) {
1571
- ruby_xfree (bufferStart );
1572
- }
1573
- # else
1574
- result = rb_utf8_str_new (bufferStart , (long )(buffer - bufferStart ));
1575
-
1576
- if (bufferSize > MAX_STACK_BUFFER_SIZE ) {
1577
- ruby_xfree (bufferStart );
1578
- }
1601
+ result = build_string (buffer , bufferStart , intern , symbolize );
1579
1602
1580
- if (intern ) {
1581
- # if STR_UMINUS_DEDUPE_FROZEN
1582
- // Starting from MRI 2.8 it is preferable to freeze the string
1583
- // before deduplication so that it can be interned directly
1584
- // otherwise it would be duplicated first which is wasteful.
1585
- result = rb_funcall (rb_str_freeze (result ), i_uminus , 0 );
1586
- # elif STR_UMINUS_DEDUPE
1587
- // MRI 2.5 and older do not deduplicate strings that are already
1588
- // frozen.
1589
- result = rb_funcall (result , i_uminus , 0 );
1590
- # else
1591
- result = rb_str_freeze (result );
1592
- # endif
1593
- }
1594
- # endif
1595
-
1596
- if (symbolize ) {
1597
- result = rb_str_intern (result );
1603
+ if (bufferSize > MAX_STACK_BUFFER_SIZE ) {
1604
+ ruby_xfree (bufferStart );
1598
1605
}
1599
1606
1600
1607
return result ;
1601
1608
}
1602
1609
1603
1610
1604
- #line 1605 "parser.c"
1611
+ #line 1612 "parser.c"
1605
1612
enum {JSON_string_start = 1 };
1606
1613
enum {JSON_string_first_final = 8 };
1607
1614
enum {JSON_string_error = 0 };
1608
1615
1609
1616
enum {JSON_string_en_main = 1 };
1610
1617
1611
1618
1612
- #line 633 "parser.rl"
1619
+ #line 640 "parser.rl"
1613
1620
1614
1621
1615
1622
static int
@@ -1630,15 +1637,15 @@ static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *resu
1630
1637
VALUE match_string ;
1631
1638
1632
1639
1633
- #line 1634 "parser.c"
1640
+ #line 1641 "parser.c"
1634
1641
{
1635
1642
cs = JSON_string_start ;
1636
1643
}
1637
1644
1638
- #line 653 "parser.rl"
1645
+ #line 660 "parser.rl"
1639
1646
json -> memo = p ;
1640
1647
1641
- #line 1642 "parser.c"
1648
+ #line 1649 "parser.c"
1642
1649
{
1643
1650
if ( p == pe )
1644
1651
goto _test_eof ;
@@ -1663,7 +1670,7 @@ case 2:
1663
1670
goto st0 ;
1664
1671
goto st2 ;
1665
1672
tr2 :
1666
- #line 620 "parser.rl"
1673
+ #line 627 "parser.rl"
1667
1674
{
1668
1675
* result = json_string_unescape (json -> memo + 1 , p , json -> parsing_name || json -> freeze , json -> parsing_name && json -> symbolize_names );
1669
1676
if (NIL_P (* result )) {
@@ -1673,14 +1680,14 @@ case 2:
1673
1680
{p = (( p + 1 ))- 1 ;}
1674
1681
}
1675
1682
}
1676
- #line 630 "parser.rl"
1683
+ #line 637 "parser.rl"
1677
1684
{ p -- ; {p ++ ; cs = 8 ; goto _out ;} }
1678
1685
goto st8 ;
1679
1686
st8 :
1680
1687
if ( ++ p == pe )
1681
1688
goto _test_eof8 ;
1682
1689
case 8 :
1683
- #line 1684 "parser.c"
1690
+ #line 1691 "parser.c"
1684
1691
goto st0 ;
1685
1692
st3 :
1686
1693
if ( ++ p == pe )
@@ -1756,7 +1763,7 @@ case 7:
1756
1763
_out : {}
1757
1764
}
1758
1765
1759
- #line 655 "parser.rl"
1766
+ #line 662 "parser.rl"
1760
1767
1761
1768
if (json -> create_additions && RTEST (match_string = json -> match_string )) {
1762
1769
VALUE klass ;
@@ -1953,15 +1960,15 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
1953
1960
}
1954
1961
1955
1962
1956
- #line 1957 "parser.c"
1963
+ #line 1964 "parser.c"
1957
1964
enum {JSON_start = 1 };
1958
1965
enum {JSON_first_final = 10 };
1959
1966
enum {JSON_error = 0 };
1960
1967
1961
1968
enum {JSON_en_main = 1 };
1962
1969
1963
1970
1964
- #line 865 "parser.rl"
1971
+ #line 872 "parser.rl"
1965
1972
1966
1973
1967
1974
/*
@@ -1979,16 +1986,16 @@ static VALUE cParser_parse(VALUE self)
1979
1986
GET_PARSER ;
1980
1987
1981
1988
1982
- #line 1983 "parser.c"
1989
+ #line 1990 "parser.c"
1983
1990
{
1984
1991
cs = JSON_start ;
1985
1992
}
1986
1993
1987
- #line 882 "parser.rl"
1994
+ #line 889 "parser.rl"
1988
1995
p = json -> source ;
1989
1996
pe = p + json -> len ;
1990
1997
1991
- #line 1992 "parser.c"
1998
+ #line 1999 "parser.c"
1992
1999
{
1993
2000
if ( p == pe )
1994
2001
goto _test_eof ;
@@ -2022,7 +2029,7 @@ case 1:
2022
2029
cs = 0 ;
2023
2030
goto _out ;
2024
2031
tr2 :
2025
- #line 857 "parser.rl"
2032
+ #line 864 "parser.rl"
2026
2033
{
2027
2034
char * np = JSON_parse_value (json , p , pe , & result , 0 );
2028
2035
if (np == NULL ) { p -- ; {p ++ ; cs = 10 ; goto _out ;} } else {p = (( np ))- 1 ;}
@@ -2032,7 +2039,7 @@ cs = 0;
2032
2039
if ( ++ p == pe )
2033
2040
goto _test_eof10 ;
2034
2041
case 10 :
2035
- #line 2036 "parser.c"
2042
+ #line 2043 "parser.c"
2036
2043
switch ( (* p ) ) {
2037
2044
case 13 : goto st10 ;
2038
2045
case 32 : goto st10 ;
@@ -2121,7 +2128,7 @@ case 9:
2121
2128
_out : {}
2122
2129
}
2123
2130
2124
- #line 885 "parser.rl"
2131
+ #line 892 "parser.rl"
2125
2132
2126
2133
if (cs >= JSON_first_final && p == pe ) {
2127
2134
return result ;
0 commit comments