Skip to content

Commit c472d72

Browse files
authored
Merge pull request #744 from eregon/optimize-utf8_to_json
Optimize and cleanup JSON::TruffleRuby::Generator.utf8_to_json
2 parents e03515a + eac287c commit c472d72

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

lib/json/truffle_ruby/generator.rb

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,30 +39,33 @@ module Generator
3939
'\\' => '\\\\',
4040
}.freeze # :nodoc:
4141

42-
ESCAPE_PATTERN = /[\/"\\\x0-\x1f]/n # :nodoc:
43-
4442
SCRIPT_SAFE_MAP = MAP.merge(
4543
'/' => '\\/',
46-
"\u2028".b => '\u2028',
47-
"\u2029".b => '\u2029',
44+
"\u2028" => '\u2028',
45+
"\u2029" => '\u2029',
4846
).freeze
4947

50-
SCRIPT_SAFE_ESCAPE_PATTERN = Regexp.union(ESCAPE_PATTERN, "\u2028".b, "\u2029".b)
48+
SCRIPT_SAFE_ESCAPE_PATTERN = /[\/"\\\x0-\x1f\u2028-\u2029]/
5149

5250
# Convert a UTF8 encoded Ruby string _string_ to a JSON string, encoded with
5351
# UTF16 big endian characters as \u????, and return it.
54-
def utf8_to_json(string, script_safe = false) # :nodoc:
55-
string = string.b
52+
def self.utf8_to_json(string, script_safe = false) # :nodoc:
5653
if script_safe
57-
string.gsub!(SCRIPT_SAFE_ESCAPE_PATTERN) { SCRIPT_SAFE_MAP[$&] || $& }
54+
if SCRIPT_SAFE_ESCAPE_PATTERN.match?(string)
55+
string.gsub(SCRIPT_SAFE_ESCAPE_PATTERN, SCRIPT_SAFE_MAP)
56+
else
57+
string
58+
end
5859
else
59-
string.gsub!(ESCAPE_PATTERN) { MAP[$&] || $& }
60+
if /["\\\x0-\x1f]/.match?(string)
61+
string.gsub(/["\\\x0-\x1f]/, MAP)
62+
else
63+
string
64+
end
6065
end
61-
string.force_encoding(::Encoding::UTF_8)
62-
string
6366
end
6467

65-
def utf8_to_json_ascii(original_string, script_safe = false) # :nodoc:
68+
def self.utf8_to_json_ascii(original_string, script_safe = false) # :nodoc:
6669
string = original_string.b
6770
map = script_safe ? SCRIPT_SAFE_MAP : MAP
6871
string.gsub!(/[\/"\\\x0-\x1f]/n) { map[$&] || $& }
@@ -86,12 +89,11 @@ def utf8_to_json_ascii(original_string, script_safe = false) # :nodoc:
8689
raise GeneratorError.new(e.message, original_string)
8790
end
8891

89-
def valid_utf8?(string)
92+
def self.valid_utf8?(string)
9093
encoding = string.encoding
9194
(encoding == Encoding::UTF_8 || encoding == Encoding::ASCII) &&
9295
string.valid_encoding?
9396
end
94-
module_function :utf8_to_json, :utf8_to_json_ascii, :valid_utf8?
9597

9698
# This class is used to create State instances, that are use to hold data
9799
# while generating a JSON text from a Ruby data structure.
@@ -380,8 +382,8 @@ def generate_new(obj, anIO = nil) # :nodoc:
380382
end
381383
raise GeneratorError.new("source sequence is illegal/malformed utf-8", string) unless string.valid_encoding?
382384

383-
if /["\\\x0-\x1f]/n.match?(string)
384-
buf << string.gsub(/["\\\x0-\x1f]/n, MAP)
385+
if /["\\\x0-\x1f]/.match?(string)
386+
buf << string.gsub(/["\\\x0-\x1f]/, MAP)
385387
else
386388
buf << string
387389
end

0 commit comments

Comments
 (0)