Commit d0683f78 by Mark Wunsch

Ensure that built-up codepoints are written in encoding traversal

parent a20768b9
...@@ -26,14 +26,12 @@ module Rumoji ...@@ -26,14 +26,12 @@ module Rumoji
sequence = if possible_emoji.empty? sequence = if possible_emoji.empty?
# If this codepoint is not part of an emoji # If this codepoint is not part of an emoji
# just write it back out, along with any previously stored codepoints. # just write it back out, along with any previously stored codepoints.
sequence_from_codepoints(previous_codepoints, codepoint).tap do dump_codepoints_to_string(previous_codepoints << codepoint)
previous_codepoints.clear
end
elsif !possible_emoji.any?(&:multiple?) elsif !possible_emoji.any?(&:multiple?)
# If this codepoint is part of an emoji, but not one # If this codepoint is part of an emoji, but not one
# that contains multiple codepoints, write out the # that contains multiple codepoints, write out the
# first possiblity's cheat code. # first possiblity's cheat code and the previously stored codepoints
possible_emoji.first.code dump_codepoints_to_string(previous_codepoints) << possible_emoji.first.code
else else
# This codepoint is a part of an emoji with multiple # This codepoint is a part of an emoji with multiple
# codepoints, so push it onto the stack. # codepoints, so push it onto the stack.
...@@ -51,7 +49,7 @@ module Rumoji ...@@ -51,7 +49,7 @@ module Rumoji
end end
# Write any remaining codepoints. # Write any remaining codepoints.
writeable.write sequence_from_codepoints(previous_codepoints) if previous_codepoints.any? writeable.write dump_codepoints_to_string(previous_codepoints) if previous_codepoints.any?
writeable writeable
end end
...@@ -65,9 +63,10 @@ module Rumoji ...@@ -65,9 +63,10 @@ module Rumoji
private private
def sequence_from_codepoints(*codepoints) def dump_codepoints_to_string(codepoints)
compacted = codepoints.flatten.compact codepoints.pack("U*").tap do
compacted.pack("U*") codepoints.clear
end
end end
end end
...@@ -94,7 +94,7 @@ module Rumoji ...@@ -94,7 +94,7 @@ module Rumoji
self.new("\u{1F3B5}", [:musical_note]), # "music, being in good mood" self.new("\u{1F3B5}", [:musical_note]), # "music, being in good mood"
self.new("\u{1F525}", [:fire], "FIRE"), self.new("\u{1F525}", [:fire], "FIRE"),
# Poop # Poop
self.new("\u{1F4A9}", [:hankey, :poop, :shit], "PILE OF POO"), # "dog dirt" self.new("\u{1F4A9}", [:poop, :hankey, :shit], "PILE OF POO"), # "dog dirt"
self.new("\u{1F44D}", [:thumbsup, :"+1"], "THUMBS UP SIGN"), self.new("\u{1F44D}", [:thumbsup, :"+1"], "THUMBS UP SIGN"),
self.new("\u{1F44E}", [:thumbsdown, :"-1"], "THUMBS DOWN SIGN"), self.new("\u{1F44E}", [:thumbsdown, :"-1"], "THUMBS DOWN SIGN"),
self.new("\u{1F44C}", [:ok_hand], "OK HAND SIGN"), self.new("\u{1F44C}", [:ok_hand], "OK HAND SIGN"),
......
...@@ -16,6 +16,7 @@ describe Rumoji do ...@@ -16,6 +16,7 @@ describe Rumoji do
it "transforms emoji into cheat-sheet form" do it "transforms emoji into cheat-sheet form" do
key = :smile key = :smile
Rumoji.encode(@smile).must_equal ":smile:" Rumoji.encode(@smile).must_equal ":smile:"
Rumoji.encode("#{@smile}").must_equal ":smile:"
end end
end end
...@@ -38,7 +39,6 @@ describe Rumoji do ...@@ -38,7 +39,6 @@ describe Rumoji do
it "keeps codepoints that match the beginnings of multi-codepoint emoji" do it "keeps codepoints that match the beginnings of multi-codepoint emoji" do
text = "i like #hashtags and 1direction they are the #1 band. end with 9" text = "i like #hashtags and 1direction they are the #1 band. end with 9"
io = StringIO.new(text) io = StringIO.new(text)
Rumoji.encode_io(io).string.must_equal text Rumoji.encode_io(io).string.must_equal text
end end
...@@ -68,6 +68,13 @@ describe Rumoji do ...@@ -68,6 +68,13 @@ describe Rumoji do
Rumoji.encode_io(io).string.must_equal "An example of a multipoint emoji is the :us: flag." Rumoji.encode_io(io).string.must_equal "An example of a multipoint emoji is the :us: flag."
end end
end end
describe "with trailing emoji" do
it "writes characters that are in a multipoint emoji followed by an emoji" do
io = StringIO.new "I would like 0#{@poop}"
Rumoji.encode_io(io).string.must_equal "I would like 0:poop:"
end
end
end end
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment