Sha256: 329048ac40741cfccf216fad5ae46e935bfdd09401be9df83420558bd1f8c342

Contents?: true

Size: 976 Bytes

Versions: 1

Compression:

Stored size: 976 Bytes

Contents

module EmojiRegex
  class Maker
    def initialize
      @input_file_path = File.join(__dir__, '../../data', 'sorted_emoji.txt')
    end

    def regex
      regex = ranges.map do |e|
        l = e[0]
        r = e[1]
        next if l == '' || r == '' || !l[/\s/].nil? || !r[/\s/].nil?
        if l == r
          "\\u{#{l}}"
        else
          "\\u{#{l}}-\\u{#{r}}"
        end
      end
      regex.unshift '['
      regex.push ']'

      Regexp.new(regex.join)
    end

    def ranges
      prev = -1
      prev_hex = ''
      left = ''

      ranges = File.open(@input_file_path).map do |line|
        hex = line.to_i(16)
        if hex == prev || hex == prev + 1
          prev = hex
          prev_hex = line.chomp
          next
        end
        range = [left, prev_hex]
        left = line.chomp
        prev = hex
        prev_hex = left
        range
      end

      ranges
        .compact
        .reject { |a, b| a.empty? || b.empty? }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
emoji-regex-0.2.0 lib/emoji-regex/maker.rb