Sha256: 159b87314ff1aae3ce6dd36ea81cf5591b97c34bcdc6bb3fa87656cebf6517c9

Contents?: true

Size: 1.07 KB

Versions: 26

Compression:

Stored size: 1.07 KB

Contents

# encoding: UTF-8

# Copyright 2012 Twitter, Inc
# http://www.apache.org/licenses/LICENSE-2.0

require 'base64'

module TwitterCldr
  module Segmentation
    class CategoryTable
      PACK_FMT_16 = 'NNn'.freeze

      class << self
        def load16(data)
          data = Base64.decode64(data)

          new(
            (0...data.size).step(10).map do |i|
              data[i...(i + 10)].unpack(PACK_FMT_16)
            end
          )
        end
      end

      attr_reader :values

      def initialize(values)
        @values = values
      end

      def get(codepoint)
        find(codepoint)[2]
      end

      def dump16
        data = ''.b.tap do |result|
          values.each do |vals|
            result << vals.pack(PACK_FMT_16)
          end
        end

        Base64.encode64(data)
      end

      private

      def find(codepoint)
        cache[codepoint] ||= values.bsearch do |entry|
          next -1 if codepoint < entry[0]
          next 1 if codepoint > entry[1]
          0
        end
      end

      def cache
        @cache ||= {}
      end
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
twitter_cldr-6.13.0 lib/twitter_cldr/segmentation/category_table.rb
twitter_cldr-6.12.1 lib/twitter_cldr/segmentation/category_table.rb
twitter_cldr-6.12.0 lib/twitter_cldr/segmentation/category_table.rb
twitter_cldr-6.11.5 lib/twitter_cldr/segmentation/category_table.rb
twitter_cldr-6.11.4 lib/twitter_cldr/segmentation/category_table.rb
twitter_cldr-6.11.3 lib/twitter_cldr/segmentation/category_table.rb
twitter_cldr-6.11.2 lib/twitter_cldr/segmentation/category_table.rb
twitter_cldr-6.11.1 lib/twitter_cldr/segmentation/category_table.rb
twitter_cldr-6.11.0 lib/twitter_cldr/segmentation/category_table.rb
twitter_cldr-6.10.0 lib/twitter_cldr/segmentation/category_table.rb
twitter_cldr-6.9.0 lib/twitter_cldr/segmentation/category_table.rb
twitter_cldr-6.8.0 lib/twitter_cldr/segmentation/category_table.rb
twitter_cldr-6.7.0 lib/twitter_cldr/segmentation/category_table.rb
twitter_cldr-6.6.2 lib/twitter_cldr/segmentation/category_table.rb
twitter_cldr-6.6.1 lib/twitter_cldr/segmentation/category_table.rb
twitter_cldr-6.6.0 lib/twitter_cldr/segmentation/category_table.rb
twitter_cldr-6.5.0 lib/twitter_cldr/segmentation/category_table.rb
twitter_cldr-6.4.0 lib/twitter_cldr/segmentation/category_table.rb
twitter_cldr-6.3.0 lib/twitter_cldr/segmentation/category_table.rb
twitter_cldr-6.2.0 lib/twitter_cldr/segmentation/category_table.rb