def unquote(q)
    q = q[1...-1]
    a = q.dup # allocate a big enough string
    # In ruby >= 1.9, a[w] is a codepoint, not a byte.
    if rubydoesenc?
      a.force_encoding('UTF-8')
    end
    r, w = 0, 0
    while r < q.length
      c = q[r]
      if c == ?\\
        r += 1
        if r >= q.length
          raise Error, "string literal ends with a \"\\\": \"#{q}\""
        end

        case q[r]
        when ?",?\\,?/,?'
          a[w] = q[r]
          r += 1
          w += 1
        when ?b,?f,?n,?r,?t
          a[w] = Unesc[q[r]]
          r += 1
          w += 1
        when ?u
          r += 1
          uchar = begin
            hexdec4(q[r,4])
          rescue RuntimeError => e
            raise Error, "invalid escape sequence \\u#{q[r,4]}: #{e}"
          end
          r += 4
          if surrogate? uchar
            if q.length >= r+6
              uchar1 = hexdec4(q[r+2,4])
              uchar = subst(uchar, uchar1)
              if uchar != Ucharerr
                # A valid pair; consume.
                r += 6
              end
            end
          end
          if rubydoesenc?
            a[w] = '' << uchar
            w += 1
          else
            w += ucharenc(a, w, uchar)
          end
        else
          raise Error, "invalid escape char #{q[r]} in \"#{q}\""
        end
      elsif c == ?" || c < Spc
        raise Error, "invalid character in string literal \"#{q}\""
      else
        # Copy anything else byte-for-byte.
        # Valid UTF-8 will remain valid UTF-8.
        # Invalid UTF-8 will remain invalid UTF-8.
        # In ruby >= 1.9, c is a codepoint, not a byte,
        # in which case this is still what we want.
        a[w] = c
        r += 1
        w += 1
      end
    end
    a[0,w]
  end
def unquote(q)
    q = q[1...-1]
    a = q.dup # allocate a big enough string
    # In ruby >= 1.9, a[w] is a codepoint, not a byte.
    if rubydoesenc?
      a.force_encoding('UTF-8')
    end
    r, w = 0, 0
    while r < q.length
      c = q[r]
      if c == ?\\
        r += 1
        if r >= q.length
          raise Error, "string literal ends with a \"\\\": \"#{q}\""
        end

        case q[r]
        when ?",?\\,?/,?'
          a[w] = q[r]
          r += 1
          w += 1
        when ?b,?f,?n,?r,?t
          a[w] = Unesc[q[r]]
          r += 1
          w += 1
        when ?u
          r += 1
          uchar = begin
            hexdec4(q[r,4])
          rescue RuntimeError => e
            raise Error, "invalid escape sequence \\u#{q[r,4]}: #{e}"
          end
          r += 4
          if surrogate? uchar
            if q.length >= r+6
              uchar1 = hexdec4(q[r+2,4])
              uchar = subst(uchar, uchar1)
              if uchar != Ucharerr
                # A valid pair; consume.
                r += 6
              end
            end
          end
          if rubydoesenc?
            a[w] = '' << uchar
            w += 1
          else
            w += ucharenc(a, w, uchar)
          end
        else
          raise Error, "invalid escape char #{q[r]} in \"#{q}\""
        end
      elsif c == ?" || c < Spc
        raise Error, "invalid character in string literal \"#{q}\""
      else
        # Copy anything else byte-for-byte.
        # Valid UTF-8 will remain valid UTF-8.
        # Invalid UTF-8 will remain invalid UTF-8.
        # In ruby >= 1.9, c is a codepoint, not a byte,
        # in which case this is still what we want.
        a[w] = c
        r += 1
        w += 1
      end
    end
    a[0,w]
  end
Converts a quoted json string literal q into a UTF-8-encoded string.
 The rules are different than for Ruby, so we cannot use eval.
 Unquote will raise an error if q contains control characters.