Sha256: ce8440451a52c14e365ac29ec7bfe8a6dbcf4e55bff5dfa007513f54919b76b9

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

require 'pstore'

module RegexpExamples
  class UnicodeCharRanges
    # These values were generated by: scripts/unicode_lister.rb
    # Note: Only the first 128 results are listed, for performance.
    # Also, some groups seem to have no matches (weird!)
    # (Don't care about ruby micro version number)
    STORE_FILENAME = "unicode_ranges_#{RUBY_VERSION[0..2]}.pstore"

    attr_reader :range_store

    def initialize(filename = STORE_FILENAME)
      @range_store = PStore.new(File.expand_path("../../../db/#{filename}", __FILE__))
    end

    def get(key)
      range_store.transaction(true) do
        ranges_to_unicode(range_store[key])
      end
    end

    alias_method :[], :get

    private

    # TODO: Document example input/output of this method
    # It's pretty simple, but this code is a little confusing!!
    def ranges_to_unicode(ranges)
      result = []
      ranges.each do |range|
        if range.is_a? Fixnum # Small hack to increase data compression
          result << hex_to_unicode(range.to_s(16))
        else
          range.each { |num| result << hex_to_unicode(num.to_s(16)) }
        end
      end
      result
    end

    def hex_to_unicode(hex)
      eval("?\\u{#{hex}}")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
regexp-examples-1.1.2 lib/regexp-examples/unicode_char_ranges.rb