mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 06:23:17 -10:00
msg.Safe is the one owner: a graphic character in any script is safe; controls, bidi controls, format characters, variation selectors, private use and the invisible fillers are not. Printable and OneLine drop what Safe refuses, agent save's diff escapes it, and forbidigo bans unicode.IsGraphic, IsPrint, IsControl and Bidi_Control outside internal/msg. Co-Authored-By: Claude Opus 5.5 <[email protected]>
38 lines
1.4 KiB
Go
38 lines
1.4 KiB
Go
package msg
|
|
|
|
import "unicode"
|
|
|
|
// Safe reports whether a terminal shows r as it is, so that text from a VM
|
|
// (or any other untrusted text) can't move the cursor, reorder, fake or
|
|
// hide what the user reads: r is graphic (a letter, mark, number,
|
|
// punctuation, symbol or space, in any script, emoji too), and not a
|
|
// format character (Cf: zero-width spaces and joiners, the word joiner,
|
|
// the soft hyphen, the tag characters), a bidi control, a variation
|
|
// selector or one of the fillers that show as nothing (Hangul's, the
|
|
// braille blank, the combining grapheme joiner). Controls, tab and
|
|
// newline among them, aren't safe: a caller that keeps lines keeps
|
|
// those itself. It's the one rule for which characters reach the
|
|
// terminal: Printable and OneLine drop the rest, a diff escapes them.
|
|
func Safe(r rune) bool {
|
|
return unicode.IsGraphic(r) &&
|
|
!unicode.Is(unicode.Cf, r) &&
|
|
!unicode.Is(unicode.Bidi_Control, r) &&
|
|
!unicode.Is(unicode.Variation_Selector, r) &&
|
|
!invisible(r)
|
|
}
|
|
|
|
// invisible is a graphic character most terminals show as blank space or
|
|
// nothing at all, though Unicode doesn't call it a space.
|
|
func invisible(r rune) bool {
|
|
switch r {
|
|
case 0x034f, // combining grapheme joiner
|
|
0x115f, 0x1160, // Hangul choseong and jungseong fillers
|
|
0x17b4, 0x17b5, // Khmer's inherent vowels
|
|
0x2800, // braille pattern blank
|
|
0x3164, // Hangul filler
|
|
0xffa0: // halfwidth Hangul filler
|
|
return true
|
|
}
|
|
return false
|
|
}
|