Module: UTF8Encoding::ControlCharacters
- Included in:
- UTF8Encoding
- Defined in:
- lib/ndr_support/utf8_encoding/control_characters.rb
Overview
Allows any supported object to have control characters escaped, using standard replacement scheme.
Constant Summary collapse
- CONTROL_CHARACTERS =
The range of characters we consider:
/[\x00-\x1f]|\x7f/
- ALLOWED_CONTROL_CHARACTERS =
Exceptions that are allowed:
%W( \x09 \x0a \x0d )
Instance Method Summary collapse
-
#escape_control_chars(string) ⇒ Object
Returns a copy of ‘string`, with any control characters escaped.
-
#escape_control_chars!(string) ⇒ Object
Escapes in-place any control characters in ‘string`, before returning it.
-
#escape_control_chars_in_array!(array) ⇒ Object
Escape control characters in elements of the given ‘array`.
-
#escape_control_chars_in_hash!(hash) ⇒ Object
Escape control characters in values of the given ‘hash`.
-
#escape_control_chars_in_object!(object) ⇒ Object
Recursively escape any control characters in ‘object`.
Instance Method Details
#escape_control_chars(string) ⇒ Object
Returns a copy of ‘string`, with any control characters escaped.
27 28 29 |
# File 'lib/ndr_support/utf8_encoding/control_characters.rb', line 27 def escape_control_chars(string) escape_control_chars!(string.dup) end |
#escape_control_chars!(string) ⇒ Object
Escapes in-place any control characters in ‘string`, before returning it.
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/ndr_support/utf8_encoding/control_characters.rb', line 32 def escape_control_chars!(string) string.gsub!(CONTROL_CHARACTERS) do |character| if ALLOWED_CONTROL_CHARACTERS.include?(character) character else UTF8Encoding::REPLACEMENT_SCHEME[character] end end string end |
#escape_control_chars_in_array!(array) ⇒ Object
Escape control characters in elements of the given ‘array`.
49 50 51 |
# File 'lib/ndr_support/utf8_encoding/control_characters.rb', line 49 def escape_control_chars_in_array!(array) array.each { |element| escape_control_chars_in_object!(element) } end |
#escape_control_chars_in_hash!(hash) ⇒ Object
Escape control characters in values of the given ‘hash`.
44 45 46 |
# File 'lib/ndr_support/utf8_encoding/control_characters.rb', line 44 def escape_control_chars_in_hash!(hash) hash.each_value { |value| escape_control_chars_in_object!(value) } end |
#escape_control_chars_in_object!(object) ⇒ Object
Recursively escape any control characters in ‘object`.
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/ndr_support/utf8_encoding/control_characters.rb', line 13 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 |