diff --git a/SWIG/Lib/python/pymacros.swg b/SWIG/Lib/python/pymacros.swg index 45b02cb4c..2a8c79f7e 100644 --- a/SWIG/Lib/python/pymacros.swg +++ b/SWIG/Lib/python/pymacros.swg @@ -3,6 +3,7 @@ #define SWIG_OLDOBJ 1 #define SWIG_NEWOBJ SWIG_OLDOBJ + 1 +#define SWIG_PYSTR SWIG_NEWOBJ + 1 #ifdef __cplusplus #define SWIGSTATICINLINE(a) static inline a diff --git a/SWIG/Lib/python/pystrings.swg b/SWIG/Lib/python/pystrings.swg index 6e7765f71..c55be83bd 100644 --- a/SWIG/Lib/python/pystrings.swg +++ b/SWIG/Lib/python/pystrings.swg @@ -4,9 +4,6 @@ * ------------------------------------------------------------ */ %types(char *); -%{ -#define SWIG_PYSTR SWIG_NEWOBJ + 1 -%} %fragment("SWIG_AsCharPtrAndSize","header") %{ /* returns SWIG_OLDOBJ if the input is a raw char*, SWIG_PYSTR if is a PyString */ SWIGSTATIC(int) @@ -15,7 +12,6 @@ SWIG_AsCharPtrAndSize(PyObject *obj, char** cptr, size_t* psize) static swig_type_info* pchar_info = 0; char* vptr = 0; if (!pchar_info) pchar_info = SWIG_TypeQuery("char *"); - if (SWIG_ConvertPtr(obj, (void**)&vptr, pchar_info, 0) != -1) { if (cptr) *cptr = vptr; if (psize) *psize = vptr ? (strlen(vptr) + 1) : 0; @@ -41,7 +37,7 @@ SWIG_AsCharPtrAndSize(PyObject *obj, char** cptr, size_t* psize) %fragment("SWIG_AsCharPtr","header", fragment="SWIG_AsCharPtrAndSize") %{ SWIGSTATICINLINE(int) - SWIG_AsCharPtr(PyObject *obj, char **val) +SWIG_AsCharPtr(PyObject *obj, char **val) { char* cptr = 0; if (SWIG_AsCharPtrAndSize(obj, &cptr, (size_t*)(0))) { @@ -80,7 +76,7 @@ SWIG_FromCharPtr(const char* cptr) %fragment("SWIG_AsNewCharPtr","header", fragment="SWIG_AsCharPtrAndSize") %{ SWIGSTATIC(int) - SWIG_AsNewCharPtr(PyObject *obj, char **val) +SWIG_AsNewCharPtr(PyObject *obj, char **val) { char* cptr = 0; size_t csize = 0; int res = SWIG_AsCharPtrAndSize(obj, &cptr, &csize); diff --git a/SWIG/Lib/python/pywstrings.swg b/SWIG/Lib/python/pywstrings.swg new file mode 100644 index 000000000..37b165e94 --- /dev/null +++ b/SWIG/Lib/python/pywstrings.swg @@ -0,0 +1,374 @@ +/* ------------------------------------------------------------ + * utility methods for wstrings handling + * ------------------------------------------------------------ */ + +%types(wchar_t *); +%{ +#include +%} +%fragment("SWIG_AsWCharPtrAndSize","header") %{ +SWIGSTATIC(int) +SWIG_AsWCharPtrAndSize(PyObject *obj, wchar_t **cptr, size_t *psize) +{ + static swig_type_info* pwchar_info = 0; + wchar_t * vptr = 0; + if (!pwchar_info) pwchar_info = SWIG_TypeQuery("wchar_t *"); + if (SWIG_ConvertPtr(obj, (void**)&vptr, pwchar_info, 0) != -1) { + if (cptr) *cptr = vptr; + if (psize) *psize = vptr ? (wcslen(vptr) + 1) : 0; + return SWIG_OLDOBJ; + } else { + int isunicode = PyUnicode_Check(obj); + if (isunicode || PyString_Check(obj)) { + if (cptr) { + int size = isunicode ? PyUnicode_GetSize(obj) : PyString_Size(obj); + wchar_t *nptr = swig_new_array(size + 1, wchar_t); + PyUnicodeObject *uni = (PyUnicodeObject *)PyUnicode_FromObject(obj); + PyUnicode_AsWideChar(uni, nptr, size); + nptr[size] = 0; + *cptr = nptr; + if (psize) { + *psize = (size_t) size + 1; + } + Py_DECREF(uni); + } + return SWIG_NEWOBJ; + } + } + if (cptr) { + PyErr_SetString(PyExc_TypeError, "a wchar_t * is expected"); + } + return 0; +} +%} + +%fragment("SWIG_AsWCharPtr","header", + fragment="SWIG_AsWCharPtrAndSize") %{ +SWIGSTATICINLINE(int) +SWIG_AsWCharPtr(PyObject *obj, wchar_t **val) +{ + wchar_t * cptr = 0; + int res = 0; + if ((res = SWIG_AsWCharPtrAndSize(obj, &cptr, (size_t*)(0)))) { + if (val) *val = cptr; + return res; + } + if (val) { + PyErr_SetString(PyExc_TypeError, "a wchar_t * is expected"); + } + return 0; +} +%} + +%fragment("SWIG_FromWCharPtr","header") %{ +SWIGSTATICINLINE(PyObject *) +SWIG_FromWCharPtr(const wchar_t * cptr) +{ + if (cptr) { + size_t size = wcslen(cptr); + if (size > INT_MAX) { + return SWIG_NewPointerObj(swig_const_cast(cptr,wchar_t *), + SWIG_TypeQuery("wchar_t *"), 0); + } else { + return PyUnicode_FromWideChar(cptr, size); + } + } + Py_INCREF(Py_None); + return Py_None; +} +%} + +%fragment("SWIG_AsNewWCharPtr","header", + fragment="SWIG_AsWCharPtrAndSize") %{ +SWIGSTATIC(int) +SWIG_AsNewWCharPtr(PyObject *obj, wchar_t **val) +{ + wchar_t * cptr = 0; size_t csize = 0; + int res = SWIG_AsWCharPtrAndSize(obj, &cptr, &csize); + if (res) { + if (val) { + if (csize) { + *val = swig_new_array(csize, wchar_t); + memcpy(*val, cptr, (--csize)*sizeof(wchar_t)); + (*val)[csize] = 0; + } else if (cptr) { + *val = swig_new_array(1, wchar_t); + (*val)[0] = 0; + } else { + *val = 0; + } + } + return SWIG_NEWOBJ; + } + if (val) { + PyErr_SetString(PyExc_TypeError, "a wchar_t * is expected"); + } + return 0; +} +%} + +%fragment("SWIG_AsWCharArray","header", + fragment="SWIG_AsWCharPtrAndSize") %{ +SWIGSTATIC(int) +SWIG_AsWCharArray(PyObject *obj, wchar_t *val, size_t size) +{ + wchar_t * cptr; size_t csize; + if (SWIG_AsWCharPtrAndSize(obj, &cptr, &csize)) { + if ((csize == size + 1) && !(cptr[csize-1])) --csize; + if (csize <= size) { + if (val) { + if (csize) memcpy(val, cptr, csize*sizeof(wchar_t)); + if (csize < size) memset(val+csize, 0, (size-csize)*sizeof(wchar_t)); + } + return 1; + } + } + if (val) { + PyErr_Format(PyExc_TypeError, + "a wchar_t array of maximum size %d is expected", + size); + } + return 0; +} +%} + +%fragment("SWIG_FromWCharArray","header") %{ +SWIGSTATICINLINE(PyObject *) +SWIG_FromWCharArray(const wchar_t * carray, size_t size) +{ + if (size > INT_MAX) { + SWIG_NewPointerObj(swig_const_cast(carray,wchar_t *), SWIG_TypeQuery("wchar_t *"), 0); + return Py_None; + } else { + return PyUnicode_FromWideChar(carray, swig_numeric_cast(size,int)); + } +} +%} + +/* ------------------------------------------------------------ + * The plain wchar_t * handling + * ------------------------------------------------------------ */ + +/* in */ + +%typemap(in,fragment="SWIG_AsWCharPtr") + wchar_t *, wchar_t const*, wchar_t *const, wchar_t const *const + "if (!SWIG_AsWCharPtr($input, (wchar_t **)&$1)) SWIG_fail;"; + +%typemap(in,fragment="SWIG_AsWCharPtr") + wchar_t const*&, wchar_t *const&, wchar_t const *const & +{ + $*ltype temp; + if (!SWIG_AsWCharPtr($input, (wchar_t **)&temp)) SWIG_fail; + $1 = &temp; +} + +/* out */ + +%typemap(out,fragment="SWIG_FromWCharPtr") + wchar_t *, wchar_t const*, wchar_t *const, wchar_t const *const + "$result = SWIG_FromWCharPtr($1);"; + +%typemap(out,fragment="SWIG_FromWCharPtr") + wchar_t *const &, wchar_t const* &, wchar_t const *const & + "$result = SWIG_FromWCharPtr(*$1);"; + +/* varin */ + +%typemap(varin,fragment="SWIG_AsNewWCharPtr") wchar_t * +{ + wchar_t *cptr = 0; + if (!SWIG_AsNewWCharPtr($input, &cptr)) { + PyErr_Clear(); + PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'"); + return 1; + } + if ($1) swig_delete_array($1); + $1 = cptr; +} + +%typemap(varin,fragment="SWIG_AsNewWCharPtr", + warning="451:Setting const wchar_t * variable may leak memory") + const wchar_t * +{ + wchar_t *cptr; + if (!SWIG_AsNewWCharPtr($input, &cptr)) { + PyErr_Clear(); + PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'"); + return 1; + } + $1 = cptr; +} + +/* varout */ + +%typemap(varout,fragment="SWIG_FromWCharPtr") + wchar_t *, wchar_t const*, wchar_t *const, wchar_t const *const + "$result = SWIG_FromWCharPtr($1);"; + +/* constant */ + +%typemap(constcode,fragment="SWIG_FromWCharPtr") + wchar_t *, wchar_t const*, wchar_t * const, wchar_t const* const + "PyDict_SetItemString(d,\"$symname\", SWIG_FromWCharPtr($value));"; + +/* directorin */ + +%typemap(directorin,fragment="SWIG_FromWCharPtr") + wchar_t *, wchar_t const*, wchar_t *const, wchar_t const *const, + wchar_t const *&, wchar_t *const &, wchar_t const *const & + "$input = SWIG_NewPointerObj((wchar_t *)($1_name), $descriptor(wchar_t *), 0);" + /* "$input = SWIG_FromWCharPtr($1_name);"; */ + + +/* directorout */ + +%typemap(directorout,fragment="SWIG_AsWCharPtr") + wchar_t *, wchar_t const*, wchar_t *const, wchar_t const* const + "if (!SWIG_AsWCharPtr($input, (wchar_t **) &$result)) { + Swig::DirectorTypeMismatchException(\"Error converting Python object into wchar_t *\"); + }"; + +%typemap(directorout,fragment="SWIG_AsWCharPtr") + wchar_t const *&, wchar_t *const &, wchar_t const *const & +{ + wchar_t * temp; + if (!SWIG_AsWCharPtr($input, &temp)) { + Swig::DirectorTypeMismatchException("Error converting Python object into wchar_t *"); + } + $result = ($1_ltype) &temp; +} + +/* typecheck */ + +%typemap(typecheck,precedence=SWIG_TYPECHECK_STRING, + fragment="SWIG_AsWCharPtr") + wchar_t *, wchar_t const*, wchar_t *const, wchar_t const *const, + wchar_t const*&, wchar_t *const&, wchar_t const *const & + "$1 = SWIG_AsWCharPtr($input, (wchar_t **)(0));"; + +/* throws */ + +%typemap(throws,fragment="SWIG_FromWCharPtr") + wchar_t *, wchar_t const*, wchar_t * const, wchar_t const* const +{ + PyErr_SetObject(PyExc_RuntimeError, SWIG_FromWCharPtr($1)); + SWIG_fail; +} + + +/* ------------------------------------------------------------ + * Fix size wcharacter array wchar_t[ANY] handling + * ------------------------------------------------------------ */ + +/* memberin and globalin typemaps */ + +%typemap(memberin) wchar_t [ANY] +{ + if ($input) memcpy($1,$input,$1_dim0*sizeof(wchar_t)); + else memset($1,0,$1_dim0*sizeof(wchar_t)); +} + +%typemap(globalin) wchar_t [ANY] +{ + if ($input) memcpy($1,$input,$1_dim0*sizeof(wchar_t)); + else memset($1,0,$1_dim0*sizeof(wchar_t)); +} + +/* in */ + +%typemap(in,fragment="SWIG_AsWCharArray") + wchar_t [ANY], const wchar_t [ANY] +{ + wchar_t temp[$1_dim0]; + if (!SWIG_AsWCharArray($input, temp, $1_dim0)) SWIG_fail; + $1 = temp; +} + +/* out */ + +%typemap(out,fragment="SWIG_FromWCharArray") + wchar_t [ANY], const wchar_t [ANY] + "$result = SWIG_FromWCharArray($1, $1_dim0);"; + +/* varin */ + +%typemap(varin,fragment="SWIG_AsWCharArray") + wchar_t [ANY] +{ + if (!SWIG_AsWCharArray($input, $1, $1_dim0)) { + PyErr_Clear(); + PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'"); + return 1; + } +} + +/* varout */ + +%typemap(varout,fragment="SWIG_FromWCharArray") + wchar_t [ANY], const wchar_t [ANY] + "$result = SWIG_FromWCharArray($1, $1_dim0);"; + + +/* constants */ + +%typemap(constcode,fragment="SWIG_FromWCharArray") + wchar_t [ANY], const wchar_t [ANY] + "PyDict_SetItemString(d,\"$symname\", SWIG_FromWCharArray($value, $value_dim0));"; + +/* directorin */ + +%typemap(directorin,fragment="SWIG_FromWCharArray") + wchar_t [ANY], const wchar_t [ANY] + "$input = SWIG_FromWCharArray($1_name, $1_dim0);"; + +/* directorout */ + +%typemap(directorout,fragment="SWIG_AsWCharArray") + wchar_t [ANY], const wchar_t [ANY] (wchar_t temp[$result_dim0]) +{ + if (!SWIG_AsWCharArray($input, temp, $result_dim0)) { + Swig::DirectorTypeMismatchException("Error converting Python object into wchar_t[$result_dim0]"); + } + $result = temp; +} + +/* typecheck */ + +%typemap(typecheck,precedence=SWIG_TYPECHECK_STRING, + fragment="SWIG_AsWCharArray") + wchar_t [ANY], const wchar_t[ANY] +{ + return SWIG_AsWCharArray($input, (wchar_t **)0, (size_t *)0); +} + +/* throw */ + +%typemap(throws,fragment="SWIG_FromWCharArray") + wchar_t[ANY], const wchar_t[ANY] { + PyErr_SetObject(PyExc_RuntimeError, SWIG_FromWCharArray($1,$1_dim0)); + SWIG_fail; +} + +/* ------------------------------------------------------------ + * --- String & length --- + * ------------------------------------------------------------ */ + +/* Here len doesn't include the '0' terminator */ +%typemap(in, fragment="SWIG_AsWCharPtrAndSize") + (wchar_t *STRING, int LENGTH) (wchar_t *buf, size_t size) +{ + int res = SWIG_AsWCharPtrAndSize($input, &buf, &size); + if (!res) SWIG_fail; + $1 = ($1_ltype) buf; + $2 = ($2_ltype) size - 1; +} + +/* Here size includes the '0' terminator */ +%typemap(in,fragment="SWIG_AsWCharPtrAndSize") + (wchar_t *STRING, int SIZE) (wchar_t *buf, size_t size) +{ + if (!SWIG_AsWCharPtrAndSize($input, &buf, &size)) SWIG_fail; + $1 = ($1_ltype) buf; + $2 = ($2_ltype) size; +} diff --git a/SWIG/Lib/python/std_basic_string.i b/SWIG/Lib/python/std_basic_string.i new file mode 100644 index 000000000..56223ed28 --- /dev/null +++ b/SWIG/Lib/python/std_basic_string.i @@ -0,0 +1,251 @@ +%include exception.i +%include std_container.i + +%{ +#include +%} + +namespace std { + template + class basic_string + { +#if !defined(SWIG_STD_MODERN_STL) || defined(SWIG_STD_NOMODERN_STL) + %ignore push_back; + %ignore clear; + %ignore compare; + %ignore append; +#endif + + public: + typedef size_t size_type; + typedef ptrdiff_t difference_type; + typedef _CharT value_type; + typedef value_type reference; + typedef value_type const_reference; + + static const size_type npos; + + basic_string(const _CharT* __s, size_type __n); + + // Capacity: + + size_type length() const; + + size_type max_size() const; + + size_type capacity() const; + + void reserve(size_type __res_arg = 0); + + + // Modifiers: + + basic_string& + append(const basic_string& __str); + + basic_string& + append(const basic_string& __str, size_type __pos, size_type __n); + + basic_string& + append(const _CharT* __s, size_type __n); + + basic_string& + append(size_type __n, _CharT __c); + + basic_string& + assign(const basic_string& __str); + + basic_string& + assign(const basic_string& __str, size_type __pos, size_type __n); + + basic_string& + assign(const _CharT* __s, size_type __n); + + basic_string& + insert(size_type __pos1, const basic_string& __str); + + basic_string& + insert(size_type __pos1, const basic_string& __str, + size_type __pos2, size_type __n); + + basic_string& + insert(size_type __pos, const _CharT* __s, size_type __n); + + basic_string& + insert(size_type __pos, size_type __n, _CharT __c); + + basic_string& + erase(size_type __pos = 0, size_type __n = npos); + + basic_string& + replace(size_type __pos, size_type __n, const basic_string& __str); + + basic_string& + replace(size_type __pos1, size_type __n1, const basic_string& __str, + size_type __pos2, size_type __n2); + + basic_string& + replace(size_type __pos, size_type __n1, const _CharT* __s, + size_type __n2); + + basic_string& + replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c); + + + size_type + copy(_CharT* __s, size_type __n, size_type __pos = 0) const; + + // String operations: + const _CharT* c_str() const; + + size_type + find(const _CharT* __s, size_type __pos, size_type __n) const; + + size_type + find(const basic_string& __str, size_type __pos = 0) const; + + size_type + find(_CharT __c, size_type __pos = 0) const; + + size_type + rfind(const basic_string& __str, size_type __pos = npos) const; + + size_type + rfind(const _CharT* __s, size_type __pos, size_type __n) const; + + size_type + rfind(_CharT __c, size_type __pos = npos) const; + + size_type + find_first_of(const basic_string& __str, size_type __pos = 0) const; + + size_type + find_first_of(const _CharT* __s, size_type __pos, size_type __n) const; + + size_type + find_first_of(_CharT __c, size_type __pos = 0) const; + + size_type + find_last_of(const basic_string& __str, size_type __pos = npos) const; + + size_type + find_last_of(const _CharT* __s, size_type __pos, size_type __n) const; + + size_type + find_last_of(_CharT __c, size_type __pos = npos) const; + + size_type + find_first_not_of(const basic_string& __str, size_type __pos = 0) const; + + size_type + find_first_not_of(const _CharT* __s, size_type __pos, + size_type __n) const; + + size_type + find_first_not_of(_CharT __c, size_type __pos = 0) const; + + size_type + find_last_not_of(const basic_string& __str, size_type __pos = npos) const; + + size_type + find_last_not_of(const _CharT* __s, size_type __pos, + size_type __n) const; + + size_type + find_last_not_of(_CharT __c, size_type __pos = npos) const; + + basic_string + substr(size_type __pos = 0, size_type __n = npos) const; + + int + compare(const basic_string& __str) const; + + int + compare(size_type __pos, size_type __n, const basic_string& __str) const; + + int + compare(size_type __pos1, size_type __n1, const basic_string& __str, + size_type __pos2, size_type __n2) const; + + + %ignore pop_back(); + %ignore front() const; + %ignore back() const; + %ignore basic_string(size_type n); + %std_sequence_methods_val(basic_string); + + + %ignore pop(); + %pysequence_methods_val(std::basic_string<_CharT>); + + #ifdef SWIG_EXPORT_ITERATOR_METHODS + iterator + insert(iterator __p, _CharT __c = _CharT()); + + iterator + erase(iterator __position); + + iterator + erase(iterator __first, iterator __last); + + void + insert(iterator __p, size_type __n, _CharT __c); + + basic_string& + replace(iterator __i1, iterator __i2, const basic_string& __str); + + basic_string& + replace(iterator __i1, iterator __i2, + const _CharT* __s, size_type __n); + + basic_string& + replace(iterator __i1, iterator __i2, const _CharT* __s); + + basic_string& + replace(iterator __i1, iterator __i2, size_type __n, _CharT __c); + + basic_string& + replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2); + + basic_string& + replace(iterator __i1, iterator __i2, const _CharT* __k1, const _CharT* __k2); + + basic_string& + replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2); + + basic_string& + replace(iterator __i1, iterator __i2, const_iterator __k1, const_iterator __k2); + #endif + + + %newobject __iadd__; + %newobject __add__; + %newobject __radd__; + %extend { + /* + swig workaround. if used as expected, __iadd__ deletes 'self'. + */ + std::basic_string<_CharT>* __iadd__(const basic_string& v) { + *self += v; + return new std::basic_string<_CharT>(*self); + } + + std::basic_string<_CharT>* __add__(const basic_string& v) { + std::basic_string<_CharT>* res = new std::basic_string<_CharT>(*self); + *res += v; + return res; + } + + std::basic_string<_CharT>* __radd__(const basic_string& v) { + std::basic_string<_CharT>* res = new std::basic_string<_CharT>(v); + *res += *self; + return res; + } + + const std::basic_string<_CharT>& __str__() { + return *self; + } + } + }; + +} diff --git a/SWIG/Lib/python/std_string.i b/SWIG/Lib/python/std_string.i index ba9ce8d79..b42c33bf0 100644 --- a/SWIG/Lib/python/std_string.i +++ b/SWIG/Lib/python/std_string.i @@ -12,264 +12,15 @@ // However, I think I'll wait until someone asks for it... // ------------------------------------------------------------------------ -// Use the following macro with modern STL implementations -//#define SWIG_STD_STRING_MODERN +%include pystrings.swg +%include std_basic_string.i +/* plain strings */ -%include exception.i -%include std_container.i - -%{ -#include -%} - -namespace std { - template - class basic_string - { -#ifndef SWIG_STD_STRING_MODERN - %ignore push_back; - %ignore clear; - %ignore compare; - %ignore append; -#endif - - public: - typedef size_t size_type; - typedef ptrdiff_t difference_type; - typedef _CharT value_type; - typedef value_type reference; - typedef value_type const_reference; - - static const size_type npos; - - basic_string(const _CharT* __s, size_type __n); - - // Capacity: - - size_type length() const; - - size_type max_size() const; - - size_type capacity() const; - - void reserve(size_type __res_arg = 0); - - - // Modifiers: - - basic_string& - append(const basic_string& __str); - - basic_string& - append(const basic_string& __str, size_type __pos, size_type __n); - - basic_string& - append(const _CharT* __s, size_type __n); - - basic_string& - append(size_type __n, _CharT __c); - - basic_string& - assign(const basic_string& __str); - - basic_string& - assign(const basic_string& __str, size_type __pos, size_type __n); - - basic_string& - assign(const _CharT* __s, size_type __n); - - basic_string& - insert(size_type __pos1, const basic_string& __str); - - basic_string& - insert(size_type __pos1, const basic_string& __str, - size_type __pos2, size_type __n); - - basic_string& - insert(size_type __pos, const _CharT* __s, size_type __n); - - basic_string& - insert(size_type __pos, size_type __n, _CharT __c); - - basic_string& - erase(size_type __pos = 0, size_type __n = npos); - - basic_string& - replace(size_type __pos, size_type __n, const basic_string& __str); - - basic_string& - replace(size_type __pos1, size_type __n1, const basic_string& __str, - size_type __pos2, size_type __n2); - - basic_string& - replace(size_type __pos, size_type __n1, const _CharT* __s, - size_type __n2); - - basic_string& - replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c); - - - size_type - copy(_CharT* __s, size_type __n, size_type __pos = 0) const; - - // String operations: - const _CharT* c_str() const; - - size_type - find(const _CharT* __s, size_type __pos, size_type __n) const; - - size_type - find(const basic_string& __str, size_type __pos = 0) const; - - size_type - find(_CharT __c, size_type __pos = 0) const; - - size_type - rfind(const basic_string& __str, size_type __pos = npos) const; - - size_type - rfind(const _CharT* __s, size_type __pos, size_type __n) const; - - size_type - rfind(_CharT __c, size_type __pos = npos) const; - - size_type - find_first_of(const basic_string& __str, size_type __pos = 0) const; - - size_type - find_first_of(const _CharT* __s, size_type __pos, size_type __n) const; - - size_type - find_first_of(_CharT __c, size_type __pos = 0) const; - - size_type - find_last_of(const basic_string& __str, size_type __pos = npos) const; - - size_type - find_last_of(const _CharT* __s, size_type __pos, size_type __n) const; - - size_type - find_last_of(_CharT __c, size_type __pos = npos) const; - - size_type - find_first_not_of(const basic_string& __str, size_type __pos = 0) const; - - size_type - find_first_not_of(const _CharT* __s, size_type __pos, - size_type __n) const; - - size_type - find_first_not_of(_CharT __c, size_type __pos = 0) const; - - size_type - find_last_not_of(const basic_string& __str, size_type __pos = npos) const; - - size_type - find_last_not_of(const _CharT* __s, size_type __pos, - size_type __n) const; - - size_type - find_last_not_of(_CharT __c, size_type __pos = npos) const; - - basic_string - substr(size_type __pos = 0, size_type __n = npos) const; - - int - compare(const basic_string& __str) const; - - int - compare(size_type __pos, size_type __n, const basic_string& __str) const; - - int - compare(size_type __pos1, size_type __n1, const basic_string& __str, - size_type __pos2, size_type __n2) const; - - - %ignore pop_back(); - %ignore front() const; - %ignore back() const; - %ignore basic_string(size_type n); - %std_sequence_methods_val(basic_string); - - - %ignore pop(); - %pysequence_methods_val(std::basic_string<_CharT>); - - #ifdef SWIG_EXPORT_ITERATOR_METHODS - iterator - insert(iterator __p, _CharT __c = _CharT()); - - iterator - erase(iterator __position); - - iterator - erase(iterator __first, iterator __last); - - void - insert(iterator __p, size_type __n, _CharT __c); - - basic_string& - replace(iterator __i1, iterator __i2, const basic_string& __str); - - basic_string& - replace(iterator __i1, iterator __i2, - const _CharT* __s, size_type __n); - - basic_string& - replace(iterator __i1, iterator __i2, const _CharT* __s); - - basic_string& - replace(iterator __i1, iterator __i2, size_type __n, _CharT __c); - - basic_string& - replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2); - - basic_string& - replace(iterator __i1, iterator __i2, const _CharT* __k1, const _CharT* __k2); - - basic_string& - replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2); - - basic_string& - replace(iterator __i1, iterator __i2, const_iterator __k1, const_iterator __k2); - #endif - }; - - %newobject basic_string::__iadd__; - %newobject basic_string::__add__; - %newobject basic_string::__radd__; - %extend basic_string { - /* - swig workaround. if used as expected, __iadd__ deletes 'self'. - */ - std::string* __iadd__(const std::string& v) { - *self += v; - return new std::string(*self); - } - - std::string* __add__(const std::string& v) { - std::string* res = new std::string(*self); - *res += v; - return res; - } - - std::string* __radd__(const std::string& v) { - std::string* res = new std::string(v); - *res += *self; - return res; - } - - const std::string& __str__() { - return *self; - } - } - - %std_equal_methods(basic_string); - %std_order_methods(basic_string); - +namespace std +{ + %std_comp_methods(basic_string); typedef basic_string string; - } /* defining the std::string asptr/from methods */ @@ -346,4 +97,3 @@ SWIGSTATICINLINE(int) %typemap_asptrfromn(SWIG_CCode(STRING), std::basic_string); %typemap_asptrfromn(SWIG_CCode(STRING), std::string); - diff --git a/SWIG/Lib/python/std_wstring.i b/SWIG/Lib/python/std_wstring.i new file mode 100644 index 000000000..3603d3565 --- /dev/null +++ b/SWIG/Lib/python/std_wstring.i @@ -0,0 +1,86 @@ +%include pywstrings.swg +%include std_basic_string.i + +/* wide strings */ + +namespace std +{ + %std_comp_methods(basic_string); + typedef basic_string wstring; +} + +/* defining the std::string asptr/from methods */ + +%fragment(SWIG_AsPtr_frag(std::basic_string),"header", + fragment="SWIG_AsWCharPtrAndSize") { +SWIGSTATICINLINE(int) + SWIG_AsPtr(std::basic_string)(PyObject* obj, std::wstring **val) + { + static swig_type_info* string_info = SWIG_TypeQuery("std::wstring *"); + std::wstring *vptr; + if (SWIG_ConvertPtr(obj, (void**)&vptr, string_info, 0) != -1) { + if (val) *val = vptr; + return SWIG_OLDOBJ; + } else { + PyErr_Clear(); + wchar_t *buf = 0 ; size_t size = 0; + int res = 0; + if ((res = SWIG_AsWCharPtrAndSize(obj, &buf, &size))) { + if (buf) { + if (val) *val = new std::wstring(buf, size - 1); + if (res == SWIG_NEWOBJ) swig_delete_array(buf); + return SWIG_NEWOBJ; + } + } else { + PyErr_Clear(); + } + if (val) { + PyErr_SetString(PyExc_TypeError,"a wstring is expected"); + } + return 0; + } + } + +SWIGSTATICINLINE(int) + SWIG_AsPtr(std::wstring)(PyObject* obj, std::wstring **val) + { + return SWIG_AsPtr(std::basic_string)(obj, val); + } +} + +%fragment(SWIG_From_frag(std::basic_string),"header", + fragment="SWIG_FromWCharArray") { +SWIGSTATICINLINE(PyObject*) + SWIG_From(std::basic_string)(const std::wstring& s) + { + return SWIG_FromWCharArray(s.data(), s.size()); + } +SWIGSTATICINLINE(PyObject*) + SWIG_From(std::wstring)(const std::wstring& s) + { + return SWIG_From(std::basic_string)(s); + } +} + + +%fragment(SWIG_AsVal_frag(std::wstring),"header", + fragment=SWIG_AsPtr_frag(std::basic_string)) { +SWIGSTATICINLINE(int) + SWIG_AsVal(std::wstring)(PyObject* obj, std::wstring *val) + { + std::wstring *s; + int res = SWIG_AsPtr(std::basic_string)(obj, &s); + if (res && s) { + if (val) *val = *s; + if (res == SWIG_NEWOBJ) delete s; + return res; + } + if (val) { + PyErr_SetString(PyExc_TypeError,"a wstring is expected"); + } + return 0; + } +} + +%typemap_asptrfromn(SWIG_CCode(STRING), std::basic_string); +%typemap_asptrfromn(SWIG_CCode(STRING), std::wstring);