Skip to content

Commit 4ae1afd

Browse files
authored
Merge pull request #656 from casperisfine/drop-stringio-dep
Drop dependency on StringIO
2 parents af2de8a + 262bbd3 commit 4ae1afd

File tree

1 file changed

+25
-28
lines changed

1 file changed

+25
-28
lines changed

lib/spring/json.rb

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ def self.dump(data)
4646

4747
# See https://github.com/kr/okjson for updates.
4848

49-
require 'stringio'
50-
5149
# Some parts adapted from
5250
# https://golang.org/src/pkg/json/decode.go and
5351
# https://golang.org/src/pkg/utf8/utf8.go
@@ -468,19 +466,18 @@ def keyenc(k)
468466

469467

470468
def strenc(s)
471-
t = StringIO.new
472-
t.putc(?")
469+
t = '"'.b
473470
r = 0
474471

475472
while r < s.length
476473
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'
484481
else
485482
c = s[r]
486483
# In ruby >= 1.9, s[r] is a codepoint, not a byte.
@@ -490,23 +487,23 @@ def strenc(s)
490487
if c.ord < Spc.ord
491488
c = "\\u%04x" % [c.ord]
492489
end
493-
t.write(c)
490+
t << c
494491
rescue
495-
t.write(Ustrerr)
492+
t << Ustrerr
496493
end
497494
elsif c < Spc
498-
t.write("\\u%04x" % c)
495+
t << "\\u%04x" % c
499496
elsif Spc <= c && c <= ?~
500-
t.putc(c)
497+
t << c
501498
else
502499
n = ucharcopy(t, s, r) # ensure valid UTF-8 output
503500
r += n - 1 # r is incremented below
504501
end
505502
end
506503
r += 1
507504
end
508-
t.putc(?")
509-
t.string
505+
t << '"'
506+
t
510507
end
511508

512509

@@ -531,7 +528,7 @@ def ucharcopy(t, s, i)
531528

532529
# 1-byte, 7-bit sequence?
533530
if c0 < Utagx
534-
t.putc(c0)
531+
t << c0
535532
return 1
536533
end
537534

@@ -544,8 +541,8 @@ def ucharcopy(t, s, i)
544541
# 2-byte, 11-bit sequence?
545542
if c0 < Utag3
546543
raise Utf8Error if ((c0&Umask2)<<6 | (c1&Umaskx)) <= Uchar1max
547-
t.putc(c0)
548-
t.putc(c1)
544+
t << c0
545+
t << c1
549546
return 2
550547
end
551548

@@ -559,9 +556,9 @@ def ucharcopy(t, s, i)
559556
if c0 < Utag4
560557
u = (c0&Umask3)<<12 | (c1&Umaskx)<<6 | (c2&Umaskx)
561558
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
565562
return 3
566563
end
567564

@@ -574,16 +571,16 @@ def ucharcopy(t, s, i)
574571
if c0 < Utag5
575572
u = (c0&Umask4)<<18 | (c1&Umaskx)<<12 | (c2&Umaskx)<<6 | (c3&Umaskx)
576573
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
581578
return 4
582579
end
583580

584581
raise Utf8Error
585582
rescue Utf8Error
586-
t.write(Ustrerr)
583+
t << Ustrerr
587584
return 1
588585
end
589586

0 commit comments

Comments
 (0)