module UTF8Encoding::ControlCharacters

Allows any supported object to have control characters escaped, using standard replacement scheme.

Constants

CONTROL_CHARACTERS

The range of characters we consider:

Public Instance Methods

escape_control_chars(string) click to toggle source

Returns a copy of ‘string`, with any control characters escaped.

# File lib/ndr_support/utf8_encoding/control_characters.rb, line 23
def escape_control_chars(string)
  escape_control_chars!(string.dup)
end
escape_control_chars!(string) click to toggle source

Escapes in-place any control characters in ‘string`, before returning it.

# File lib/ndr_support/utf8_encoding/control_characters.rb, line 28
def escape_control_chars!(string)
  string.gsub!(CONTROL_CHARACTERS) do |character|
    UTF8Encoding::REPLACEMENT_SCHEME[character]
  end
  string
end
escape_control_chars_in_array!(array) click to toggle source

Escape control characters in elements of the given ‘array`.

# File lib/ndr_support/utf8_encoding/control_characters.rb, line 41
def escape_control_chars_in_array!(array)
  array.each { |element| escape_control_chars_in_object!(element) }
end
escape_control_chars_in_hash!(hash) click to toggle source

Escape control characters in values of the given ‘hash`.

# File lib/ndr_support/utf8_encoding/control_characters.rb, line 36
def escape_control_chars_in_hash!(hash)
  hash.each_value { |value| escape_control_chars_in_object!(value) }
end
escape_control_chars_in_object!(object) click to toggle source

Recursively escape any control characters in ‘object`.

# File lib/ndr_support/utf8_encoding/control_characters.rb, line 9
def escape_control_chars_in_object!(object)
  case object
  when String
    escape_control_chars!(object)
  when Hash
    escape_control_chars_in_hash!(object)
  when Array
    escape_control_chars_in_array!(object)
  else
    object
  end
end