lib/charwidth.rb in charwidth-0.1.5 vs lib/charwidth.rb in charwidth-0.2.0

- old
+ new

@@ -37,11 +37,11 @@ ], space: [ Characters::SPACE, Characters::IDEOGRAPHIC_SPACE, ], - } + }.transform_values(&:freeze).freeze # Normalize Unicode fullwidth / halfwidth (zenkaku / hankaku) characters # options: { # only: [:ascii, :white_parenthesis, :cjk_punctuation, :katakana, :space], # except: [:ascii, :white_parenthesis, :cjk_punctuation, :katakana, :space] @@ -61,11 +61,12 @@ end def to_full_width!(src) unify_voiced_katakana!(src) - before, after = "", "" + before = "" + after = "" HALFWIDTH_TO_FULLWIDTH.each_value do |half, full| before << half after << full end @@ -75,11 +76,11 @@ end private TYPES = [ :ascii, :white_parenthesis, :cjk_punctuation, :katakana, :hangul, - :latin_1_punctuation_and_symbols, :mathematical_symbols, :space + :latin_1_punctuation_and_symbols, :mathematical_symbols, :space, ].freeze def normalize_charwidth!(src, options = {}) types = TYPES.dup # Check options @@ -89,32 +90,35 @@ if options[:only] unless (unexpected_types = options[:only] - TYPES).empty? raise "Unexpected normalize type(s): #{unexpected_types.inspect}" end - types = types & options[:only] + + types &= options[:only] end if options[:expect] unless (unexpected_types = options[:expected] - TYPES).empty? - raise "Unexpected normalize type(s): #{t.inspect}" + raise "Unexpected normalize type(s): #{unexpected_types.inspect}" end - types = types - options[:expect] + + types -= options[:expect] end - before, after = "", "" + before = "" + after = "" types.each do |type| case type when :ascii, :white_parenthesis, :latin_1_punctuation_and_symbols, :space # convert fullwidth to halfwidth - HALFWIDTH_TO_FULLWIDTH[type].tap{|half, full| + HALFWIDTH_TO_FULLWIDTH[type].tap {|half, full| before << full after << half } when :cjk_punctuation, :katakana, :hangul, :mathematical_symbols # convert halfwidth to fullwidth - HALFWIDTH_TO_FULLWIDTH[type].tap{|half, full| + HALFWIDTH_TO_FULLWIDTH[type].tap {|half, full| before << half after << full } end end @@ -137,13 +141,13 @@ end end def escape_for_tr!(s) s.gsub!('\\', '\\\\') - s.gsub!('-', '\\-') - s.gsub!('^', '\\^') - s.gsub!('[', '\\[') - s.gsub!(']', '\\]') + s.gsub!("-", '\\-') + s.gsub!("^", '\\^') + s.gsub!("[", '\\[') + s.gsub!("]", '\\]') s end end extend ClassMethods