Module: TabularCode::Base

Included in:
KSX1001, Kuten, Quwei
Defined in:
lib/tabular_code/base.rb

Instance Method Summary collapse

Instance Method Details

#from_char(c) ⇒ Object

Converts a character to corresponding row-cell notation.

Returns nil if no such notation exists.



13
14
15
16
17
18
# File 'lib/tabular_code/base.rb', line 13

def from_char(c)
  c = c.encode(@encoding).ord rescue (return nil)
  x = c / 0x100 - 0xA0
  y = c % 0x100 - 0xA0
  valid?(x, y) ? x * 100 + y : nil
end

#from_str(s, options = {:replace => nil}) ⇒ Object

Converts a String to an Array of row-cell notations.

If :replace is given, replaces undefined characters with :replace. Otherwise, raises ConversionError for undefined characters.

Raises:



34
35
36
37
38
# File 'lib/tabular_code/base.rb', line 34

def from_str(s, options = {:replace => nil})
  ret = s.chars.map{|i| from_char(i)}
  raise ConversionError if !options.has_key?(:replace) && ret.include?(nil)
  ret.map{|i| i.nil? ? options[:replace] : i}
end

#to_char(c) ⇒ Object

Converts a row-cell notation to corresponding character.

Returns nil if no such character exists.



23
24
25
26
27
28
# File 'lib/tabular_code/base.rb', line 23

def to_char(c)
  x = c / 100
  y = c % 100
  c = valid?(x, y) ? (0xA0 + x) * 0x100 + (0xA0 + y) : nil
  c.chr(@encoding).encode(Encoding::UTF_8) rescue (return nil)
end

#to_str(s, options = {:replace => nil}) ⇒ Object

Converts an Array of row-cell notations to a String.

If :replace is given, replaces undefined characters with :replace. Otherwise, raises ConversionError for undefined characters.

Raises:



44
45
46
47
48
# File 'lib/tabular_code/base.rb', line 44

def to_str(s, options = {:replace => nil})
  ret = s.map{|i| to_char(i)}
  raise ConversionError if !options.has_key?(:replace) && ret.include?(nil)
  ret.map{|i| i.nil? ? options[:replace] : i} * ''
end