Created
December 17, 2016 21:55
-
-
Save sobataro/5efdf3368fa532c80a2c51be0d626838 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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
For anyone else coming across this, from Ruby 2.5.x or later this is a lot simpler with
str.grapheme_clusters.length
: