Commit b61bf925 by Jake Boxer

Handle possible emoji as the last char in a string

parent 0dfdf628
...@@ -28,8 +28,7 @@ module Rumoji ...@@ -28,8 +28,7 @@ module Rumoji
sequence = if last_emoji.nil? || !last_emoji.codepoints.include?(codepoint) sequence = if last_emoji.nil? || !last_emoji.codepoints.include?(codepoint)
if possible_emoji.empty? || last_emoji if possible_emoji.empty? || last_emoji
last_codepoint = last_emoji && last_emoji.codepoints.first last_codepoint = last_emoji && last_emoji.codepoints.first
sequence_codepoints = [last_codepoint, codepoint].compact sequence_from_codepoints(last_codepoint, codepoint)
sequence_codepoints.pack("U" * sequence_codepoints.size)
else else
multiple_codepoint_emoji = possible_emoji.select(&:multiple?) multiple_codepoint_emoji = possible_emoji.select(&:multiple?)
if multiple_codepoint_emoji.empty? if multiple_codepoint_emoji.empty?
...@@ -44,6 +43,14 @@ module Rumoji ...@@ -44,6 +43,14 @@ module Rumoji
writer.write sequence writer.write sequence
end end
# If the last character was the beginning of a possible emoji, tack the
# first codepoint onto the end.
if last_emoji = previous_emoji.pop
writeable.write sequence_from_codepoints(last_emoji.codepoints.first)
end
writeable
end end
def decode_io(readable, writeable=StringIO.new("")) def decode_io(readable, writeable=StringIO.new(""))
...@@ -55,5 +62,9 @@ module Rumoji ...@@ -55,5 +62,9 @@ module Rumoji
private private
def sequence_from_codepoints(*codepoints)
compacted = codepoints.flatten.compact
compacted.pack("U" * compacted.size)
end
end end
...@@ -31,7 +31,7 @@ describe Rumoji do ...@@ -31,7 +31,7 @@ describe Rumoji do
end end
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" 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
......
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