Skip to content

Commit

Permalink
Proposed changes for mandatory escapes for N-Triples/Quads canonicali…
Browse files Browse the repository at this point in the history
…zation.
  • Loading branch information
gkellogg committed Mar 18, 2023
1 parent d83714d commit 1cb1562
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
23 changes: 13 additions & 10 deletions lib/rdf/ntriples/writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,19 @@ def self.escape_unicode(u, encoding)
# @see http://www.w3.org/TR/n-triples/
def self.escape_ascii(u, encoding)
case (u = u.ord)
when (0x00..0x07) then escape_utf16(u)
when (0x0A) then "\\n"
when (0x0D) then "\\r"
when (0x0E..0x1F) then escape_utf16(u)
when (0x22) then "\\\""
when (0x5C) then "\\\\"
when (0x7F) then escape_utf16(u)
when (0x00..0x7F) then u.chr
else
raise ArgumentError.new("expected an ASCII character in (0x00..0x7F), but got 0x#{u.to_s(16)}")
when (0x08) then "\\b"
when (0x09) then "\\t"
when (0x0A) then "\\n"
when (0x0C) then "\\f"
when (0x0D) then "\\r"
when (0x22) then "\\\""
when (0x27) then "\\'"
when (0x5C) then "\\\\"
when (0x00..0x1F) then escape_utf16(u)
when (0x7F) then escape_utf16(u)
when (0x00..0x7F) then u.chr
else
raise ArgumentError.new("expected an ASCII character in (0x00..0x7F), but got 0x#{u.to_s(16)}")
end
end

Expand Down
20 changes: 10 additions & 10 deletions spec/ntriples_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -848,17 +848,17 @@
# @see http://www.w3.org/TR/rdf-testcases/#ntrip_strings
it "should correctly escape ASCII characters (#x0-#x7F)" do
(0x00..0x07).each { |u| expect(writer.escape(u.chr, encoding)).to eq "\\u#{u.to_s(16).upcase.rjust(4, '0')}" }
expect(writer.escape(0x08.chr, encoding)).to eq "\b"
expect(writer.escape(0x09.chr, encoding)).to eq "\t"
expect(writer.escape(0x08.chr, encoding)).to eq "\\b"
expect(writer.escape(0x09.chr, encoding)).to eq "\\t"
expect(writer.escape(0x0A.chr, encoding)).to eq "\\n"
expect(writer.escape(0x0B.chr, encoding)).to eq "\v"
expect(writer.escape(0x0C.chr, encoding)).to eq "\f"
expect(writer.escape(0x0B.chr, encoding)).to eq "\\u000B"
expect(writer.escape(0x0C.chr, encoding)).to eq "\\f"
expect(writer.escape(0x0D.chr, encoding)).to eq "\\r"
(0x0E..0x1F).each { |u| expect(writer.escape(u.chr, encoding)).to eq "\\u#{u.to_s(16).upcase.rjust(4, '0')}" }
(0x20..0x21).each { |u| expect(writer.escape(u.chr, encoding)).to eq u.chr }
expect(writer.escape(0x22.chr, encoding)).to eq "\\\""
(0x23..0x26).each { |u| expect(writer.escape(u.chr, encoding)).to eq u.chr }
expect(writer.escape(0x27.chr, encoding)).to eq "'"
expect(writer.escape(0x27.chr, encoding)).to eq "\\'"
(0x28..0x5B).each { |u| expect(writer.escape(u.chr, encoding)).to eq u.chr }
expect(writer.escape(0x5C.chr, encoding)).to eq "\\\\"
(0x5D..0x7E).each { |u| expect(writer.escape(u.chr, encoding)).to eq u.chr }
Expand Down Expand Up @@ -910,17 +910,17 @@
# @see http://www.w3.org/TR/rdf-testcases/#ntrip_strings
it "should correctly escape ASCII characters (#x0-#x7F)" do
(0x00..0x07).each { |u| expect(writer.escape(u.chr, encoding)).to eq "\\u#{u.to_s(16).upcase.rjust(4, '0')}" }
expect(writer.escape(0x08.chr, encoding)).to eq "\b"
expect(writer.escape(0x09.chr, encoding)).to eq "\t"
expect(writer.escape(0x08.chr, encoding)).to eq "\\b"
expect(writer.escape(0x09.chr, encoding)).to eq "\\t"
expect(writer.escape(0x0A.chr, encoding)).to eq "\\n"
expect(writer.escape(0x0B.chr, encoding)).to eq "\v"
expect(writer.escape(0x0C.chr, encoding)).to eq "\f"
expect(writer.escape(0x0B.chr, encoding)).to eq "\\u000B"
expect(writer.escape(0x0C.chr, encoding)).to eq "\\f"
expect(writer.escape(0x0D.chr, encoding)).to eq "\\r"
(0x0E..0x1F).each { |u| expect(writer.escape(u.chr, encoding)).to eq "\\u#{u.to_s(16).upcase.rjust(4, '0')}" }
(0x20..0x21).each { |u| expect(writer.escape(u.chr, encoding)).to eq u.chr }
expect(writer.escape(0x22.chr, encoding)).to eq "\\\""
(0x23..0x26).each { |u| expect(writer.escape(u.chr, encoding)).to eq u.chr }
expect(writer.escape(0x27.chr, encoding)).to eq "'"
expect(writer.escape(0x27.chr, encoding)).to eq "\\'"
(0x28..0x5B).each { |u| expect(writer.escape(u.chr, encoding)).to eq u.chr }
expect(writer.escape(0x5C.chr, encoding)).to eq "\\\\"
(0x5D..0x7E).each { |u| expect(writer.escape(u.chr, encoding)).to eq u.chr }
Expand Down

0 comments on commit 1cb1562

Please sign in to comment.