Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#! /usr/bin/env ruby
#load 'unicode.rb' # ActiveSupport::Multibyte::Unicode.unpack_graphemes
def lesser_graphemes_counter(str)
codepoints = str.unpack("U*")
variation_selectors = codepoints.count { |c| (0xFE00..0xFE0F).include?(c) }
skintone_modifiers = codepoints.count { |c| (0x1F3FB..0x1F3FF).include?(c) }
regional_indicators = codepoints.count { |c| (0x1F1E6..0x1F1FF).include?(c) }
zero_width_joiners = codepoints.count(0x200D)
enclosing_keycaps = codepoints.count(0x20E3)
codepoints.count - variation_selectors - skintone_modifiers - (regional_indicators / 2) - (zero_width_joiners * 2) - enclosing_keycaps
end
puts "RUBY_VERSION = #{RUBY_VERSION}"
[
[0x31, 0xFE0F, 0x20E3], # Keycap Digit One 1️⃣
[0x1F44F, 0x1F3FD], # Clapping Hands Sign, Type-4 👏🏽
[0x1F468, 0x200D, 0x1F468, 0x200D, 0x1F466], # Family: Man, Man, Boy 👨‍👨‍👦
[0x1F1EF, 0x1F1F5], # 🇯🇵
[0x1F1EF, 0x1F1F5, 0x1F1EF, 0x1F1F5, 0x1F1EF, 0x1F1F5], # 🇯🇵🇯🇵🇯🇵
].map { |e| e.pack("U*") }.map do |emoji|
puts "\n"
puts "\"#{emoji}\".length = #{emoji.length}"
puts "\"#{emoji}\".codepoints.count = #{emoji.codepoints.count}"
puts "\"#{emoji}\".scan(/\\X/).count = #{emoji.scan(/\X/).count}"
# puts "ActiveSupport::Multibyte::Unicode.unpack_graphemes(\"#{emoji}\").count = #{ActiveSupport::Multibyte::Unicode.unpack_graphemes(emoji).count}"
puts "lesser_graphemes_counter(\"#{emoji}\") = #{lesser_graphemes_counter(emoji)}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.