module JSON::TruffleRuby::Generator
def utf8_to_json(string, script_safe = false) # :nodoc:
UTF16 big endian characters as \u????, and return it.
Convert a UTF8 encoded Ruby string _string_ to a JSON string, encoded with
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
def utf8_to_json_ascii(string, script_safe = false) # :nodoc:
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
def valid_utf8?(string)
def valid_utf8?(string) encoding = string.encoding (encoding == Encoding::UTF_8 || encoding == Encoding::ASCII) && string.valid_encoding? end