Skip to content

Commit

Permalink
Composing preliminar performance tune-up.
Browse files Browse the repository at this point in the history
  • Loading branch information
mudasobwa committed Dec 21, 2014
1 parent 56865f2 commit 5a2014f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/forkforge/internal/monkeypatches.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def vacant?
self.nil? ||
self.respond_to?(:strip) && self.strip.empty? ||
self.respond_to?(:zero?) && self.zero? ||
Array === self && (self - [nil]).empty?
Array === self && self.compact.empty?
end
end

Expand Down
18 changes: 13 additions & 5 deletions lib/forkforge/internal/unicode_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ module UnicodeData
REMOTE = 'Public/UCD/latest/ucd'
FILE = 'UnicodeData.txt'

@cdm = {}

def hash
i_hash(REMOTE, LOCAL, FILE, CodePoint::UNICODE_FIELDS, false)
end
Expand Down Expand Up @@ -55,10 +57,16 @@ def to_codepoint cp
}
}

def compose_cp cp, tag = :font
all_character_decomposition_mapping(/\A#{CharacterDecompositionMapping::Tag.tag(tag).tag}\s+#{__to_code_point cp}\Z/).values.map { |uf|
Forkforge::CodePoint.new(uf)
}
def compose_cp cp, tag = :font, thorough = true
cp = __to_code_point cp
return Forkforge::CodePoint.new(hash[cp]) unless (t = CharacterDecompositionMapping::Tag.tag(tag)).valid?

@cdm[tag] = all_character_decomposition_mapping(/#{t.tag}/).values if @cdm[tag].nil?
# FIXME Could we distinguish “<wide> 0ABC” and “0A00 0ABC” in more elegant way?
lmbd = ->(v) { v[:character_decomposition_mapping] =~ /[^\dA-Fa-f]\s+#{cp}\Z/ }
thorough ? \
@cdm[tag].select(&lmbd).map { |cp| Forkforge::CodePoint.new(cp) } :
Forkforge::CodePoint.new(@cdm[tag].find(&lmbd) || hash[cp])
end

def decompose_cp cp, tags = []
Expand All @@ -68,7 +76,7 @@ def decompose_cp cp, tags = []

cps = mapping.split ' '

return normalized if ![*tags].empty? && \
return normalized if ![*tags].vacant? && \
cps.inject(false) { |memo, cp|
memo || (CharacterDecompositionMapping::Tag::tag?(cp) && ![*tags].include?(CharacterDecompositionMapping::Tag::tag(cp).sym))
}
Expand Down
3 changes: 2 additions & 1 deletion lib/forkforge/unicode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ def lookup pattern
# NB This is not a composition as it is understood by Unicode.Org (love them.)
def compose s, tag = :font, format = :full
composed = s.codepoints.map { |cp|
(result = Forkforge::UnicodeData::compose_cp(cp, tag)).vacant? ? [Forkforge::UnicodeData::to_codepoint(cp)] : result
(result = Forkforge::UnicodeData::compose_cp(cp, tag, format == :full)).vacant? ? \
[Forkforge::UnicodeData::to_codepoint(cp)] : result
}

raise UnicodeException, "AMBIGUITIES FOUND, FIXME FIXME FIXME" \
Expand Down

0 comments on commit 5a2014f

Please sign in to comment.