-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStr.go
185 lines (157 loc) · 4.5 KB
/
Str.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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package wingo
import (
"fmt"
"strings"
"syscall"
"unsafe"
)
type _StrT struct{}
// String utilities, including wide char UTF-16 string conversion functions.
var Str _StrT
// Formats a number of bytes into KB, MB, GB or TB.
func (_StrT) FmtBytes(numBytes uint64) string {
switch {
case numBytes < 1024:
return fmt.Sprintf("%d bytes", numBytes)
case numBytes < 1024*1024:
return fmt.Sprintf("%.2f KB", float64(numBytes)/1024)
case numBytes < 1024*1024*1024:
return fmt.Sprintf("%.2f MB", float64(numBytes)/1024/1024)
case numBytes < 1024*1024*1024*1024:
return fmt.Sprintf("%.2f GB", float64(numBytes)/1024/1024/1024)
default:
return fmt.Sprintf("%.2f TB", float64(numBytes)/1024/1024/1024/1024)
}
}
// Converts a null-terminated *uint16 to string.
//
// Copied from syscall_windows.go, utf16PtrToString() private function.
func (_StrT) FromNativePtr(p *uint16) string {
if p == nil {
return ""
}
// Find null terminator.
pRun := unsafe.Pointer(p)
sLen := 0
for *(*uint16)(pRun) != 0 {
pRun = unsafe.Add(pRun, unsafe.Sizeof(*p)) // pRun++
sLen++
}
slice := unsafe.Slice(p, sLen) // create slice without terminating null
return Str.FromNativeSlice(slice)
}
// Converts a multi null-terminated *uint16 to []string.
//
// Source must have 2 terminating nulls.
func (_StrT) FromNativePtrMulti(p *uint16) []string {
values := make([]string, 0)
if p == nil {
return values
}
pRun := unsafe.Pointer(p)
sLen := 0
for {
if *(*uint16)(pRun) == 0 { // terminating null found
if sLen == 0 {
break // two terminating nulls
}
slice := unsafe.Slice(p, sLen) // create slice without terminating null
values = append(values, Str.FromNativeSlice(slice))
pRun = unsafe.Add(pRun, unsafe.Sizeof(*p)) // pRun++
p = (*uint16)(pRun)
sLen = 0
} else {
pRun = unsafe.Add(pRun, unsafe.Sizeof(*p)) // pRun++
sLen++
}
}
return values
}
// Converts a null-terminated []uint16 to string.
//
// Simple wrapper to syscall.UTF16ToString().
func (_StrT) FromNativeSlice(s []uint16) string {
return syscall.UTF16ToString(s)
}
// Returns a new string with all diacritics removed.
func (_StrT) RemoveDiacritics(s string) string {
diacs := []rune("ÁáÀàÃãÂâÄäÉéÈèÊêËëÍíÌìÎîÏïÓóÒòÕõÔôÖöÚúÙùÛûÜüÇçÅåÐðÑñØøÝý")
repls := []rune("AaAaAaAaAaEeEeEeEeIiIiIiIiOoOoOoOoOoUuUuUuUuCcAaDdNnOoYy")
strBuf := strings.Builder{}
strBuf.Grow(len(s))
for _, ch := range []rune(s) {
replaced := false
for i, diac := range diacs {
if ch == diac {
strBuf.WriteRune(repls[i])
replaced = true
break
}
}
if !replaced {
strBuf.WriteRune(ch)
}
}
return strBuf.String()
}
// Extracts a substring from a string, UTF-8-aware.
//
// https://stackoverflow.com/a/56129336/6923555
func (_StrT) Substr(s string, start, length int) string {
asRunes := []rune(s)
if start >= len(asRunes) {
return ""
}
if start+length > len(asRunes) {
length = len(asRunes) - start
}
return string(asRunes[start : start+length])
}
// Converts string to null-terminated *uint16.
//
// Ideal to pass strings to syscalls. We won't return an uintptr right away
// because it has no pointer semantics, it's just a number, so pointed memory
// can be garbage-collected.
//
// https://stackoverflow.com/a/51188315
//
// Wrapper to syscall.UTF16PtrFromString(). Panics on error.
func (_StrT) ToNativePtr(s string) *uint16 {
pstr, err := syscall.UTF16PtrFromString(s)
if err != nil {
panic(fmt.Sprintf("Str.ToNativePtr() failed \"%s\": %s", s, err))
}
return pstr
}
// Converts []string to multi null-terminated *uint16.
//
// Memory block will have 2 terminating nulls.
func (_StrT) ToNativePtrMulti(ss []string) *uint16 {
slice := Str.ToNativeSliceMulti(ss)
return &slice[0]
}
// Converts string to null-terminated []uint16.
//
// Wrapper to syscall.UTF16FromString(). Panics on error.
func (_StrT) ToNativeSlice(s string) []uint16 {
sli, err := syscall.UTF16FromString(s)
if err != nil {
panic(fmt.Sprintf("Str.ToNativeSlice() failed \"%s\": %s", s, err))
}
return sli
}
// Converts []string to multi null-terminated []uint16.
//
// Returned slice will have 2 terminating nulls.
func (_StrT) ToNativeSliceMulti(ss []string) []uint16 {
estimatedLen := 0
for _, s := range ss {
estimatedLen += len(s) + 1 // also count terminating null; can be more than needed
}
buf := make([]uint16, 0, estimatedLen+1) // prealloc; room for two terminating nulls
for _, s := range ss {
buf = append(buf, Str.ToNativeSlice(s)...)
}
buf = append(buf, 0) // 2nd terminating null
return buf
}