diff options
Diffstat (limited to 'sip/tqt/tqstring.sip')
-rw-r--r-- | sip/tqt/tqstring.sip | 946 |
1 files changed, 946 insertions, 0 deletions
diff --git a/sip/tqt/tqstring.sip b/sip/tqt/tqstring.sip new file mode 100644 index 0000000..116140e --- /dev/null +++ b/sip/tqt/tqstring.sip @@ -0,0 +1,946 @@ +// This is the SIP interface definition for TQChar and TQString. +// +// Copyright (c) 2007 +// Riverbank Computing Limited <info@riverbankcomputing.co.uk> +// +// This file is part of PyTQt. +// +// This copy of PyTQt is free software; you can redistribute it and/or modify it +// under the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 2, or (at your option) any later +// version. +// +// PyTQt is supplied in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +// details. +// +// You should have received a copy of the GNU General Public License along with +// PyTQt; see the file LICENSE. If not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + +%ExportedDoc +<Sect2><Title>TQChar (TQt v2+)</Title> +<FuncSynopsis> + <FuncDef>uchar &<Function>cell</Function> const</FuncDef> + <ParamDef></ParamDef> +</FuncSynopsis> +<Para> +Not implemented. +</Para> + +<FuncSynopsis> + <FuncDef>uchar &<Function>row</Function> const</FuncDef> + <ParamDef></ParamDef> +</FuncSynopsis> +<Para> +Not implemented. +</Para> +</Sect2> + +<Sect2><Title>TQString</Title> +<Para> +A Python string object (or Unicode object) can be used whenever a +<Literal>TQString</Literal> can be used. A <Literal>TQString</Literal> can be +converted to a Python string object using the Python <Literal>str()</Literal> +function, and to a Python Unicode object using the Python +<Literal>unicode()</Literal> function. +</Para> + +<Para> +The Python <Literal>+</Literal>, <Literal>+=</Literal>, <Literal>*</Literal>, +<Literal>*=</Literal>, <Literal>len</Literal>, <Literal>[]</Literal> +(for reading slices and individual characters), <Literal>in</Literal> and +comparison operators are supported. +</Para> + +<FuncSynopsis> + <FuncDef>TQCharRef <Function>at</Function></FuncDef> + <ParamDef>uint <Parameter>i</Parameter></ParamDef> +</FuncSynopsis> +<Para> +Not yet implemented. (TQt v2+) +</Para> + +<FuncSynopsis> + <FuncDef>TQChar <Function>constref</Function> const</FuncDef> + <ParamDef>uint <Parameter>i</Parameter></ParamDef> +</FuncSynopsis> +<Para> +Not yet implemented. (TQt v2+) +</Para> + +<FuncSynopsis> + <FuncDef>TQChar &<Function>ref</Function></FuncDef> + <ParamDef>uint <Parameter>i</Parameter></ParamDef> +</FuncSynopsis> +<Para> +Not yet implemented. (TQt v2+) +</Para> + +<FuncSynopsis> + <FuncDef>TQString &<Function>setUnicodeCodes</Function></FuncDef> + <ParamDef>const ushort *<Parameter>unicode_as_shorts</Parameter></ParamDef> + <ParamDef>uint <Parameter>len</Parameter></ParamDef> +</FuncSynopsis> +<Para> +Not yet implemented. (TQt v2.1+) +</Para> + +<FuncSynopsis> + <FuncDef>TQString &<Function>sprintf</Function></FuncDef> + <ParamDef>const char *<Parameter>format</Parameter></ParamDef> + <ParamDef>...</ParamDef> +</FuncSynopsis> +<Para> +Not implemented. +</Para> + +<FuncSynopsis> + <FuncDef>short <Function>toShort</Function></FuncDef> + <ParamDef>bool *<Parameter>ok</Parameter> = 0</ParamDef> +</FuncSynopsis> +<Para> +This returns a tuple of the <Literal>short</Literal> result and the +<Literal>ok</Literal> value. +</Para> + +<FuncSynopsis> + <FuncDef>ushort <Function>toUShort</Function></FuncDef> + <ParamDef>bool *<Parameter>ok</Parameter> = 0</ParamDef> +</FuncSynopsis> +<Para> +This returns a tuple of the <Literal>ushort</Literal> result and the +<Literal>ok</Literal> value. +</Para> + +<FuncSynopsis> + <FuncDef>int <Function>toInt</Function></FuncDef> + <ParamDef>bool *<Parameter>ok</Parameter> = 0</ParamDef> +</FuncSynopsis> +<Para> +This returns a tuple of the <Literal>int</Literal> result and the +<Literal>ok</Literal> value. +</Para> + +<FuncSynopsis> + <FuncDef>uint <Function>toUInt</Function></FuncDef> + <ParamDef>bool *<Parameter>ok</Parameter> = 0</ParamDef> +</FuncSynopsis> +<Para> +This returns a tuple of the <Literal>uint</Literal> result and the +<Literal>ok</Literal> value. +</Para> + +<FuncSynopsis> + <FuncDef>long <Function>toLong</Function></FuncDef> + <ParamDef>bool *<Parameter>ok</Parameter> = 0</ParamDef> +</FuncSynopsis> +<Para> +This returns a tuple of the <Literal>long</Literal> result and the +<Literal>ok</Literal> value. +</Para> + +<FuncSynopsis> + <FuncDef>ulong <Function>toULong</Function></FuncDef> + <ParamDef>bool *<Parameter>ok</Parameter> = 0</ParamDef> +</FuncSynopsis> +<Para> +This returns a tuple of the <Literal>ulong</Literal> result and the +<Literal>ok</Literal> value. +</Para> + +<FuncSynopsis> + <FuncDef>float <Function>toFloat</Function></FuncDef> + <ParamDef>bool *<Parameter>ok</Parameter> = 0</ParamDef> +</FuncSynopsis> +<Para> +This returns a tuple of the <Literal>float</Literal> result and the +<Literal>ok</Literal> value. +</Para> + +<FuncSynopsis> + <FuncDef>double <Function>toDouble</Function></FuncDef> + <ParamDef>bool *<Parameter>ok</Parameter> = 0</ParamDef> +</FuncSynopsis> +<Para> +This returns a tuple of the <Literal>double</Literal> result and the +<Literal>ok</Literal> value. +</Para> +</Sect2> +%End + + +class TQChar +{ +%TypeHeaderCode +#include <tqstring.h> +%End + +public: + TQChar(); + TQChar(char); +// TQChar(uchar); + TQChar(uchar,uchar); + TQChar(const TQChar &); +// TQChar(ushort); +// TQChar(short); +// TQChar(uint); + TQChar(int); + + static const TQChar null; + static const TQChar replacement; + static const TQChar byteOrderMark; + static const TQChar byteOrderSwapped; + static const TQChar nbsp; + + enum Category { + NoCategory, + + Mark_NonSpacing, + Mark_SpacingCombining, + Mark_Enclosing, + + Number_DecimalDigit, + Number_Letter, + Number_Other, + + Separator_Space, + Separator_Line, + Separator_Paragraph, + + Other_Control, + Other_Format, + Other_Surrogate, + Other_PrivateUse, + Other_NotAssigned, + + Letter_Uppercase, + Letter_Lowercase, + Letter_Titlecase, + Letter_Modifier, + Letter_Other, + + Punctuation_Connector, + Punctuation_Dash, + Punctuation_Open, + Punctuation_Close, + Punctuation_InitialQuote, + Punctuation_FinalQuote, + Punctuation_Other, + + Symbol_Math, + Symbol_Currency, + Symbol_Modifier, + Symbol_Other + }; + + enum Direction { + DirL, + DirR, + DirEN, + DirES, + DirET, + DirAN, + DirCS, + DirB, + DirS, + DirWS, + DirON, + DirLRE, + DirLRO, + DirAL, + DirRLE, + DirRLO, + DirPDF, + DirNSM, + DirBN + }; + + enum Decomposition { + Single, + Canonical, + Font, + NoBreak, + Initial, + Medial, + Final, + Isolated, + Circle, + Super, + Sub, + Vertical, + Wide, + Narrow, + Small, + Square, + Compat, + Fraction + }; + + enum Joining { + OtherJoining, + Dual, + Right, + Center + }; + + enum CombiningClass { + Combining_BelowLeftAttached, + Combining_BelowAttached, + Combining_BelowRightAttached, + Combining_LeftAttached, + Combining_RightAttached, + Combining_AboveLeftAttached, + Combining_AboveAttached, + Combining_AboveRightAttached, + + Combining_BelowLeft, + Combining_Below, + Combining_BelowRight, + Combining_Left, + Combining_Right, + Combining_AboveLeft, + Combining_Above, + Combining_AboveRight, + + Combining_DoubleBelow, + Combining_DoubleAbove, + Combining_IotaSubscript + }; + + int digitValue() const; + TQChar lower() const; + TQChar upper() const; + + Category category() const; + Direction direction() const; + Joining joining() const; + bool mirrored() const; + TQChar mirroredChar() const; + const TQString &decomposition() const; + Decomposition decompositionTag() const; + unsigned char combiningClass() const; + + char latin1() const; + ushort unicode() const; + + bool isNull() const; + bool isPrint() const; + bool isPunct() const; + bool isSpace() const; + bool isMark() const; + bool isLetter() const; + bool isNumber() const; + bool isLetterOrNumber() const; + bool isDigit() const; + bool isSymbol() const; + +// uchar& cell(); +// uchar& row(); + uchar cell() const; + uchar row() const; + void setCell(uchar); + void setRow(uchar); + + static bool networkOrdered(); +}; + +bool operator==(TQChar,char); +bool operator==(TQChar,TQChar); +bool operator!=(TQChar,TQChar); +bool operator!=(TQChar,char); +bool operator<=(TQChar,char); +bool operator<=(TQChar,TQChar); +bool operator>=(TQChar,char); +bool operator>=(TQChar,TQChar); +bool operator<(TQChar,char); +bool operator<(TQChar,TQChar); +bool operator>(TQChar,char); +bool operator>(TQChar,TQChar); + + +class TQString +{ +%TypeHeaderCode +#include <tqstring.h> +#include <tqtextcodec.h> +%End + +public: + TQString(); + TQString(TQChar); + TQString(const TQString &); + TQString(const TQByteArray &); +// TQString(const TQChar *,uint); +// TQString(const char *); + + // This is how we implement TQUrl::operator TQString() const. + TQString(const TQUrl &); + + // This is how we implement TQKeySequence::operator TQString() const. + TQString(const TQKeySequence &); + + // This is how we implement TQUuid::operator TQString() const. + TQString(const TQUuid &); + + static const TQString null; + + bool isNull() const; + bool isEmpty() const; + uint length() const; + void truncate(uint); + TQString &fill(TQChar,int = -1); + TQString copy() const; + + TQString arg(int /Constrained/,int = 0,int = 10) const; + TQString arg(double /Constrained/,int = 0,char = 'g',int = -1) const; +// TQString arg(TQ_LLONG,int = 0,int = 10) const; +// TQString arg(TQ_ULLONG,int = 0,int = 10) const; + TQString arg(long,int = 0,int = 10) const; + TQString arg(ulong,int = 0,int = 10) const; +// TQString arg(uint,int = 0,int = 10) const; +// TQString arg(short,int = 0,int = 10) const; +// TQString arg(ushort,int = 0,int = 10) const; + TQString arg(char,int = 0) const; + TQString arg(TQChar,int = 0) const; + TQString arg(const TQString&,int = 0) const; + TQString arg(const TQString &,const TQString &) const; + TQString arg(const TQString &,const TQString &,const TQString &) const; + TQString arg(const TQString &,const TQString &,const TQString &, + const TQString &) const; + +// TQString &sprintf(const char *,...); + + int find(TQChar,int = 0,bool = 1) const; + int find(char,int = 0,bool = 1) const; + int find(const TQString &,int = 0,bool = 1) const; + int find(const TQRegExp &,int = 0) const; +// int find(const char *,int = 0) const; + int findRev(TQChar,int = -1,bool = 1) const; + int findRev(char,int = -1,bool = 1) const; + int findRev(const TQString &,int = -1,bool = 1) const; + int findRev(const TQRegExp &,int = -1) const; +// int findRev(const char *,int = -1) const; + int contains(TQChar,bool = 1) const; + int contains(char,bool = 1) const; +// int contains(const char *,bool = 1) const; + int contains(const TQString &,bool = 1) const; + int contains(const TQRegExp &) const; + + enum SectionFlags { + SectionDefault, + SectionSkipEmpty, + SectionIncludeLeadingSep, + SectionIncludeTrailingSep, + SectionCaseInsensitiveSeps + }; + + TQString section(TQChar,int,int = 0xffffffff,int = SectionDefault) const; +// TQString section(char,int,int = 0xffffffff,int = SectionDefault) const; +// TQString section(const char *,int,int = 0xffffffff, +// int = SectionDefault) const; + TQString section(const TQString &,int,int = 0xffffffff, + int = SectionDefault) const; + TQString section(const TQRegExp &,int,int = 0xffffffff, + int = SectionDefault) const; + + TQString left(uint) const; + TQString right(uint) const; + TQString mid(uint,uint = 0xffffffff) const; + TQString leftJustify(uint,TQChar = ' ',bool = 0) const; + TQString rightJustify(uint,TQChar = ' ',bool = 0) const; + TQString lower() const; + TQString upper() const; + TQString stripWhiteSpace() const; + TQString simplifyWhiteSpace() const; + + TQString &insert(uint,const TQString &); + TQString &insert(uint,const TQByteArray &); +// TQString &insert(uint,const char *); + TQString &insert(uint,TQChar *,uint); + TQString &insert(uint,TQChar); + TQString &insert(uint,char); + + TQString &append(char); + TQString &append(TQChar); + TQString &append(const TQString &); + TQString &prepend(char); + TQString &prepend(TQChar); + TQString &prepend(const TQString &); + TQString &remove(uint,uint); + TQString &remove(const TQString &); + TQString &remove(const TQString &,bool); + TQString &remove(TQChar); + TQString &remove(char); + TQString &remove(const TQRegExp &); + TQString &replace(uint,uint,const TQString &); + TQString &replace(uint,uint,const TQChar *,uint); + TQString &replace(uint,uint,TQChar); + TQString &replace(uint,uint,char); + TQString &replace(TQChar,const TQString &); + TQString &replace(TQChar,const TQString &,bool); + TQString &replace(char,const TQString &); + TQString &replace(char,const TQString &,bool); + TQString &replace(const TQString &,const TQString &); + TQString &replace(const TQString &,const TQString &,bool); + TQString &replace(const TQRegExp &,const TQString &); + TQString &replace(TQChar,TQChar); + + short toShort(bool * = 0,int = 10) const; + ushort toUShort(bool * = 0,int = 10) const; + int toInt(bool * = 0,int = 10) const; + uint toUInt(bool * = 0,int = 10) const; + long toLong(bool * = 0,int = 10) const; + ulong toULong(bool * = 0,int = 10) const; +// TQ_LLONG toLongLong(bool * = 0,int = 10) const; +// TQ_ULLONG toULongLong(bool * = 0,int = 10) const; + float toFloat(bool * = 0) const; + double toDouble(bool * = 0) const; + + TQString &setNum(int /Constrained/,int = 10); + TQString &setNum(double /Constrained/,char = 'g',int = 6); +// TQString &setNum(short,int = 10); +// TQString &setNum(ushort,int = 10); +// TQString &setNum(uint,int = 10); + TQString &setNum(long,int = 10); + TQString &setNum(ulong,int = 10); +// TQString &setNum(TQ_LLONG,int = 10); +// TQString &setNum(TQ_ULLONG,int = 10); +// TQString &setNum(float,char = 'g',int = 6); + + static TQString number(int /Constrained/,int = 10); + static TQString number(double /Constrained/,char = 'g',int = 6); + static TQString number(long,int = 10); + static TQString number(ulong,int = 10); +// static TQString number(TQ_LLONG,int = 10); +// static TQString number(TQ_ULLONG,int = 10); +// static TQString number(uint,int = 10); + + void setExpand(uint,TQChar); + + TQString &operator+=(const TQString &); + TQString &operator+=(const TQByteArray &); + TQString &operator+=(TQChar); + //TQString &operator+=(char); + + TQString operator*(int); +%MethodCode + sipRes = new TQString(); + + while (a0-- > 0) + *sipRes += *sipCpp; +%End + + TQString &operator*=(int); +%MethodCode + TQString orig(*sipCpp); + + sipCpp -> truncate(0); + + while (a0-- > 0) + *sipCpp += orig; +%End + + TQChar at(uint) const; +// TQCharRef at(uint); +// TQChar constref(uint) const; +// TQChar &ref(uint); +// const TQChar *unicode() const; + const char *ascii() const; + static TQString fromAscii(const char *,int = -1); + const char *latin1() const; + static TQString fromLatin1(const char *,int = -1); + TQCString utf8() const; + static TQString fromUtf8(const char *,int = -1); + TQCString local8Bit() const; + static TQString fromLocal8Bit(const char *,int = -1); + +// static TQString fromUcs2(const unsigned short *); +// const unsigned short *ucs2() const; + +// TQString &setUnicode(const TQChar *,uint); +// TQString &setUnicodeCodes(const ushort *,uint); + TQString &setAscii(const char *,int = -1); + TQString &setLatin1(const char *,int = -1); + + int compare(const TQString &) const; + static int compare(const TQString &,const TQString &); + + int localeAwareCompare(const TQString &) const; + static int localeAwareCompare(const TQString &,const TQString &); + + void compose(); + bool startsWith(const TQString &) const; + bool startsWith(const TQString &,bool) const; + bool endsWith(const TQString &) const; + bool endsWith(const TQString &,bool) const; + + void setLength(uint); + + uint capacity() const; + void reserve(uint); + void squeeze(); + + bool simpleText() const; + bool isRightToLeft() const; + + // Force the numeric interpretation so that str + TQString gets handled + // as we want. + const TQString operator+(const TQString &) /Numeric/; + + TQString operator[](int) const; +%MethodCode + int len; + + len = sipCpp -> length(); + + if ((a0 = (int)sipConvertFromSequenceIndex(a0,len)) < 0) + sipIsErr = 1; + else + sipRes = new TQString(sipCpp -> at(a0)); +%End + + TQString operator[](SIP_PYSLICE) const; +%MethodCode + Py_ssize_t len, start, stop, step, slicelength, i; + + len = sipCpp -> length(); + + if (sipConvertFromSliceObject(a0,len,&start,&stop,&step,&slicelength) < 0) + sipIsErr = 1; + else + { + sipRes = new TQString(); + + for (i = 0; i < slicelength; ++i) + { + sipRes -> append(sipCpp -> at(start)); + start += step; + } + } +%End + + int __len__() const; +%MethodCode + sipRes = sipCpp -> length(); +%End + + int __contains__(const TQString &) const; +%MethodCode + sipRes = (sipCpp -> contains(*a0) > 0); +%End + + SIP_PYOBJECT __unicode__(); +%MethodCode + sipRes = PyTQt_tqt_PyObject_FromTQString(sipCpp); +%End + + SIP_PYOBJECT __str__(); +%MethodCode + sipRes = PyTQt_tqt_PyObject_FromTQString(sipCpp); +%End + +%ConvertToTypeCode + // Allow a Python string (or a unicode string) whenever a TQString is + // expected. + + if (sipIsErr == NULL) + return (PyBytes_Check(sipPy) || + PyUnicode_Check(sipPy) || + sipCanConvertToInstance(sipPy,sipClass_TQString,SIP_NO_CONVERTORS)); + + *sipCppPtr = PyTQt_tqt_PyObject_AsTQString(sipPy); + + if (*sipCppPtr) + return sipGetState(sipTransferObj); + + *sipCppPtr = reinterpret_cast<TQString *>(sipConvertToInstance(sipPy,sipClass_TQString,sipTransferObj,SIP_NO_CONVERTORS,0,sipIsErr)); + + return 0; +%End + +%BIGetReadBufferCode + if (sipSegment != 0) + { + PyErr_SetString(PyExc_SystemError, "accessing non-existent TQString segment"); + sipRes = -1; + } + else + { + *sipPtrPtr = (void *)sipCpp->ucs2(); + sipRes = sipCpp->length() * sizeof (ushort); + } +%End + +%BIGetSegCountCode + if (sipLenPtr) + *sipLenPtr = sipCpp->length() * sizeof (ushort); + + sipRes = 1; +%End + +%BIGetCharBufferCode + if (sipSegment != 0) + { + PyErr_SetString(PyExc_SystemError, "accessing non-existent TQString segment"); + sipRes = -1; + } + else + { + static TQTextCodec *codec = 0; + static bool check_codec = TRUE; + + // For the first time only, try and find a TQt codec that + // corresponds to the default Python codec. If one wasn't + // found then fall back to ASCII (which is actually the codec + // returned by TQTextCodec::codecForCStrings()). + if (check_codec) + { + codec = TQTextCodec::codecForName(PyUnicode_GetDefaultEncoding()); + check_codec = FALSE; + } + + if (codec) + { + // A Python string is the most convenient method to + // save the encoded version on the heap. + PyObject *encobj = ((sipSimpleWrapper *)sipSelf)->user; + TQCString enc; + + Py_XDECREF(encobj); + + enc = codec->fromUnicode(*sipCpp); + + if ((encobj = PyBytes_FromString(enc.data())) != NULL) + { + *sipPtrPtr = (void *)PyString_AS_STRING(encobj); + sipRes = PyString_GET_SIZE(encobj); + + ((sipSimpleWrapper *)sipSelf)->user = encobj; + } + else + sipRes = -1; + } + else + { + const char *asc = sipCpp->ascii(); + + *sipPtrPtr = (void *)asc; + sipRes = tqstrlen(asc); + } + } +%End +}; + +bool operator==(const TQString &,const TQString &); +bool operator!=(const TQString &,const TQString &); +bool operator<(const TQString &,const TQString &); +bool operator<=(const TQString &,const TQString &); +bool operator>(const TQString &,const TQString &); +bool operator>=(const TQString &,const TQString &); + + +%ModuleHeaderCode +extern PyObject *PyTQt_tqt_PyObject_FromTQString(const TQString *tqstr); +%End + +%ModuleCode +// Convert a TQString to a Python Unicode object. +PyObject *PyTQt_tqt_PyObject_FromTQString(const TQString *tqstr) +{ + PyObject *obj; + +#if PY_VERSION_HEX >= 0x03030000 + // We have to work out exactly which kind to use. We assume ASCII while we + // are checking so that we only go through the string once in the most + // common case. Note that we can't use PyUnicode_FromKindAndData() because + // it doesn't handle surrogates in UCS2 strings. + + int tqt_len = tqstr->length(); + Py_UCS4 maxchar = 0x007f; + + for (int tqt_i = 0; tqt_i < tqt_len && maxchar < 0xffff; ++tqt_i) + { + Py_UCS4 uch = tqstr->at(tqt_i).unicode(); + + if (uch > 0x00ff) + { + maxchar = 0xffff; + } + else if (uch > 0x007f) + { + maxchar = 0x00ff; + } + } + + // Create the correctly sized object. + if ((obj = PyUnicode_New(tqt_len, maxchar)) == NULL) + return NULL; + + int kind = PyUnicode_KIND(obj); + void *data = PyUnicode_DATA(obj); + + for (int tqt_i = 0; tqt_i < tqt_len; ++tqt_i) + { + Py_UCS4 uch = tqstr->at(tqt_i).unicode(); + PyUnicode_WRITE(kind, data, tqt_i, uch); + } +#elif defined(Py_UNICODE_WIDE) + // Note that this doesn't handle code points greater than 0xffff. It could + // but it's only an issue for old versions of TQt. + + if ((obj = PyUnicode_FromUnicode(NULL, tqstr->length())) == NULL) + return NULL; + + Py_UNICODE *pyu = PyUnicode_AS_UNICODE(obj); + + for (unsigned int i = 0; i < tqstr->length(); ++i) + *pyu++ = (tqstr->at(i)).unicode(); +#else + if ((obj = PyUnicode_FromUnicode(NULL, tqstr->length())) == NULL) + return NULL; + + memcpy(PyUnicode_AS_UNICODE(obj), tqstr->ucs2(), + tqstr->length() * sizeof (Py_UNICODE)); +#endif + + return obj; +} +%End + + +%ModuleHeaderCode +extern TQString *PyTQt_tqt_PyObject_AsTQString(PyObject *obj); +%End + +%ModuleCode +// Convert a Python Unicode object to a TQString. +TQString *PyTQt_tqt_PyObject_AsTQString(PyObject *obj) +{ + if (PyUnicode_Check(obj)) + { +#if PY_VERSION_HEX >= 0x03030000 + SIP_SSIZE_T len = PyUnicode_GET_LENGTH(obj); + + switch (PyUnicode_KIND(obj)) + { + case PyUnicode_1BYTE_KIND: + return new TQString(TQString::fromUtf8((char *)PyUnicode_1BYTE_DATA(obj), len)); + + case PyUnicode_2BYTE_KIND: + // The (TQChar *) cast should be safe. + return new TQString((TQChar *)PyUnicode_2BYTE_DATA(obj), len); + + case PyUnicode_4BYTE_KIND: + // Note that this doesn't handle code points greater than 0xffff. It + // could but it's only an issue for old versions of TQt. + + TQString *tqstr = new TQString; + + Py_UCS4 *ucode = PyUnicode_4BYTE_DATA(obj); + + for (SIP_SSIZE_T i = 0; i < len; ++i) + tqstr->append(TQChar((uint)ucode[i])); + + return tqstr; + } + + return NULL; +#else + TQString *tqstr = new TQString; + +# ifdef Py_UNICODE_WIDE + Py_UNICODE *ucode = PyUnicode_AS_UNICODE(obj); + SIP_SSIZE_T len = PyUnicode_GET_SIZE(obj); + + for (SIP_SSIZE_T i = 0; i < len; ++i) + tqstr->append(TQChar((uint)ucode[i])); +# else + tqstr->setUnicodeCodes((ushort *)PyUnicode_AS_UNICODE(obj),PyUnicode_GET_SIZE(obj)); +# endif + + return tqstr; +#endif + } + else if (PyBytes_Check(obj)) + { + return new TQString(PyBytes_AS_STRING(obj)); + } + + return NULL; +} +%End + + +%ModuleHeaderCode +extern const char *PyTQt_tqt_encode(PyObject **s, TQApplication::Encoding encoding); +%End + +%ModuleCode +// Convert a Python unicode/string/bytes object to a character string encoded +// according to the given encoding. Update the object with a new reference to +// the object that owns the data. +const char *PyTQt_tqt_encode(PyObject **s, TQApplication::Encoding encoding) +{ + PyObject *obj = *s; + const char *es = 0; + SIP_SSIZE_T sz; + + if (PyUnicode_Check(obj)) + { + if (encoding == TQApplication::UnicodeUTF8) + { + obj = PyUnicode_AsUTF8String(obj); + } + else + { + TQTextCodec *codec = TQTextCodec::codecForTr(); + + if (codec) + { + // Use the TQt codec to get to a byte string, and then to a + // Python object. + TQString *tqstr = PyTQt_tqt_PyObject_AsTQString(obj); + TQString tqs = *tqstr; + TQByteArray ba = codec->fromUnicode(tqs); + delete tqstr; + + obj = PyBytes_FromStringAndSize(ba.data(), ba.size()); + } + else + { + obj = PyUnicode_AsLatin1String(obj); + } + } + + if (obj) + { + es = PyBytes_AS_STRING(obj); + } + } + else if (PyBytes_Check(obj)) + { + es = PyBytes_AS_STRING(obj); + Py_INCREF(obj); + } + else if (PyObject_AsCharBuffer(obj, &es, &sz) >= 0) + { + Py_INCREF(obj); + } + + if (es) + { + *s = obj; + } + else + { + PyErr_Format(PyExc_UnicodeEncodeError, + "unable to convert '%s' to requested encoding", + Py_TYPE(*s)->tp_name); + } + + return es; +} +%End |