From 35bd6bd5d9de42262b7fa9fb4d6bd9ebe90716c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Co=C3=AAlho?= Date: Fri, 1 Dec 2023 17:40:30 +0000 Subject: [PATCH] Handle name escaping --- lib/rubrik/document/serialize_object.rb | 25 +++++++++++++++--- test/rubrik/document/serialize_object_test.rb | 22 +++++++++++---- .../inline_interactive_form.expected.pdf | Bin 9426 -> 9466 bytes test/support/inline_interactive_form.pdf | Bin 526 -> 547 bytes 4 files changed, 39 insertions(+), 8 deletions(-) diff --git a/lib/rubrik/document/serialize_object.rb b/lib/rubrik/document/serialize_object.rb index 4d7a4aa..74e322c 100644 --- a/lib/rubrik/document/serialize_object.rb +++ b/lib/rubrik/document/serialize_object.rb @@ -15,7 +15,7 @@ def [](obj) serialized_objs = obj.flatten.map { |e| SerializeObject[e] } "<<#{serialized_objs.join(" ")}>>" when Symbol - "/#{obj}" + serialize_symbol(obj) when Array serialized_objs = obj.map { |e| SerializeObject[e] } "[#{serialized_objs.join(" ")}]" @@ -51,7 +51,7 @@ def [](obj) private - ESCAPE_MAP = { + STRING_ESCAPE_MAP = { "\n" => "\\\n", "\r" => "\\\r", "\t" => "\\\t", @@ -64,7 +64,26 @@ def [](obj) sig {params(string: String).returns(String)} def serialize_string(string) - "(#{string.gsub(/[\n\r\t\b\f\\()]/n, ESCAPE_MAP)})" + "(#{string.gsub(/[\n\r\t\b\f\\()]/n, STRING_ESCAPE_MAP)})" + end + + DELIMITERS = "()<>[]{}/%".bytes.freeze + REGULAR_CHARACTERS = + "!\"\#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~".bytes.freeze + + + NAME_ESCAPE_CHARACTERS = (0..255).to_a - REGULAR_CHARACTERS + DELIMITERS + "#".bytes + NAME_ESCAPE_CHARACTERS.freeze + + NAME_ESCAPE_MAP = NAME_ESCAPE_CHARACTERS.each_with_object({}) do |char, escape_map| + escape_map[char.chr] = "##{char.to_s(16).rjust(2, "0")}" + end.freeze + + NAME_ESCAPE_REGEX = /[#{Regexp.escape(NAME_ESCAPE_CHARACTERS.map(&:chr).join)}]/ + + sig {params(symbol: Symbol).returns(String)} + def serialize_symbol(symbol) + "/#{symbol.to_s.b.gsub(NAME_ESCAPE_REGEX, NAME_ESCAPE_MAP)}" end end end diff --git a/test/rubrik/document/serialize_object_test.rb b/test/rubrik/document/serialize_object_test.rb index e8bd523..5d93ec7 100644 --- a/test/rubrik/document/serialize_object_test.rb +++ b/test/rubrik/document/serialize_object_test.rb @@ -17,11 +17,23 @@ def test_hash_serialization end def test_symbol_serialization - # Act - result = SerializeObject[:Test] - - # Assert - assert_equal("/Test", result) + # Act + Assert + [ + ["/Name1", :"Name1"], + ["/ASomewhatLongerName", :"ASomewhatLongerName"], + ["/A;Name_With-Various***Characters?", :"A;Name_With-Various***Characters?"], + ["/1.2", :"1.2"], + ["/$$", :"$$"], + ["/@pattern", :"@pattern"], + ["/.notdef", :".notdef"], + ["/Lime#20Green", :"Lime Green"], + ["/paired#28#29parentheses", :"paired()parentheses"], + ["/The_Key_of_F#23_Minor", :"The_Key_of_F#_Minor"], + ["/AB", :"AB"], + ["/H#c3#b6#c3#9fgang", :"Hößgang"], + ].each do |(expected, subject)| + assert_equal(expected, SerializeObject[subject]) + end end def test_array_serialization diff --git a/test/support/inline_interactive_form.expected.pdf b/test/support/inline_interactive_form.expected.pdf index df68028571db7df39058bb06d1a7ffd502404635..53e3cd510f431a26f8ce8f4dcd4728f46d3a3183 100644 GIT binary patch delta 197 zcmccQ`O9;{9%f}T!^sBsF<0B5xSVAfzjmijM9@AtB6WjnAzEJ6_+Fyl~fd^rg2#snVE5^s=E5SaRC6i CQ!}#w delta 158 zcmez6dC7Cap23#yg@~J@_$BYCNtB`GECnTxJ}FzEG$h76-+ElC(A0o zoGhtgD{X3Nu3!KH3V8}##tMd9V2**Mp`pd(LKQJBBP3~vl!2LhHz{0JuX5umAu6