Method: String#rdf_escape

Defined in:
lib/rdf_context/string_hacks.rb

#rdf_escapeObject

Convert a UTF8 encoded Ruby string string to an escaped string, encoded with UTF16 big endian characters as U????, and return it.

\

Backslash

'

Single quote

"

Double quot

n

ASCII Linefeed

r

ASCII Carriage Return

t

ASCCII Horizontal Tab

uhhhh

character in BMP with Unicode value U+hhhh

U00hhhhhh

character in plane 1-16 with Unicode value U+hhhhhh



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rdf_context/string_hacks.rb', line 67

def rdf_escape
  string = self + '' # XXX workaround: avoid buffer sharing
  string.gsub!(/["\\\/\x0-\x1f]/) { RDF_MAP[$&] }
  if defined?(::Encoding)
    string.force_encoding(Encoding::UTF_8)
    string.gsub!(ESCAPE_RE) { |c|
                    s = c.dump.sub(/\"\\u\{(.+)\}\"/, '\1').upcase
                    (s.length <= 4 ? "\\u0000"[0,6-s.length] : "\\U00000000"[0,10-s.length]) + s
                  }
    string.force_encoding(Encoding::ASCII_8BIT)
  else
    string.gsub!(ESCAPE_RE) { |c|
                    s = Iconv.new('utf-16be', 'utf-8').iconv(c).unpack('H*').first.upcase
                    "\\u" + s
                  }
  end
  string
end