2016-07-20 18:58:48 +00:00
|
|
|
package mtree
|
|
|
|
|
|
|
|
// #include "vis.h"
|
2016-07-21 01:18:27 +00:00
|
|
|
// #include <stdlib.h>
|
2016-07-20 18:58:48 +00:00
|
|
|
import "C"
|
2016-07-21 01:18:27 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"unsafe"
|
|
|
|
)
|
2016-07-20 18:58:48 +00:00
|
|
|
|
2016-07-21 01:18:27 +00:00
|
|
|
// Vis is a wrapper of the C implementation of the function vis, which encodes
|
|
|
|
// a character with a particular format/style
|
|
|
|
func Vis(src string) (string, error) {
|
|
|
|
// dst needs to be 4 times the length of str, must check appropriate size
|
|
|
|
if uint32(len(src)*4+1) >= math.MaxUint32/4 {
|
|
|
|
return "", fmt.Errorf("failed to encode: %q", src)
|
2016-07-20 18:58:48 +00:00
|
|
|
}
|
2016-07-21 01:18:27 +00:00
|
|
|
dst := string(make([]byte, 4*len(src)+1))
|
|
|
|
cDst, cSrc := C.CString(dst), C.CString(src)
|
|
|
|
defer C.free(unsafe.Pointer(cDst))
|
|
|
|
defer C.free(unsafe.Pointer(cSrc))
|
|
|
|
C.strvis(cDst, cSrc, C.VIS_WHITE|C.VIS_OCTAL|C.VIS_GLOB)
|
|
|
|
|
|
|
|
return C.GoString(cDst), nil
|
2016-07-20 18:58:48 +00:00
|
|
|
}
|