package nstd

import (
	
)

// A ByteSeq is either a string or a byte slice.
type ByteSeq interface{ ~string | ~[]byte }

// ByteSeqCommonPrefixes returns the common prefixes of two [ByteSeq] values.
func [,  ByteSeq]( ,  ) ( ,  ) {
	// Tried several coding mamners and found
	// the current one is inline-able, with cost 80.
	 := minOfTwo(len(), len())
	,  = [:], [:]
	if len() == len() { // BCE hint
		for  := 0;  < len(); ++ {
			if [] != [] {
				return [:], [:]
			}
		}
	}
	return
}

var (
	_, _ = ByteSeqCommonPrefix("", []byte{})
	_, _ = ByteSeqCommonPrefix("", "")
	_, _ = ByteSeqCommonPrefix([]byte{}, "")
	_, _ = ByteSeqCommonPrefix([]byte{}, []byte{})
)

// ReverseBytes inverts the bytes in a byte slice.
// The argument is returned so that calls to ReverseBytes can be used as expressions.
func [ ~[]byte]( )  {
	if len() == 0 {
		return [:0]
	}
	var ,  = len() - 1, 0
	for  >  {
		[], [] = [], []
		++
		--
	}
	return 
}

// ReverseByteSeq returnes a copy (in the form of byte slice) of
// a byte sequence (in the form of either string or byte slice) but reversed.
func [ ByteSeq]( ) []byte {
	if len() == 0 {
		return []byte([:0])
	}
	var  = make([]byte, len())
	var  = 0
	for  := len() - 1;  >= 0; -- {
		[] = []
		++
	}
	return 
}

// ReverseRuneSeq returnes a copy (in the form of byte slice) of
// a rune sequence (in the form of either string or byte slice) but reversed.
//
// See:
//
// * https://github.com/golang/go/issues/14777
// * https://github.com/golang/go/issues/68348
func [ ByteSeq]( ) []byte {
	if len() == 0 {
		return []byte([:0])
	}

	var  = make([]byte, len())
	var  = []byte() // doesn't allocate since Go toolchain 1.22
	var  = len()
	for {
		,  := utf8.DecodeRune()
		if  == 0 {
			if  != 0 {
				Panicf("i (%v) != 0", )
			}
			break
		}
		if  <  {
			Panicf("i (%v) < size (%v)", , )
		}
		 -= 
		var ,  = , 0
		for  <  {
			[] = []
			++
			++
		}
		 = [:]
	}

	return 
}