@@ -46,8 +46,6 @@ def self.dump(data)
46
46
47
47
# See https://github.com/kr/okjson for updates.
48
48
49
- require 'stringio'
50
-
51
49
# Some parts adapted from
52
50
# https://golang.org/src/pkg/json/decode.go and
53
51
# https://golang.org/src/pkg/utf8/utf8.go
@@ -468,19 +466,18 @@ def keyenc(k)
468
466
469
467
470
468
def strenc ( s )
471
- t = StringIO . new
472
- t . putc ( ?")
469
+ t = '"' . b
473
470
r = 0
474
471
475
472
while r < s . length
476
473
case s [ r ]
477
- when ?" then t . print ( '\\"' )
478
- when ?\\ then t . print ( '\\\\' )
479
- when ?\b then t . print ( '\\b' )
480
- when ?\f then t . print ( '\\f' )
481
- when ?\n then t . print ( '\\n' )
482
- when ?\r then t . print ( '\\r' )
483
- when ?\t then t . print ( '\\t' )
474
+ when ?" then t << '\\"'
475
+ when ?\\ then t << '\\\\'
476
+ when ?\b then t << '\\b'
477
+ when ?\f then t << '\\f'
478
+ when ?\n then t << '\\n'
479
+ when ?\r then t << '\\r'
480
+ when ?\t then t << '\\t'
484
481
else
485
482
c = s [ r ]
486
483
# In ruby >= 1.9, s[r] is a codepoint, not a byte.
@@ -490,23 +487,23 @@ def strenc(s)
490
487
if c . ord < Spc . ord
491
488
c = "\\ u%04x" % [ c . ord ]
492
489
end
493
- t . write ( c )
490
+ t << c
494
491
rescue
495
- t . write ( Ustrerr )
492
+ t << Ustrerr
496
493
end
497
494
elsif c < Spc
498
- t . write ( "\\ u%04x" % c )
495
+ t << "\\ u%04x" % c
499
496
elsif Spc <= c && c <= ?~
500
- t . putc ( c )
497
+ t << c
501
498
else
502
499
n = ucharcopy ( t , s , r ) # ensure valid UTF-8 output
503
500
r += n - 1 # r is incremented below
504
501
end
505
502
end
506
503
r += 1
507
504
end
508
- t . putc ( ?" )
509
- t . string
505
+ t << '"'
506
+ t
510
507
end
511
508
512
509
@@ -531,7 +528,7 @@ def ucharcopy(t, s, i)
531
528
532
529
# 1-byte, 7-bit sequence?
533
530
if c0 < Utagx
534
- t . putc ( c0 )
531
+ t << c0
535
532
return 1
536
533
end
537
534
@@ -544,8 +541,8 @@ def ucharcopy(t, s, i)
544
541
# 2-byte, 11-bit sequence?
545
542
if c0 < Utag3
546
543
raise Utf8Error if ( ( c0 &Umask2 ) <<6 | ( c1 &Umaskx ) ) <= Uchar1max
547
- t . putc ( c0 )
548
- t . putc ( c1 )
544
+ t << c0
545
+ t << c1
549
546
return 2
550
547
end
551
548
@@ -559,9 +556,9 @@ def ucharcopy(t, s, i)
559
556
if c0 < Utag4
560
557
u = ( c0 &Umask3 ) <<12 | ( c1 &Umaskx ) <<6 | ( c2 &Umaskx )
561
558
raise Utf8Error if u <= Uchar2max
562
- t . putc ( c0 )
563
- t . putc ( c1 )
564
- t . putc ( c2 )
559
+ t << c0
560
+ t << c1
561
+ t << c2
565
562
return 3
566
563
end
567
564
@@ -574,16 +571,16 @@ def ucharcopy(t, s, i)
574
571
if c0 < Utag5
575
572
u = ( c0 &Umask4 ) <<18 | ( c1 &Umaskx ) <<12 | ( c2 &Umaskx ) <<6 | ( c3 &Umaskx )
576
573
raise Utf8Error if u <= Uchar3max
577
- t . putc ( c0 )
578
- t . putc ( c1 )
579
- t . putc ( c2 )
580
- t . putc ( c3 )
574
+ t << c0
575
+ t << c1
576
+ t << c2
577
+ t << c3
581
578
return 4
582
579
end
583
580
584
581
raise Utf8Error
585
582
rescue Utf8Error
586
- t . write ( Ustrerr )
583
+ t << Ustrerr
587
584
return 1
588
585
end
589
586
0 commit comments