package ansi

import (
	
	
	
)

// zeroWidthTables are the categories whose runes don't advance the cursor:
// nonspacing and enclosing marks, and format characters such as ZWJ and the
// variation selectors.
var zeroWidthTables = []*unicode.RangeTable{unicode.Mn, unicode.Me, unicode.Cf}

// zeroWidthBMP is a bitset of the [zeroWidthTables] runes in the basic
// multilingual plane. Measuring runs a lookup per rune, and a bit test is a
// good deal cheaper than searching three range tables. Runes above the BMP are
// rare enough in terminal output to leave on the slow path.
var zeroWidthBMP [0x10000 / 64]uint64

// zeroWidthHigh holds the [zeroWidthTables] runes above the BMP, expanded and
// sorted so a lookup is one binary search.
var zeroWidthHigh []rune

func () {
	for ,  := range zeroWidthTables {
		for ,  := range .R16 {
			for  := rune(.Lo);  <= rune(.Hi);  += rune(.Stride) {
				zeroWidthBMP[>>6] |= 1 << ( & 63)
			}
		}
		for ,  := range .R32 {
			// R32 range bounds are always valid runes.
			for  := rune(.Lo);  <= rune(.Hi);  += rune(.Stride) { //nolint:gosec
				zeroWidthHigh = append(zeroWidthHigh, )
			}
		}
	}
	slices.Sort(zeroWidthHigh)
}

// isZeroWidthHigh reports whether a rune above the BMP is zero width.
func ( rune) bool {
	,  := slices.BinarySearch(zeroWidthHigh, )
	return 
}

// wcRuneWidth returns the number of columns a single rune advances the cursor,
// i.e. wcwidth(3).
//
// Nonspacing and enclosing marks, along with format characters such as ZWJ and
// the variation selectors, never advance the cursor. [runewidth] reports some
// of them, most notably the Indic combining marks, as single width, so they're
// classified here instead.
func ( rune) int {
	switch {
	case  < 0x20,  >= 0x7f &&  < 0xa0:
		// C0 and C1 controls, and DEL, don't print.
		return 0
	case  < 0x7f:
		// Printable ASCII. Everything above it can be East Asian Ambiguous,
		// so it has to go through runewidth.
		return 1
	case  < 0x10000:
		if zeroWidthBMP[>>6]&(1<<(&63)) != 0 {
			return 0
		}
	case isZeroWidthHigh():
		return 0
	}
	return wcOptions.RuneWidth()
}

// wcClusterWidth returns the number of columns a grapheme cluster occupies
// under [WcWidth], which is the sum of the widths of its codepoints.
//
// Terminals that don't implement Unicode core mode (DEC mode 2027) advance the
// cursor once per codepoint and know nothing about cluster boundaries. So a
// cluster can be wider than the glyph it draws: Unicode 15.1 merged Indic
// conjuncts into single clusters, and a terminal still puts "स्ते" in two
// columns, one per consonant. ZWJ emoji sequences behave the same way, with
// "👨‍👩‍👧‍👦" taking the eight columns of the four emoji it's built from.
//
// Measuring the cluster as a unit is [GraphemeWidth]'s job.
func [ string | []byte]( ) int {
	var  int
	switch c := any().(type) {
	case string:
		for ,  := range  {
			 += wcRuneWidth()
		}
	case []byte:
		// Ranging over string(c) would copy the slice, so decode in place.
		for len() > 0 {
			,  := utf8.DecodeRune()
			 += wcRuneWidth()
			 = [:]
		}
	}
	return 
}