Skip to content

Instantly share code, notes, and snippets.

@rtweeks
Created May 1, 2022 14:36
Show Gist options
  • Save rtweeks/64179028773490c37588c6d165c50429 to your computer and use it in GitHub Desktop.
Save rtweeks/64179028773490c37588c6d165c50429 to your computer and use it in GitHub Desktop.
FairCG JS character classification helper
class CharClassData
def initialize(char_classes)
@switch_points = []
@result_ccs = []
prev_cc = nil
max_ch = char_classes.flatten.max + 1
(0..max_ch).each do |ch|
next if prev_cc && char_classes[prev_cc].include?(ch)
cc = char_classes.find_index {|g| g.include?(ch)}
if prev_cc != cc
@switch_points << ch
@result_ccs << (cc || char_classes.length)
end
prev_cc = cc
end
end
attr_reader :switch_points, :result_ccs
def to_a
other_cc = self.result_ccs.max
[].tap do |result|
cur_cc = other_cc
cur_ch = 0
self.result_ccs.zip(self.switch_points).each do |cc, ch_start|
result.concat([cur_cc] * (ch_start - cur_ch))
cur_cc = cc
cur_ch = ch_start
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment