Qt 4.8
Macros | Functions | Variables
qbytearray.cpp File Reference
#include "qbytearray.h"
#include "qbytearraymatcher.h"
#include "qtools_p.h"
#include "qstring.h"
#include "qlist.h"
#include "qlocale.h"
#include "qlocale_p.h"
#include "qscopedpointer.h"
#include <qdatastream.h>
#include <zlib.h>
#include <ctype.h>
#include <limits.h>
#include <string.h>
#include <stdlib.h>

Go to the source code of this file.

Macros

#define IS_RAW_DATA(d)   ((d)->data != (d)->array)
 
#define REHASH(a)
 

Functions

static int lastIndexOfHelper (const char *haystack, int l, const char *needle, int ol, int from)
 
static void q_fromPercentEncoding (QByteArray *ba, char percent)
 
void q_fromPercentEncoding (QByteArray *ba)
 
void q_normalizePercentEncoding (QByteArray *ba, const char *exclude)
 
static bool q_strchr (const char str[], char chr)
 
static void q_toPercentEncoding (QByteArray *ba, const char *dontEncode, const char *alsoEncode, char percent)
 
void q_toPercentEncoding (QByteArray *ba, const char *exclude, const char *include)
 
int qAllocMore (int alloc, int extra)
 
static QByteArrayqbytearray_insert (QByteArray *ba, int pos, const char *arr, int len)
 Inserts len bytes from the array arr at position pos and returns a reference the modified byte array. More...
 
int qFindByteArray (const char *haystack0, int haystackLen, int from, const char *needle0, int needleLen)
 
static bool qIsUpper (char c)
 
int qstrcmp (const QByteArray &str1, const char *str2)
 
int qstrcmp (const QByteArray &str1, const QByteArray &str2)
 
static char qToLower (char c)
 
static char toHexHelper (char c)
 

Variables

static const quint16 crc_tbl [16]
 

Macro Definition Documentation

◆ IS_RAW_DATA

#define IS_RAW_DATA (   d)    ((d)->data != (d)->array)

Definition at line 60 of file qbytearray.cpp.

Referenced by QByteArray::append(), and QByteArray::prepend().

◆ REHASH

#define REHASH (   a)
Value:
if (ol_minus_1 < sizeof(uint) * CHAR_BIT) \
hashHaystack -= (a) << ol_minus_1; \
hashHaystack <<= 1
long ASN1_INTEGER_get ASN1_INTEGER * a
unsigned int uint
Definition: qglobal.h:996

Definition at line 2372 of file qbytearray.cpp.

Referenced by lastIndexOfHelper().

Function Documentation

◆ lastIndexOfHelper()

static int lastIndexOfHelper ( const char *  haystack,
int  l,
const char *  needle,
int  ol,
int  from 
)
static

Definition at line 2476 of file qbytearray.cpp.

Referenced by QByteArray::lastIndexOf().

2477 {
2478  int delta = l - ol;
2479  if (from < 0)
2480  from = delta;
2481  if (from < 0 || from > l)
2482  return -1;
2483  if (from > delta)
2484  from = delta;
2485 
2486  const char *end = haystack;
2487  haystack += from;
2488  const uint ol_minus_1 = ol - 1;
2489  const char *n = needle + ol_minus_1;
2490  const char *h = haystack + ol_minus_1;
2491  uint hashNeedle = 0, hashHaystack = 0;
2492  int idx;
2493  for (idx = 0; idx < ol; ++idx) {
2494  hashNeedle = ((hashNeedle<<1) + *(n-idx));
2495  hashHaystack = ((hashHaystack<<1) + *(h-idx));
2496  }
2497  hashHaystack -= *haystack;
2498  while (haystack >= end) {
2499  hashHaystack += *haystack;
2500  if (hashHaystack == hashNeedle && memcmp(needle, haystack, ol) == 0)
2501  return haystack - end;
2502  --haystack;
2503  REHASH(*(haystack + ol));
2504  }
2505  return -1;
2506 
2507 }
unsigned int uint
Definition: qglobal.h:996
QFactoryLoader * l
#define REHASH(a)
static const KeyPair *const end

◆ q_fromPercentEncoding() [1/2]

static void q_fromPercentEncoding ( QByteArray ba,
char  percent 
)
static

Definition at line 4291 of file qbytearray.cpp.

Referenced by QByteArray::fromPercentEncoding(), q_fromPercentEncoding(), and q_normalizePercentEncoding().

4292 {
4293  if (ba->isEmpty())
4294  return;
4295 
4296  char *data = ba->data();
4297  const char *inputPtr = data;
4298 
4299  int i = 0;
4300  int len = ba->count();
4301  int outlen = 0;
4302  int a, b;
4303  char c;
4304  while (i < len) {
4305  c = inputPtr[i];
4306  if (c == percent && i + 2 < len) {
4307  a = inputPtr[++i];
4308  b = inputPtr[++i];
4309 
4310  if (a >= '0' && a <= '9') a -= '0';
4311  else if (a >= 'a' && a <= 'f') a = a - 'a' + 10;
4312  else if (a >= 'A' && a <= 'F') a = a - 'A' + 10;
4313 
4314  if (b >= '0' && b <= '9') b -= '0';
4315  else if (b >= 'a' && b <= 'f') b = b - 'a' + 10;
4316  else if (b >= 'A' && b <= 'F') b = b - 'A' + 10;
4317 
4318  *data++ = (char)((a << 4) | b);
4319  } else {
4320  *data++ = c;
4321  }
4322 
4323  ++i;
4324  ++outlen;
4325  }
4326 
4327  if (outlen != len)
4328  ba->truncate(outlen);
4329 }
void truncate(int pos)
Truncates the byte array at index position pos.
unsigned char c[8]
Definition: qnumeric_p.h:62
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
long ASN1_INTEGER_get ASN1_INTEGER * a
static const char * data(const QByteArray &arr)
int count(char c) const
Returns the number of occurrences of character ch in the byte array.
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421

◆ q_fromPercentEncoding() [2/2]

void q_fromPercentEncoding ( QByteArray ba)

Definition at line 4331 of file qbytearray.cpp.

Referenced by fromPercentEncodingHelper(), and fromPercentEncodingMutable().

4332 {
4333  q_fromPercentEncoding(ba, '%');
4334 }
static void q_fromPercentEncoding(QByteArray *ba, char percent)

◆ q_normalizePercentEncoding()

void q_normalizePercentEncoding ( QByteArray ba,
const char *  exclude 
)

Definition at line 4429 of file qbytearray.cpp.

Referenced by QUrlPrivate::normalized().

4430 {
4431  q_fromPercentEncoding(ba, '%');
4432  q_toPercentEncoding(ba, exclude, 0, '%');
4433 }
static void q_fromPercentEncoding(QByteArray *ba, char percent)
static void q_toPercentEncoding(QByteArray *ba, const char *dontEncode, const char *alsoEncode, char percent)

◆ q_strchr()

static bool q_strchr ( const char  str[],
char  chr 
)
inlinestatic

Definition at line 4366 of file qbytearray.cpp.

Referenced by q_toPercentEncoding().

4367 {
4368  if (!str) return false;
4369 
4370  const char *ptr = str;
4371  char c;
4372  while ((c = *ptr++))
4373  if (c == chr)
4374  return true;
4375  return false;
4376 }
unsigned char c[8]
Definition: qnumeric_p.h:62
const T * ptr(const T &t)

◆ q_toPercentEncoding() [1/2]

static void q_toPercentEncoding ( QByteArray ba,
const char *  dontEncode,
const char *  alsoEncode,
char  percent 
)
static

Definition at line 4384 of file qbytearray.cpp.

Referenced by q_normalizePercentEncoding(), q_toPercentEncoding(), and QByteArray::toPercentEncoding().

4385 {
4386  if (ba->isEmpty())
4387  return;
4388 
4389  QByteArray input = *ba;
4390  int len = input.count();
4391  const char *inputData = input.constData();
4392  char *output = 0;
4393  int length = 0;
4394 
4395  for (int i = 0; i < len; ++i) {
4396  unsigned char c = *inputData++;
4397  if (((c >= 0x61 && c <= 0x7A) // ALPHA
4398  || (c >= 0x41 && c <= 0x5A) // ALPHA
4399  || (c >= 0x30 && c <= 0x39) // DIGIT
4400  || c == 0x2D // -
4401  || c == 0x2E // .
4402  || c == 0x5F // _
4403  || c == 0x7E // ~
4404  || q_strchr(dontEncode, c))
4405  && !q_strchr(alsoEncode, c)) {
4406  if (output)
4407  output[length] = c;
4408  ++length;
4409  } else {
4410  if (!output) {
4411  // detach now
4412  ba->resize(len*3); // worst case
4413  output = ba->data();
4414  }
4415  output[length++] = percent;
4416  output[length++] = toHexHelper((c & 0xf0) >> 4);
4417  output[length++] = toHexHelper(c & 0xf);
4418  }
4419  }
4420  if (output)
4421  ba->truncate(length);
4422 }
void truncate(int pos)
Truncates the byte array at index position pos.
unsigned char c[8]
Definition: qnumeric_p.h:62
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
static bool q_strchr(const char str[], char chr)
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
int count(char c) const
Returns the number of occurrences of character ch in the byte array.
void resize(int size)
Sets the size of the byte array to size bytes.
static char toHexHelper(char c)
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421

◆ q_toPercentEncoding() [2/2]

void q_toPercentEncoding ( QByteArray ba,
const char *  exclude,
const char *  include 
)

Definition at line 4424 of file qbytearray.cpp.

Referenced by toPercentEncodingHelper().

4425 {
4426  q_toPercentEncoding(ba, exclude, include, '%');
4427 }
static void q_toPercentEncoding(QByteArray *ba, const char *dontEncode, const char *alsoEncode, char percent)

◆ qAllocMore()

int qAllocMore ( int  alloc,
int  extra 
)

Definition at line 70 of file qbytearray.cpp.

Referenced by QByteArray::append(), QFragmentMapData< QTextBlockData >::createFragment(), grow(), QVectorData::grow(), QString::grow(), and QByteArray::prepend().

71 {
72  Q_ASSERT(alloc >= 0 && extra >= 0);
73  Q_ASSERT_X(alloc < (1 << 30) - extra, "qAllocMore", "Requested size is too large!");
74 
75  if (alloc == 0 && extra == 0)
76  return 0;
77  const int page = 1 << 12;
78  int nalloc;
79  alloc += extra;
80  if (alloc < 1<<6) {
81  nalloc = (1<<3) + ((alloc >>3) << 3);
82  } else {
83  // don't do anything if the loop will overflow signed int.
84  if (alloc >= INT_MAX/2)
85  return INT_MAX;
86  nalloc = (alloc < page) ? 1 << 3 : page;
87  while (nalloc < alloc) {
88  if (nalloc <= 0)
89  return INT_MAX;
90  nalloc *= 2;
91  }
92  }
93  return nalloc - extra;
94 }
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
#define INT_MAX

◆ qbytearray_insert()

static QByteArray& qbytearray_insert ( QByteArray ba,
int  pos,
const char *  arr,
int  len 
)
inlinestatic

Inserts len bytes from the array arr at position pos and returns a reference the modified byte array.

Warning
This function is not part of the public interface.

Definition at line 1834 of file qbytearray.cpp.

Referenced by QByteArray::insert(), and QByteArray::replace().

1836 {
1837  Q_ASSERT(pos >= 0);
1838 
1839  if (pos < 0 || len <= 0 || arr == 0)
1840  return *ba;
1841 
1842  int oldsize = ba->size();
1843  ba->resize(qMax(pos, oldsize) + len);
1844  char *dst = ba->data();
1845  if (pos > oldsize)
1846  ::memset(dst + oldsize, 0x20, pos - oldsize);
1847  else
1848  ::memmove(dst + pos + len, dst + pos, oldsize - pos);
1849  memcpy(dst + pos, arr, len);
1850  return *ba;
1851 }
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
void resize(int size)
Sets the size of the byte array to size bytes.
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402

◆ qFindByteArray()

int qFindByteArray ( const char *  haystack0,
int  haystackLen,
int  from,
const char *  needle,
int  needleLen 
)
Warning
This function is not part of the public interface.

Definition at line 275 of file qbytearraymatcher.cpp.

Referenced by QByteArray::indexOf().

278 {
279  const int l = haystackLen;
280  const int sl = needleLen;
281  if (from < 0)
282  from += l;
283  if (uint(sl + from) > (uint)l)
284  return -1;
285  if (!sl)
286  return from;
287  if (!l)
288  return -1;
289 
290  if (sl == 1)
291  return findChar(haystack0, haystackLen, needle[0], from);
292 
293  /*
294  We use the Boyer-Moore algorithm in cases where the overhead
295  for the skip table should pay off, otherwise we use a simple
296  hash function.
297  */
298  if (l > 500 && sl > 5)
299  return qFindByteArrayBoyerMoore(haystack0, haystackLen, from,
300  needle, needleLen);
301 
302  /*
303  We use some hashing for efficiency's sake. Instead of
304  comparing strings, we compare the hash value of str with that
305  of a part of this QString. Only if that matches, we call memcmp().
306  */
307  const char *haystack = haystack0 + from;
308  const char *end = haystack0 + (l - sl);
309  const uint sl_minus_1 = sl - 1;
310  uint hashNeedle = 0, hashHaystack = 0;
311  int idx;
312  for (idx = 0; idx < sl; ++idx) {
313  hashNeedle = ((hashNeedle<<1) + needle[idx]);
314  hashHaystack = ((hashHaystack<<1) + haystack[idx]);
315  }
316  hashHaystack -= *(haystack + sl_minus_1);
317 
318  while (haystack <= end) {
319  hashHaystack += *(haystack + sl_minus_1);
320  if (hashHaystack == hashNeedle && *needle == *haystack
321  && memcmp(needle, haystack, sl) == 0)
322  return haystack - haystack0;
323 
324  REHASH(*haystack);
325  ++haystack;
326  }
327  return -1;
328 }
static int findChar(const char *str, int len, char ch, int from)
unsigned int uint
Definition: qglobal.h:996
static int qFindByteArrayBoyerMoore(const char *haystack, int haystackLen, int haystackOffset, const char *needle, int needleLen)
QFactoryLoader * l
static const KeyPair *const end
#define REHASH(a)

◆ qIsUpper()

static bool qIsUpper ( char  c)
inlinestatic

Definition at line 637 of file qbytearray.cpp.

Referenced by QByteArray::setNum().

638 {
639  return c >= 'A' && c <= 'Z';
640 }
unsigned char c[8]
Definition: qnumeric_p.h:62

◆ qstrcmp() [1/2]

int qstrcmp ( const QByteArray str1,
const char *  str2 
)
Warning
This function is not part of the public interface.

Definition at line 336 of file qbytearray.cpp.

Referenced by QAccessibleObjectPrivate::actionList(), QDBusAdaptorConnector::addAdaptor(), QPrintPropertiesDialog::addItemToOptions(), QAudioDeviceInfoInternal::availableDevices(), QLocalePrivate::bytearrayToDouble(), QMetaObject::checkConnectArgs(), QCUPSSupport::collectMarkedOptionsHelper(), QPSQLDriver::commitTransaction(), QTest::compare_string_helper(), QFutureWatcherBase::connectNotify(), QNetworkSession::connectNotify(), QDBusAbstractInterface::connectNotify(), QAxClass< T >::createObject(), QDeclarativeVisualDataModelDataMetaObject::createProperty(), QScript::QMetaObjectWrapperObject::deleteProperty(), QFutureWatcherBase::disconnectNotify(), QNetworkSession::disconnectNotify(), QProxyStylePrivate::ensureBaseStyle(), QScript::QMetaObjectWrapperObject::getOwnPropertyDescriptor(), QScript::QMetaObjectWrapperObject::getOwnPropertySlot(), getStyle(), QObject::inherits(), QDeclarativeJS::integerFromString(), QScript::integerFromString(), QMetaMethod::invoke(), isProcessBeingDebugged(), QObject::killTimer(), QStyleSheetStyle::nativeFrameWidth(), QStyleSheetStyleSelector::nodeNameEquals(), QStyleSheetStyleSelector::nodeNames(), operator!=(), operator<(), operator<=(), operator==(), operator>(), operator>=(), QCUPSSupport::pageRect(), QCUPSSupport::paperRect(), parentWidget(), QPPDOptionsModel::parseChoices(), QCUPSSupport::ppdOption(), QApplicationPrivate::process_cmdline(), QVideoSurfaceFormat::property(), QScript::QMetaObjectWrapperObject::put(), QTest::qExec(), QTest::qInvokeTestMethod(), QRenderRule::QRenderRule(), QSQLite2DriverPrivate::QSQLite2DriverPrivate(), qt_check_if_internal_widget(), qt_init(), QAxObject::qt_metacast(), QAxWidget::qt_metacast(), QAxHostWidget::qt_metacast(), qt_parseEtcLpPrinters(), QBBSystemLocaleData::readHourFormat(), QBBSystemLocaleData::readMeasurementSystem(), QCUPSSupport::saveOptions(), QWizard::setDefaultProperty(), QFontDialogPrivate::setFont(), QObject::setObjectName(), QVideoSurfaceFormat::setProperty(), QDBusConnectionInterface::unregisterService(), and QAccessible::updateAccessibility().

337 {
338  if (!str2)
339  return str1.isEmpty() ? 0 : +1;
340 
341  const char *str1data = str1.constData();
342  const char *str1end = str1data + str1.length();
343  for ( ; str1data < str1end && *str2; ++str1data, ++str2) {
344  register int diff = int(uchar(*str1data)) - uchar(*str2);
345  if (diff)
346  // found a difference
347  return diff;
348  }
349 
350  // Why did we stop?
351  if (*str2 != '\0')
352  // not the null, so we stopped because str1 is shorter
353  return -1;
354  if (str1data < str1end)
355  // we haven't reached the end, so str1 must be longer
356  return +1;
357  return 0;
358 }
unsigned char uchar
Definition: qglobal.h:994
static Bigint * diff(Bigint *a, Bigint *b)
int length() const
Same as size().
Definition: qbytearray.h:356
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421

◆ qstrcmp() [2/2]

int qstrcmp ( const QByteArray str1,
const QByteArray str2 
)
Warning
This function is not part of the public interface.

Definition at line 363 of file qbytearray.cpp.

364 {
365  int l1 = str1.length();
366  int l2 = str2.length();
367  int ret = memcmp(str1, str2, qMin(l1, l2));
368  if (ret != 0)
369  return ret;
370 
371  // they matched qMin(l1, l2) bytes
372  // so the longer one is lexically after the shorter one
373  return l1 - l2;
374 }
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
int length() const
Same as size().
Definition: qbytearray.h:356

◆ qToLower()

static char qToLower ( char  c)
inlinestatic

Definition at line 642 of file qbytearray.cpp.

Referenced by QByteArray::setNum().

643 {
644  if (c >= 'A' && c <= 'Z')
645  return c - 'A' + 'a';
646  else
647  return c;
648 }
unsigned char c[8]
Definition: qnumeric_p.h:62

◆ toHexHelper()

static char toHexHelper ( char  c)
inlinestatic

Definition at line 4378 of file qbytearray.cpp.

Referenced by q_toPercentEncoding().

4379 {
4380  static const char hexnumbers[] = "0123456789ABCDEF";
4381  return hexnumbers[c & 0xf];
4382 }
unsigned char c[8]
Definition: qnumeric_p.h:62

Variable Documentation

◆ crc_tbl

const quint16 crc_tbl[16]
static
Initial value:
= {
0x0000, 0x1081, 0x2102, 0x3183,
0x4204, 0x5285, 0x6306, 0x7387,
0x8408, 0x9489, 0xa50a, 0xb58b,
0xc60c, 0xd68d, 0xe70e, 0xf78f
}

Definition at line 413 of file qbytearray.cpp.

Referenced by QByteArray::qChecksum(), and qstrcmp().