Commit 43edb43c by Mark Wunsch

Don't lookahead for emoji, instead store previous potential emoji

A continuation of #3
parent 8df02271
...@@ -18,19 +18,30 @@ module Rumoji ...@@ -18,19 +18,30 @@ module Rumoji
end end
def encode_io(readable, writeable=StringIO.new("")) def encode_io(readable, writeable=StringIO.new(""))
codepoints = readable.each_codepoint.entries codepoints = readable.each_codepoint
codepoints.each_with_index do |codepoint, index| previous_emoji = []
codepoints.each_with_object(writeable) do |codepoint, writer|
possible_emoji = Emoji.select_by_codepoint(codepoint) possible_emoji = Emoji.select_by_codepoint(codepoint)
sequence = if possible_emoji.empty? last_emoji = previous_emoji.pop
[codepoint].pack("U")
elsif possible_emoji.size.eql?(1) sequence = if last_emoji.nil? || !last_emoji.codepoints.include?(codepoint)
possible_emoji.first.code if possible_emoji.empty?
else [codepoint].pack("U")
find_multipoint_emoji_in_set(possible_emoji, codepoints[index + 1]) else
end multiple_codepoint_emoji = possible_emoji.select(&:multiple?)
writeable.write sequence if multiple_codepoint_emoji.empty?
possible_emoji.first.code
else
previous_emoji.concat(multiple_codepoint_emoji) ; ""
end
end
else
last_emoji.code
end
writer.write sequence
end end
writeable
end end
def decode_io(readable, writeable=StringIO.new("")) def decode_io(readable, writeable=StringIO.new(""))
...@@ -42,11 +53,5 @@ module Rumoji ...@@ -42,11 +53,5 @@ module Rumoji
private private
def find_multipoint_emoji_in_set(possible_emoji_set, next_codepoint)
if next_codepoint
emoji_or_nil = possible_emoji_set.find {|emoji| emoji.codepoints.include?(next_codepoint) }
emoji_or_nil.nil? ? "" : emoji_or_nil.code
end
end
end end
...@@ -38,6 +38,10 @@ module Rumoji ...@@ -38,6 +38,10 @@ module Rumoji
@codepoints.entries @codepoints.entries
end end
def multiple?
codepoints.size > 1
end
autoload :PEOPLE, 'rumoji/emoji/people' autoload :PEOPLE, 'rumoji/emoji/people'
autoload :NATURE, 'rumoji/emoji/nature' autoload :NATURE, 'rumoji/emoji/nature'
autoload :OBJECTS, 'rumoji/emoji/objects' autoload :OBJECTS, 'rumoji/emoji/objects'
...@@ -55,7 +59,7 @@ module Rumoji ...@@ -55,7 +59,7 @@ module Rumoji
end end
def self.select_by_codepoint(codepoint) def self.select_by_codepoint(codepoint)
ALL.select {|emoji| emoji.codepoints.include? codepoint } ALL.select {|emoji| emoji.codepoints.first.eql? codepoint }
end end
def self.find_by_codepoint(codepoint) def self.find_by_codepoint(codepoint)
......
...@@ -86,8 +86,8 @@ module Rumoji ...@@ -86,8 +86,8 @@ module Rumoji
self.new("\u{2733}" , [:eight_spoked_asterisk]), self.new("\u{2733}" , [:eight_spoked_asterisk]),
self.new("\u{1f51a}", [:end]), self.new("\u{1f51a}", [:end]),
self.new("\u{23e9}" , [:fast_forward]), self.new("\u{23e9}" , [:fast_forward]),
self.new("\u{0035}" , [:five]), self.new("\u{0035 20e3}" , [:five]),
self.new("\u{0034}" , [:four]), self.new("\u{0034 20e3}" , [:four]),
self.new("\u{1f193}", [:free]), self.new("\u{1f193}", [:free]),
self.new("\u{264a}" , [:gemini]), self.new("\u{264a}" , [:gemini]),
self.new("\u{0023 20e3}" , [:hash]), self.new("\u{0023 20e3}" , [:hash]),
......
...@@ -38,6 +38,12 @@ describe Rumoji do ...@@ -38,6 +38,12 @@ describe Rumoji do
Rumoji.encode_io(io2).string.must_equal ":us:" Rumoji.encode_io(io2).string.must_equal ":us:"
end end
it "transforms a stream of many emoji" do
num = ":one::two::three::four::five::six::seven::eight::nine::hash:"
emoji = StringIO.new("1⃣2⃣3⃣4⃣5⃣6⃣7⃣8⃣9⃣#⃣")
Rumoji.encode_io(emoji).string.must_equal num
end
describe "with leading and trailing characters" do describe "with leading and trailing characters" do
it "is able to pull multipoint emoji out of a sequence" do it "is able to pull multipoint emoji out of a sequence" do
io = StringIO.new("An example of a multipoint emoji is the #{@us} flag.") io = StringIO.new("An example of a multipoint emoji is the #{@us} flag.")
......
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