|
| 1 | +package textinput |
| 2 | + |
| 3 | +import ( |
| 4 | + "unicode" |
| 5 | + "unicode/utf8" |
| 6 | +) |
| 7 | + |
| 8 | +// Sanitizer is a helper for bubble widgets that want to process |
| 9 | +// Runes from input key messages. |
| 10 | +type Sanitizer interface { |
| 11 | + // Sanitize removes control characters from runes in a KeyRunes |
| 12 | + // message, and optionally replaces newline/carriage return/tabs by a |
| 13 | + // specified character. |
| 14 | + // |
| 15 | + // The rune array is modified in-place if possible. In that case, the |
| 16 | + // returned slice is the original slice shortened after the control |
| 17 | + // characters have been removed/translated. |
| 18 | + Sanitize(runes []rune) []rune |
| 19 | +} |
| 20 | + |
| 21 | +// NewSanitizer constructs a rune sanitizer. |
| 22 | +func NewSanitizer(opts ...Option) Sanitizer { |
| 23 | + s := sanitizer{ |
| 24 | + replaceNewLine: []rune("\n"), |
| 25 | + replaceTab: []rune(" "), |
| 26 | + } |
| 27 | + for _, o := range opts { |
| 28 | + s = o(s) |
| 29 | + } |
| 30 | + return &s |
| 31 | +} |
| 32 | + |
| 33 | +// Option is the type of option that can be passed to Sanitize(). |
| 34 | +type Option func(sanitizer) sanitizer |
| 35 | + |
| 36 | +// ReplaceTabs replaces tabs by the specified string. |
| 37 | +func ReplaceTabs(tabRepl string) Option { |
| 38 | + return func(s sanitizer) sanitizer { |
| 39 | + s.replaceTab = []rune(tabRepl) |
| 40 | + return s |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// ReplaceNewlines replaces newline characters by the specified string. |
| 45 | +func ReplaceNewlines(nlRepl string) Option { |
| 46 | + return func(s sanitizer) sanitizer { |
| 47 | + s.replaceNewLine = []rune(nlRepl) |
| 48 | + return s |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func (s *sanitizer) Sanitize(runes []rune) []rune { |
| 53 | + // dstrunes are where we are storing the result. |
| 54 | + dstrunes := runes[:0:len(runes)] |
| 55 | + // copied indicates whether dstrunes is an alias of runes |
| 56 | + // or a copy. We need a copy when dst moves past src. |
| 57 | + // We use this as an optimization to avoid allocating |
| 58 | + // a new rune slice in the common case where the output |
| 59 | + // is smaller or equal to the input. |
| 60 | + copied := false |
| 61 | + |
| 62 | + for src := 0; src < len(runes); src++ { |
| 63 | + r := runes[src] |
| 64 | + switch { |
| 65 | + case r == utf8.RuneError: |
| 66 | + // skip |
| 67 | + |
| 68 | + case r == '\r' || r == '\n': |
| 69 | + if len(dstrunes)+len(s.replaceNewLine) > src && !copied { |
| 70 | + dst := len(dstrunes) |
| 71 | + dstrunes = make([]rune, dst, len(runes)+len(s.replaceNewLine)) |
| 72 | + copy(dstrunes, runes[:dst]) |
| 73 | + copied = true |
| 74 | + } |
| 75 | + dstrunes = append(dstrunes, s.replaceNewLine...) |
| 76 | + |
| 77 | + case r == '\t': |
| 78 | + if len(dstrunes)+len(s.replaceTab) > src && !copied { |
| 79 | + dst := len(dstrunes) |
| 80 | + dstrunes = make([]rune, dst, len(runes)+len(s.replaceTab)) |
| 81 | + copy(dstrunes, runes[:dst]) |
| 82 | + copied = true |
| 83 | + } |
| 84 | + dstrunes = append(dstrunes, s.replaceTab...) |
| 85 | + |
| 86 | + case unicode.IsControl(r): |
| 87 | + // Other control characters: skip. |
| 88 | + |
| 89 | + default: |
| 90 | + // Keep the character. |
| 91 | + dstrunes = append(dstrunes, runes[src]) |
| 92 | + } |
| 93 | + } |
| 94 | + return dstrunes |
| 95 | +} |
| 96 | + |
| 97 | +type sanitizer struct { |
| 98 | + replaceNewLine []rune |
| 99 | + replaceTab []rune |
| 100 | +} |
0 commit comments