-
Notifications
You must be signed in to change notification settings - Fork 10
/
fixed_string.go
41 lines (33 loc) · 719 Bytes
/
fixed_string.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
package ecs
import (
"fmt"
"unsafe"
)
const (
__FixedMax = 128
)
type FixedString[T any] struct {
data T
len int
}
func (f *FixedString[T]) Clear() {
f.len = 0
}
func (f *FixedString[T]) Empty() bool {
return f.len == 0
}
func (f *FixedString[T]) Len() int {
return f.len
}
func (f *FixedString[T]) String() string {
return string((*(*[__FixedMax]byte)(unsafe.Pointer(&(f.data))))[:f.len])
}
func (f *FixedString[T]) Set(s string) {
if len(s) > int(unsafe.Sizeof(f.data)) {
panic(fmt.Sprintf("fixed string max size: %d, received size: %d", unsafe.Sizeof(f.data), len(s)))
}
f.len = len(s)
if f.len != 0 {
copy((*(*[__FixedMax]byte)(unsafe.Pointer(&(f.data))))[:unsafe.Sizeof(f.data)], s)
}
}