Source File
ansi.go
Belonging Package
github.com/clipperhouse/uax29/v2/graphemes
package graphemes// ansiEscapeLength returns the byte length of a valid 7-bit ANSI escape// sequence at the start of data, or 0 if none.//// Recognized forms (ECMA-48 / ISO 6429):// - CSI: ESC [ then parameter bytes (0x30-0x3F), intermediate (0x20-0x2F), final (0x40-0x7E)// - OSC: ESC ] then payload until BEL (0x07), 7-bit ST (ESC \), CAN (0x18), or SUB (0x1A)// - DCS, SOS, PM, APC: ESC P/X/^/_ then payload until 7-bit ST (ESC \), CAN, or SUB// - Two-byte: ESC + Fe/Fs (0x40-0x7E excluding above), or Fp (0x30-0x3F), or nF (0x20-0x2F then final)func ansiEscapeLength[ ~string | ~[]byte]( ) int {:= len()if < 2 || [0] != esc {return 0}:= [1]switch {case '[': // CSI:= csiBodyLength([2:])if == 0 {return 0}return 2 +case ']': // OSC - allows BEL or 7-bit ST terminator:= oscLength([2:])if < 0 {return 0}return 2 +case 'P', 'X', '^', '_': // DCS, SOS, PM, APC:= stSequenceLength([2:])if < 0 {return 0}return 2 +}if >= 0x40 && <= 0x7E {// Fe/Fs two-byte; [ ] P X ^ _ handled abovereturn 2}if >= 0x30 && <= 0x3F {// Fp (private) two-bytereturn 2}if >= 0x20 && <= 0x2F {// nF: intermediates then one final (0x30-0x7E):= 2for < && [] >= 0x20 && [] <= 0x2F {++}if < && [] >= 0x30 && [] <= 0x7E {return + 1}return 0}return 0}// csiBodyLength returns the length of the CSI body (param/intermediate/final bytes).// data is the slice after "ESC [".// Per ECMA-48, the CSI body has the form://// parameters (0x30–0x3F)*, intermediates (0x20–0x2F)*, final (0x40–0x7E)//// Once an intermediate byte is seen, subsequent parameter bytes are invalid.func csiBodyLength[ ~string | ~[]byte]( ) int {:= falsefor := 0; < len(); ++ {:= []if >= 0x30 && <= 0x3F {if {return 0}continue}if >= 0x20 && <= 0x2F {= truecontinue}if >= 0x40 && <= 0x7E {return + 1}return 0}return 0}// oscLength returns the length of the OSC body.// data is the slice after "ESC ]".//// Returns:// - n >= 0: consumed body length (includes BEL/ST terminator when present)// - -1: not terminated in the provided data//// OSC accepts BEL (0x07) or 7-bit ST (ESC \) as terminators by widespread convention.// Per ECMA-48, CAN (0x18) and SUB (0x1A) cancel the control string; in that// case they are not part of the OSC sequence length.func oscLength[ ~string | ~[]byte]( ) int {for := 0; < len(); ++ {:= []if == bel {return + 1}if == can || == sub {return}if == esc && +1 < len() && [+1] == '\\' {return + 2}}return -1}// stSequenceLength returns the length of a control-string body.// data is the slice after "ESC x".//// Returns:// - n >= 0: consumed body length (includes ST terminator when present)// - -1: not terminated in the provided data//// Used for DCS, SOS, PM, and APC, which per ECMA-48 terminate with ST.// ST here is the 7-bit form (ESC \).// CAN (0x18) and SUB (0x1A) cancel the control string; in that case they are// not part of the sequence length.func stSequenceLength[ ~string | ~[]byte]( ) int {for := 0; < len(); ++ {if [] == can || [] == sub {return}if [] == esc && +1 < len() && [+1] == '\\' {return + 2}}return -1}
![]() |
The pages are generated with Golds v0.8.7. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |