-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathencoder.go
47 lines (38 loc) · 969 Bytes
/
encoder.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
package rapidyenc
/*
#cgo CFLAGS: -I${SRCDIR}/src
#cgo darwin LDFLAGS: ${SRCDIR}/librapidyenc_darwin.a -lstdc++
#cgo windows,amd64 LDFLAGS: ${SRCDIR}/librapidyenc_windows_amd64.a -lstdc++
#cgo linux,amd64 LDFLAGS: ${SRCDIR}/librapidyenc_linux_amd64.a -lstdc++
#cgo linux,arm64 LDFLAGS: ${SRCDIR}/librapidyenc_linux_arm64.a -lstdc++
#include "rapidyenc.h"
*/
import "C"
import (
"sync"
"unsafe"
)
func MaxLength(length, lineLength int) int {
return int(C.rapidyenc_encode_max_length(C.size_t(length), C.int(lineLength)))
}
type Encoder struct {
LineLength int
}
func NewEncoder() *Encoder {
return &Encoder{
LineLength: 128,
}
}
var encodeInitOnce sync.Once
func (e *Encoder) Encode(src []byte) []byte {
encodeInitOnce.Do(func() {
C.rapidyenc_encode_init()
})
dst := make([]byte, MaxLength(len(src), e.LineLength))
length := C.rapidyenc_encode(
unsafe.Pointer(&src[0]),
unsafe.Pointer(&dst[0]),
C.size_t(len(src)),
)
return dst[:length]
}