Skip to content

Commit

Permalink
Update N-Triples/Quads ASCII escapes when writing literals based on p…
Browse files Browse the repository at this point in the history
…roposed RDF 1.2 behavior.
  • Loading branch information
gkellogg committed Mar 18, 2023
1 parent 1cb1562 commit ac80b2f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
3 changes: 1 addition & 2 deletions lib/rdf/ntriples/writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,10 @@ def self.escape_ascii(u, encoding)
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
when (0x20..0x7E) then u.chr
else
raise ArgumentError.new("expected an ASCII character in (0x00..0x7F), but got 0x#{u.to_s(16)}")
end
Expand Down
10 changes: 7 additions & 3 deletions spec/ntriples_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@
end
end

context "Writing a Statements" do
context "Writing Statements" do
let(:statements) {[
RDF::Statement(RDF::URI('s'), RDF::URI('p'), RDF::URI('o1')),
RDF::Statement(RDF::URI('s'), RDF::URI('p'), RDF::URI('o2'))
Expand All @@ -600,6 +600,10 @@
expect(writer.new.format_literal(RDF::Literal.new('Hello, world!'))).to eq '"Hello, world!"'
end

it "should correctly format string literals" do
expect(writer.new.format_literal(RDF::Literal.new('Hello, world!', datatype: RDF::XSD.string))).to eq '"Hello, world!"'
end

it "should correctly format language-tagged literals" do
expect(writer.new.format_literal(RDF::Literal.new('Hello, world!', language: :en))).to eq '"Hello, world!"@en'
end
Expand Down Expand Up @@ -858,7 +862,7 @@
(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 @@ -920,7 +924,7 @@
(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 ac80b2f

Please sign in to comment.