package httpbinding

import (
	
	
)

const (
	uriTokenStart = '{'
	uriTokenStop  = '}'
	uriTokenSkip  = '+'
)

func ( []byte,  int) []byte {
	if cap() <  {
		return make([]byte, 0, )
	}

	return [0:0]
}

// replacePathElement replaces a single element in the path []byte.
// Escape is used to control whether the value will be escaped using Amazon path escape style.
func (,  []byte, ,  string,  bool) ([]byte, []byte, error) {
	 = bufCap(, len()+3) // { <key> [+] }
	 = append(, uriTokenStart)
	 = append(, ...)

	 := bytes.Index(, )
	 :=  + len()
	if  < 0 || len([:]) == 0 {
		// TODO what to do about error?
		return , , fmt.Errorf("invalid path index, start=%d,end=%d. %s", , , )
	}

	 := true
	if [] == uriTokenSkip {
		// '+' token means do not escape slashes
		 = false
		++
	}

	if  {
		 = EscapePath(, )
	}

	if [] != uriTokenStop {
		return , , fmt.Errorf("invalid path element, does not contain token stop, %s", )
	}
	++

	 = bufCap(, len())
	 = append(, ...)

	 :=  - 
	 := len()

	if  ==  {
		copy([:], )
		return , , nil
	}

	 := len() + ( - )
	if len() <  {
		 = [:cap()]
	}
	if cap() <  {
		 := make([]byte, )
		copy(, )
		 = 
	}

	// shift
	copy([+:], [:])
	 = [:]
	copy([:], )

	return , , nil
}

// EscapePath escapes part of a URL path in Amazon style.
func ( string,  bool) string {
	var  bytes.Buffer
	for  := 0;  < len(); ++ {
		 := []
		if noEscape[] || ( == '/' && !) {
			.WriteByte()
		} else {
			fmt.Fprintf(&, "%%%02X", )
		}
	}
	return .String()
}

var noEscape [256]bool

func () {
	for  := 0;  < len(noEscape); ++ {
		// AWS expects every character except these to be escaped
		noEscape[] = ( >= 'A' &&  <= 'Z') ||
			( >= 'a' &&  <= 'z') ||
			( >= '0' &&  <= '9') ||
			 == '-' ||
			 == '.' ||
			 == '_' ||
			 == '~'
	}
}