Module: Sconv

Included in:
String
Defined in:
lib/sconv.rb

Overview

:include: ../README

Instance Method Summary collapse

Instance Method Details

#EUC?Boolean

Check whether the string is EUC-JP or not (more Ruby-like method name)

Returns:

  • (Boolean)


69
70
71
72
73
74
75
# File 'lib/sconv.rb', line 69

def EUC?
  if iseuc == nil
    false
  else
    true
  end
end

#guess_cesObject

Guess the string’s CES; return the result as a string



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/sconv.rb', line 98

def guess_ces
  case Kconv.guess(self)
  when 1
    'ISO-2022-JP'
  when 2
    'EUC-JP'
  when 3
    'Shift_JIS'
  when 4
    'BINARY'
  when 5
    'ASCII'
  when 6
    'UTF-8'
  when 8
    'UTF-16'
  end
end

#normalizeObject

Return name of CES without is not removed hyphens and underlines.



59
60
61
62
63
64
65
# File 'lib/sconv.rb', line 59

def normalize
  ces = Hash.new
  open('ceslist','r').read.scan(/\S+\n/).each do |i|
    ces[i.chomp.delete('\-,_')] = i.chomp
  end
  ces.fetch(self)
end

#sconv(from, to) ⇒ Object

Convert CES (Character Encoding Scheme) of a string from “from” code to “to” code (selects kconv for Japanese encodings, iconv for others)



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sconv.rb', line 9

def sconv(from, to)
  if (from =~ /iso-2022-jp|jis|shift_jis|sjis|euc-jp|utf-8|utf-16/i)&&(to =~ /iso-2022-jp|jis|shift_jis|sjis|euc-jp|utf-8|utf-16/i)
    
    in_code = Kconv::JIS   if from =~ /^i|^j/i
    in_code = Kconv::SJIS  if from =~ /^s/i
    in_code = Kconv::EUC   if from =~ /^e/i
    in_code = Kconv::UTF8  if from =~ /8$/ 
    in_code = Kconv::UTF16 if from =~ /6$/
    
    out_code = Kconv::JIS   if to =~ /^i|^j/i
    out_code = Kconv::SJIS  if to =~ /^s/i
    out_code = Kconv::EUC   if to =~ /^e/i
    out_code = Kconv::UTF8  if to =~ /8$/ 
    out_code = Kconv::UTF16 if to =~ /6$/
  
    kconv(out_code, in_code)    
  
  elsif from =~ /iso-2022-jp|jis|shift_jis|sjis|euc-jp|utf-8|utf-16/i
    in_code = Kconv::JIS   if from =~ /^i|^j/i
    in_code = Kconv::SJIS  if from =~ /^s/i
    in_code = Kconv::EUC   if from =~ /^e/i
    in_code = Kconv::UTF8  if from =~ /8$/ 
    in_code = Kconv::UTF16 if from =~ /6$/
    
    out_code = to
    
    str_utf8 = self.kconv(Kconv::UTF8, in_code)
    Iconv.conv(out_code, 'UTF-8', str_utf8)
    
  elsif to =~ /iso-2022-jp|jis|shift_jis|sjis|euc-jp|utf-8|utf-16/i
    in_code = from
    
    out_code = Kconv::JIS   if to =~ /^i|^j/i
    out_code = Kconv::SJIS  if to =~ /^s/i
    out_code = Kconv::EUC   if to =~ /^e/i
    out_code = Kconv::UTF8  if to =~ /8$/ 
    out_code = Kconv::UTF16 if to =~ /6$/
    
    Iconv.conv('UTF-8', in_code, self).kconv(out_code, Kconv::UTF8)
  
  else
    in_code  = from
    out_code = to
    
    Iconv.conv(out_code, in_code, self)
  end  
end

#SJIS?Boolean

Check whether the string is Shift_JIS or not (more Ruby-like method name)

Returns:

  • (Boolean)


79
80
81
82
83
84
85
# File 'lib/sconv.rb', line 79

def SJIS?
  if issjis == nil
    false
  else
    true
  end
end

#UTF8?Boolean

Check whether the string is UTF-8 or not (more Ruby-like method name)

Returns:

  • (Boolean)


89
90
91
92
93
94
95
# File 'lib/sconv.rb', line 89

def UTF8?
  if isutf8 == nil
    false
  else
    true
  end
end