Module: CompactData::UNICODE

Defined in:
lib/compactdata/util/unicode.rb

Overview

Handle escaping of unicode characters.

Constant Summary collapse

VERTICAL_TAB =
0x0b
BACKSPACE =
0x08
FORMFEED =
0x0c
LINEFEED =
0x0a
CARRIAGE_RETURN =
0x0d
TAB =
0x09

Class Method Summary collapse

Class Method Details

.escape(str) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/compactdata/util/unicode.rb', line 35

def self.escape(str)
  result = ''

  str.each_codepoint do |c|
    result << escape_char(c)
  end
  result
end

.escape_char(chr) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/compactdata/util/unicode.rb', line 13

def self.escape_char(chr)
  return '' unless chr

  if chr >= 32 && chr <= 127
    '' << chr
  elsif chr == VERTICAL_TAB
    '~u000B'
  elsif chr == BACKSPACE
    '\\b'
  elsif chr == FORMFEED
    '\\f'
  elsif chr == LINEFEED
    '\\n'
  elsif chr == CARRIAGE_RETURN
    '\\r'
  elsif chr == TAB
    '\\t'
  else
    '' << chr
  end
end