-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement unicode/bytes utility routines in Cython code
- Loading branch information
Showing
6 changed files
with
40 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#------------------------------------------------------------------------------ | ||
|
||
cdef extern from *: | ||
enum: PY3 "(PY_MAJOR_VERSION>=3)" | ||
int PyUnicode_Check(object) | ||
object PyUnicode_AsUTF8String(object) | ||
object PyUnicode_AsASCIIString(object) | ||
object PyUnicode_FromString(const char[]) | ||
object PyUnicode_FromStringAndSize(const char[],Py_ssize_t) | ||
object PyBytes_FromString(const char[]) | ||
object PyBytes_FromStringAndSize(const char[],Py_ssize_t) | ||
int PyBytes_AsStringAndSize(object,char*[],Py_ssize_t*) except -1 | ||
|
||
#------------------------------------------------------------------------------ | ||
|
||
cdef inline object asmpistr(object ob, char *s[]): | ||
if PyUnicode_Check(ob): | ||
if PY3: ob = PyUnicode_AsUTF8String(ob); | ||
else: ob = PyUnicode_AsASCIIString(ob); | ||
PyBytes_AsStringAndSize(ob, s, NULL) | ||
return ob | ||
|
||
cdef inline object tompistr(const char s[], int n): | ||
if PY3: return PyUnicode_FromStringAndSize(s, n) | ||
else: return PyBytes_FromStringAndSize(s, n) | ||
|
||
cdef inline object mpistr(const char s[]): | ||
if PY3: return PyUnicode_FromString(s) | ||
else: return PyBytes_FromString(s) | ||
|
||
cdef inline object pystr(const char s[]): | ||
if PY3: return PyUnicode_FromString(s) | ||
else: return PyBytes_FromString(s) | ||
|
||
#------------------------------------------------------------------------------ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters