1// qbytearray.sip generated by MetaSIP
2//
3// This file is part of the QtCore Python extension module.
4//
5// Copyright (c) 2021 Riverbank Computing Limited <info@riverbankcomputing.com>
6//
7// This file is part of PyQt5.
8//
9// This file may be used under the terms of the GNU General Public License
10// version 3.0 as published by the Free Software Foundation and appearing in
11// the file LICENSE included in the packaging of this file.  Please review the
12// following information to ensure the GNU General Public License version 3.0
13// requirements will be met: http://www.gnu.org/copyleft/gpl.html.
14//
15// If you do not wish to use this file under the terms of the GPL version 3.0
16// then you may purchase a commercial license.  For more information contact
17// info@riverbankcomputing.com.
18//
19// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
20// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21
22
23%ModuleCode
24#include <qbytearray.h>
25%End
26
27class QByteArray /TypeHintIn="Union[QByteArray, bytes, bytearray]"/
28{
29%TypeHeaderCode
30#include <qbytearray.h>
31%End
32
33%TypeCode
34// This is needed by __hash__().
35#include <qhash.h>
36
37
38// Convenience function for converting a QByteArray to a Python str object.
39static PyObject *QByteArrayToPyStr(QByteArray *ba)
40{
41    char *data = ba->data();
42
43    if (data)
44        // QByteArrays may have embedded '\0's so set the size explicitly.
45        return SIPBytes_FromStringAndSize(data, ba->size());
46
47    return SIPBytes_FromString("");
48}
49%End
50
51%ConvertToTypeCode
52// We have to be very careful about what we allow to be converted to a
53// QByteArray and to a QString as we need to take into account the v1 and v2
54// APIs and Python v2.x and v3.x.
55//
56// QSvgRenderer() is a good example of what needs to work "naturally".  This
57// has a ctor that takes a QString argument that is the name of the SVG file.
58// It has another ctor that takes a QByteArray argument that is the SVG data.
59//
60// In Python v2.x we want a str object to be interpreted as the name of the
61// file (as that is the historical behaviour).  This has the following
62// implications.
63//
64// - The QString version of the ctor must appear before the QByteArray version
65//   in the .sip file.  This rule should be applied wherever a similar
66//   situation arises.
67// - A QString must not automatically convert a QByteArray.
68// - QByteArray must also exist in the v2 API.
69//
70// In Python v3.x we want a bytes object to be used wherever a QByteArray is
71// expected.  This means that a QString must not automatically convert a bytes
72// object.
73//
74// In PyQt v5.4 and earlier a QByteArray could be created from a Latin-1
75// encoded string.  This was a mistaken attempt to ease the porting of Python2
76// code to Python3.
77
78if (sipIsErr == NULL)
79    return (PyByteArray_Check(sipPy) || SIPBytes_Check(sipPy) ||
80            sipCanConvertToType(sipPy, sipType_QByteArray, SIP_NO_CONVERTORS));
81
82if (PyByteArray_Check(sipPy))
83{
84    *sipCppPtr = new QByteArray(PyByteArray_AsString(sipPy),
85            PyByteArray_Size(sipPy));
86
87    return sipGetState(sipTransferObj);
88}
89
90if (SIPBytes_Check(sipPy))
91{
92    *sipCppPtr = new QByteArray(SIPBytes_AsString(sipPy),
93            SIPBytes_Size(sipPy));
94
95    return sipGetState(sipTransferObj);
96}
97
98*sipCppPtr = reinterpret_cast<QByteArray *>(sipConvertToType(sipPy,
99        sipType_QByteArray, sipTransferObj, SIP_NO_CONVERTORS, 0, sipIsErr));
100
101return 0;
102%End
103
104%BIGetBufferCode
105    #if defined(Py_LIMITED_API)
106        Q_UNUSED(sipSelf);
107
108        sipBuffer->bd_buffer = sipCpp->data();
109        sipBuffer->bd_length = sipCpp->size();
110        sipBuffer->bd_readonly = 0;
111        sipRes = 0;
112    #else
113        sipRes = PyBuffer_FillInfo(sipBuffer, sipSelf, sipCpp->data(),
114                sipCpp->size(), 0, sipFlags);
115    #endif
116%End
117
118%BIGetReadBufferCode
119    if (sipSegment != 0)
120    {
121        PyErr_SetString(PyExc_SystemError, "accessing non-existent QByteArray segment");
122        sipRes = -1;
123    }
124    else
125    {
126        *sipPtrPtr = (void *)sipCpp->data();
127        sipRes = sipCpp->size();
128    }
129%End
130
131%BIGetSegCountCode
132    if (sipLenPtr)
133        *sipLenPtr = sipCpp->size();
134
135    sipRes = 1;
136%End
137
138%BIGetCharBufferCode
139    if (sipSegment != 0)
140    {
141        PyErr_SetString(PyExc_SystemError, "accessing non-existent QByteArray segment");
142        sipRes = -1;
143    }
144    else
145    {
146        *sipPtrPtr = (void *)sipCpp->data();
147        sipRes = sipCpp->size();
148    }
149%End
150
151%PickleCode
152    #if PY_MAJOR_VERSION >= 3
153    sipRes = Py_BuildValue((char *)"(y#)", sipCpp->data(), sipCpp->size());
154    #else
155    sipRes = Py_BuildValue((char *)"(s#)", sipCpp->data(), sipCpp->size());
156    #endif
157%End
158
159public:
160    QByteArray();
161    QByteArray(int size, char c);
162    QByteArray(const QByteArray &a);
163    ~QByteArray();
164    void resize(int size);
165    QByteArray &fill(char ch, int size = -1);
166    void clear();
167    int indexOf(const QByteArray &ba, int from = 0) const;
168    int indexOf(const QString &str, int from = 0) const;
169    int lastIndexOf(const QByteArray &ba, int from = -1) const;
170    int lastIndexOf(const QString &str, int from = -1) const;
171    int count(const QByteArray &a) const;
172    QByteArray left(int len) const;
173    QByteArray right(int len) const;
174    QByteArray mid(int pos, int length = -1) const;
175    bool startsWith(const QByteArray &a) const;
176    bool endsWith(const QByteArray &a) const;
177    void truncate(int pos);
178    void chop(int n);
179    QByteArray toLower() const;
180    QByteArray toUpper() const;
181    QByteArray trimmed() const;
182    QByteArray simplified() const;
183    QByteArray leftJustified(int width, char fill = ' ', bool truncate = false) const;
184    QByteArray rightJustified(int width, char fill = ' ', bool truncate = false) const;
185    QByteArray &prepend(const QByteArray &a);
186    QByteArray &append(const QByteArray &a);
187    QByteArray &append(const QString &s);
188    QByteArray &insert(int i, const QByteArray &a);
189    QByteArray &insert(int i, const QString &s);
190    QByteArray &remove(int index, int len);
191    QByteArray &replace(int index, int len, const QByteArray &s);
192    QByteArray &replace(const QByteArray &before, const QByteArray &after);
193    QByteArray &replace(const QString &before, const QByteArray &after);
194    QList<QByteArray> split(char sep) const;
195    QByteArray &operator+=(const QByteArray &a);
196    QByteArray &operator+=(const QString &s);
197    bool operator==(const QString &s2) const;
198    bool operator!=(const QString &s2) const;
199    bool operator<(const QString &s2) const;
200    bool operator>(const QString &s2) const;
201    bool operator<=(const QString &s2) const;
202    bool operator>=(const QString &s2) const;
203    short toShort(bool *ok = 0, int base = 10) const;
204    ushort toUShort(bool *ok = 0, int base = 10) const;
205    int toInt(bool *ok = 0, int base = 10) const;
206    uint toUInt(bool *ok = 0, int base = 10) const;
207    long toLong(bool *ok = 0, int base = 10) const;
208    ulong toULong(bool *ok = 0, int base = 10) const;
209    qlonglong toLongLong(bool *ok = 0, int base = 10) const;
210    qulonglong toULongLong(bool *ok = 0, int base = 10) const;
211    float toFloat(bool *ok = 0) const;
212    double toDouble(bool *ok = 0) const;
213    QByteArray toBase64() const;
214    QByteArray &setNum(double n /Constrained/, char format = 'g', int precision = 6);
215    QByteArray &setNum(SIP_PYOBJECT n /TypeHint="int"/, int base = 10);
216%MethodCode
217        #if PY_MAJOR_VERSION < 3
218        if (PyInt_Check(a0))
219        {
220            qlonglong val = PyInt_AsLong(a0);
221
222            sipRes = &sipCpp->setNum(val, a1);
223        }
224        else
225        #endif
226        {
227            qlonglong val = sipLong_AsLongLong(a0);
228
229            if (!PyErr_Occurred())
230            {
231                sipRes = &sipCpp->setNum(val, a1);
232            }
233            else
234            {
235                // If it is positive then it might fit an unsigned long long.
236
237                qulonglong uval = sipLong_AsUnsignedLongLong(a0);
238
239                if (!PyErr_Occurred())
240                {
241                    sipRes = &sipCpp->setNum(uval, a1);
242                }
243                else
244                {
245                    sipError = (PyErr_ExceptionMatches(PyExc_OverflowError)
246                            ? sipErrorFail : sipErrorContinue);
247                }
248            }
249        }
250%End
251
252    static QByteArray number(double n /Constrained/, char format = 'g', int precision = 6);
253    static QByteArray number(SIP_PYOBJECT n /TypeHint="int"/, int base = 10);
254%MethodCode
255        #if PY_MAJOR_VERSION < 3
256        if (PyInt_Check(a0))
257        {
258            qlonglong val = PyInt_AsLong(a0);
259
260            sipRes = new QByteArray(QByteArray::number(val, a1));
261        }
262        else
263        #endif
264        {
265            qlonglong val = sipLong_AsLongLong(a0);
266
267            if (!PyErr_Occurred())
268            {
269                sipRes = new QByteArray(QByteArray::number(val, a1));
270            }
271            else
272            {
273                // If it is positive then it might fit an unsigned long long.
274
275                qulonglong uval = sipLong_AsUnsignedLongLong(a0);
276
277                if (!PyErr_Occurred())
278                {
279                    sipRes = new QByteArray(QByteArray::number(uval, a1));
280                }
281                else
282                {
283                    sipError = (PyErr_ExceptionMatches(PyExc_OverflowError)
284                            ? sipErrorFail : sipErrorContinue);
285                }
286            }
287        }
288%End
289
290    static QByteArray fromBase64(const QByteArray &base64);
291    static QByteArray fromRawData(const char * /Array/, int size /ArraySize/);
292    static QByteArray fromHex(const QByteArray &hexEncoded);
293    int count() const /__len__/;
294    int length() const;
295    bool isNull() const;
296    int size() const;
297    char at(int i) const /Encoding="None"/;
298    char operator[](int i) const /Encoding="None"/;
299%MethodCode
300        Py_ssize_t idx = sipConvertFromSequenceIndex(a0, sipCpp->count());
301
302        if (idx < 0)
303            sipIsErr = 1;
304        else
305            sipRes = sipCpp->operator[]((int)idx);
306%End
307
308    QByteArray operator[](SIP_PYSLICE slice) const;
309%MethodCode
310        Py_ssize_t start, stop, step, slicelength;
311
312        if (sipConvertFromSliceObject(a0, sipCpp->length(), &start, &stop, &step, &slicelength) < 0)
313        {
314            sipIsErr = 1;
315        }
316        else
317        {
318            sipRes = new QByteArray();
319
320            for (Py_ssize_t i = 0; i < slicelength; ++i)
321            {
322                sipRes -> append(sipCpp->at(start));
323                start += step;
324            }
325        }
326%End
327
328    int __contains__(const QByteArray &a) const;
329%MethodCode
330        // It looks like you can't assign QBool to int.
331        sipRes = bool(sipCpp->contains(*a0));
332%End
333
334    long __hash__() const;
335%MethodCode
336        sipRes = qHash(*sipCpp);
337%End
338
339    SIP_PYOBJECT __str__() const /TypeHint="str"/;
340%MethodCode
341        sipRes = QByteArrayToPyStr(sipCpp);
342
343        #if PY_MAJOR_VERSION >= 3
344        PyObject *repr = PyObject_Repr(sipRes);
345
346        if (repr)
347        {
348            Py_DECREF(sipRes);
349            sipRes = repr;
350        }
351        #endif
352%End
353
354    SIP_PYOBJECT __repr__() const /TypeHint="str"/;
355%MethodCode
356        if (sipCpp->isNull())
357        {
358        #if PY_MAJOR_VERSION >= 3
359            sipRes = PyUnicode_FromString("PyQt5.QtCore.QByteArray()");
360        #else
361            sipRes = PyString_FromString("PyQt5.QtCore.QByteArray()");
362        #endif
363        }
364        else
365        {
366            PyObject *str = QByteArrayToPyStr(sipCpp);
367
368            if (str)
369            {
370        #if PY_MAJOR_VERSION >= 3
371                sipRes = PyUnicode_FromFormat("PyQt5.QtCore.QByteArray(%R)", str);
372        #else
373                sipRes = PyString_FromString("PyQt5.QtCore.QByteArray(");
374                PyString_ConcatAndDel(&sipRes, PyObject_Repr(str));
375                PyString_ConcatAndDel(&sipRes, PyString_FromString(")"));
376        #endif
377
378                Py_DECREF(str);
379            }
380        }
381%End
382
383    QByteArray operator*(int m) const;
384%MethodCode
385        sipRes = new QByteArray();
386
387        while (a0-- > 0)
388            *sipRes += *sipCpp;
389%End
390
391    QByteArray &operator*=(int m);
392%MethodCode
393        QByteArray orig(*sipCpp);
394
395        sipCpp->clear();
396
397        while (a0-- > 0)
398            *sipCpp += orig;
399%End
400
401    bool isEmpty() const;
402    SIP_PYOBJECT data() /TypeHint="Py_v3:bytes;str"/;
403%MethodCode
404        // QByteArrays may contain embedded '\0's so set the size explicitly.
405
406        char *res = sipCpp->data();
407        int len = sipCpp->size();
408
409        if (res)
410        {
411            if ((sipRes = SIPBytes_FromStringAndSize(res, len)) == NULL)
412                sipIsErr = 1;
413        }
414        else
415        {
416            Py_INCREF(Py_None);
417            sipRes = Py_None;
418        }
419%End
420
421    int capacity() const;
422    void reserve(int size);
423    void squeeze();
424    void push_back(const QByteArray &a);
425    void push_front(const QByteArray &a);
426    bool contains(const QByteArray &a) const;
427    QByteArray toHex() const;
428    QByteArray toPercentEncoding(const QByteArray &exclude = QByteArray(), const QByteArray &include = QByteArray(), char percent = '%') const;
429    static QByteArray fromPercentEncoding(const QByteArray &input, char percent = '%');
430    QByteArray repeated(int times) const;
431    void swap(QByteArray &other /Constrained/);
432%If (Qt_5_2_0 -)
433
434    enum Base64Option
435    {
436        Base64Encoding,
437        Base64UrlEncoding,
438        KeepTrailingEquals,
439        OmitTrailingEquals,
440%If (Qt_5_15_0 -)
441        IgnoreBase64DecodingErrors,
442%End
443%If (Qt_5_15_0 -)
444        AbortOnBase64DecodingErrors,
445%End
446    };
447
448%End
449%If (Qt_5_2_0 -)
450    typedef QFlags<QByteArray::Base64Option> Base64Options;
451%End
452%If (Qt_5_2_0 -)
453    QByteArray toBase64(QByteArray::Base64Options options) const;
454%End
455%If (Qt_5_2_0 -)
456    static QByteArray fromBase64(const QByteArray &base64, QByteArray::Base64Options options);
457%End
458%If (Qt_5_7_0 -)
459    QByteArray &prepend(int count, char c /Encoding="None"/);
460%End
461%If (Qt_5_7_0 -)
462    QByteArray &append(int count, char c /Encoding="None"/);
463%End
464%If (Qt_5_7_0 -)
465    QByteArray &insert(int i, int count, char c /Encoding="None"/);
466%End
467%If (Qt_5_9_0 -)
468    QByteArray toHex(char separator) const;
469%End
470%If (Qt_5_10_0 -)
471    QByteArray chopped(int len) const;
472%End
473%If (Qt_5_12_0 -)
474    int compare(const QByteArray &a, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
475%End
476%If (Qt_5_12_0 -)
477    bool isUpper() const;
478%End
479%If (Qt_5_12_0 -)
480    bool isLower() const;
481%End
482%If (Qt_5_15_0 -)
483
484    enum class Base64DecodingStatus
485    {
486        Ok,
487        IllegalInputLength,
488        IllegalCharacter,
489        IllegalPadding,
490    };
491
492%End
493%If (Qt_5_15_0 -)
494    static QByteArray::FromBase64Result fromBase64Encoding(const QByteArray &base64, QByteArray::Base64Options options = QByteArray::Base64Encoding);
495%End
496%If (Qt_5_15_0 -)
497
498    class FromBase64Result
499    {
500%TypeHeaderCode
501#include <qbytearray.h>
502%End
503
504    public:
505        QByteArray decoded;
506        QByteArray::Base64DecodingStatus decodingStatus;
507        void swap(QByteArray::FromBase64Result &other /Constrained/);
508        operator bool() const;
509%MethodCode
510            // This is required because SIP doesn't handle operator bool() properly.
511            sipRes = sipCpp->operator bool();
512%End
513
514        long __hash__() const;
515%MethodCode
516            sipRes = qHash(*sipCpp);
517%End
518    };
519
520%End
521};
522
523bool operator==(const QByteArray &a1, const QByteArray &a2);
524bool operator!=(const QByteArray &a1, const QByteArray &a2);
525bool operator<(const QByteArray &a1, const QByteArray &a2);
526bool operator<=(const QByteArray &a1, const QByteArray &a2);
527bool operator>(const QByteArray &a1, const QByteArray &a2);
528bool operator>=(const QByteArray &a1, const QByteArray &a2);
529const QByteArray operator+(const QByteArray &a1, const QByteArray &a2);
530QDataStream &operator<<(QDataStream &, const QByteArray & /Constrained/) /ReleaseGIL/;
531QDataStream &operator>>(QDataStream &, QByteArray & /Constrained/) /ReleaseGIL/;
532QByteArray qCompress(const QByteArray &data, int compressionLevel = -1);
533QByteArray qUncompress(const QByteArray &data);
534%If (Qt_5_2_0 -)
535QFlags<QByteArray::Base64Option> operator|(QByteArray::Base64Option f1, QFlags<QByteArray::Base64Option> f2);
536%End
537quint16 qChecksum(const char *s /Array/, uint len /ArraySize/);
538%If (Qt_5_9_0 -)
539quint16 qChecksum(const char *s /Array/, uint len /ArraySize/, Qt::ChecksumType standard);
540%End
541%If (Qt_5_15_0 -)
542bool operator==(const QByteArray::FromBase64Result &lhs, const QByteArray::FromBase64Result &rhs);
543%End
544%If (Qt_5_15_0 -)
545bool operator!=(const QByteArray::FromBase64Result &lhs, const QByteArray::FromBase64Result &rhs);
546%End
547