Skip to content

Commit 7622a28

Browse files
committed
Replace StringScanner
1 parent cae8390 commit 7622a28

File tree

1 file changed

+7
-17
lines changed

1 file changed

+7
-17
lines changed

lib/rdf/ntriples/reader.rb

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ class Reader < RDF::Reader
4040

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

4747

@@ -187,22 +187,12 @@ def self.unescape(string)
187187
# Note: avoiding copying the input string when no escaping is needed
188188
# greatly reduces the number of allocations and the processing time.
189189
string = string.dup.force_encoding(Encoding::UTF_8) unless string.encoding == Encoding::UTF_8
190-
scanner = StringScanner.new(string)
191-
192-
buffer = ""
193-
194-
while !scanner.eos?
195-
buffer << if scanner.scan(ESCAPE_CHARS_ESCAPED_REGEXP)
196-
ESCAPE_CHARS_ESCAPED[scanner.matched]
197-
elsif scanner.scan(UCHAR)
198-
scanner.matched.sub(UCHAR) {[($1 || $2).hex].pack('U*')}
199-
else
200-
# Scan one character
201-
scanner.getch
202-
end
203-
end
204190

205-
buffer
191+
string
192+
.gsub(UCHAR) do
193+
[($1 || $2).hex].pack('U*')
194+
end
195+
.gsub(ESCAPE_CHARS_ESCAPED_REGEXP, ESCAPE_CHARS_ESCAPED)
206196
end
207197

208198
##

0 commit comments

Comments
 (0)