Skip to content

Commit 1cb1562

Browse files
committed
Proposed changes for mandatory escapes for N-Triples/Quads canonicalization.
1 parent d83714d commit 1cb1562

2 files changed

Lines changed: 23 additions & 20 deletions

File tree

lib/rdf/ntriples/writer.rb

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,19 @@ def self.escape_unicode(u, encoding)
125125
# @see http://www.w3.org/TR/n-triples/
126126
def self.escape_ascii(u, encoding)
127127
case (u = u.ord)
128-
when (0x00..0x07) then escape_utf16(u)
129-
when (0x0A) then "\\n"
130-
when (0x0D) then "\\r"
131-
when (0x0E..0x1F) then escape_utf16(u)
132-
when (0x22) then "\\\""
133-
when (0x5C) then "\\\\"
134-
when (0x7F) then escape_utf16(u)
135-
when (0x00..0x7F) then u.chr
136-
else
137-
raise ArgumentError.new("expected an ASCII character in (0x00..0x7F), but got 0x#{u.to_s(16)}")
128+
when (0x08) then "\\b"
129+
when (0x09) then "\\t"
130+
when (0x0A) then "\\n"
131+
when (0x0C) then "\\f"
132+
when (0x0D) then "\\r"
133+
when (0x22) then "\\\""
134+
when (0x27) then "\\'"
135+
when (0x5C) then "\\\\"
136+
when (0x00..0x1F) then escape_utf16(u)
137+
when (0x7F) then escape_utf16(u)
138+
when (0x00..0x7F) then u.chr
139+
else
140+
raise ArgumentError.new("expected an ASCII character in (0x00..0x7F), but got 0x#{u.to_s(16)}")
138141
end
139142
end
140143

spec/ntriples_spec.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -848,17 +848,17 @@
848848
# @see http://www.w3.org/TR/rdf-testcases/#ntrip_strings
849849
it "should correctly escape ASCII characters (#x0-#x7F)" do
850850
(0x00..0x07).each { |u| expect(writer.escape(u.chr, encoding)).to eq "\\u#{u.to_s(16).upcase.rjust(4, '0')}" }
851-
expect(writer.escape(0x08.chr, encoding)).to eq "\b"
852-
expect(writer.escape(0x09.chr, encoding)).to eq "\t"
851+
expect(writer.escape(0x08.chr, encoding)).to eq "\\b"
852+
expect(writer.escape(0x09.chr, encoding)).to eq "\\t"
853853
expect(writer.escape(0x0A.chr, encoding)).to eq "\\n"
854-
expect(writer.escape(0x0B.chr, encoding)).to eq "\v"
855-
expect(writer.escape(0x0C.chr, encoding)).to eq "\f"
854+
expect(writer.escape(0x0B.chr, encoding)).to eq "\\u000B"
855+
expect(writer.escape(0x0C.chr, encoding)).to eq "\\f"
856856
expect(writer.escape(0x0D.chr, encoding)).to eq "\\r"
857857
(0x0E..0x1F).each { |u| expect(writer.escape(u.chr, encoding)).to eq "\\u#{u.to_s(16).upcase.rjust(4, '0')}" }
858858
(0x20..0x21).each { |u| expect(writer.escape(u.chr, encoding)).to eq u.chr }
859859
expect(writer.escape(0x22.chr, encoding)).to eq "\\\""
860860
(0x23..0x26).each { |u| expect(writer.escape(u.chr, encoding)).to eq u.chr }
861-
expect(writer.escape(0x27.chr, encoding)).to eq "'"
861+
expect(writer.escape(0x27.chr, encoding)).to eq "\\'"
862862
(0x28..0x5B).each { |u| expect(writer.escape(u.chr, encoding)).to eq u.chr }
863863
expect(writer.escape(0x5C.chr, encoding)).to eq "\\\\"
864864
(0x5D..0x7E).each { |u| expect(writer.escape(u.chr, encoding)).to eq u.chr }
@@ -910,17 +910,17 @@
910910
# @see http://www.w3.org/TR/rdf-testcases/#ntrip_strings
911911
it "should correctly escape ASCII characters (#x0-#x7F)" do
912912
(0x00..0x07).each { |u| expect(writer.escape(u.chr, encoding)).to eq "\\u#{u.to_s(16).upcase.rjust(4, '0')}" }
913-
expect(writer.escape(0x08.chr, encoding)).to eq "\b"
914-
expect(writer.escape(0x09.chr, encoding)).to eq "\t"
913+
expect(writer.escape(0x08.chr, encoding)).to eq "\\b"
914+
expect(writer.escape(0x09.chr, encoding)).to eq "\\t"
915915
expect(writer.escape(0x0A.chr, encoding)).to eq "\\n"
916-
expect(writer.escape(0x0B.chr, encoding)).to eq "\v"
917-
expect(writer.escape(0x0C.chr, encoding)).to eq "\f"
916+
expect(writer.escape(0x0B.chr, encoding)).to eq "\\u000B"
917+
expect(writer.escape(0x0C.chr, encoding)).to eq "\\f"
918918
expect(writer.escape(0x0D.chr, encoding)).to eq "\\r"
919919
(0x0E..0x1F).each { |u| expect(writer.escape(u.chr, encoding)).to eq "\\u#{u.to_s(16).upcase.rjust(4, '0')}" }
920920
(0x20..0x21).each { |u| expect(writer.escape(u.chr, encoding)).to eq u.chr }
921921
expect(writer.escape(0x22.chr, encoding)).to eq "\\\""
922922
(0x23..0x26).each { |u| expect(writer.escape(u.chr, encoding)).to eq u.chr }
923-
expect(writer.escape(0x27.chr, encoding)).to eq "'"
923+
expect(writer.escape(0x27.chr, encoding)).to eq "\\'"
924924
(0x28..0x5B).each { |u| expect(writer.escape(u.chr, encoding)).to eq u.chr }
925925
expect(writer.escape(0x5C.chr, encoding)).to eq "\\\\"
926926
(0x5D..0x7E).each { |u| expect(writer.escape(u.chr, encoding)).to eq u.chr }

0 commit comments

Comments
 (0)