Skip to content

Commit

Permalink
Drop dependency on StringIO
Browse files Browse the repository at this point in the history
StringIO was extracted as a gem, so spring can no
longer use it for the same reason it can't use the `json`
gem.

Otherwise you'll see warnings like:

```
...stringio.bundle: warning: already initialized constant StringIO::VERSION
```

This can be reproduced on Ruby 3.0, by installing `stringio 3.0.1`
and starting a Rails app with spring.
  • Loading branch information
byroot committed Nov 24, 2021
1 parent af2de8a commit 262bbd3
Showing 1 changed file with 25 additions and 28 deletions.
53 changes: 25 additions & 28 deletions lib/spring/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ def self.dump(data)

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

require 'stringio'

# Some parts adapted from
# https://golang.org/src/pkg/json/decode.go and
# https://golang.org/src/pkg/utf8/utf8.go
Expand Down Expand Up @@ -468,19 +466,18 @@ def keyenc(k)


def strenc(s)
t = StringIO.new
t.putc(?")
t = '"'.b
r = 0

while r < s.length
case s[r]
when ?" then t.print('\\"')
when ?\\ then t.print('\\\\')
when ?\b then t.print('\\b')
when ?\f then t.print('\\f')
when ?\n then t.print('\\n')
when ?\r then t.print('\\r')
when ?\t then t.print('\\t')
when ?" then t << '\\"'
when ?\\ then t << '\\\\'
when ?\b then t << '\\b'
when ?\f then t << '\\f'
when ?\n then t << '\\n'
when ?\r then t << '\\r'
when ?\t then t << '\\t'
else
c = s[r]
# In ruby >= 1.9, s[r] is a codepoint, not a byte.
Expand All @@ -490,23 +487,23 @@ def strenc(s)
if c.ord < Spc.ord
c = "\\u%04x" % [c.ord]
end
t.write(c)
t << c
rescue
t.write(Ustrerr)
t << Ustrerr
end
elsif c < Spc
t.write("\\u%04x" % c)
t << "\\u%04x" % c
elsif Spc <= c && c <= ?~
t.putc(c)
t << c
else
n = ucharcopy(t, s, r) # ensure valid UTF-8 output
r += n - 1 # r is incremented below
end
end
r += 1
end
t.putc(?")
t.string
t << '"'
t
end


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

# 1-byte, 7-bit sequence?
if c0 < Utagx
t.putc(c0)
t << c0
return 1
end

Expand All @@ -544,8 +541,8 @@ def ucharcopy(t, s, i)
# 2-byte, 11-bit sequence?
if c0 < Utag3
raise Utf8Error if ((c0&Umask2)<<6 | (c1&Umaskx)) <= Uchar1max
t.putc(c0)
t.putc(c1)
t << c0
t << c1
return 2
end

Expand All @@ -559,9 +556,9 @@ def ucharcopy(t, s, i)
if c0 < Utag4
u = (c0&Umask3)<<12 | (c1&Umaskx)<<6 | (c2&Umaskx)
raise Utf8Error if u <= Uchar2max
t.putc(c0)
t.putc(c1)
t.putc(c2)
t << c0
t << c1
t << c2
return 3
end

Expand All @@ -574,16 +571,16 @@ def ucharcopy(t, s, i)
if c0 < Utag5
u = (c0&Umask4)<<18 | (c1&Umaskx)<<12 | (c2&Umaskx)<<6 | (c3&Umaskx)
raise Utf8Error if u <= Uchar3max
t.putc(c0)
t.putc(c1)
t.putc(c2)
t.putc(c3)
t << c0
t << c1
t << c2
t << c3
return 4
end

raise Utf8Error
rescue Utf8Error
t.write(Ustrerr)
t << Ustrerr
return 1
end

Expand Down

0 comments on commit 262bbd3

Please sign in to comment.