Class: Fontisan::Ucd::Index
- Inherits:
-
Object
- Object
- Fontisan::Ucd::Index
- Includes:
- Enumerable
- Defined in:
- lib/fontisan/ucd/index.rb
Overview
Sorted, run-length-encoded lookup table over Unicode codepoints.
One Index answers "what
Instance Attribute Summary collapse
- #entries ⇒ Array<RangeEntry> readonly
Class Method Summary collapse
-
.from_triples(triples) ⇒ Index
Build an Index from raw [first_cp, last_cp, name] triples.
-
.load(path) ⇒ Index
Load from a YAML file previously written by #save.
Instance Method Summary collapse
- #each ⇒ Object
-
#each_overlapping(first, last) ⇒ Enumerator<RangeEntry>
Enumerate every range whose [first_cp, last_cp] overlaps the inclusive query range.
-
#initialize(entries) ⇒ Index
constructor
A new instance of Index.
-
#lookup(codepoint) ⇒ String?
The name of the range covering
codepoint, or nil. -
#save(path) ⇒ void
Serialize to a YAML file.
- #size ⇒ Object
Constructor Details
#initialize(entries) ⇒ Index
Returns a new instance of Index.
16 17 18 |
# File 'lib/fontisan/ucd/index.rb', line 16 def initialize(entries) @entries = entries.sort end |
Instance Attribute Details
#entries ⇒ Array<RangeEntry> (readonly)
21 22 23 |
# File 'lib/fontisan/ucd/index.rb', line 21 def entries @entries end |
Class Method Details
.from_triples(triples) ⇒ Index
Build an Index from raw [first_cp, last_cp, name] triples.
76 77 78 |
# File 'lib/fontisan/ucd/index.rb', line 76 def self.from_triples(triples) new(triples.map { |first, last, name| RangeEntry.new(first, last, name) }) end |
.load(path) ⇒ Index
Load from a YAML file previously written by #save.
68 69 70 71 |
# File 'lib/fontisan/ucd/index.rb', line 68 def self.load(path) hashes = YAML.load_file(path) new(hashes.map { |h| RangeEntry.from_h(h) }) end |
Instance Method Details
#each ⇒ Object
23 24 25 |
# File 'lib/fontisan/ucd/index.rb', line 23 def each(&) @entries.each(&) end |
#each_overlapping(first, last) ⇒ Enumerator<RangeEntry>
Enumerate every range whose [first_cp, last_cp] overlaps the inclusive query range.
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/fontisan/ucd/index.rb', line 43 def each_overlapping(first, last, &) return enum_for(:each_overlapping, first, last) unless block_given? start_idx = bsearch_first_overlap(first) return if start_idx.nil? @entries[start_idx..].each do |entry| break if entry.first_cp > last yield entry if entry.last_cp >= first end end |
#lookup(codepoint) ⇒ String?
Returns the name of the range covering codepoint, or nil.
33 34 35 36 |
# File 'lib/fontisan/ucd/index.rb', line 33 def lookup(codepoint) idx = bsearch_index(codepoint) idx && @entries[idx].name end |
#save(path) ⇒ void
This method returns an undefined value.
Serialize to a YAML file.
59 60 61 62 63 |
# File 'lib/fontisan/ucd/index.rb', line 59 def save(path) File.open(path, "w") do |file| YAML.dump(@entries.map(&:to_h), file) end end |
#size ⇒ Object
27 28 29 |
# File 'lib/fontisan/ucd/index.rb', line 27 def size @entries.size end |