Skip to content

Commit

Permalink
Finish 1.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
gkellogg committed Jan 17, 2022
2 parents c1cbb66 + 8f6ab9a commit fa31e4e
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
ruby:
- 2.6
- 2.7
- 3.0
- "3.0"
- 3.1
- ruby-head
- jruby
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.0
1.2.1
24 changes: 20 additions & 4 deletions lib/sxp/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,27 @@ def to_sxp(**options)
# Extensions for Ruby's `String` class.
class String
##
# Returns the SXP representation of this object.
# Returns the SXP representation of this object. Uses SPARQL-like escaping.
#
# @return [String]
def to_sxp(**options)
inspect
buffer = ""
each_char do |u|
buffer << case u.ord
when (0x00..0x07) then sprintf("\\u%04X", u.ord)
when (0x08) then '\b'
when (0x09) then '\t'
when (0x0A) then '\n'
when (0x0C) then '\f'
when (0x0D) then '\r'
when (0x0E..0x1F) then sprintf("\\u%04X", u.ord)
when (0x22) then '\"'
when (0x5C) then '\\\\'
when (0x7F) then sprintf("\\u%04X", u.ord)
else u.chr
end
end
'"' + buffer + '"'
end
end

Expand Down Expand Up @@ -229,7 +245,7 @@ def to_sxp(prefixes: nil, base_uri: nil, **options)

class RDF::URI
##
# Returns the SXP representation of this a URI. Uses Lexical representation, if set, otherwise, any PName match, otherwise, the relativized version of the URI if a base_uri is given, otherwise just the URI.
# Returns the SXP representation of this URI. Uses Lexical representation, if set, otherwise, any PName match, otherwise, the relativized version of the URI if a base_uri is given, otherwise just the URI.
#
# @param [Hash{Symbol => RDF::URI}] prefixes(nil)
# @param [RDF::URI] base_uri(nil)
Expand Down Expand Up @@ -268,7 +284,7 @@ def to_sxp(**options)
# Retain stated lexical form if possible
valid? ? to_s : object.to_sxp(**options)
else
text = value.dump
text = value.to_sxp
text << "@#{language}" if self.has_language?
text << "^^#{datatype.to_sxp(**options)}" if self.has_datatype?
text
Expand Down
8 changes: 4 additions & 4 deletions lib/sxp/reader/basic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def read_atom
##
# @return [String]
def read_string
buffer = String.new
buffer = ""
skip_char # '"'
until peek_char == ?" #"
buffer <<
Expand All @@ -57,8 +57,8 @@ def read_character
when ?n then ?\n
when ?r then ?\r
when ?t then ?\t
when ?u then read_chars(4).to_i(16).chr
when ?U then read_chars(8).to_i(16).chr
when ?u then read_chars(4).to_i(16).chr(Encoding::UTF_8)
when ?U then read_chars(8).to_i(16).chr(Encoding::UTF_8)
when ?" then char #"
when ?\\ then char
else char
Expand All @@ -69,7 +69,7 @@ def read_character
# @return [String]
def read_literal
grammar = self.class.const_get(:ATOM)
buffer = String.new
buffer = ""
buffer << read_char while !eof? && peek_char.chr =~ grammar
buffer
end
Expand Down
2 changes: 1 addition & 1 deletion lib/sxp/reader/common_lisp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def read_sharp
##
# @return [Symbol]
def read_symbol(delimiter = nil)
buffer = String.new
buffer = ""
skip_char # '|'
until delimiter === peek_char
buffer <<
Expand Down
2 changes: 1 addition & 1 deletion lib/sxp/reader/sparql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def read_rdf_literal
#
# @return [RDF::URI]
def read_rdf_uri
buffer = String.new
buffer = ""
skip_char # '<'
return :< if (char = peek_char).nil? || char.chr !~ ATOM # FIXME: nasty special case for the '< symbol
return :<= if peek_char.chr.eql?(?=.chr) && read_char # FIXME: nasty special case for the '<= symbol
Expand Down
60 changes: 60 additions & 0 deletions spec/extensions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,36 @@
expect(value.to_sxp).to eq result
end
end

describe "string escapes" do
{
"\b" => %{"\\b"},
"\f" => %{"\\f"},
"\n" => %{"\\n"},
"\r" => %{"\\r"},
"\t" => %{"\\t"},
"\\" => %{"\\\\"},
"\u0080" => %{"\u0080"},
"\u07FF" => %("\u07FF"),
"\u0800" => %("\u0800"),
"\u0FFF" => %("\u0FFF"),
"\u1000" => %("\u1000"),
"\uD000" => %("\uD000"),
"\uD7FF" => %("\uD7FF"),
"\uE000" => %("\uE000"),
"\uFFFD" => %("\uFFFD"),
"\u{10000}" => %("\u{010000}"),
"\u{3FFFD}" => %("\u{03FFFD}"),
"\u{40000}" => %("\u{040000}"),
"\u{FFFFD}" => %("\u{0FFFFD}"),
"\u{100000}" => %("\u{100000}"),
"\u{10FFFD}" => %("\u{10FFFD}"),
}.each do |value, result|
it "writes #{value} as #{result.inspect}" do
expect(value.to_sxp).to eq result
end
end
end
end

describe "Vector" do
Expand Down Expand Up @@ -56,6 +86,36 @@
}.each_pair do |l, sxp|
specify {expect(l.to_sxp).to eq sxp}
end

describe "string escapes" do
{
"\b" => %{"\\b"},
"\f" => %{"\\f"},
"\n" => %{"\\n"},
"\r" => %{"\\r"},
"\t" => %{"\\t"},
"\\" => %{"\\\\"},
"\u0080" => %{"\u0080"},
"\u07FF" => %("\u07FF"),
"\u0800" => %("\u0800"),
"\u0FFF" => %("\u0FFF"),
"\u1000" => %("\u1000"),
"\uD000" => %("\uD000"),
"\uD7FF" => %("\uD7FF"),
"\uE000" => %("\uE000"),
"\uFFFD" => %("\uFFFD"),
"\u{10000}" => %("\u{010000}"),
"\u{3FFFD}" => %("\u{03FFFD}"),
"\u{40000}" => %("\u{040000}"),
"\u{FFFFD}" => %("\u{0FFFFD}"),
"\u{100000}" => %("\u{100000}"),
"\u{10FFFD}" => %("\u{10FFFD}"),
}.each do |value, result|
it "writes #{value} as #{result.inspect}" do
expect(RDF::Literal(value).to_sxp).to eq result
end
end
end
end

describe "RDF::Literal#to_sxp with prefix" do
Expand Down
29 changes: 29 additions & 0 deletions spec/reader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,35 @@
end
end

context "escapes in strings" do
{
%q{"\b"} => "\b",
%q{"\f"} => "\f",
%q{"\n"} => "\n",
%q{"\r"} => "\r",
%q{"\t"} => "\t",
%q{"\u0080"} => "\u0080",
%q("\u07FF") => "\u07FF",
%q("\u0800") => "\u0800",
%q("\u0FFF") => "\u0FFF",
%q("\u1000") => "\u1000",
%q("\uD000") => "\uD000",
%q("\uD7FF") => "\uD7FF",
%q("\uE000") => "\uE000",
%q("\uFFFD") => "\uFFFD",
%q("\U00010000") => "\u{10000}",
%q("\U0003FFFD") => "\u{3FFFD}",
%q("\U00040000") => "\u{40000}",
%q("\U000FFFFD") => "\u{FFFFD}",
%q("\U00100000") => "\u{100000}",
%q("\U0010FFFD") => "\u{10FFFD}",
}.each do |input, output|
it "reads #{input} as #{output.inspect}" do
expect(read(input)).to eq output
end
end
end

context "problematic examples" do
{
%q{"\t'[]()-"} => "\t'[]()-",
Expand Down

0 comments on commit fa31e4e

Please sign in to comment.