-
Notifications
You must be signed in to change notification settings - Fork 9
/
unicode.go
52 lines (42 loc) · 1.16 KB
/
unicode.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
// Copyright 2011 Julian Phillips. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package py
// #include "utils.h"
// static inline int unicodeCheck(PyObject *o) { return PyUnicode_Check(o); }
import "C"
import "unsafe"
type Unicode struct {
AbstractObject
o C.PyUnicodeObject
}
// UnicodeType is the Type object that represents the Unicode type.
var UnicodeType = (*Type)(unsafe.Pointer(C.getBasePyType(C.GoPyUnicode_Type)))
func unicodeCheck(obj Object) bool {
return C.unicodeCheck(c(obj)) != 0
}
func newUnicode(obj *C.PyObject) *Unicode {
return (*Unicode)(unsafe.Pointer(obj))
}
func NewUnicode(s string) (*Unicode, error) {
cs := C.CString(s)
defer C.free(unsafe.Pointer(cs))
ret := C.PyUnicode_FromStringAndSize(cs, C.Py_ssize_t(len(s)))
if ret == nil {
return nil, exception()
}
return newUnicode(ret), nil
}
func (s *Unicode) String() string {
if s == nil {
return "<nil>"
}
return stringify(s)
}
func (s *Unicode) Format(args *Tuple) (*Unicode, error) {
ret := C.PyUnicode_Format(c(s), c(args))
if ret == nil {
return nil, exception()
}
return newUnicode(ret), nil
}