Module: TuiTui::TextSanitizer
- Defined in:
- lib/tui_tui/text_sanitizer.rb
Overview
Character-level text hygiene: keeps malformed or control bytes out of the render/input pipeline so they are displayed safely (or rejected as input) instead of raising encoding errors or emitting raw control codes.
Class Method Summary collapse
-
.printable?(string) ⇒ Boolean
Whether ‘string` is safe to insert as literal text: every byte is a printable character (no C0 controls and no DEL).
- .sanitize(string) ⇒ Object
Class Method Details
.printable?(string) ⇒ Boolean
Whether ‘string` is safe to insert as literal text: every byte is a printable character (no C0 controls and no DEL). Multibyte UTF-8 passes, since its bytes are all >= 0x80.
17 18 19 |
# File 'lib/tui_tui/text_sanitizer.rb', line 17 def printable?(string) string.bytes.all? { |byte| byte >= 0x20 && byte != 0x7F } end |
.sanitize(string) ⇒ Object
10 11 12 |
# File 'lib/tui_tui/text_sanitizer.rb', line 10 def sanitize(string) string.valid_encoding? ? string : string.scrub("?") end |