/src/corelib/tools/qstringbuilder.cpp

https://bitbucket.org/ultra_iter/qt-vtl · C++ · 196 lines · 42 code · 15 blank · 139 comment · 14 complexity · cf25e34180f49a1141a3857a3c07f6ca MD5 · raw file

  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
  4. ** All rights reserved.
  5. ** Contact: Nokia Corporation (qt-info@nokia.com)
  6. **
  7. ** This file is part of the QtCore module of the Qt Toolkit.
  8. **
  9. ** $QT_BEGIN_LICENSE:LGPL$
  10. ** GNU Lesser General Public License Usage
  11. ** This file may be used under the terms of the GNU Lesser General Public
  12. ** License version 2.1 as published by the Free Software Foundation and
  13. ** appearing in the file LICENSE.LGPL included in the packaging of this
  14. ** file. Please review the following information to ensure the GNU Lesser
  15. ** General Public License version 2.1 requirements will be met:
  16. ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  17. **
  18. ** In addition, as a special exception, Nokia gives you certain additional
  19. ** rights. These rights are described in the Nokia Qt LGPL Exception
  20. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  21. **
  22. ** GNU General Public License Usage
  23. ** Alternatively, this file may be used under the terms of the GNU General
  24. ** Public License version 3.0 as published by the Free Software Foundation
  25. ** and appearing in the file LICENSE.GPL included in the packaging of this
  26. ** file. Please review the following information to ensure the GNU General
  27. ** Public License version 3.0 requirements will be met:
  28. ** http://www.gnu.org/copyleft/gpl.html.
  29. **
  30. ** Other Usage
  31. ** Alternatively, this file may be used in accordance with the terms and
  32. ** conditions contained in a signed written agreement between you and Nokia.
  33. **
  34. **
  35. **
  36. **
  37. **
  38. ** $QT_END_LICENSE$
  39. **
  40. ****************************************************************************/
  41. #include "qstringbuilder.h"
  42. #include <QtCore/qtextcodec.h>
  43. QT_BEGIN_NAMESPACE
  44. /*!
  45. \class QLatin1Literal
  46. \internal
  47. \reentrant
  48. \since 4.6
  49. \brief The QLatin1Literal class provides a thin wrapper around string
  50. literals used in source code.
  51. \ingroup tools
  52. \ingroup shared
  53. \ingroup string-processing
  54. Unlike \c QLatin1String, a \c QLatin1Literal can retrieve its size
  55. without iterating over the literal.
  56. The main use of \c QLatin1Literal is in conjunction with \c QStringBuilder
  57. to reduce the number of reallocations needed to build up a string from
  58. smaller chunks.
  59. \sa QStringBuilder, QLatin1String, QString, QStringRef
  60. */
  61. /*! \fn int QLatin1Literal::size() const
  62. Returns the number of characters in the literal \e{excluding} the trailing
  63. NULL char.
  64. */
  65. /*! \fn QLatin1Literal::QLatin1Literal(const char str)
  66. Constructs a new literal from the string \a str.
  67. */
  68. /*! \fn const char *QLatin1Literal::data() const
  69. Returns a pointer to the first character of the string literal.
  70. The string literal is terminated by a NUL character.
  71. */
  72. /*!
  73. \class QStringBuilder
  74. \internal
  75. \reentrant
  76. \since 4.6
  77. \brief The QStringBuilder class is a template class that provides a facility to build up QStrings from smaller chunks.
  78. \ingroup tools
  79. \ingroup shared
  80. \ingroup string-processing
  81. To build a QString by multiple concatenations, QString::operator+()
  82. is typically used. This causes \e{n - 1} reallocations when building
  83. a string from \e{n} chunks.
  84. QStringBuilder uses expression templates to collect the individual
  85. chunks, compute the total size, allocate the required amount of
  86. memory for the final QString object, and copy the chunks into the
  87. allocated memory.
  88. The QStringBuilder class is not to be used explicitly in user
  89. code. Instances of the class are created as return values of the
  90. operator%() function, acting on objects of type QString,
  91. QLatin1String, QLatin1Literal, QStringRef, QChar, QCharRef,
  92. QLatin1Char, and \c char.
  93. Concatenating strings with operator%() generally yields better
  94. performance then using \c QString::operator+() on the same chunks
  95. if there are three or more of them, and performs equally well in other
  96. cases.
  97. \sa QLatin1Literal, QString
  98. */
  99. /*! \fn QStringBuilder::QStringBuilder(const A &a, const B &b)
  100. Constructs a QStringBuilder from \a a and \a b.
  101. */
  102. /* \fn QStringBuilder::operator%(const A &a, const B &b)
  103. Returns a \c QStringBuilder object that is converted to a QString object
  104. when assigned to a variable of QString type or passed to a function that
  105. takes a QString parameter.
  106. This function is usable with arguments of type \c QString,
  107. \c QLatin1String, \c QLatin1Literal, \c QStringRef,
  108. \c QChar, \c QCharRef, \c QLatin1Char, and \c char.
  109. */
  110. /*! \fn QByteArray QStringBuilder::toLatin1() const
  111. Returns a Latin-1 representation of the string as a QByteArray. The
  112. returned byte array is undefined if the string contains non-Latin1
  113. characters.
  114. */
  115. /*!
  116. \fn operator QStringBuilder::QString() const
  117. Converts the \c QLatin1Literal into a \c QString object.
  118. */
  119. /*! \internal
  120. Note: The len contains the ending \0
  121. */
  122. void QAbstractConcatenable::convertFromAscii(const char *a, int len, QChar *&out)
  123. {
  124. #ifndef QT_NO_TEXTCODEC
  125. if (QString::codecForCStrings && len) {
  126. QString tmp = QString::fromAscii(a, len > 0 ? len - 1 : -1);
  127. memcpy(out, reinterpret_cast<const char *>(tmp.constData()), sizeof(QChar) * tmp.size());
  128. out += tmp.length();
  129. return;
  130. }
  131. #endif
  132. if (len == -1) {
  133. if (!a)
  134. return;
  135. while (*a)
  136. *out++ = QLatin1Char(*a++);
  137. } else {
  138. for (int i = 0; i < len - 1; ++i)
  139. *out++ = QLatin1Char(a[i]);
  140. }
  141. }
  142. /*! \internal */
  143. void QAbstractConcatenable::convertToAscii(const QChar* a, int len, char*& out)
  144. {
  145. #ifndef QT_NO_TEXTCODEC
  146. if (QString::codecForCStrings) {
  147. QByteArray tmp = QString::codecForCStrings->fromUnicode(a, len);
  148. memcpy(out, tmp.constData(), tmp.size());
  149. out += tmp.length();
  150. return;
  151. }
  152. #endif
  153. if (len == -1) {
  154. while (a->unicode())
  155. convertToLatin1(*a++, out);
  156. } else {
  157. for (int i = 0; i < len; ++i)
  158. convertToLatin1(a[i], out);
  159. }
  160. }
  161. QT_END_NAMESPACE