File: blocks.rb

package info (click to toggle)
ruby-unicode-blocks 1.9.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 140 kB
  • sloc: ruby: 90; makefile: 4
file content (28 lines) | stat: -rw-r--r-- 811 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
require_relative "blocks/constants"

module Unicode
  module Blocks
    def self.blocks(string)
      res = []
      string.each_char{ |char|
        block_name = block(char)
        res << block_name unless res.include?(block_name)
      }
      res.sort
    end
    class << self; alias of blocks; end

    def self.block(char)
      require_relative 'blocks/index' unless defined? ::Unicode::Blocks::INDEX
      codepoint = char.unpack("U")[0] or raise(ArgumentError, "Unicode::Blocks.block must be given a valid char")
      block_info = INDEX.bsearch{ |block_info| codepoint <= block_info[1] }
      codepoint >= block_info[0] ? block_info[2] : "No_Block"
    end

    def self.names
      require_relative 'blocks/index' unless defined? ::Unicode::Blocks::INDEX
      INDEX.map(&:last)
    end
  end
end