module BBS::Text
Text a caller typed or a remote service sent, made safe to keep and to show: valid UTF-8, no control characters, no bidirectional overrides.
BBS::Text.clean("Ada\e[2J\u202Etxt.exe") # => "Ada[2Jtxt.exe" BBS::Text.line("Ada\n Lovelace\t") # => "Ada Lovelace" BBS::Text.printable?('é') # => true
Constants
- BIDI_CONTROLS
-
The bidirectional embedding, override and isolate characters, which can make a line read differently from what it holds.
- CONTROLS
-
The C0 controls except tab and LF, DEL, the C1 controls and the
BIDI_CONTROLS. - LINE_END
-
A line end: CR LF, LF or CR.
- PRINTABLE_CHAR
-
One printable character: no control, no bidirectional control.
- WHITESPACE
-
A run of whitespace.
Public Instance Methods
# File lib/bbs/text.rb, line 30 def clean(text, keep_newlines: true) text = utf8(text) text = text.gsub(LINE_END, ' ') unless keep_newlines text.gsub(CONTROLS, '') end
Returns text (anything with to_s) as a new valid UTF-8 String with invalid bytes, C0 controls (but tab and LF), DEL, C1 controls and bidirectional controls removed. With keep_newlines: false each line end becomes a space, so the words of two lines stay apart.
Source
# File lib/bbs/text.rb, line 38 def line(text) = clean(text, keep_newlines: false).gsub(WHITESPACE, ' ').strip
Returns text as one clean line: clean, every run of whitespace (line ends and tabs included) one space, and no space at either end.
Source
# File lib/bbs/text.rb, line 42 def printable?(char) = char.is_a?(String) && char.match?(PRINTABLE_CHAR)
Returns whether char is one printable character (a String of length one that PRINTABLE_CHAR matches).
Source
# File lib/bbs/text.rb, line 45 def utf8(text) = text.to_s.dup.force_encoding(Encoding::UTF_8).scrub('')
Returns text as a new UTF-8 String with invalid bytes removed.