class TwitterCldr::Shared::Caser

Constants

REGEX

Public Class Methods

downcase(string) click to toggle source
# File lib/twitter_cldr/shared/caser.rb, line 16
def downcase(string)
  string.gsub(REGEX, lowercasing_hash)
end
titlecase(string) click to toggle source

toTitlecase(X): Find the word boundaries in X according to Unicode Standard Annex #29, “Unicode Text Segmentation.” For each word boundary, find the first cased character F following the word boundary. If F exists, map F to Titlecase_Mapping(F); then map all characters C between F and the following word boundary to Lowercase_Mapping(C).

# File lib/twitter_cldr/shared/caser.rb, line 26
def titlecase(string)
  string.dup.tap do |result|
    word_iterator.each_word(result) do |_, *boundary_pair|
      if cased_pos = first_cased(string, *boundary_pair)
        result[cased_pos] = titlecasing_hash[result[cased_pos]]

        (cased_pos + 1).upto(boundary_pair.last - 1) do |pos|
          result[pos] = lowercasing_hash[result[pos]]
        end
      end
    end
  end
end
upcase(string) click to toggle source
# File lib/twitter_cldr/shared/caser.rb, line 12
def upcase(string)
  string.gsub(REGEX, uppercasing_hash)
end

Private Class Methods

cased?(char) click to toggle source
# File lib/twitter_cldr/shared/caser.rb, line 54
def cased?(char)
  props = CodePoint.properties_for_code_point(char.ord)
  props.general_category.include?('Lt') ||
    props.uppercase || props.lowercase
end
first_cased(string, start_pos, end_pos) click to toggle source
# File lib/twitter_cldr/shared/caser.rb, line 42
def first_cased(string, start_pos, end_pos)
  end_pos = string.length - 1 if end_pos >= string.length

  start_pos.upto(end_pos) do |pos|
    return pos if cased?(string[pos])
  end
end
lowercasing_hash() click to toggle source
# File lib/twitter_cldr/shared/caser.rb, line 66
def lowercasing_hash
  @lowercasing_hash ||= Hash.new do |hash, key|
    memoize_value(:simple_lowercase_map, hash, key)
  end
end
memoize_value(field, hash, key) click to toggle source
# File lib/twitter_cldr/shared/caser.rb, line 78
def memoize_value(field, hash, key)
  cp = TwitterCldr::Shared::CodePoint.get(key.ord)
  mapped_result = cp.send(field)
  hash[key] = mapped_result ? mapped_result : key
end
titlecasing_hash() click to toggle source
# File lib/twitter_cldr/shared/caser.rb, line 72
def titlecasing_hash
  @titlecasing_hash ||= Hash.new do |hash, key|
    memoize_value(:simple_titlecase_map, hash, key)
  end
end
uppercasing_hash() click to toggle source
# File lib/twitter_cldr/shared/caser.rb, line 60
def uppercasing_hash
  @uppercasing_hash ||= Hash.new do |hash, key|
    memoize_value(:simple_uppercase_map, hash, key)
  end
end
word_iterator() click to toggle source
# File lib/twitter_cldr/shared/caser.rb, line 50
def word_iterator
  @word_iterator ||= Segmentation::BreakIterator.new(:en)
end