package libp2pwebrtc

// The code in this file is adapted from the Go standard library's hex package.
// As found in https://cs.opensource.google/go/go/+/refs/tags/go1.20.2:src/encoding/hex/hex.go
//
// The reason we adapted the original code is to allow us to deal with interspersed requirements
// while at the same time hex encoding/decoding, without having to do so in two passes.

import (
	
	
)

// encodeInterspersedHex encodes a byte slice into a string of hex characters,
// separating each encoded byte with a colon (':').
//
// Example: { 0x01, 0x02, 0x03 } -> "01:02:03"
func encodeInterspersedHex( []byte) string {
	if len() == 0 {
		return ""
	}
	 := hex.EncodeToString()
	 := len()
	// Determine number of colons
	 :=  / 2
	if %2 == 0 {
		--
	}
	 := make([]byte, +)

	for ,  := 0, 0;  < ; ,  = +2, +3 {
		copy([:+2], [:+2])
		if +3 < len() {
			[+2] = ':'
		}
	}
	return string()
}

var errUnexpectedIntersperseHexChar = errors.New("unexpected character in interspersed hex string")

// decodeInterspersedHexFromASCIIString decodes an ASCII string of hex characters into a byte slice,
// where the hex characters are expected to be separated by a colon (':').
//
// NOTE that this function returns an error in case the input string contains non-ASCII characters.
//
// Example: "01:02:03" -> { 0x01, 0x02, 0x03 }
func decodeInterspersedHexFromASCIIString( string) ([]byte, error) {
	 := len()
	 := make([]byte, /3*2+%3)
	 := 0
	for  := 0;  < ; ++ {
		if %3 == 2 {
			if [] != ':' {
				return nil, errUnexpectedIntersperseHexChar
			}
		} else {
			if [] == ':' {
				return nil, errUnexpectedIntersperseHexChar
			}
			[] = []
			++
		}
	}
	 := make([]byte, hex.DecodedLen(len()))
	if ,  := hex.Decode(, );  != nil {
		return nil, 
	}
	return , nil
}