diff options
author | Renato Filho <renato.filho@openbossa.org> | 2011-10-05 19:30:09 -0300 |
---|---|---|
committer | Hugo Parente Lima <hugo.pl@gmail.com> | 2012-03-08 16:18:19 -0300 |
commit | adefbf0bb47a92dd8282ff1a1af57af1280c728d (patch) | |
tree | 587a8861c1b8bf64a7a3927ab868ddf994c914d0 | |
parent | a1295af4fbb91fb3e416a58a80926278354279dc (diff) |
fix string class.
-rw-r--r-- | libshiboken/sbkstring.cpp | 73 |
1 files changed, 47 insertions, 26 deletions
diff --git a/libshiboken/sbkstring.cpp b/libshiboken/sbkstring.cpp index ebe1efb2f..c20c487e7 100644 --- a/libshiboken/sbkstring.cpp +++ b/libshiboken/sbkstring.cpp @@ -30,28 +30,24 @@ namespace String bool checkType(PyTypeObject* type) { -#if PY_MAJOR_VERSION >= 3 - return type == &PyUnicode_Type; -#else - return type == &PyString_Type || - type == &PyUnicode_Type; + return type == &PyUnicode_Type +#if PY_MAJOR_VERSION < 3 + || type == &PyString_Type #endif + ; } bool check(PyObject* obj) { return obj == Py_None || -#if PY_MAJOR_VERSION >= 3 - PyUnicode_Check(obj); -#else - PyString_Check(obj); +#if PY_MAJOR_VERSION < 3 + PyString_Check(obj) || #endif + PyUnicode_Check(obj); } bool checkChar(PyObject* pyobj) { - if (PyBytes_Check(pyobj) && (PyBytes_GET_SIZE(pyobj) == 1)) - return true; if (check(pyobj) && (len(pyobj) == 1)) return true; @@ -85,15 +81,25 @@ const char* toCString(PyObject* str) bool concat(PyObject** val1, PyObject* val2) { -#if PY_MAJOR_VERSION >= 3 - PyObject* result = PyUnicode_Concat(*val1, val2); - Py_DECREF(*val1); - *val1 = result; - return true; -#else - PyString_Concat(val1, val2); - return true; + if (PyUnicode_Check(val1) && PyUnicode_Check(val2)) { + PyObject* result = PyUnicode_Concat(*val1, val2); + Py_DECREF(*val1); + *val1 = result; + return true; + } + + if (PyBytes_Check(val1) && PyBytes_Check(val2)) { + PyBytes_Concat(val1, val2); + return true; + } + +#if PY_MAJOR_VERSION < 3 + if (PyString_Check(val1) && PyString_Check(val2)) { + PyString_Concat(val1, val2); + return true; + } #endif + return false; } PyObject* fromFormat(const char* format, ...) @@ -121,25 +127,40 @@ PyObject* fromStringAndSize(const char* str, Py_ssize_t size) int compare(PyObject* val1, const char* val2) { + if (PyUnicode_Check(val1)) #if PY_MAJOR_VERSION >= 3 - return PyUnicode_CompareWithASCIIString(val1, val2); + return PyUnicode_CompareWithASCIIString(val1, val2); #else - return strcmp(PyString_AS_STRING(val1), val2); + { + PyObject* uVal2 = PyUnicode_FromFormat("%s", val2); + bool result = PyUnicode_Compare(val1, uVal2); + Py_XDECREF(uVal2); + return result; + } + if (PyString_Check(val1)) + return strcmp(PyString_AS_STRING(val1), val2); #endif + return 0; + } Py_ssize_t len(PyObject* str) { - printf("TYPE NAME: %s\n", Py_TYPE(str)->tp_name); if (str == Py_None) return 0; -#if PY_MAJOR_VERSION >= 3 - return PyUnicode_GET_SIZE(str); -#else - return PyString_GET_SIZE(str); + if (PyUnicode_Check(str)) + return PyUnicode_GET_SIZE(str); + + if (PyBytes_Check(str)) + return PyBytes_GET_SIZE(str); + +#if PY_MAJOR_VERSION < 3 + if (PyString_Check(str)) + return PyString_GET_SIZE(str); #endif + return 0; } } // namespace String |