Add dotdump_utf8 helper for UTF-8-aware file viewer rendering#2146
Open
C-Oliver wants to merge 1 commit into
Open
Add dotdump_utf8 helper for UTF-8-aware file viewer rendering#2146C-Oliver wants to merge 1 commit into
C-Oliver wants to merge 1 commit into
Conversation
Adds a new str_utils.dotdump_utf8() helper that preserves valid UTF-8 multi-byte characters (accented Latin, CJK, emoji, etc.) while replacing only invalid or non-printable bytes with '.'. Reuses the existing _valid_utf8 regex so the validation rules stay consistent with escape_str_strict. This unblocks the File Viewer ASCII tab being able to display UTF-8 text instead of dots.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
dotdump_utf8()toassemblyline.common.str_utils— a UTF-8-awarecounterpart to the existing
dotdump()helper. Valid UTF-8 multi-bytesequences (accented Latin, CJK, emoji, etc.) are preserved as their decoded
characters; only invalid or non-printable bytes are replaced with
..This is the supporting library change for an upcoming
assemblyline-uiPR that switches the File Viewer ASCII tab to renderUTF-8 instead of dot-replacing every non-ASCII byte.
Why
The current File Viewer applies
bytes.translate(FILTER_ASCII), which mapsevery byte outside printable ASCII (32–126, plus tab/LF/CR) to
.. Becauseevery byte of a UTF-8 multi-byte sequence is in the
0x80–0xFFrange,files containing
café,日本語,🎉, etc. are rendered as solid dots.Implementation
_valid_utf8regex (RFC 3629, already used byescape_str_strict) so the definition of "valid UTF-8" stays consistentacross the codebase.
_valid_utf8.split(s)returns alternating non-matching / capturedsegments. Captured (valid UTF-8) segments are decoded to
str;non-matching bytes are replaced one-for-one with
., preserving thebyte length of invalid sections.
Behaviour
b'hello\nworld''hello\nworld''café'.encode()'café''日本語'.encode()'日本語''🎉ok'.encode()'🎉ok'b'A\x00B\x07C''A.B.C'b'\xc3('(bad 2-byte start)'.('b'\xed\xa0\x80'(UTF-16 surrogate)'...'b'\t\r\n abc''\t\r\n abc'Risk
_valid_utf8regex.