Method: String#to_half_characters

Defined in:
lib/coaster/core_ext/string.rb

#to_half_characters(alpha: true, number: true, symbol: false) ⇒ String

Note:

전각 문자 -> 반각 문자

Parameters:

  • alpha (TrueClass, FalseClass) (defaults to: true)
  • number (TrueClass, FalseClass) (defaults to: true)
  • symbol (TrueClass, FalseClass) (defaults to: false)

Returns:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/coaster/core_ext/string.rb', line 35

def to_half_characters(alpha: true, number: true, symbol: false)
  result = String.new
  return result if self.blank?

  (0...self.size).each do |i|
    full_ord = self[i].ord
    half_ord = full_ord - 0xfee0
    char_ord = case full_ord
               when 0x3000 then 0x20
               when ('0'.ord + 0xfee0)..('9'.ord + 0xfee0) then number ? half_ord : full_ord
               when ('A'.ord + 0xfee0)..('Z'.ord + 0xfee0) then alpha ? half_ord : full_ord
               when ('a'.ord + 0xfee0)..('z'.ord + 0xfee0) then alpha ? half_ord : full_ord
               when ('!'.ord + 0xfee0)..('~'.ord + 0xfee0) then symbol ? half_ord : full_ord
               else full_ord
               end
    result << char_ord.chr('UTF-8')
  end
  result
end