Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoding.go
More file actions
Latest commit
100 lines (89 loc) · 2.75 KB
/
Copy pathencoding.go
File metadata and controls
100 lines (89 loc) · 2.75 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package encoding
import (
"fmt"
"reflect"
"unsafe"
)
// MustFromEncoding parse specific encoding string to universal Go data.
// It panic if error occur.
funcMustFromEncoding(encodingstring, sourcestring) interface{} {
d, err:=FromEncoding(encoding, source)
iferr!=nil {
panic(err)
}
returnd
}
// MustToEncoding encode a universal Go data into specific encoding string.
// It panic if error occur.
funcMustToEncoding(encodingstring, dinterface{}, prettybool) string {
s, err:=ToEncoding(encoding, d, pretty)
iferr!=nil {
panic(err)
}
returns
}
// MustConvertEncoding converts source text from encoding to dstEncoding.
// It panic if error occur.
funcMustConvertEncoding(encodingstring, sourcestring, dstEncodingstring, prettybool) string {
s, err:=ConvertEncoding(encoding, source, dstEncoding, pretty)
iferr!=nil {
panic(err)
}
returns
}
//------------------------------------------------------------------------------
// FromEncoding parse specific encoding string to universal Go data.
funcFromEncoding(encodingstring, sourcestring) (interface{}, error) {
switchenc:=encoding; enc {
case"", consts.EncodingJson:
returnFromJson(source)
caseconsts.EncodingXml:
returnFromXml(source)
caseconsts.EncodingYaml:
returnFromYaml(source)
default:
returnnil, fmt.Errorf("encoding: unsupported encoding %s", enc)
}
}
// ToEncoding encode a universal Go data into specific encoding string.
funcToEncoding(encodingstring, dinterface{}, prettybool) (string, error) {
switchenc:=encoding; enc {
case"", consts.EncodingJson:
returnToJson(d, pretty)
caseconsts.EncodingXml:
returnToXml(d, pretty)
caseconsts.EncodingYaml:
returnToYaml(d, pretty)
default:
return"", fmt.Errorf("encoding: unsupported encoding %s", enc)
}
}
// ConvertEncoding converts source text from encoding to dstEncoding.
funcConvertEncoding(encodingstring, sourcestring, dstEncodingstring, prettybool) (string, error) {
ifencoding==dstEncoding {
returnsource, nil
}
obj, err:=FromEncoding(encoding, source)
iferr!=nil {
return"", err
}
dst, err:=ToEncoding(dstEncoding, obj, pretty)
iferr!=nil {
return"", err
}
returndst, err
}
//------------------------------------------------------------------------------
// unsafeByteString convert []byte to string without copy
// the origin []byte **MUST NOT** accessed after that
funcunsafeByteString(b []byte) string {
return*(*string)(unsafe.Pointer(&b))
}
// unsafeStringBytes return GoString's buffer slice
// ** NEVER modify returned []byte **
funcunsafeStringBytes(sstring) []byte {
varbh reflect.SliceHeader
sh:= (*reflect.StringHeader)(unsafe.Pointer(&s))
bh.Data, bh.Len, bh.Cap=sh.Data, sh.Len, sh.Len
return*(*[]byte)(unsafe.Pointer(&bh))
}