forked from dromara/dongle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdongle.go
52 lines (45 loc) · 1.26 KB
/
dongle.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// @Package dongle
// @Description a simple, semantic and developer-friendly golang package for encoding&decoding and encryption&decryption
// @Page github.com/golang-module/dongle
// @Version v0.0.2
// @Author gouguoyin
// @Blog www.gouguoyin.cn
// @Email [email protected]
// Package dongle is a simple, semantic and developer-friendly golang package for encoding&decoding and encryption&decryption.
package dongle
import (
"unsafe"
)
// dongle defines a dongle struct.
type dongle struct {
input []byte
output []byte
Error error
}
var (
// emptyString defines a empty string
emptyString = ""
// emptyBytes defines a empty byte slice
emptyBytes = []byte{}
// Encode returns a new encode instance
Encode = newEncode()
// Decode returns a new decode instance
Decode = newDecode()
// Encrypt returns a new encrypt instance
Encrypt = newEncrypt()
// Decrypt returns a new decrypt instance
Decrypt = newDecrypt()
)
// string2bytes converts string to byte slice without a memory allocation.
func string2bytes(s string) []byte {
return *(*[]byte)(unsafe.Pointer(
&struct {
string
Cap int
}{s, len(s)},
))
}
// bytes2string converts byte slice to string without a memory allocation.
func bytes2string(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}