package strutil

import (
	
	
	
	
	
	
)

//
// -------------------- escape --------------------
//

// EscapeJS escape javascript string
func ( string) string {
	return template.JSEscapeString()
}

// EscapeHTML escape html string
func ( string) string {
	return template.HTMLEscapeString()
}

// AddSlashes add slashes for the string.
func ( string) string {
	if  := len();  == 0 {
		return ""
	}

	var  bytes.Buffer
	for ,  := range  {
		switch  {
		case '\'', '"', '\\':
			.WriteRune('\\')
		}
		.WriteRune()
	}

	return .String()
}

// StripSlashes strip slashes for the string.
func ( string) string {
	 := len()
	if  == 0 {
		return ""
	}

	var  bool
	var  bytes.Buffer

	for ,  := range  {
		if  {
			 = false
		} else if  == '\\' {
			if +1 <  && [+1] == '\\' {
				 = true
			}
			continue
		}
		.WriteRune()
	}

	return .String()
}

//
// -------------------- encode --------------------
//

// URLEncode encode url string.
func ( string) string {
	if  := strings.IndexRune(, '?');  > -1 { // escape query data
		return [0:+1] + url.QueryEscape([+1:])
	}
	return 
}

// URLDecode decode url string.
func ( string) string {
	if  := strings.IndexRune(, '?');  > -1 { // un-escape query data
		,  := url.QueryUnescape([+1:])
		if  == nil {
			return [0:+1] + 
		}
	}

	return 
}

//
// -------------------- base encode --------------------
//

// base32 encoding with no padding
var (
	B32Std = base32.StdEncoding.WithPadding(base32.NoPadding)
	B32Hex = base32.HexEncoding.WithPadding(base32.NoPadding)
)

// B32Encode base32 encode
func ( string) string {
	return B32Std.EncodeToString([]byte())
}

// B32Decode base32 decode
func ( string) string {
	,  := B32Std.DecodeString()
	return string()
}

// B64Std base64 encoding with no padding
var B64Std = base64.StdEncoding.WithPadding(base64.NoPadding)

// B64Encode base64 encode
func ( string) string {
	return B64Std.EncodeToString([]byte())
}

// B64EncodeBytes base64 encode
func ( []byte) []byte {
	 := make([]byte, B64Std.EncodedLen(len()))
	B64Std.Encode(, )
	return 
}

// B64Decode base64 decode
func ( string) string {
	,  := B64Std.DecodeString()
	return string()
}

// B64DecodeBytes base64 decode
func ( []byte) []byte {
	 := make([]byte, B64Std.DecodedLen(len()))
	,  := B64Std.Decode(, )
	return [:]
}