package displaywidth

import (
	

	
)

// String calculates the display width of a string,
// by iterating over grapheme clusters in the string
// and summing their widths.
func ( string) int {
	return DefaultOptions.String()
}

// String calculates the display width of a string, for the given options, by
// iterating over grapheme clusters in the string and summing their widths.
func ( Options) ( string) int {
	 := 0
	 := 0

	for  < len() {
		// Try ASCII optimization
		 := printableASCIILength([:])
		if  > 0 {
			 += 
			 += 
			continue
		}

		// Not ASCII, use grapheme parsing
		 := graphemes.FromString([:])
		.AnsiEscapeSequences = .ControlSequences
		.AnsiEscapeSequences8Bit = .ControlSequences8Bit

		 := 

		for .Next() {
			 := .Value()
			 += graphemeWidth(, )
			 += len()

			// Quick check: if remaining might have printable ASCII, break to outer loop
			if  < len() && [] >= 0x20 && [] <= 0x7E {
				break
			}
		}

		// Defensive, should not happen: if no progress was made,
		// skip a byte to prevent infinite loop. Only applies if
		// the grapheme parser misbehaves.
		if  ==  {
			++
		}
	}

	return 
}

// Bytes calculates the display width of a []byte,
// by iterating over grapheme clusters in the byte slice
// and summing their widths.
func ( []byte) int {
	return DefaultOptions.Bytes()
}

// Bytes calculates the display width of a []byte, for the given options, by
// iterating over grapheme clusters in the slice and summing their widths.
func ( Options) ( []byte) int {
	 := 0
	 := 0

	for  < len() {
		// Try ASCII optimization
		 := printableASCIILength([:])
		if  > 0 {
			 += 
			 += 
			continue
		}

		// Not ASCII, use grapheme parsing
		 := graphemes.FromBytes([:])
		.AnsiEscapeSequences = .ControlSequences
		.AnsiEscapeSequences8Bit = .ControlSequences8Bit

		 := 

		for .Next() {
			 := .Value()
			 += graphemeWidth(, )
			 += len()

			// Quick check: if remaining might have printable ASCII, break to outer loop
			if  < len() && [] >= 0x20 && [] <= 0x7E {
				break
			}
		}

		// Defensive, should not happen: if no progress was made,
		// skip a byte to prevent infinite loop. Only applies if
		// the grapheme parser misbehaves.
		if  ==  {
			++
		}
	}

	return 
}

// Rune calculates the display width of a rune. You
// should almost certainly use [String] or [Bytes] for
// most purposes.
//
// The smallest unit of display width is a grapheme
// cluster, not a rune. Iterating over runes to measure
// width is incorrect in many cases.
func ( rune) int {
	return DefaultOptions.Rune()
}

// Rune calculates the display width of a rune, for the given options.
//
// You should almost certainly use [String] or [Bytes] for most purposes.
//
// The smallest unit of display width is a grapheme cluster, not a rune.
// Iterating over runes to measure width is incorrect in many cases.
func ( Options) ( rune) int {
	if  < utf8.RuneSelf {
		return asciiWidth(byte())
	}

	// Surrogates (U+D800-U+DFFF) are invalid UTF-8.
	if  >= 0xD800 &&  <= 0xDFFF {
		return 0
	}

	var  [4]byte
	 := utf8.EncodeRune([:], )

	// Skip the grapheme iterator
	return graphemeWidth([:], )
}

const _Default property = 0

// graphemeWidth returns the display width of a grapheme cluster.
// The passed string must be a single grapheme cluster.
func [ ~string | []byte]( ,  Options) int {
	if len() == 0 {
		return 0
	}

	// C1 controls (0x80-0x9F) are zero-width when 8-bit control sequences
	// are enabled. This must be checked before the single-byte optimization
	// below, which would otherwise return width 1 for these bytes.
	if .ControlSequences8Bit && [0] >= 0x80 && [0] <= 0x9F {
		return 0
	}

	// Optimization: single-byte graphemes need no property lookup
	if len() == 1 {
		return asciiWidth([0])
	}

	// Multi-byte grapheme clusters led by a C0 control (0x00-0x1F)
	if [0] <= 0x1F {
		return 0
	}

	,  := lookup()
	 := property()

	// Variation Selector 16 (VS16) requests emoji presentation
	if  != _Wide &&  > 0 && len() >= +3 {
		 := [ : +3]
		if isVS16() {
			 = _Wide
		}
		// VS15 (0x8E) requests text presentation but does not affect width,
		// in my reading of Unicode TR51. Falls through to return the base
		// character's property.
	}

	if .EastAsianWidth &&  == _East_Asian_Ambiguous {
		 = _Wide
	}

	if  > upperBound {
		 = _Default
	}

	return propertyWidths[]
}

func ( byte) int {
	if  <= 0x1F ||  == 0x7F {
		return 0
	}
	return 1
}

// printableASCIILength returns the length of consecutive printable ASCII bytes
// starting at the beginning of s.
func [ string | []byte]( ) int {
	 := 0
	for ;  < len(); ++ {
		 := []
		// Printable ASCII is 0x20-0x7E (space through tilde)
		if  < 0x20 ||  > 0x7E {
			break
		}
	}

	// If the next byte is non-ASCII (>= 0x80), back off by 1. The grapheme
	// parser may group the last ASCII byte with subsequent non-ASCII bytes,
	// such as combining marks.
	if  > 0 &&  < len() && [] >= 0x80 {
		--
	}

	return 
}

// isVS16 checks if the slice matches VS16 (U+FE0F) UTF-8 encoding
// (EF B8 8F). It assumes len(s) >= 3.
func [ ~string | []byte]( ) bool {
	return [0] == 0xEF && [1] == 0xB8 && [2] == 0x8F
}

// propertyWidths is a jump table of sorts, instead of a switch
var propertyWidths = [4]int{
	_Default:              1,
	_Zero_Width:           0,
	_Wide:                 2,
	_East_Asian_Ambiguous: 1,
}

const upperBound = property(len(propertyWidths) - 1)