-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
62 lines (49 loc) · 1.37 KB
/
common.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
53
54
55
56
57
58
59
60
61
62
package stringdedup
import (
"runtime"
"sync"
"unsafe"
)
var lock sync.RWMutex
type weakdata struct {
data uintptr
length int
}
func (wd weakdata) Uintptr() uintptr {
return wd.data
}
func (wd weakdata) Pointer() *byte {
return (*byte)(unsafe.Pointer(wd.data))
}
func weakString(in string) weakdata {
ws := weakdata{
data: uintptr(unsafe.Pointer(unsafe.StringData(in))),
length: len(in),
}
return ws
}
func weakBytes(in []byte) weakdata {
ws := weakdata{
data: uintptr(unsafe.Pointer(&in[0])),
length: len(in),
}
return ws
}
func (wd weakdata) String() string {
return unsafe.String((*byte)(unsafe.Pointer(wd.data)), wd.length)
}
func (wd weakdata) Bytes() []byte {
return unsafe.Slice((*byte)(unsafe.Pointer(wd.data)), wd.length)
}
func castStringToBytes(in string) []byte {
return unsafe.Slice(unsafe.StringData(in), len(in))
}
func castBytesToString(in []byte) string {
out := unsafe.String(&in[0], len(in))
runtime.KeepAlive(in)
return out
}
// ValidateResults ensures that no collisions in returned strings are possible. This is enabled default, but you can speed things up by setting this to false
var ValidateResults = true
// YesIKnowThisCouldGoHorriblyWrong requires you to read the source code to understand what it does. This is intentional, as usage is only for very specific an careful scenarios
var YesIKnowThisCouldGoHorriblyWrong = false