PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/unicode.go

http://github.com/qur/gopy
Go | 208 lines | 170 code | 33 blank | 5 comment | 33 complexity | 599e37b0c3ea7191a285c2d82fed53a8 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. // Copyright 2011 Julian Phillips. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package py
  5. // #include "utils.h"
  6. import "C"
  7. import "unsafe"
  8. type Unicode struct {
  9. AbstractObject
  10. SequenceProtocol
  11. o C.PyUnicodeObject
  12. }
  13. // UnicodeType is the Type object that represents the Unicode type.
  14. var UnicodeType = (*Type)(unsafe.Pointer(&C.PyUnicode_Type))
  15. func unicodeCheck(obj Object) bool {
  16. return C.unicodeCheck(c(obj)) != 0
  17. }
  18. func newUnicode(obj *C.PyObject) *Unicode {
  19. return (*Unicode)(unsafe.Pointer(obj))
  20. }
  21. func NewUnicode(s string) (*Unicode, error) {
  22. cs := C.CString(s)
  23. defer C.free(unsafe.Pointer(cs))
  24. ret := C.PyUnicode_FromString(cs)
  25. if ret == nil {
  26. return nil, exception()
  27. }
  28. return newUnicode(ret), nil
  29. }
  30. func (u *Unicode) String() string {
  31. if u == nil {
  32. return "<nil>"
  33. }
  34. s := C.PyUnicode_AsUTF8String(c(u))
  35. if s == nil {
  36. panic(exception())
  37. }
  38. defer C.decref(s)
  39. ret := C.PyString_AsString(s)
  40. if ret == nil {
  41. panic(exception())
  42. }
  43. return C.GoString(ret)
  44. }
  45. func (u *Unicode) Size() int64 {
  46. ret := C.PyUnicode_GetSize(c(u))
  47. return int64(ret)
  48. }
  49. func (u *Unicode) EncodeString(encoding, errors string) (Object, error) {
  50. var cEncoding, cErrors *C.char
  51. if encoding == "" {
  52. cEncoding = C.CString(encoding)
  53. defer C.free(unsafe.Pointer(cEncoding))
  54. }
  55. if errors != "" {
  56. cErrors = C.CString(errors)
  57. defer C.free(unsafe.Pointer(cErrors))
  58. }
  59. ret := C.PyUnicode_AsEncodedString(c(u), cEncoding, cErrors)
  60. return obj2ObjErr(ret)
  61. }
  62. func (u *Unicode) UTF8String() (Object, error) {
  63. ret := C.PyUnicode_AsUTF8String(c(u))
  64. return obj2ObjErr(ret)
  65. }
  66. func (u *Unicode) UTF16String() (Object, error) {
  67. ret := C.PyUnicode_AsUTF16String(c(u))
  68. return obj2ObjErr(ret)
  69. }
  70. func (u *Unicode) UTF32String() (Object, error) {
  71. ret := C.PyUnicode_AsUTF32String(c(u))
  72. return obj2ObjErr(ret)
  73. }
  74. func (u *Unicode) UnicodeEscapeString() (Object, error) {
  75. ret := C.PyUnicode_AsUnicodeEscapeString(c(u))
  76. return obj2ObjErr(ret)
  77. }
  78. func (u *Unicode) RawUnicodeEscapeString() (Object, error) {
  79. ret := C.PyUnicode_AsRawUnicodeEscapeString(c(u))
  80. return obj2ObjErr(ret)
  81. }
  82. func (u *Unicode) Latin1String() (Object, error) {
  83. ret := C.PyUnicode_AsLatin1String(c(u))
  84. return obj2ObjErr(ret)
  85. }
  86. func (u *Unicode) ASCIIString() (Object, error) {
  87. ret := C.PyUnicode_AsASCIIString(c(u))
  88. return obj2ObjErr(ret)
  89. }
  90. func (u *Unicode) CharmapString(mapping Object) (Object, error) {
  91. ret := C.PyUnicode_AsCharmapString(c(u), c(mapping))
  92. return obj2ObjErr(ret)
  93. }
  94. func (u *Unicode) Encode(encoding, errors string) (Object, error) {
  95. var cEncoding, cErrors *C.char
  96. if encoding == "" {
  97. cEncoding = C.CString(encoding)
  98. defer C.free(unsafe.Pointer(cEncoding))
  99. }
  100. if errors != "" {
  101. cErrors = C.CString(errors)
  102. defer C.free(unsafe.Pointer(cErrors))
  103. }
  104. ret := C.PyUnicode_AsEncodedObject(c(u), cEncoding, cErrors)
  105. return obj2ObjErr(ret)
  106. }
  107. func (left *Unicode) Concat(right Object) (Object, error) {
  108. ret := C.PyUnicode_Concat(c(left), c(right))
  109. return obj2ObjErr(ret)
  110. }
  111. func (u *Unicode) Split(sep Object, maxsplit int64) (Object, error) {
  112. ret := C.PyUnicode_Split(c(u), c(sep), C.Py_ssize_t(maxsplit))
  113. return obj2ObjErr(ret)
  114. }
  115. func (u *Unicode) Splitlines(keepend bool) (Object, error) {
  116. cKeepend := C.int(0)
  117. if keepend {
  118. cKeepend = 1
  119. }
  120. ret := C.PyUnicode_Splitlines(c(u), cKeepend)
  121. return obj2ObjErr(ret)
  122. }
  123. func (u *Unicode) Translate(table Object, errors string) (Object, error) {
  124. var cErrors *C.char
  125. if errors != "" {
  126. cErrors = C.CString(errors)
  127. defer C.free(unsafe.Pointer(cErrors))
  128. }
  129. ret := C.PyUnicode_Translate(c(u), c(table), cErrors)
  130. return obj2ObjErr(ret)
  131. }
  132. func (u *Unicode) Join(seq Object) (Object, error) {
  133. ret := C.PyUnicode_Join(c(u), c(seq))
  134. return obj2ObjErr(ret)
  135. }
  136. func (u *Unicode) Tailmatch(substr Object, start, end int64, direction int) (bool, error) {
  137. ret := C.PyUnicode_Tailmatch(c(u), c(substr), C.Py_ssize_t(start), C.Py_ssize_t(end), C.int(direction))
  138. return int2BoolErr(C.int(ret))
  139. }
  140. func (u *Unicode) Find(substr Object, start, end int64, direction int) (int64, bool, error) {
  141. ret := C.PyUnicode_Find(c(u), c(substr), C.Py_ssize_t(start), C.Py_ssize_t(end), C.int(direction))
  142. if ret >= 0 {
  143. return int64(ret), true, nil
  144. } else if ret == -1 {
  145. return 0, false, nil
  146. }
  147. return 0, false, exception()
  148. }
  149. func (u *Unicode) Count(substr Object, start, end int64) (int64, error) {
  150. ret := C.PyUnicode_Count(c(u), c(substr), C.Py_ssize_t(start), C.Py_ssize_t(end))
  151. return ssize_t2Int64Err(ret)
  152. }
  153. func (u *Unicode) Replace(substr, replstr Object, maxcount int64) (Object, error) {
  154. ret := C.PyUnicode_Replace(c(u), c(substr), c(replstr), C.Py_ssize_t(maxcount))
  155. return obj2ObjErr(ret)
  156. }
  157. func (u *Unicode) Compare(right Object) (int, error) {
  158. ret := C.PyUnicode_Compare(c(u), c(right))
  159. return int(ret), exception()
  160. }
  161. func (u *Unicode) RichCompare(right Object, op Op) (Object, error) {
  162. ret := C.PyUnicode_RichCompare(c(u), c(right), C.int(op))
  163. return obj2ObjErr(ret)
  164. }
  165. func (u *Unicode) Format(args *Tuple) (*Unicode, error) {
  166. ret := C.PyUnicode_Format(c(u), c(args))
  167. if ret == nil {
  168. return nil, exception()
  169. }
  170. return newUnicode(ret), nil
  171. }
  172. func (u *Unicode) Contains(element Object) (bool, error) {
  173. ret := C.PyUnicode_Contains(c(u), c(element))
  174. return int2BoolErr(ret)
  175. }