-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathunicode.go
More file actions
41 lines (33 loc) · 759 Bytes
/
unicode.go
File metadata and controls
41 lines (33 loc) · 759 Bytes
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 gp
/*
#include <Python.h>
*/
import "C"
import "unsafe"
type Str struct {
Object
}
func newStr(obj *cPyObject) Str {
return Str{newObject(obj)}
}
func MakeStr(s string) Str {
ptr := (*Char)(unsafe.Pointer(unsafe.StringData(s)))
length := C.Py_ssize_t(len(s))
return newStr(C.PyUnicode_FromStringAndSize(ptr, length))
}
func (s Str) String() string {
var l C.Py_ssize_t
buf := C.PyUnicode_AsUTF8AndSize(s.obj, &l)
return GoStringN((*Char)(buf), int(l))
}
func (s Str) Len() int {
return int(C.PyUnicode_GetLength(s.obj))
}
func (s Str) ByteLen() int {
var l C.Py_ssize_t
_ = C.PyUnicode_AsUTF8AndSize(s.obj, &l)
return int(l)
}
func (s Str) Encode(encoding string) Bytes {
return cast[Bytes](s.Call("encode", MakeStr(encoding)))
}