package strutil
import (
"bytes"
"encoding/base32"
"encoding/base64"
"net/url"
"strings"
"text/template"
)
func EscapeJS (s string ) string {
return template .JSEscapeString (s )
}
func EscapeHTML (s string ) string {
return template .HTMLEscapeString (s )
}
func AddSlashes (s string ) string {
if ln := len (s ); ln == 0 {
return ""
}
var buf bytes .Buffer
for _ , char := range s {
switch char {
case '\'' , '"' , '\\' :
buf .WriteRune ('\\' )
}
buf .WriteRune (char )
}
return buf .String ()
}
func StripSlashes (s string ) string {
ln := len (s )
if ln == 0 {
return ""
}
var skip bool
var buf bytes .Buffer
for i , char := range s {
if skip {
skip = false
} else if char == '\\' {
if i +1 < ln && s [i +1 ] == '\\' {
skip = true
}
continue
}
buf .WriteRune (char )
}
return buf .String ()
}
func URLEncode (s string ) string {
if pos := strings .IndexRune (s , '?' ); pos > -1 {
return s [0 :pos +1 ] + url .QueryEscape (s [pos +1 :])
}
return s
}
func URLDecode (s string ) string {
if pos := strings .IndexRune (s , '?' ); pos > -1 {
qy , err := url .QueryUnescape (s [pos +1 :])
if err == nil {
return s [0 :pos +1 ] + qy
}
}
return s
}
var (
B32Std = base32 .StdEncoding .WithPadding (base32 .NoPadding )
B32Hex = base32 .HexEncoding .WithPadding (base32 .NoPadding )
)
func B32Encode (str string ) string {
return B32Std .EncodeToString ([]byte (str ))
}
func B32Decode (str string ) string {
dec , _ := B32Std .DecodeString (str )
return string (dec )
}
var B64Std = base64 .StdEncoding .WithPadding (base64 .NoPadding )
func B64Encode (str string ) string {
return B64Std .EncodeToString ([]byte (str ))
}
func B64EncodeBytes (src []byte ) []byte {
buf := make ([]byte , B64Std .EncodedLen (len (src )))
B64Std .Encode (buf , src )
return buf
}
func B64Decode (str string ) string {
dec , _ := B64Std .DecodeString (str )
return string (dec )
}
func B64DecodeBytes (str []byte ) []byte {
dbuf := make ([]byte , B64Std .DecodedLen (len (str )))
n , _ := B64Std .Decode (dbuf , str )
return dbuf [:n ]
}
The pages are generated with Golds v0.8.4 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .