Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 7 additions & 17 deletions lib/rdf/ntriples/reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ class Reader < RDF::Reader

# @see http://www.w3.org/TR/rdf-testcases/#ntrip_strings
ESCAPE_CHARS = ["\b", "\f", "\t", "\n", "\r", "\"", "'", "\\"].freeze
UCHAR4 = /\\u([0-9A-Fa-f]{4,4})/.freeze
UCHAR8 = /\\U([0-9A-Fa-f]{8,8})/.freeze
UCHAR4 = /(?<!\\)\\(?!\\)u([0-9A-Fa-f]{4,4})/.freeze
UCHAR8 = /(?<!\\)\\(?!\\)U([0-9A-Fa-f]{8,8})/.freeze
UCHAR = Regexp.union(UCHAR4, UCHAR8).freeze


Expand Down Expand Up @@ -187,22 +187,12 @@ def self.unescape(string)
# Note: avoiding copying the input string when no escaping is needed
# greatly reduces the number of allocations and the processing time.
string = string.dup.force_encoding(Encoding::UTF_8) unless string.encoding == Encoding::UTF_8
scanner = StringScanner.new(string)

buffer = ""

while !scanner.eos?
buffer << if scanner.scan(ESCAPE_CHARS_ESCAPED_REGEXP)
ESCAPE_CHARS_ESCAPED[scanner.matched]
elsif scanner.scan(UCHAR)
scanner.matched.sub(UCHAR) {[($1 || $2).hex].pack('U*')}
else
# Scan one character
scanner.getch
end
end

buffer
string
.gsub(UCHAR) do
[($1 || $2).hex].pack('U*')
end
.gsub(ESCAPE_CHARS_ESCAPED_REGEXP, ESCAPE_CHARS_ESCAPED)
end

##
Expand Down