Module: JSON::TruffleRuby::Generator
- Defined in:
- lib/json/truffle_ruby/generator.rb
Defined Under Namespace
Modules: GeneratorMethods Classes: State
Constant Summary collapse
- MAP =
{ "\x0" => '\u0000', "\x1" => '\u0001', "\x2" => '\u0002', "\x3" => '\u0003', "\x4" => '\u0004', "\x5" => '\u0005', "\x6" => '\u0006', "\x7" => '\u0007', "\b" => '\b', "\t" => '\t', "\n" => '\n', "\xb" => '\u000b', "\f" => '\f', "\r" => '\r', "\xe" => '\u000e', "\xf" => '\u000f', "\x10" => '\u0010', "\x11" => '\u0011', "\x12" => '\u0012', "\x13" => '\u0013', "\x14" => '\u0014', "\x15" => '\u0015', "\x16" => '\u0016', "\x17" => '\u0017', "\x18" => '\u0018', "\x19" => '\u0019', "\x1a" => '\u001a', "\x1b" => '\u001b', "\x1c" => '\u001c', "\x1d" => '\u001d', "\x1e" => '\u001e', "\x1f" => '\u001f', '"' => '\"', '\\' => '\\\\', }.freeze
- ESCAPE_PATTERN =
:nodoc:
/[\/"\\\x0-\x1f]/n
- SCRIPT_SAFE_MAP =
MAP.merge( '/' => '\\/', "\u2028".b => '\u2028', "\u2029".b => '\u2029', ).freeze
- SCRIPT_SAFE_ESCAPE_PATTERN =
Regexp.union(ESCAPE_PATTERN, "\u2028".b, "\u2029".b)
Class Method Summary collapse
-
.utf8_to_json(string, script_safe = false) ⇒ Object
Convert a UTF8 encoded Ruby string string to a JSON string, encoded with UTF16 big endian characters as u????, and return it.
-
.utf8_to_json_ascii(string, script_safe = false) ⇒ Object
:nodoc:.
- .valid_utf8?(string) ⇒ Boolean
Class Method Details
.utf8_to_json(string, script_safe = false) ⇒ Object
Convert a UTF8 encoded Ruby string string to a JSON string, encoded with UTF16 big endian characters as u????, and return it.
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/json/truffle_ruby/generator.rb', line 54 def utf8_to_json(string, script_safe = false) # :nodoc: string = string.b if script_safe string.gsub!(SCRIPT_SAFE_ESCAPE_PATTERN) { SCRIPT_SAFE_MAP[$&] || $& } else string.gsub!(ESCAPE_PATTERN) { MAP[$&] || $& } end string.force_encoding(::Encoding::UTF_8) string end |
.utf8_to_json_ascii(string, script_safe = false) ⇒ Object
:nodoc:
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/json/truffle_ruby/generator.rb', line 65 def utf8_to_json_ascii(string, script_safe = false) # :nodoc: string = string.b map = script_safe ? SCRIPT_SAFE_MAP : MAP string.gsub!(/[\/"\\\x0-\x1f]/n) { map[$&] || $& } string.gsub!(/( (?: [\xc2-\xdf][\x80-\xbf] | [\xe0-\xef][\x80-\xbf]{2} | [\xf0-\xf4][\x80-\xbf]{3} )+ | [\x80-\xc1\xf5-\xff] # invalid )/nx) { |c| c.size == 1 and raise GeneratorError, "invalid utf8 byte: '#{c}'" s = c.encode(::Encoding::UTF_16BE, ::Encoding::UTF_8).unpack('H*')[0] s.force_encoding(::Encoding::BINARY) s.gsub!(/.{4}/n, '\\\\u\&') s.force_encoding(::Encoding::UTF_8) } string.force_encoding(::Encoding::UTF_8) string rescue => e raise GeneratorError.wrap(e) end |
.valid_utf8?(string) ⇒ Boolean
89 90 91 92 93 |
# File 'lib/json/truffle_ruby/generator.rb', line 89 def valid_utf8?(string) encoding = string.encoding (encoding == Encoding::UTF_8 || encoding == Encoding::ASCII) && string.valid_encoding? end |