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
end
def encode_io(readable, writeable=StringIO.new(""))
codepoints = readable.each_codepoint.entries
codepoints.each_with_index do |codepoint, index|
codepoints = readable.each_codepoint
previous_emoji = []
codepoints.each_with_object(writeable) do |codepoint, writer|
possible_emoji = Emoji.select_by_codepoint(codepoint)
sequence = if possible_emoji.empty?
[codepoint].pack("U")
elsif possible_emoji.size.eql?(1)
possible_emoji.first.code
else
find_multipoint_emoji_in_set(possible_emoji, codepoints[index + 1])
end
writeable.write sequence
last_emoji = previous_emoji.pop
sequence = if last_emoji.nil? || !last_emoji.codepoints.include?(codepoint)
if possible_emoji.empty?
[codepoint].pack("U")
else
multiple_codepoint_emoji = possible_emoji.select(&:multiple?)
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
writeable
end
def decode_io(readable, writeable=StringIO.new(""))
......@@ -42,11 +53,5 @@ module Rumoji
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
......@@ -38,6 +38,10 @@ module Rumoji
@codepoints.entries
end
def multiple?
codepoints.size > 1
end
autoload :PEOPLE, 'rumoji/emoji/people'
autoload :NATURE, 'rumoji/emoji/nature'
autoload :OBJECTS, 'rumoji/emoji/objects'
......@@ -55,7 +59,7 @@ module Rumoji
end
def self.select_by_codepoint(codepoint)
ALL.select {|emoji| emoji.codepoints.include? codepoint }
ALL.select {|emoji| emoji.codepoints.first.eql? codepoint }
end
def self.find_by_codepoint(codepoint)
......
......@@ -86,8 +86,8 @@ module Rumoji
self.new("\u{2733}" , [:eight_spoked_asterisk]),
self.new("\u{1f51a}", [:end]),
self.new("\u{23e9}" , [:fast_forward]),
self.new("\u{0035}" , [:five]),
self.new("\u{0034}" , [:four]),
self.new("\u{0035 20e3}" , [:five]),
self.new("\u{0034 20e3}" , [:four]),
self.new("\u{1f193}", [:free]),
self.new("\u{264a}" , [:gemini]),
self.new("\u{0023 20e3}" , [:hash]),
......
......@@ -38,6 +38,12 @@ describe Rumoji do
Rumoji.encode_io(io2).string.must_equal ":us:"
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
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.")
......
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