module JSON::TruffleRuby::Generator

def utf8_to_json_ascii(string, script_safe = false) # :nodoc:

: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