Skip to content

Commit

Permalink
up: update some for string convert and encode func
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed May 27, 2022
1 parent 6f2769b commit acc6ab7
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
5 changes: 5 additions & 0 deletions strutil/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ func Join(sep string, ss ...string) string {
return strings.Join(ss, sep)
}

// JoinSubs alias of strings.Join
func JoinSubs(sep string, ss []string) string {
return strings.Join(ss, sep)
}

// Implode alias of strings.Join
func Implode(sep string, ss ...string) string {
return strings.Join(ss, sep)
Expand Down
55 changes: 48 additions & 7 deletions strutil/encode.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
package strutil

import (
"encoding/base32"
"encoding/base64"
"net/url"
"strings"
"text/template"
)

var (
// EscapeJS escape javascript string
EscapeJS = template.JSEscapeString
// EscapeHTML escape html string
EscapeHTML = template.HTMLEscapeString
)
// EscapeJS escape javascript string
func EscapeJS(s string) string {
return template.JSEscapeString(s)
}

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

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

// B32Decode base32 decode
func B32Decode(str string) string {
dec, _ := base32.StdEncoding.DecodeString(str)
return string(dec)
}

// Base64 encode
func Base64(str string) string {
Expand All @@ -24,12 +39,17 @@ func B64Encode(str string) string {
return base64.StdEncoding.EncodeToString([]byte(str))
}

// B64Decode base64 decode
func B64Decode(str string) string {
dec, _ := base64.StdEncoding.DecodeString(str)
return string(dec)
}

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

return s
}

Expand All @@ -44,3 +64,24 @@ func URLDecode(s string) string {

return s
}

// BaseEncoder struct
type BaseEncoder struct {
// Base value
Base int
}

// NewBaseEncoder instance
func NewBaseEncoder(base int) *BaseEncoder {
return &BaseEncoder{Base: base}
}

// Encode handle
func (be *BaseEncoder) Encode(s string) string {
return s
}

// Decode handle
func (be *BaseEncoder) Decode(s string) (string, error) {
return s, nil
}

0 comments on commit acc6ab7

Please sign in to comment.