| 1 | /****************************************************************************
|
|---|
| 2 | ** $Id: qstring.cpp 2 2005-11-16 15:49:26Z dmik $
|
|---|
| 3 | **
|
|---|
| 4 | ** Implementation of the QString class and related Unicode functions
|
|---|
| 5 | **
|
|---|
| 6 | ** Created : 920722
|
|---|
| 7 | **
|
|---|
| 8 | ** Copyright (C) 1992-2002 Trolltech AS. All rights reserved.
|
|---|
| 9 | **
|
|---|
| 10 | ** This file is part of the tools module of the Qt GUI Toolkit.
|
|---|
| 11 | **
|
|---|
| 12 | ** This file may be distributed under the terms of the Q Public License
|
|---|
| 13 | ** as defined by Trolltech AS of Norway and appearing in the file
|
|---|
| 14 | ** LICENSE.QPL included in the packaging of this file.
|
|---|
| 15 | **
|
|---|
| 16 | ** This file may be distributed and/or modified under the terms of the
|
|---|
| 17 | ** GNU General Public License version 2 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 19 | ** packaging of this file.
|
|---|
| 20 | **
|
|---|
| 21 | ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
|
|---|
| 22 | ** licenses may use this file in accordance with the Qt Commercial License
|
|---|
| 23 | ** Agreement provided with the Software.
|
|---|
| 24 | **
|
|---|
| 25 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
|---|
| 26 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
|---|
| 27 | **
|
|---|
| 28 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
|
|---|
| 29 | ** information about Qt Commercial License Agreements.
|
|---|
| 30 | ** See http://www.trolltech.com/qpl/ for QPL licensing information.
|
|---|
| 31 | ** See http://www.trolltech.com/gpl/ for GPL licensing information.
|
|---|
| 32 | **
|
|---|
| 33 | ** Contact info@trolltech.com if any conditions of this licensing are
|
|---|
| 34 | ** not clear to you.
|
|---|
| 35 | **
|
|---|
| 36 | **********************************************************************/
|
|---|
| 37 |
|
|---|
| 38 | // Don't define it while compiling this module, or USERS of Qt will
|
|---|
| 39 | // not be able to link.
|
|---|
| 40 | #ifdef QT_NO_CAST_ASCII
|
|---|
| 41 | #undef QT_NO_CAST_ASCII
|
|---|
| 42 | #endif
|
|---|
| 43 |
|
|---|
| 44 | #include "qstring.h"
|
|---|
| 45 | #include "qregexp.h"
|
|---|
| 46 | #include "qdatastream.h"
|
|---|
| 47 | #ifndef QT_NO_TEXTCODEC
|
|---|
| 48 | #include "qtextcodec.h"
|
|---|
| 49 | #endif
|
|---|
| 50 | #include "qlocale.h"
|
|---|
| 51 | #include "qlocale_p.h"
|
|---|
| 52 |
|
|---|
| 53 | #include "qunicodetables_p.h"
|
|---|
| 54 | #include <limits.h>
|
|---|
| 55 | #include <stdarg.h>
|
|---|
| 56 | #include <stdio.h>
|
|---|
| 57 | #include <stdlib.h>
|
|---|
| 58 | #include <string.h>
|
|---|
| 59 | #ifndef Q_OS_TEMP
|
|---|
| 60 | #include <locale.h>
|
|---|
| 61 | #endif
|
|---|
| 62 | #if defined(Q_WS_WIN)
|
|---|
| 63 | #include "qt_windows.h"
|
|---|
| 64 | #endif
|
|---|
| 65 | #if !defined( QT_NO_COMPONENT ) && !defined( QT_LITE_COMPONENT )
|
|---|
| 66 | #include "qcleanuphandler.h"
|
|---|
| 67 | #endif
|
|---|
| 68 |
|
|---|
| 69 | #ifndef LLONG_MAX
|
|---|
| 70 | #define LLONG_MAX Q_INT64_C(9223372036854775807)
|
|---|
| 71 | #endif
|
|---|
| 72 | #ifndef LLONG_MIN
|
|---|
| 73 | #define LLONG_MIN (-LLONG_MAX - Q_INT64_C(1))
|
|---|
| 74 | #endif
|
|---|
| 75 | #ifndef ULLONG_MAX
|
|---|
| 76 | #define ULLONG_MAX Q_UINT64_C(18446744073709551615)
|
|---|
| 77 | #endif
|
|---|
| 78 |
|
|---|
| 79 | static int ucstrcmp( const QString &as, const QString &bs )
|
|---|
| 80 | {
|
|---|
| 81 | const QChar *a = as.unicode();
|
|---|
| 82 | const QChar *b = bs.unicode();
|
|---|
| 83 | if ( a == b )
|
|---|
| 84 | return 0;
|
|---|
| 85 | if ( a == 0 )
|
|---|
| 86 | return 1;
|
|---|
| 87 | if ( b == 0 )
|
|---|
| 88 | return -1;
|
|---|
| 89 | int l=QMIN(as.length(),bs.length());
|
|---|
| 90 | while ( l-- && *a == *b )
|
|---|
| 91 | a++,b++;
|
|---|
| 92 | if ( l==-1 )
|
|---|
| 93 | return ( as.length()-bs.length() );
|
|---|
| 94 | return a->unicode() - b->unicode();
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | static int ucstrncmp( const QChar *a, const QChar *b, int l )
|
|---|
| 98 | {
|
|---|
| 99 | while ( l-- && *a == *b )
|
|---|
| 100 | a++,b++;
|
|---|
| 101 | if ( l==-1 )
|
|---|
| 102 | return 0;
|
|---|
| 103 | return a->unicode() - b->unicode();
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | static int ucstrnicmp( const QChar *a, const QChar *b, int l )
|
|---|
| 107 | {
|
|---|
| 108 | while ( l-- && ::lower( *a ) == ::lower( *b ) )
|
|---|
| 109 | a++,b++;
|
|---|
| 110 | if ( l==-1 )
|
|---|
| 111 | return 0;
|
|---|
| 112 | return ::lower( *a ).unicode() - ::lower( *b ).unicode();
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | static uint computeNewMax( uint len )
|
|---|
| 116 | {
|
|---|
| 117 | uint newMax = 4;
|
|---|
| 118 | while ( newMax < len )
|
|---|
| 119 | newMax *= 2;
|
|---|
| 120 | // try to save some memory
|
|---|
| 121 | if ( newMax >= 1024 * 1024 && len <= newMax - (newMax >> 2) )
|
|---|
| 122 | newMax -= newMax >> 2;
|
|---|
| 123 | return newMax;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | static bool qIsUpper(char c)
|
|---|
| 127 | {
|
|---|
| 128 | return c >= 'A' && c <= 'Z';
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | static bool qIsDigit(char c)
|
|---|
| 132 | {
|
|---|
| 133 | return c >= '0' && c <= '9';
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | static char qToLower(char c)
|
|---|
| 137 | {
|
|---|
| 138 | if (c >= 'A' && c <= 'Z')
|
|---|
| 139 | return c - 'A' + 'a';
|
|---|
| 140 | else
|
|---|
| 141 | return c;
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | /*!
|
|---|
| 145 | \class QCharRef qstring.h
|
|---|
| 146 | \reentrant
|
|---|
| 147 | \brief The QCharRef class is a helper class for QString.
|
|---|
| 148 |
|
|---|
| 149 | \ingroup text
|
|---|
| 150 |
|
|---|
| 151 | When you get an object of type QCharRef, if you can assign to it,
|
|---|
| 152 | the assignment will apply to the character in the string from
|
|---|
| 153 | which you got the reference. That is its whole purpose in life.
|
|---|
| 154 | The QCharRef becomes invalid once modifications are made to the
|
|---|
| 155 | string: if you want to keep the character, copy it into a QChar.
|
|---|
| 156 |
|
|---|
| 157 | Most of the QChar member functions also exist in QCharRef.
|
|---|
| 158 | However, they are not explicitly documented here.
|
|---|
| 159 |
|
|---|
| 160 | \sa QString::operator[]() QString::at() QChar
|
|---|
| 161 | */
|
|---|
| 162 |
|
|---|
| 163 | /*!
|
|---|
| 164 | \class QChar qstring.h
|
|---|
| 165 | \reentrant
|
|---|
| 166 | \brief The QChar class provides a lightweight Unicode character.
|
|---|
| 167 |
|
|---|
| 168 | \ingroup text
|
|---|
| 169 |
|
|---|
| 170 | Unicode characters are (so far) 16-bit entities without any markup
|
|---|
| 171 | or structure. This class represents such an entity. It is
|
|---|
| 172 | lightweight, so it can be used everywhere. Most compilers treat it
|
|---|
| 173 | like a "short int". (In a few years it may be necessary to make
|
|---|
| 174 | QChar 32-bit when more than 65536 Unicode code points have been
|
|---|
| 175 | defined and come into use.)
|
|---|
| 176 |
|
|---|
| 177 | QChar provides a full complement of testing/classification
|
|---|
| 178 | functions, converting to and from other formats, converting from
|
|---|
| 179 | composed to decomposed Unicode, and trying to compare and
|
|---|
| 180 | case-convert if you ask it to.
|
|---|
| 181 |
|
|---|
| 182 | The classification functions include functions like those in
|
|---|
| 183 | ctype.h, but operating on the full range of Unicode characters.
|
|---|
| 184 | They all return TRUE if the character is a certain type of
|
|---|
| 185 | character; otherwise they return FALSE. These classification
|
|---|
| 186 | functions are isNull() (returns TRUE if the character is U+0000),
|
|---|
| 187 | isPrint() (TRUE if the character is any sort of printable
|
|---|
| 188 | character, including whitespace), isPunct() (any sort of
|
|---|
| 189 | punctation), isMark() (Unicode Mark), isLetter (a letter),
|
|---|
| 190 | isNumber() (any sort of numeric character), isLetterOrNumber(),
|
|---|
| 191 | and isDigit() (decimal digits). All of these are wrappers around
|
|---|
| 192 | category() which return the Unicode-defined category of each
|
|---|
| 193 | character.
|
|---|
| 194 |
|
|---|
| 195 | QChar further provides direction(), which indicates the "natural"
|
|---|
| 196 | writing direction of this character. The joining() function
|
|---|
| 197 | indicates how the character joins with its neighbors (needed
|
|---|
| 198 | mostly for Arabic) and finally mirrored(), which indicates whether
|
|---|
| 199 | the character needs to be mirrored when it is printed in its
|
|---|
| 200 | "unnatural" writing direction.
|
|---|
| 201 |
|
|---|
| 202 | Composed Unicode characters (like å) can be converted to
|
|---|
| 203 | decomposed Unicode ("a" followed by "ring above") by using
|
|---|
| 204 | decomposition().
|
|---|
| 205 |
|
|---|
| 206 | In Unicode, comparison is not necessarily possible and case
|
|---|
| 207 | conversion is very difficult at best. Unicode, covering the
|
|---|
| 208 | "entire" world, also includes most of the world's case and sorting
|
|---|
| 209 | problems. Qt tries, but not very hard: operator==() and friends
|
|---|
| 210 | will do comparison based purely on the numeric Unicode value (code
|
|---|
| 211 | point) of the characters, and upper() and lower() will do case
|
|---|
| 212 | changes when the character has a well-defined upper/lower-case
|
|---|
| 213 | equivalent. There is no provision for locale-dependent case
|
|---|
| 214 | folding rules or comparison; these functions are meant to be fast
|
|---|
| 215 | so they can be used unambiguously in data structures. (See
|
|---|
| 216 | QString::localeAwareCompare() though.)
|
|---|
| 217 |
|
|---|
| 218 | The conversion functions include unicode() (to a scalar), latin1()
|
|---|
| 219 | (to scalar, but converts all non-Latin-1 characters to 0), row()
|
|---|
| 220 | (gives the Unicode row), cell() (gives the Unicode cell),
|
|---|
| 221 | digitValue() (gives the integer value of any of the numerous digit
|
|---|
| 222 | characters), and a host of constructors.
|
|---|
| 223 |
|
|---|
| 224 | More information can be found in the document \link unicode.html
|
|---|
| 225 | About Unicode. \endlink
|
|---|
| 226 |
|
|---|
| 227 | \sa QString QCharRef
|
|---|
| 228 | */
|
|---|
| 229 |
|
|---|
| 230 | /*!
|
|---|
| 231 | \enum QChar::Category
|
|---|
| 232 |
|
|---|
| 233 | This enum maps the Unicode character categories.
|
|---|
| 234 |
|
|---|
| 235 | The following characters are normative in Unicode:
|
|---|
| 236 |
|
|---|
| 237 | \value Mark_NonSpacing Unicode class name Mn
|
|---|
| 238 |
|
|---|
| 239 | \value Mark_SpacingCombining Unicode class name Mc
|
|---|
| 240 |
|
|---|
| 241 | \value Mark_Enclosing Unicode class name Me
|
|---|
| 242 |
|
|---|
| 243 | \value Number_DecimalDigit Unicode class name Nd
|
|---|
| 244 |
|
|---|
| 245 | \value Number_Letter Unicode class name Nl
|
|---|
| 246 |
|
|---|
| 247 | \value Number_Other Unicode class name No
|
|---|
| 248 |
|
|---|
| 249 | \value Separator_Space Unicode class name Zs
|
|---|
| 250 |
|
|---|
| 251 | \value Separator_Line Unicode class name Zl
|
|---|
| 252 |
|
|---|
| 253 | \value Separator_Paragraph Unicode class name Zp
|
|---|
| 254 |
|
|---|
| 255 | \value Other_Control Unicode class name Cc
|
|---|
| 256 |
|
|---|
| 257 | \value Other_Format Unicode class name Cf
|
|---|
| 258 |
|
|---|
| 259 | \value Other_Surrogate Unicode class name Cs
|
|---|
| 260 |
|
|---|
| 261 | \value Other_PrivateUse Unicode class name Co
|
|---|
| 262 |
|
|---|
| 263 | \value Other_NotAssigned Unicode class name Cn
|
|---|
| 264 |
|
|---|
| 265 |
|
|---|
| 266 | The following categories are informative in Unicode:
|
|---|
| 267 |
|
|---|
| 268 | \value Letter_Uppercase Unicode class name Lu
|
|---|
| 269 |
|
|---|
| 270 | \value Letter_Lowercase Unicode class name Ll
|
|---|
| 271 |
|
|---|
| 272 | \value Letter_Titlecase Unicode class name Lt
|
|---|
| 273 |
|
|---|
| 274 | \value Letter_Modifier Unicode class name Lm
|
|---|
| 275 |
|
|---|
| 276 | \value Letter_Other Unicode class name Lo
|
|---|
| 277 |
|
|---|
| 278 | \value Punctuation_Connector Unicode class name Pc
|
|---|
| 279 |
|
|---|
| 280 | \value Punctuation_Dash Unicode class name Pd
|
|---|
| 281 |
|
|---|
| 282 | \value Punctuation_Open Unicode class name Ps
|
|---|
| 283 |
|
|---|
| 284 | \value Punctuation_Close Unicode class name Pe
|
|---|
| 285 |
|
|---|
| 286 | \value Punctuation_InitialQuote Unicode class name Pi
|
|---|
| 287 |
|
|---|
| 288 | \value Punctuation_FinalQuote Unicode class name Pf
|
|---|
| 289 |
|
|---|
| 290 | \value Punctuation_Other Unicode class name Po
|
|---|
| 291 |
|
|---|
| 292 | \value Symbol_Math Unicode class name Sm
|
|---|
| 293 |
|
|---|
| 294 | \value Symbol_Currency Unicode class name Sc
|
|---|
| 295 |
|
|---|
| 296 | \value Symbol_Modifier Unicode class name Sk
|
|---|
| 297 |
|
|---|
| 298 | \value Symbol_Other Unicode class name So
|
|---|
| 299 |
|
|---|
| 300 |
|
|---|
| 301 | There are two categories that are specific to Qt:
|
|---|
| 302 |
|
|---|
| 303 | \value NoCategory used when Qt is dazed and confused and cannot
|
|---|
| 304 | make sense of anything.
|
|---|
| 305 |
|
|---|
| 306 | \value Punctuation_Dask old typo alias for Punctuation_Dash
|
|---|
| 307 |
|
|---|
| 308 | */
|
|---|
| 309 |
|
|---|
| 310 | /*!
|
|---|
| 311 | \enum QChar::Direction
|
|---|
| 312 |
|
|---|
| 313 | This enum type defines the Unicode direction attributes. See \link
|
|---|
| 314 | http://www.unicode.org/ the Unicode Standard\endlink for a
|
|---|
| 315 | description of the values.
|
|---|
| 316 |
|
|---|
| 317 | In order to conform to C/C++ naming conventions "Dir" is prepended
|
|---|
| 318 | to the codes used in the Unicode Standard.
|
|---|
| 319 | */
|
|---|
| 320 |
|
|---|
| 321 | /*!
|
|---|
| 322 | \enum QChar::Decomposition
|
|---|
| 323 |
|
|---|
| 324 | This enum type defines the Unicode decomposition attributes. See
|
|---|
| 325 | \link http://www.unicode.org/ the Unicode Standard\endlink for a
|
|---|
| 326 | description of the values.
|
|---|
| 327 | */
|
|---|
| 328 |
|
|---|
| 329 | /*!
|
|---|
| 330 | \enum QChar::Joining
|
|---|
| 331 |
|
|---|
| 332 | This enum type defines the Unicode joining attributes. See \link
|
|---|
| 333 | http://www.unicode.org/ the Unicode Standard\endlink for a
|
|---|
| 334 | description of the values.
|
|---|
| 335 | */
|
|---|
| 336 |
|
|---|
| 337 | /*!
|
|---|
| 338 | \enum QChar::CombiningClass
|
|---|
| 339 |
|
|---|
| 340 | This enum type defines names for some of the Unicode combining
|
|---|
| 341 | classes. See \link http://www.unicode.org/ the Unicode
|
|---|
| 342 | Standard\endlink for a description of the values.
|
|---|
| 343 | */
|
|---|
| 344 |
|
|---|
| 345 | /*!
|
|---|
| 346 | \fn void QChar::setCell( uchar cell )
|
|---|
| 347 | \internal
|
|---|
| 348 | */
|
|---|
| 349 |
|
|---|
| 350 | /*!
|
|---|
| 351 | \fn void QChar::setRow( uchar row )
|
|---|
| 352 | \internal
|
|---|
| 353 | */
|
|---|
| 354 |
|
|---|
| 355 |
|
|---|
| 356 | /*!
|
|---|
| 357 | \fn QChar::QChar()
|
|---|
| 358 |
|
|---|
| 359 | Constructs a null QChar (one that isNull()).
|
|---|
| 360 | */
|
|---|
| 361 |
|
|---|
| 362 |
|
|---|
| 363 | /*!
|
|---|
| 364 | \fn QChar::QChar( char c )
|
|---|
| 365 |
|
|---|
| 366 | Constructs a QChar corresponding to ASCII/Latin-1 character \a c.
|
|---|
| 367 | */
|
|---|
| 368 |
|
|---|
| 369 |
|
|---|
| 370 | /*!
|
|---|
| 371 | \fn QChar::QChar( uchar c )
|
|---|
| 372 |
|
|---|
| 373 | Constructs a QChar corresponding to ASCII/Latin-1 character \a c.
|
|---|
| 374 | */
|
|---|
| 375 |
|
|---|
| 376 |
|
|---|
| 377 | /*!
|
|---|
| 378 | \fn QChar::QChar( uchar c, uchar r )
|
|---|
| 379 |
|
|---|
| 380 | Constructs a QChar for Unicode cell \a c in row \a r.
|
|---|
| 381 | */
|
|---|
| 382 |
|
|---|
| 383 |
|
|---|
| 384 | /*!
|
|---|
| 385 | \fn QChar::QChar( const QChar& c )
|
|---|
| 386 |
|
|---|
| 387 | Constructs a copy of \a c. This is a deep copy, if such a
|
|---|
| 388 | lightweight object can be said to have deep copies.
|
|---|
| 389 | */
|
|---|
| 390 |
|
|---|
| 391 |
|
|---|
| 392 | /*!
|
|---|
| 393 | \fn QChar::QChar( ushort rc )
|
|---|
| 394 |
|
|---|
| 395 | Constructs a QChar for the character with Unicode code point \a rc.
|
|---|
| 396 | */
|
|---|
| 397 |
|
|---|
| 398 |
|
|---|
| 399 | /*!
|
|---|
| 400 | \fn QChar::QChar( short rc )
|
|---|
| 401 |
|
|---|
| 402 | Constructs a QChar for the character with Unicode code point \a rc.
|
|---|
| 403 | */
|
|---|
| 404 |
|
|---|
| 405 |
|
|---|
| 406 | /*!
|
|---|
| 407 | \fn QChar::QChar( uint rc )
|
|---|
| 408 |
|
|---|
| 409 | Constructs a QChar for the character with Unicode code point \a rc.
|
|---|
| 410 | */
|
|---|
| 411 |
|
|---|
| 412 |
|
|---|
| 413 | /*!
|
|---|
| 414 | \fn QChar::QChar( int rc )
|
|---|
| 415 |
|
|---|
| 416 | Constructs a QChar for the character with Unicode code point \a rc.
|
|---|
| 417 | */
|
|---|
| 418 |
|
|---|
| 419 |
|
|---|
| 420 | /*!
|
|---|
| 421 | \fn bool QChar::networkOrdered ()
|
|---|
| 422 |
|
|---|
| 423 | \obsolete
|
|---|
| 424 |
|
|---|
| 425 | Returns TRUE if this character is in network byte order (MSB
|
|---|
| 426 | first); otherwise returns FALSE. This is platform dependent.
|
|---|
| 427 | */
|
|---|
| 428 |
|
|---|
| 429 |
|
|---|
| 430 | /*!
|
|---|
| 431 | \fn bool QChar::isNull() const
|
|---|
| 432 |
|
|---|
| 433 | Returns TRUE if the character is the Unicode character 0x0000
|
|---|
| 434 | (ASCII NUL); otherwise returns FALSE.
|
|---|
| 435 | */
|
|---|
| 436 |
|
|---|
| 437 | /*!
|
|---|
| 438 | \fn uchar QChar::cell () const
|
|---|
| 439 |
|
|---|
| 440 | Returns the cell (least significant byte) of the Unicode
|
|---|
| 441 | character.
|
|---|
| 442 | */
|
|---|
| 443 |
|
|---|
| 444 | /*!
|
|---|
| 445 | \fn uchar QChar::row () const
|
|---|
| 446 |
|
|---|
| 447 | Returns the row (most significant byte) of the Unicode character.
|
|---|
| 448 | */
|
|---|
| 449 |
|
|---|
| 450 | /*!
|
|---|
| 451 | Returns TRUE if the character is a printable character; otherwise
|
|---|
| 452 | returns FALSE. This is any character not of category Cc or Cn.
|
|---|
| 453 |
|
|---|
| 454 | Note that this gives no indication of whether the character is
|
|---|
| 455 | available in a particular \link QFont font\endlink.
|
|---|
| 456 | */
|
|---|
| 457 | bool QChar::isPrint() const
|
|---|
| 458 | {
|
|---|
| 459 | Category c = ::category( *this );
|
|---|
| 460 | return !(c == Other_Control || c == Other_NotAssigned);
|
|---|
| 461 | }
|
|---|
| 462 |
|
|---|
| 463 | /*!
|
|---|
| 464 | Returns TRUE if the character is a separator character
|
|---|
| 465 | (Separator_* categories); otherwise returns FALSE.
|
|---|
| 466 | */
|
|---|
| 467 | bool QChar::isSpace() const
|
|---|
| 468 | {
|
|---|
| 469 | return ::isSpace( *this );
|
|---|
| 470 | }
|
|---|
| 471 |
|
|---|
| 472 | /*!
|
|---|
| 473 | Returns TRUE if the character is a mark (Mark_* categories);
|
|---|
| 474 | otherwise returns FALSE.
|
|---|
| 475 | */
|
|---|
| 476 | bool QChar::isMark() const
|
|---|
| 477 | {
|
|---|
| 478 | Category c = ::category( *this );
|
|---|
| 479 | return c >= Mark_NonSpacing && c <= Mark_Enclosing;
|
|---|
| 480 | }
|
|---|
| 481 |
|
|---|
| 482 | /*!
|
|---|
| 483 | Returns TRUE if the character is a punctuation mark (Punctuation_*
|
|---|
| 484 | categories); otherwise returns FALSE.
|
|---|
| 485 | */
|
|---|
| 486 | bool QChar::isPunct() const
|
|---|
| 487 | {
|
|---|
| 488 | Category c = ::category( *this );
|
|---|
| 489 | return (c >= Punctuation_Connector && c <= Punctuation_Other);
|
|---|
| 490 | }
|
|---|
| 491 |
|
|---|
| 492 | /*!
|
|---|
| 493 | Returns TRUE if the character is a letter (Letter_* categories);
|
|---|
| 494 | otherwise returns FALSE.
|
|---|
| 495 | */
|
|---|
| 496 | bool QChar::isLetter() const
|
|---|
| 497 | {
|
|---|
| 498 | Category c = ::category( *this );
|
|---|
| 499 | return (c >= Letter_Uppercase && c <= Letter_Other);
|
|---|
| 500 | }
|
|---|
| 501 |
|
|---|
| 502 | /*!
|
|---|
| 503 | Returns TRUE if the character is a number (of any sort - Number_*
|
|---|
| 504 | categories); otherwise returns FALSE.
|
|---|
| 505 |
|
|---|
| 506 | \sa isDigit()
|
|---|
| 507 | */
|
|---|
| 508 | bool QChar::isNumber() const
|
|---|
| 509 | {
|
|---|
| 510 | Category c = ::category( *this );
|
|---|
| 511 | return c >= Number_DecimalDigit && c <= Number_Other;
|
|---|
| 512 | }
|
|---|
| 513 |
|
|---|
| 514 | /*!
|
|---|
| 515 | Returns TRUE if the character is a letter or number (Letter_* or
|
|---|
| 516 | Number_* categories); otherwise returns FALSE.
|
|---|
| 517 | */
|
|---|
| 518 | bool QChar::isLetterOrNumber() const
|
|---|
| 519 | {
|
|---|
| 520 | Category c = ::category( *this );
|
|---|
| 521 | return (c >= Letter_Uppercase && c <= Letter_Other)
|
|---|
| 522 | || (c >= Number_DecimalDigit && c <= Number_Other);
|
|---|
| 523 | }
|
|---|
| 524 |
|
|---|
| 525 |
|
|---|
| 526 | /*!
|
|---|
| 527 | Returns TRUE if the character is a decimal digit
|
|---|
| 528 | (Number_DecimalDigit); otherwise returns FALSE.
|
|---|
| 529 | */
|
|---|
| 530 | bool QChar::isDigit() const
|
|---|
| 531 | {
|
|---|
| 532 | return (::category( *this ) == Number_DecimalDigit);
|
|---|
| 533 | }
|
|---|
| 534 |
|
|---|
| 535 |
|
|---|
| 536 | /*!
|
|---|
| 537 | Returns TRUE if the character is a symbol (Symbol_* categories);
|
|---|
| 538 | otherwise returns FALSE.
|
|---|
| 539 | */
|
|---|
| 540 | bool QChar::isSymbol() const
|
|---|
| 541 | {
|
|---|
| 542 | Category c = ::category( *this );
|
|---|
| 543 | return c >= Symbol_Math && c <= Symbol_Other;
|
|---|
| 544 | }
|
|---|
| 545 |
|
|---|
| 546 | /*!
|
|---|
| 547 | Returns the numeric value of the digit, or -1 if the character is
|
|---|
| 548 | not a digit.
|
|---|
| 549 | */
|
|---|
| 550 | int QChar::digitValue() const
|
|---|
| 551 | {
|
|---|
| 552 | #ifndef QT_NO_UNICODETABLES
|
|---|
| 553 | register int pos = QUnicodeTables::decimal_info[row()];
|
|---|
| 554 | if( !pos )
|
|---|
| 555 | return -1;
|
|---|
| 556 | return QUnicodeTables::decimal_info[(pos<<8) + cell()];
|
|---|
| 557 | #else
|
|---|
| 558 | // ##### just latin1
|
|---|
| 559 | if ( ucs < '0' || ucs > '9' )
|
|---|
| 560 | return -1;
|
|---|
| 561 | else
|
|---|
| 562 | return ucs - '0';
|
|---|
| 563 | #endif
|
|---|
| 564 | }
|
|---|
| 565 |
|
|---|
| 566 | /*!
|
|---|
| 567 | Returns the character category.
|
|---|
| 568 |
|
|---|
| 569 | \sa Category
|
|---|
| 570 | */
|
|---|
| 571 | QChar::Category QChar::category() const
|
|---|
| 572 | {
|
|---|
| 573 | return ::category( *this );
|
|---|
| 574 | }
|
|---|
| 575 |
|
|---|
| 576 | /*!
|
|---|
| 577 | Returns the character's direction.
|
|---|
| 578 |
|
|---|
| 579 | \sa Direction
|
|---|
| 580 | */
|
|---|
| 581 | QChar::Direction QChar::direction() const
|
|---|
| 582 | {
|
|---|
| 583 | return ::direction( *this );
|
|---|
| 584 | }
|
|---|
| 585 |
|
|---|
| 586 | /*!
|
|---|
| 587 | \warning This function is not supported (it may change to use
|
|---|
| 588 | Unicode character classes).
|
|---|
| 589 |
|
|---|
| 590 | Returns information about the joining properties of the character
|
|---|
| 591 | (needed for example, for Arabic).
|
|---|
| 592 | */
|
|---|
| 593 | QChar::Joining QChar::joining() const
|
|---|
| 594 | {
|
|---|
| 595 | return ::joining( *this );
|
|---|
| 596 | }
|
|---|
| 597 |
|
|---|
| 598 |
|
|---|
| 599 | /*!
|
|---|
| 600 | Returns TRUE if the character is a mirrored character (one that
|
|---|
| 601 | should be reversed if the text direction is reversed); otherwise
|
|---|
| 602 | returns FALSE.
|
|---|
| 603 | */
|
|---|
| 604 | bool QChar::mirrored() const
|
|---|
| 605 | {
|
|---|
| 606 | return ::mirrored( *this );
|
|---|
| 607 | }
|
|---|
| 608 |
|
|---|
| 609 | /*!
|
|---|
| 610 | Returns the mirrored character if this character is a mirrored
|
|---|
| 611 | character, otherwise returns the character itself.
|
|---|
| 612 | */
|
|---|
| 613 | QChar QChar::mirroredChar() const
|
|---|
| 614 | {
|
|---|
| 615 | return ::mirroredChar( *this );
|
|---|
| 616 | }
|
|---|
| 617 |
|
|---|
| 618 | #ifndef QT_NO_UNICODETABLES
|
|---|
| 619 | // ### REMOVE ME 4.0
|
|---|
| 620 | static QString shared_decomp;
|
|---|
| 621 | #endif
|
|---|
| 622 | /*!
|
|---|
| 623 | \nonreentrant
|
|---|
| 624 |
|
|---|
| 625 | Decomposes a character into its parts. Returns QString::null if no
|
|---|
| 626 | decomposition exists.
|
|---|
| 627 | */
|
|---|
| 628 | const QString &QChar::decomposition() const
|
|---|
| 629 | {
|
|---|
| 630 | #ifndef QT_NO_UNICODETABLES
|
|---|
| 631 | register int pos = QUnicodeTables::decomposition_info[row()];
|
|---|
| 632 | if(!pos) return QString::null;
|
|---|
| 633 |
|
|---|
| 634 | pos = QUnicodeTables::decomposition_info[(pos<<8)+cell()];
|
|---|
| 635 | if(!pos) return QString::null;
|
|---|
| 636 | pos+=2;
|
|---|
| 637 |
|
|---|
| 638 | QString s;
|
|---|
| 639 | Q_UINT16 c;
|
|---|
| 640 | while ( (c = QUnicodeTables::decomposition_map[pos++]) != 0 )
|
|---|
| 641 | s += QChar( c );
|
|---|
| 642 | // ### In 4.0, return s, and not shared_decomp. shared_decomp
|
|---|
| 643 | // prevents this function from being reentrant.
|
|---|
| 644 | shared_decomp = s;
|
|---|
| 645 | return shared_decomp;
|
|---|
| 646 | #else
|
|---|
| 647 | return QString::null;
|
|---|
| 648 | #endif
|
|---|
| 649 | }
|
|---|
| 650 |
|
|---|
| 651 | /*!
|
|---|
| 652 | Returns the tag defining the composition of the character. Returns
|
|---|
| 653 | QChar::Single if no decomposition exists.
|
|---|
| 654 | */
|
|---|
| 655 | QChar::Decomposition QChar::decompositionTag() const
|
|---|
| 656 | {
|
|---|
| 657 | #ifndef QT_NO_UNICODETABLES
|
|---|
| 658 | register int pos = QUnicodeTables::decomposition_info[row()];
|
|---|
| 659 | if(!pos) return QChar::Single;
|
|---|
| 660 |
|
|---|
| 661 | pos = QUnicodeTables::decomposition_info[(pos<<8)+cell()];
|
|---|
| 662 | if(!pos) return QChar::Single;
|
|---|
| 663 |
|
|---|
| 664 | return (QChar::Decomposition) QUnicodeTables::decomposition_map[pos];
|
|---|
| 665 | #else
|
|---|
| 666 | return Single; // ########### FIX eg. just latin1
|
|---|
| 667 | #endif
|
|---|
| 668 | }
|
|---|
| 669 |
|
|---|
| 670 | /*!
|
|---|
| 671 | Returns the combining class for the character as defined in the
|
|---|
| 672 | Unicode standard. This is mainly useful as a positioning hint for
|
|---|
| 673 | marks attached to a base character.
|
|---|
| 674 |
|
|---|
| 675 | The Qt text rendering engine uses this information to correctly
|
|---|
| 676 | position non spacing marks around a base character.
|
|---|
| 677 | */
|
|---|
| 678 | unsigned char QChar::combiningClass() const
|
|---|
| 679 | {
|
|---|
| 680 | return ::combiningClass( *this );
|
|---|
| 681 | }
|
|---|
| 682 |
|
|---|
| 683 |
|
|---|
| 684 | /*!
|
|---|
| 685 | Returns the lowercase equivalent if the character is uppercase;
|
|---|
| 686 | otherwise returns the character itself.
|
|---|
| 687 | */
|
|---|
| 688 | QChar QChar::lower() const
|
|---|
| 689 | {
|
|---|
| 690 | return ::lower( *this );
|
|---|
| 691 | }
|
|---|
| 692 |
|
|---|
| 693 | /*!
|
|---|
| 694 | Returns the uppercase equivalent if the character is lowercase;
|
|---|
| 695 | otherwise returns the character itself.
|
|---|
| 696 | */
|
|---|
| 697 | QChar QChar::upper() const
|
|---|
| 698 | {
|
|---|
| 699 | return ::upper( *this );
|
|---|
| 700 | }
|
|---|
| 701 |
|
|---|
| 702 | /*!
|
|---|
| 703 | \fn QChar::operator char() const
|
|---|
| 704 |
|
|---|
| 705 | Returns the Latin-1 character equivalent to the QChar, or 0. This
|
|---|
| 706 | is mainly useful for non-internationalized software.
|
|---|
| 707 |
|
|---|
| 708 | \sa unicode()
|
|---|
| 709 | */
|
|---|
| 710 |
|
|---|
| 711 | /*!
|
|---|
| 712 | \fn ushort QChar::unicode() const
|
|---|
| 713 |
|
|---|
| 714 | Returns the numeric Unicode value equal to the QChar. Normally,
|
|---|
| 715 | you should use QChar objects as they are equivalent, but for some
|
|---|
| 716 | low-level tasks (e.g. indexing into an array of Unicode
|
|---|
| 717 | information), this function is useful.
|
|---|
| 718 | */
|
|---|
| 719 |
|
|---|
| 720 | /*!
|
|---|
| 721 | \fn ushort & QChar::unicode()
|
|---|
| 722 |
|
|---|
| 723 | \overload
|
|---|
| 724 |
|
|---|
| 725 | Returns a reference to the numeric Unicode value equal to the
|
|---|
| 726 | QChar.
|
|---|
| 727 | */
|
|---|
| 728 |
|
|---|
| 729 | /*****************************************************************************
|
|---|
| 730 | Documentation of QChar related functions
|
|---|
| 731 | *****************************************************************************/
|
|---|
| 732 |
|
|---|
| 733 | /*!
|
|---|
| 734 | \fn bool operator==( QChar c1, QChar c2 )
|
|---|
| 735 |
|
|---|
| 736 | \relates QChar
|
|---|
| 737 |
|
|---|
| 738 | Returns TRUE if \a c1 and \a c2 are the same Unicode character;
|
|---|
| 739 | otherwise returns FALSE.
|
|---|
| 740 | */
|
|---|
| 741 |
|
|---|
| 742 | /*!
|
|---|
| 743 | \fn bool operator==( char ch, QChar c )
|
|---|
| 744 |
|
|---|
| 745 | \overload
|
|---|
| 746 | \relates QChar
|
|---|
| 747 |
|
|---|
| 748 | Returns TRUE if \a c is the ASCII/Latin-1 character \a ch;
|
|---|
| 749 | otherwise returns FALSE.
|
|---|
| 750 | */
|
|---|
| 751 |
|
|---|
| 752 | /*!
|
|---|
| 753 | \fn bool operator==( QChar c, char ch )
|
|---|
| 754 |
|
|---|
| 755 | \overload
|
|---|
| 756 | \relates QChar
|
|---|
| 757 |
|
|---|
| 758 | Returns TRUE if \a c is the ASCII/Latin-1 character \a ch;
|
|---|
| 759 | otherwise returns FALSE.
|
|---|
| 760 | */
|
|---|
| 761 |
|
|---|
| 762 | /*!
|
|---|
| 763 | \fn int operator!=( QChar c1, QChar c2 )
|
|---|
| 764 |
|
|---|
| 765 | \relates QChar
|
|---|
| 766 |
|
|---|
| 767 | Returns TRUE if \a c1 and \a c2 are not the same Unicode
|
|---|
| 768 | character; otherwise returns FALSE.
|
|---|
| 769 | */
|
|---|
| 770 |
|
|---|
| 771 | /*!
|
|---|
| 772 | \fn int operator!=( char ch, QChar c )
|
|---|
| 773 |
|
|---|
| 774 | \overload
|
|---|
| 775 | \relates QChar
|
|---|
| 776 |
|
|---|
| 777 | Returns TRUE if \a c is not the ASCII/Latin-1 character \a ch;
|
|---|
| 778 | otherwise returns FALSE.
|
|---|
| 779 | */
|
|---|
| 780 |
|
|---|
| 781 | /*!
|
|---|
| 782 | \fn int operator!=( QChar c, char ch )
|
|---|
| 783 |
|
|---|
| 784 | \overload
|
|---|
| 785 | \relates QChar
|
|---|
| 786 |
|
|---|
| 787 | Returns TRUE if \a c is not the ASCII/Latin-1 character \a ch;
|
|---|
| 788 | otherwise returns FALSE.
|
|---|
| 789 | */
|
|---|
| 790 |
|
|---|
| 791 | /*!
|
|---|
| 792 | \fn int operator<=( QChar c1, QChar c2 )
|
|---|
| 793 |
|
|---|
| 794 | \relates QChar
|
|---|
| 795 |
|
|---|
| 796 | Returns TRUE if the numeric Unicode value of \a c1 is less than
|
|---|
| 797 | that of \a c2, or they are the same Unicode character; otherwise
|
|---|
| 798 | returns FALSE.
|
|---|
| 799 | */
|
|---|
| 800 |
|
|---|
| 801 | /*!
|
|---|
| 802 | \fn int operator<=( QChar c, char ch )
|
|---|
| 803 |
|
|---|
| 804 | \overload
|
|---|
| 805 | \relates QChar
|
|---|
| 806 |
|
|---|
| 807 | Returns TRUE if the numeric Unicode value of \a c is less than or
|
|---|
| 808 | equal to that of the ASCII/Latin-1 character \a ch; otherwise
|
|---|
| 809 | returns FALSE.
|
|---|
| 810 | */
|
|---|
| 811 |
|
|---|
| 812 | /*!
|
|---|
| 813 | \fn int operator<=( char ch, QChar c )
|
|---|
| 814 |
|
|---|
| 815 | \overload
|
|---|
| 816 | \relates QChar
|
|---|
| 817 |
|
|---|
| 818 | Returns TRUE if the numeric Unicode value of the ASCII/Latin-1
|
|---|
| 819 | character \a ch is less than or equal to that of \a c; otherwise
|
|---|
| 820 | returns FALSE.
|
|---|
| 821 | */
|
|---|
| 822 |
|
|---|
| 823 | /*!
|
|---|
| 824 | \fn int operator>=( QChar c1, QChar c2 )
|
|---|
| 825 |
|
|---|
| 826 | \relates QChar
|
|---|
| 827 |
|
|---|
| 828 | Returns TRUE if the numeric Unicode value of \a c1 is greater than
|
|---|
| 829 | that of \a c2, or they are the same Unicode character; otherwise
|
|---|
| 830 | returns FALSE.
|
|---|
| 831 | */
|
|---|
| 832 |
|
|---|
| 833 | /*!
|
|---|
| 834 | \fn int operator>=( QChar c, char ch )
|
|---|
| 835 |
|
|---|
| 836 | \overload
|
|---|
| 837 | \relates QChar
|
|---|
| 838 |
|
|---|
| 839 | Returns TRUE if the numeric Unicode value of \a c is greater than
|
|---|
| 840 | or equal to that of the ASCII/Latin-1 character \a ch; otherwise
|
|---|
| 841 | returns FALSE.
|
|---|
| 842 | */
|
|---|
| 843 |
|
|---|
| 844 | /*!
|
|---|
| 845 | \fn int operator>=( char ch, QChar c )
|
|---|
| 846 |
|
|---|
| 847 | \overload
|
|---|
| 848 | \relates QChar
|
|---|
| 849 |
|
|---|
| 850 | Returns TRUE if the numeric Unicode value of the ASCII/Latin-1
|
|---|
| 851 | character \a ch is greater than or equal to that of \a c;
|
|---|
| 852 | otherwise returns FALSE.
|
|---|
| 853 | */
|
|---|
| 854 |
|
|---|
| 855 | /*!
|
|---|
| 856 | \fn int operator<( QChar c1, QChar c2 )
|
|---|
| 857 |
|
|---|
| 858 | \relates QChar
|
|---|
| 859 |
|
|---|
| 860 | Returns TRUE if the numeric Unicode value of \a c1 is less than
|
|---|
| 861 | that of \a c2; otherwise returns FALSE.
|
|---|
| 862 | */
|
|---|
| 863 |
|
|---|
| 864 | /*!
|
|---|
| 865 | \fn int operator<( QChar c, char ch )
|
|---|
| 866 |
|
|---|
| 867 | \overload
|
|---|
| 868 | \relates QChar
|
|---|
| 869 |
|
|---|
| 870 | Returns TRUE if the numeric Unicode value of \a c is less than that
|
|---|
| 871 | of the ASCII/Latin-1 character \a ch; otherwise returns FALSE.
|
|---|
| 872 | */
|
|---|
| 873 |
|
|---|
| 874 | /*!
|
|---|
| 875 | \fn int operator<( char ch, QChar c )
|
|---|
| 876 |
|
|---|
| 877 | \overload
|
|---|
| 878 | \relates QChar
|
|---|
| 879 |
|
|---|
| 880 | Returns TRUE if the numeric Unicode value of the ASCII/Latin-1
|
|---|
| 881 | character \a ch is less than that of \a c; otherwise returns
|
|---|
| 882 | FALSE.
|
|---|
| 883 | */
|
|---|
| 884 |
|
|---|
| 885 | /*!
|
|---|
| 886 | \fn int operator>( QChar c1, QChar c2 )
|
|---|
| 887 |
|
|---|
| 888 | \relates QChar
|
|---|
| 889 |
|
|---|
| 890 | Returns TRUE if the numeric Unicode value of \a c1 is greater than
|
|---|
| 891 | that of \a c2; otherwise returns FALSE.
|
|---|
| 892 | */
|
|---|
| 893 |
|
|---|
| 894 | /*!
|
|---|
| 895 | \fn int operator>( QChar c, char ch )
|
|---|
| 896 |
|
|---|
| 897 | \overload
|
|---|
| 898 | \relates QChar
|
|---|
| 899 |
|
|---|
| 900 | Returns TRUE if the numeric Unicode value of \a c is greater than
|
|---|
| 901 | that of the ASCII/Latin-1 character \a ch; otherwise returns FALSE.
|
|---|
| 902 | */
|
|---|
| 903 |
|
|---|
| 904 | /*!
|
|---|
| 905 | \fn int operator>( char ch, QChar c )
|
|---|
| 906 |
|
|---|
| 907 | \overload
|
|---|
| 908 | \relates QChar
|
|---|
| 909 |
|
|---|
| 910 | Returns TRUE if the numeric Unicode value of the ASCII/Latin-1
|
|---|
| 911 | character \a ch is greater than that of \a c; otherwise returns
|
|---|
| 912 | FALSE.
|
|---|
| 913 | */
|
|---|
| 914 |
|
|---|
| 915 | #ifndef QT_NO_UNICODETABLES
|
|---|
| 916 |
|
|---|
| 917 | // small class used internally in QString::Compose()
|
|---|
| 918 | class QLigature
|
|---|
| 919 | {
|
|---|
| 920 | public:
|
|---|
| 921 | QLigature( QChar c );
|
|---|
| 922 |
|
|---|
| 923 | Q_UINT16 first() { cur = ligatures; return cur ? *cur : 0; }
|
|---|
| 924 | Q_UINT16 next() { return cur && *cur ? *(cur++) : 0; }
|
|---|
| 925 | Q_UINT16 current() { return cur ? *cur : 0; }
|
|---|
| 926 |
|
|---|
| 927 | int match(QString & str, unsigned int index);
|
|---|
| 928 | QChar head();
|
|---|
| 929 | QChar::Decomposition tag();
|
|---|
| 930 |
|
|---|
| 931 | private:
|
|---|
| 932 | Q_UINT16 *ligatures;
|
|---|
| 933 | Q_UINT16 *cur;
|
|---|
| 934 | };
|
|---|
| 935 |
|
|---|
| 936 | QLigature::QLigature( QChar c )
|
|---|
| 937 | {
|
|---|
| 938 | register int pos = QUnicodeTables::ligature_info[c.row()];
|
|---|
| 939 | if( !pos )
|
|---|
| 940 | ligatures = 0;
|
|---|
| 941 | else
|
|---|
| 942 | {
|
|---|
| 943 | pos = QUnicodeTables::ligature_info[(pos<<8)+c.cell()];
|
|---|
| 944 | ligatures = (Q_UINT16 *)&(QUnicodeTables::ligature_map[pos]);
|
|---|
| 945 | }
|
|---|
| 946 | cur = ligatures;
|
|---|
| 947 | }
|
|---|
| 948 |
|
|---|
| 949 | QChar QLigature::head()
|
|---|
| 950 | {
|
|---|
| 951 | if(current())
|
|---|
| 952 | return QChar(QUnicodeTables::decomposition_map[current()+1]);
|
|---|
| 953 |
|
|---|
| 954 | return QChar::null;
|
|---|
| 955 | }
|
|---|
| 956 |
|
|---|
| 957 | QChar::Decomposition QLigature::tag()
|
|---|
| 958 | {
|
|---|
| 959 | if(current())
|
|---|
| 960 | return (QChar::Decomposition) QUnicodeTables::decomposition_map[current()];
|
|---|
| 961 |
|
|---|
| 962 | return QChar::Canonical;
|
|---|
| 963 | }
|
|---|
| 964 |
|
|---|
| 965 | int QLigature::match(QString & str, unsigned int index)
|
|---|
| 966 | {
|
|---|
| 967 | unsigned int i=index;
|
|---|
| 968 |
|
|---|
| 969 | if(!current()) return 0;
|
|---|
| 970 |
|
|---|
| 971 | Q_UINT16 lig = current() + 2;
|
|---|
| 972 | Q_UINT16 ch;
|
|---|
| 973 |
|
|---|
| 974 | while ((i < str.length()) && (ch = QUnicodeTables::decomposition_map[lig])) {
|
|---|
| 975 | if (str[(int)i] != QChar(ch))
|
|---|
| 976 | return 0;
|
|---|
| 977 | i++;
|
|---|
| 978 | lig++;
|
|---|
| 979 | }
|
|---|
| 980 |
|
|---|
| 981 | if (!QUnicodeTables::decomposition_map[lig])
|
|---|
| 982 | {
|
|---|
| 983 | return i-index;
|
|---|
| 984 | }
|
|---|
| 985 | return 0;
|
|---|
| 986 | }
|
|---|
| 987 |
|
|---|
| 988 |
|
|---|
| 989 | // this function is just used in QString::compose()
|
|---|
| 990 | static inline bool format(QChar::Decomposition tag, QString & str,
|
|---|
| 991 | int index, int len)
|
|---|
| 992 | {
|
|---|
| 993 | unsigned int l = index + len;
|
|---|
| 994 | unsigned int r = index;
|
|---|
| 995 |
|
|---|
| 996 | bool left = FALSE, right = FALSE;
|
|---|
| 997 |
|
|---|
| 998 | left = ((l < str.length()) &&
|
|---|
| 999 | ((str[(int)l].joining() == QChar::Dual) ||
|
|---|
| 1000 | (str[(int)l].joining() == QChar::Right)));
|
|---|
| 1001 | if (r > 0) {
|
|---|
| 1002 | r--;
|
|---|
| 1003 | //printf("joining(right) = %d\n", str[(int)r].joining());
|
|---|
| 1004 | right = (str[(int)r].joining() == QChar::Dual);
|
|---|
| 1005 | }
|
|---|
| 1006 |
|
|---|
| 1007 |
|
|---|
| 1008 | switch (tag) {
|
|---|
| 1009 | case QChar::Medial:
|
|---|
| 1010 | return (left & right);
|
|---|
| 1011 | case QChar::Initial:
|
|---|
| 1012 | return (left && !right);
|
|---|
| 1013 | case QChar::Final:
|
|---|
| 1014 | return (right);// && !left);
|
|---|
| 1015 | case QChar::Isolated:
|
|---|
| 1016 | default:
|
|---|
| 1017 | return (!right && !left);
|
|---|
| 1018 | }
|
|---|
| 1019 | } // format()
|
|---|
| 1020 | #endif
|
|---|
| 1021 |
|
|---|
| 1022 | /*
|
|---|
| 1023 | QString::compose() and visual() were developed by Gordon Tisher
|
|---|
| 1024 | <tisher@uniserve.ca>, with input from Lars Knoll <knoll@mpi-hd.mpg.de>,
|
|---|
| 1025 | who developed the unicode data tables.
|
|---|
| 1026 | */
|
|---|
| 1027 | /*!
|
|---|
| 1028 | \warning This function is not supported in Qt 3.x. It is provided
|
|---|
| 1029 | for experimental and illustrative purposes only. It is mainly of
|
|---|
| 1030 | interest to those experimenting with Arabic and other
|
|---|
| 1031 | composition-rich texts.
|
|---|
| 1032 |
|
|---|
| 1033 | Applies possible ligatures to a QString. Useful when
|
|---|
| 1034 | composition-rich text requires rendering with glyph-poor fonts,
|
|---|
| 1035 | but it also makes compositions such as QChar(0x0041) ('A') and
|
|---|
| 1036 | QChar(0x0308) (Unicode accent diaresis), giving QChar(0x00c4)
|
|---|
| 1037 | (German A Umlaut).
|
|---|
| 1038 | */
|
|---|
| 1039 | void QString::compose()
|
|---|
| 1040 | {
|
|---|
| 1041 | #ifndef QT_NO_UNICODETABLES
|
|---|
| 1042 | unsigned int index=0, len;
|
|---|
| 1043 | unsigned int cindex = 0;
|
|---|
| 1044 |
|
|---|
| 1045 | QChar code, head;
|
|---|
| 1046 |
|
|---|
| 1047 | QMemArray<QChar> dia;
|
|---|
| 1048 |
|
|---|
| 1049 | QString composed = *this;
|
|---|
| 1050 |
|
|---|
| 1051 | while (index < length()) {
|
|---|
| 1052 | code = at(index);
|
|---|
| 1053 | //printf("\n\nligature for 0x%x:\n", code.unicode());
|
|---|
| 1054 | QLigature ligature(code);
|
|---|
| 1055 | ligature.first();
|
|---|
| 1056 | while ( ligature.current() ) {
|
|---|
| 1057 | if ((len = ligature.match(*this, index)) != 0) {
|
|---|
| 1058 | head = ligature.head();
|
|---|
| 1059 | unsigned short code = head.unicode();
|
|---|
| 1060 | // we exclude Arabic presentation forms A and a few
|
|---|
| 1061 | // other ligatures, which are undefined in most fonts
|
|---|
| 1062 | if(!(code > 0xfb50 && code < 0xfe80) &&
|
|---|
| 1063 | !(code > 0xfb00 && code < 0xfb2a)) {
|
|---|
| 1064 | // joining info is only needed for Arabic
|
|---|
| 1065 | if (format(ligature.tag(), *this, index, len)) {
|
|---|
| 1066 | //printf("using ligature 0x%x, len=%d\n",code,len);
|
|---|
| 1067 | // replace letter
|
|---|
| 1068 | composed.replace(cindex, len, QChar(head));
|
|---|
| 1069 | index += len-1;
|
|---|
| 1070 | // we continue searching in case we have a final
|
|---|
| 1071 | // form because medial ones are preferred.
|
|---|
| 1072 | if ( len != 1 || ligature.tag() !=QChar::Final )
|
|---|
| 1073 | break;
|
|---|
| 1074 | }
|
|---|
| 1075 | }
|
|---|
| 1076 | }
|
|---|
| 1077 | ligature.next();
|
|---|
| 1078 | }
|
|---|
| 1079 | cindex++;
|
|---|
| 1080 | index++;
|
|---|
| 1081 | }
|
|---|
| 1082 | *this = composed;
|
|---|
| 1083 | #endif
|
|---|
| 1084 | }
|
|---|
| 1085 |
|
|---|
| 1086 |
|
|---|
| 1087 | // These macros are used for efficient allocation of QChar strings.
|
|---|
| 1088 | // IMPORTANT! If you change these, make sure you also change the
|
|---|
| 1089 | // "delete unicode" statement in ~QStringData() in qstring.h correspondingly!
|
|---|
| 1090 |
|
|---|
| 1091 | #define QT_ALLOC_QCHAR_VEC( N ) (QChar*) new char[ sizeof(QChar)*( N ) ]
|
|---|
| 1092 | #define QT_DELETE_QCHAR_VEC( P ) delete[] ((char*)( P ))
|
|---|
| 1093 |
|
|---|
| 1094 |
|
|---|
| 1095 | /*!
|
|---|
| 1096 | This utility function converts the 8-bit string \a ba to Unicode,
|
|---|
| 1097 | returning the result.
|
|---|
| 1098 |
|
|---|
| 1099 | The caller is responsible for deleting the return value with
|
|---|
| 1100 | delete[].
|
|---|
| 1101 | */
|
|---|
| 1102 |
|
|---|
| 1103 | QChar* QString::latin1ToUnicode( const QByteArray& ba, uint* len )
|
|---|
| 1104 | {
|
|---|
| 1105 | if ( ba.isNull() ) {
|
|---|
| 1106 | *len = 0;
|
|---|
| 1107 | return 0;
|
|---|
| 1108 | }
|
|---|
| 1109 | int l = 0;
|
|---|
| 1110 | while ( l < (int)ba.size() && ba[l] )
|
|---|
| 1111 | l++;
|
|---|
| 1112 | char* str = ba.data();
|
|---|
| 1113 | QChar *uc = new QChar[ l ]; // Can't use macro, since function is public
|
|---|
| 1114 | QChar *result = uc;
|
|---|
| 1115 | if ( len )
|
|---|
| 1116 | *len = l;
|
|---|
| 1117 | while (l--)
|
|---|
| 1118 | *uc++ = *str++;
|
|---|
| 1119 | return result;
|
|---|
| 1120 | }
|
|---|
| 1121 |
|
|---|
| 1122 | static QChar* internalLatin1ToUnicode( const QByteArray& ba, uint* len )
|
|---|
| 1123 | {
|
|---|
| 1124 | if ( ba.isNull() ) {
|
|---|
| 1125 | *len = 0;
|
|---|
| 1126 | return 0;
|
|---|
| 1127 | }
|
|---|
| 1128 | int l = 0;
|
|---|
| 1129 | while ( l < (int)ba.size() && ba[l] )
|
|---|
| 1130 | l++;
|
|---|
| 1131 | char* str = ba.data();
|
|---|
| 1132 | QChar *uc = QT_ALLOC_QCHAR_VEC( l );
|
|---|
| 1133 | QChar *result = uc;
|
|---|
| 1134 | if ( len )
|
|---|
| 1135 | *len = l;
|
|---|
| 1136 | while (l--)
|
|---|
| 1137 | *uc++ = *str++;
|
|---|
| 1138 | return result;
|
|---|
| 1139 | }
|
|---|
| 1140 |
|
|---|
| 1141 | /*!
|
|---|
| 1142 | \overload
|
|---|
| 1143 |
|
|---|
| 1144 | This utility function converts the '\0'-terminated 8-bit string \a
|
|---|
| 1145 | str to Unicode, returning the result and setting \a *len to the
|
|---|
| 1146 | length of the Unicode string.
|
|---|
| 1147 |
|
|---|
| 1148 | The caller is responsible for deleting the return value with
|
|---|
| 1149 | delete[].
|
|---|
| 1150 | */
|
|---|
| 1151 |
|
|---|
| 1152 | QChar* QString::latin1ToUnicode( const char *str, uint* len, uint maxlen )
|
|---|
| 1153 | {
|
|---|
| 1154 | QChar* result = 0;
|
|---|
| 1155 | uint l = 0;
|
|---|
| 1156 | if ( str ) {
|
|---|
| 1157 | if ( maxlen != (uint)-1 ) {
|
|---|
| 1158 | while ( l < maxlen && str[l] )
|
|---|
| 1159 | l++;
|
|---|
| 1160 | } else {
|
|---|
| 1161 | // Faster?
|
|---|
| 1162 | l = strlen( str );
|
|---|
| 1163 | }
|
|---|
| 1164 | QChar *uc = new QChar[ l ]; // Can't use macro since function is public
|
|---|
| 1165 | result = uc;
|
|---|
| 1166 | uint i = l;
|
|---|
| 1167 | while ( i-- )
|
|---|
| 1168 | *uc++ = *str++;
|
|---|
| 1169 | }
|
|---|
| 1170 | if ( len )
|
|---|
| 1171 | *len = l;
|
|---|
| 1172 | return result;
|
|---|
| 1173 | }
|
|---|
| 1174 |
|
|---|
| 1175 | static QChar* internalLatin1ToUnicode( const char *str, uint* len,
|
|---|
| 1176 | uint maxlen = (uint)-1 )
|
|---|
| 1177 | {
|
|---|
| 1178 | QChar* result = 0;
|
|---|
| 1179 | uint l = 0;
|
|---|
| 1180 | if ( str ) {
|
|---|
| 1181 | if ( maxlen != (uint)-1 ) {
|
|---|
| 1182 | while ( l < maxlen && str[l] )
|
|---|
| 1183 | l++;
|
|---|
| 1184 | } else {
|
|---|
| 1185 | // Faster?
|
|---|
| 1186 | l = strlen( str );
|
|---|
| 1187 | }
|
|---|
| 1188 | QChar *uc = QT_ALLOC_QCHAR_VEC( l );
|
|---|
| 1189 | result = uc;
|
|---|
| 1190 | uint i = l;
|
|---|
| 1191 | while ( i-- )
|
|---|
| 1192 | *uc++ = *str++;
|
|---|
| 1193 | }
|
|---|
| 1194 | if ( len )
|
|---|
| 1195 | *len = l;
|
|---|
| 1196 | return result;
|
|---|
| 1197 | }
|
|---|
| 1198 |
|
|---|
| 1199 | /*!
|
|---|
| 1200 | This utility function converts \a l 16-bit characters from \a uc
|
|---|
| 1201 | to ASCII, returning a '\0'-terminated string.
|
|---|
| 1202 |
|
|---|
| 1203 | The caller is responsible for deleting the resultant string with
|
|---|
| 1204 | delete[].
|
|---|
| 1205 | */
|
|---|
| 1206 | char* QString::unicodeToLatin1(const QChar *uc, uint l)
|
|---|
| 1207 | {
|
|---|
| 1208 | if (!uc) {
|
|---|
| 1209 | return 0;
|
|---|
| 1210 | }
|
|---|
| 1211 | char *a = new char[l+1];
|
|---|
| 1212 | char *result = a;
|
|---|
| 1213 | while (l--) {
|
|---|
| 1214 | *a++ = (uc->unicode() > 0xff) ? '?' : (char)uc->unicode();
|
|---|
| 1215 | uc++;
|
|---|
| 1216 | }
|
|---|
| 1217 | *a = '\0';
|
|---|
| 1218 | return result;
|
|---|
| 1219 | }
|
|---|
| 1220 |
|
|---|
| 1221 | /*****************************************************************************
|
|---|
| 1222 | QString member functions
|
|---|
| 1223 | *****************************************************************************/
|
|---|
| 1224 |
|
|---|
| 1225 | /*!
|
|---|
| 1226 | \class QString qstring.h
|
|---|
| 1227 | \reentrant
|
|---|
| 1228 |
|
|---|
| 1229 | \brief The QString class provides an abstraction of Unicode text
|
|---|
| 1230 | and the classic C '\0'-terminated char array.
|
|---|
| 1231 |
|
|---|
| 1232 | \ingroup tools
|
|---|
| 1233 | \ingroup shared
|
|---|
| 1234 | \ingroup text
|
|---|
| 1235 | \mainclass
|
|---|
| 1236 |
|
|---|
| 1237 | QString uses \link shclass.html implicit sharing\endlink, which
|
|---|
| 1238 | makes it very efficient and easy to use.
|
|---|
| 1239 |
|
|---|
| 1240 | In all of the QString methods that take \c {const char *}
|
|---|
| 1241 | parameters, the \c {const char *} is interpreted as a classic
|
|---|
| 1242 | C-style '\0'-terminated ASCII string. It is legal for the \c
|
|---|
| 1243 | {const char *} parameter to be 0. If the \c {const char *} is not
|
|---|
| 1244 | '\0'-terminated, the results are undefined. Functions that copy
|
|---|
| 1245 | classic C strings into a QString will not copy the terminating
|
|---|
| 1246 | '\0' character. The QChar array of the QString (as returned by
|
|---|
| 1247 | unicode()) is generally not terminated by a '\0'. If you need to
|
|---|
| 1248 | pass a QString to a function that requires a C '\0'-terminated
|
|---|
| 1249 | string use latin1().
|
|---|
| 1250 |
|
|---|
| 1251 | \keyword QString::null
|
|---|
| 1252 | A QString that has not been assigned to anything is \e null, i.e.
|
|---|
| 1253 | both the length and data pointer is 0. A QString that references
|
|---|
| 1254 | the empty string ("", a single '\0' char) is \e empty. Both null
|
|---|
| 1255 | and empty QStrings are legal parameters to the methods. Assigning
|
|---|
| 1256 | \c{(const char *) 0} to QString gives a null QString. For
|
|---|
| 1257 | convenience, \c QString::null is a null QString. When sorting,
|
|---|
| 1258 | empty strings come first, followed by non-empty strings, followed
|
|---|
| 1259 | by null strings. We recommend using \c{if ( !str.isNull() )} to
|
|---|
| 1260 | check for a non-null string rather than \c{if ( !str )}; see \l
|
|---|
| 1261 | operator!() for an explanation.
|
|---|
| 1262 |
|
|---|
| 1263 | Note that if you find that you are mixing usage of \l QCString,
|
|---|
| 1264 | QString, and \l QByteArray, this causes lots of unnecessary
|
|---|
| 1265 | copying and might indicate that the true nature of the data you
|
|---|
| 1266 | are dealing with is uncertain. If the data is '\0'-terminated 8-bit
|
|---|
| 1267 | data, use \l QCString; if it is unterminated (i.e. contains '\0's)
|
|---|
| 1268 | 8-bit data, use \l QByteArray; if it is text, use QString.
|
|---|
| 1269 |
|
|---|
| 1270 | Lists of strings are handled by the QStringList class. You can
|
|---|
| 1271 | split a string into a list of strings using QStringList::split(),
|
|---|
| 1272 | and join a list of strings into a single string with an optional
|
|---|
| 1273 | separator using QStringList::join(). You can obtain a list of
|
|---|
| 1274 | strings from a string list that contain a particular substring or
|
|---|
| 1275 | that match a particular \link qregexp.html regex\endlink using
|
|---|
| 1276 | QStringList::grep().
|
|---|
| 1277 |
|
|---|
| 1278 | <b>Note for C programmers</b>
|
|---|
| 1279 |
|
|---|
| 1280 | Due to C++'s type system and the fact that QString is implicitly
|
|---|
| 1281 | shared, QStrings can be treated like ints or other simple base
|
|---|
| 1282 | types. For example:
|
|---|
| 1283 |
|
|---|
| 1284 | \code
|
|---|
| 1285 | QString boolToString( bool b )
|
|---|
| 1286 | {
|
|---|
| 1287 | QString result;
|
|---|
| 1288 | if ( b )
|
|---|
| 1289 | result = "True";
|
|---|
| 1290 | else
|
|---|
| 1291 | result = "False";
|
|---|
| 1292 | return result;
|
|---|
| 1293 | }
|
|---|
| 1294 | \endcode
|
|---|
| 1295 |
|
|---|
| 1296 | The variable, result, is an auto variable allocated on the stack.
|
|---|
| 1297 | When return is called, because we're returning by value, The copy
|
|---|
| 1298 | constructor is called and a copy of the string is returned. (No
|
|---|
| 1299 | actual copying takes place thanks to the implicit sharing, see
|
|---|
| 1300 | below.)
|
|---|
| 1301 |
|
|---|
| 1302 | Throughout Qt's source code you will encounter QString usages like
|
|---|
| 1303 | this:
|
|---|
| 1304 | \code
|
|---|
| 1305 | QString func( const QString& input )
|
|---|
| 1306 | {
|
|---|
| 1307 | QString output = input;
|
|---|
| 1308 | // process output
|
|---|
| 1309 | return output;
|
|---|
| 1310 | }
|
|---|
| 1311 | \endcode
|
|---|
| 1312 |
|
|---|
| 1313 | The 'copying' of input to output is almost as fast as copying a
|
|---|
| 1314 | pointer because behind the scenes copying is achieved by
|
|---|
| 1315 | incrementing a reference count. QString (like all Qt's implicitly
|
|---|
| 1316 | shared classes) operates on a copy-on-write basis, only copying if
|
|---|
| 1317 | an instance is actually changed.
|
|---|
| 1318 |
|
|---|
| 1319 | If you wish to create a deep copy of a QString without losing any
|
|---|
| 1320 | Unicode information then you should use QDeepCopy.
|
|---|
| 1321 |
|
|---|
| 1322 | \sa QChar QCString QByteArray QConstString
|
|---|
| 1323 | */
|
|---|
| 1324 |
|
|---|
| 1325 | /*! \enum Qt::ComparisonFlags
|
|---|
| 1326 | \internal
|
|---|
| 1327 | */
|
|---|
| 1328 | /*!
|
|---|
| 1329 | \enum Qt::StringComparisonMode
|
|---|
| 1330 |
|
|---|
| 1331 | This enum type is used to set the string comparison mode when
|
|---|
| 1332 | searching for an item. It is used by QListBox, QListView and
|
|---|
| 1333 | QIconView, for example. We'll refer to the string being searched
|
|---|
| 1334 | as the 'target' string.
|
|---|
| 1335 |
|
|---|
| 1336 | \value CaseSensitive The strings must match case sensitively.
|
|---|
| 1337 | \value ExactMatch The target and search strings must match exactly.
|
|---|
| 1338 | \value BeginsWith The target string begins with the search string.
|
|---|
| 1339 | \value EndsWith The target string ends with the search string.
|
|---|
| 1340 | \value Contains The target string contains the search string.
|
|---|
| 1341 |
|
|---|
| 1342 | If you OR these flags together (excluding \c CaseSensitive), the
|
|---|
| 1343 | search criteria be applied in the following order: \c ExactMatch,
|
|---|
| 1344 | \c BeginsWith, \c EndsWith, \c Contains.
|
|---|
| 1345 |
|
|---|
| 1346 | Matching is case-insensitive unless \c CaseSensitive is set. \c
|
|---|
| 1347 | CaseSensitive can be OR-ed with any combination of the other
|
|---|
| 1348 | flags.
|
|---|
| 1349 |
|
|---|
| 1350 | */
|
|---|
| 1351 | Q_EXPORT QStringData *QString::shared_null = 0;
|
|---|
| 1352 | QT_STATIC_CONST_IMPL QString QString::null;
|
|---|
| 1353 | QT_STATIC_CONST_IMPL QChar QChar::null;
|
|---|
| 1354 | QT_STATIC_CONST_IMPL QChar QChar::replacement((ushort)0xfffd);
|
|---|
| 1355 | QT_STATIC_CONST_IMPL QChar QChar::byteOrderMark((ushort)0xfeff);
|
|---|
| 1356 | QT_STATIC_CONST_IMPL QChar QChar::byteOrderSwapped((ushort)0xfffe);
|
|---|
| 1357 | QT_STATIC_CONST_IMPL QChar QChar::nbsp((ushort)0x00a0);
|
|---|
| 1358 |
|
|---|
| 1359 | QStringData* QString::makeSharedNull()
|
|---|
| 1360 | {
|
|---|
| 1361 | QString::shared_null = new QStringData;
|
|---|
| 1362 | #if defined( Q_OS_MAC ) || defined(Q_OS_SOLARIS) || defined(Q_OS_HPUX) || defined(Q_OS_AIX)
|
|---|
| 1363 | QString *that = const_cast<QString *>(&QString::null);
|
|---|
| 1364 | that->d = QString::shared_null;
|
|---|
| 1365 | #endif
|
|---|
| 1366 | return QString::shared_null;
|
|---|
| 1367 | }
|
|---|
| 1368 |
|
|---|
| 1369 | /*!
|
|---|
| 1370 | \fn QString::QString()
|
|---|
| 1371 |
|
|---|
| 1372 | Constructs a null string, i.e. both the length and data pointer
|
|---|
| 1373 | are 0.
|
|---|
| 1374 |
|
|---|
| 1375 | \sa isNull()
|
|---|
| 1376 | */
|
|---|
| 1377 |
|
|---|
| 1378 | /*!
|
|---|
| 1379 | Constructs a string of length one, containing the character \a ch.
|
|---|
| 1380 | */
|
|---|
| 1381 | QString::QString( QChar ch )
|
|---|
| 1382 | {
|
|---|
| 1383 | d = new QStringData( QT_ALLOC_QCHAR_VEC( 1 ), 1, 1 );
|
|---|
| 1384 | d->unicode[0] = ch;
|
|---|
| 1385 | }
|
|---|
| 1386 |
|
|---|
| 1387 | /*!
|
|---|
| 1388 | Constructs an implicitly shared copy of \a s. This is very fast
|
|---|
| 1389 | since it only involves incrementing a reference count.
|
|---|
| 1390 | */
|
|---|
| 1391 | QString::QString( const QString &s ) :
|
|---|
| 1392 | d(s.d)
|
|---|
| 1393 | {
|
|---|
| 1394 | d->ref();
|
|---|
| 1395 | }
|
|---|
| 1396 |
|
|---|
| 1397 | /*!
|
|---|
| 1398 | \internal
|
|---|
| 1399 |
|
|---|
| 1400 | Private function.
|
|---|
| 1401 |
|
|---|
| 1402 | Constructs a string with preallocated space for \a size characters.
|
|---|
| 1403 |
|
|---|
| 1404 | The string is empty.
|
|---|
| 1405 |
|
|---|
| 1406 | \sa isNull()
|
|---|
| 1407 | */
|
|---|
| 1408 |
|
|---|
| 1409 | QString::QString( int size, bool /*dummy*/ )
|
|---|
| 1410 | {
|
|---|
| 1411 | if ( size ) {
|
|---|
| 1412 | int l = size;
|
|---|
| 1413 | QChar* uc = QT_ALLOC_QCHAR_VEC( l );
|
|---|
| 1414 | d = new QStringData( uc, 0, l );
|
|---|
| 1415 | } else {
|
|---|
| 1416 | d = shared_null ? shared_null : (shared_null=new QStringData);
|
|---|
| 1417 | d->ref();
|
|---|
| 1418 | }
|
|---|
| 1419 | }
|
|---|
| 1420 |
|
|---|
| 1421 | /*!
|
|---|
| 1422 | Constructs a string that is a deep copy of \a ba interpreted as a
|
|---|
| 1423 | classic C string.
|
|---|
| 1424 | */
|
|---|
| 1425 |
|
|---|
| 1426 | QString::QString( const QByteArray& ba )
|
|---|
| 1427 | {
|
|---|
| 1428 | #ifndef QT_NO_TEXTCODEC
|
|---|
| 1429 | if ( QTextCodec::codecForCStrings() ) {
|
|---|
| 1430 | d = 0;
|
|---|
| 1431 | *this = fromAscii( ba.data(), ba.size() );
|
|---|
| 1432 | return;
|
|---|
| 1433 | }
|
|---|
| 1434 | #endif
|
|---|
| 1435 | uint l;
|
|---|
| 1436 | QChar *uc = internalLatin1ToUnicode(ba,&l);
|
|---|
| 1437 | d = new QStringData(uc,l,l);
|
|---|
| 1438 | }
|
|---|
| 1439 |
|
|---|
| 1440 | /*!
|
|---|
| 1441 | Constructs a string that is a deep copy of the first \a length
|
|---|
| 1442 | characters in the QChar array.
|
|---|
| 1443 |
|
|---|
| 1444 | If \a unicode and \a length are 0, then a null string is created.
|
|---|
| 1445 |
|
|---|
| 1446 | If only \a unicode is 0, the string is empty but has \a length
|
|---|
| 1447 | characters of space preallocated: QString expands automatically
|
|---|
| 1448 | anyway, but this may speed up some cases a little. We recommend
|
|---|
| 1449 | using the plain constructor and setLength() for this purpose since
|
|---|
| 1450 | it will result in more readable code.
|
|---|
| 1451 |
|
|---|
| 1452 | \sa isNull() setLength()
|
|---|
| 1453 | */
|
|---|
| 1454 |
|
|---|
| 1455 | QString::QString( const QChar* unicode, uint length )
|
|---|
| 1456 | {
|
|---|
| 1457 | if ( !unicode && !length ) {
|
|---|
| 1458 | d = shared_null ? shared_null : makeSharedNull();
|
|---|
| 1459 | d->ref();
|
|---|
| 1460 | } else {
|
|---|
| 1461 | QChar* uc = QT_ALLOC_QCHAR_VEC( length );
|
|---|
| 1462 | if ( unicode )
|
|---|
| 1463 | memcpy(uc, unicode, length*sizeof(QChar));
|
|---|
| 1464 | d = new QStringData(uc,unicode ? length : 0,length);
|
|---|
| 1465 | }
|
|---|
| 1466 | }
|
|---|
| 1467 |
|
|---|
| 1468 | /*!
|
|---|
| 1469 | Constructs a string that is a deep copy of \a str, interpreted as
|
|---|
| 1470 | a classic C string.
|
|---|
| 1471 |
|
|---|
| 1472 | If \a str is 0, then a null string is created.
|
|---|
| 1473 |
|
|---|
| 1474 | This is a cast constructor, but it is perfectly safe: converting a
|
|---|
| 1475 | Latin-1 \c{const char *} to QString preserves all the information. You
|
|---|
| 1476 | can disable this constructor by defining \c QT_NO_CAST_ASCII when
|
|---|
| 1477 | you compile your applications. You can also make QString objects
|
|---|
| 1478 | by using setLatin1(), fromLatin1(), fromLocal8Bit(), and
|
|---|
| 1479 | fromUtf8(). Or whatever encoding is appropriate for the 8-bit data
|
|---|
| 1480 | you have.
|
|---|
| 1481 |
|
|---|
| 1482 | \sa isNull()
|
|---|
| 1483 | */
|
|---|
| 1484 |
|
|---|
| 1485 | QString::QString( const char *str )
|
|---|
| 1486 | {
|
|---|
| 1487 | #ifndef QT_NO_TEXTCODEC
|
|---|
| 1488 | if ( QTextCodec::codecForCStrings() ) {
|
|---|
| 1489 | d = 0;
|
|---|
| 1490 | *this = fromAscii( str );
|
|---|
| 1491 | return;
|
|---|
| 1492 | }
|
|---|
| 1493 | #endif
|
|---|
| 1494 | uint l;
|
|---|
| 1495 | QChar *uc = internalLatin1ToUnicode(str,&l);
|
|---|
| 1496 | d = new QStringData(uc,l,l);
|
|---|
| 1497 | }
|
|---|
| 1498 |
|
|---|
| 1499 | #ifndef QT_NO_STL
|
|---|
| 1500 | /*!
|
|---|
| 1501 | Constructs a string that is a deep copy of \a str.
|
|---|
| 1502 |
|
|---|
| 1503 | This is the same as fromAscii(\a str).
|
|---|
| 1504 | */
|
|---|
| 1505 |
|
|---|
| 1506 | QString::QString( const std::string &str )
|
|---|
| 1507 | {
|
|---|
| 1508 | #ifndef QT_NO_TEXTCODEC
|
|---|
| 1509 | if ( QTextCodec::codecForCStrings() ) {
|
|---|
| 1510 | d = 0;
|
|---|
| 1511 | *this = fromAscii( str.c_str() );
|
|---|
| 1512 | return;
|
|---|
| 1513 | }
|
|---|
| 1514 | #endif
|
|---|
| 1515 | uint l;
|
|---|
| 1516 | QChar *uc = internalLatin1ToUnicode(str.c_str(),&l);
|
|---|
| 1517 | d = new QStringData(uc,l,l);
|
|---|
| 1518 | }
|
|---|
| 1519 | #endif
|
|---|
| 1520 |
|
|---|
| 1521 | /*!
|
|---|
| 1522 | \fn QString::~QString()
|
|---|
| 1523 |
|
|---|
| 1524 | Destroys the string and frees the string's data if this is the
|
|---|
| 1525 | last reference to the string.
|
|---|
| 1526 | */
|
|---|
| 1527 |
|
|---|
| 1528 |
|
|---|
| 1529 | /*!
|
|---|
| 1530 | Deallocates any space reserved solely by this QString.
|
|---|
| 1531 |
|
|---|
| 1532 | If the string does not share its data with another QString
|
|---|
| 1533 | instance, nothing happens; otherwise the function creates a new,
|
|---|
| 1534 | unique copy of this string. This function is called whenever the
|
|---|
| 1535 | string is modified.
|
|---|
| 1536 | */
|
|---|
| 1537 |
|
|---|
| 1538 | void QString::real_detach()
|
|---|
| 1539 | {
|
|---|
| 1540 | setLength( length() );
|
|---|
| 1541 | }
|
|---|
| 1542 |
|
|---|
| 1543 | void QString::deref()
|
|---|
| 1544 | {
|
|---|
| 1545 | if ( d && d->deref() ) {
|
|---|
| 1546 | if ( d != shared_null )
|
|---|
| 1547 | delete d;
|
|---|
| 1548 | d = 0;
|
|---|
| 1549 | }
|
|---|
| 1550 | }
|
|---|
| 1551 |
|
|---|
| 1552 | void QStringData::deleteSelf()
|
|---|
| 1553 | {
|
|---|
| 1554 | delete this;
|
|---|
| 1555 | }
|
|---|
| 1556 |
|
|---|
| 1557 | /*!
|
|---|
| 1558 | \fn QString& QString::operator=( QChar c )
|
|---|
| 1559 |
|
|---|
| 1560 | Sets the string to contain just the single character \a c.
|
|---|
| 1561 | */
|
|---|
| 1562 |
|
|---|
| 1563 | /*!
|
|---|
| 1564 | \fn QString& QString::operator=( const std::string& s )
|
|---|
| 1565 |
|
|---|
| 1566 | \overload
|
|---|
| 1567 |
|
|---|
| 1568 | Makes a deep copy of \a s and returns a reference to the deep
|
|---|
| 1569 | copy.
|
|---|
| 1570 | */
|
|---|
| 1571 |
|
|---|
| 1572 | /*!
|
|---|
| 1573 | \fn QString& QString::operator=( char c )
|
|---|
| 1574 |
|
|---|
| 1575 | \overload
|
|---|
| 1576 |
|
|---|
| 1577 | Sets the string to contain just the single character \a c.
|
|---|
| 1578 | */
|
|---|
| 1579 |
|
|---|
| 1580 | /*!
|
|---|
| 1581 | \overload
|
|---|
| 1582 |
|
|---|
| 1583 | Assigns a shallow copy of \a s to this string and returns a
|
|---|
| 1584 | reference to this string. This is very fast because the string
|
|---|
| 1585 | isn't actually copied.
|
|---|
| 1586 | */
|
|---|
| 1587 | QString &QString::operator=( const QString &s )
|
|---|
| 1588 | {
|
|---|
| 1589 | s.d->ref();
|
|---|
| 1590 | deref();
|
|---|
| 1591 | d = s.d;
|
|---|
| 1592 | return *this;
|
|---|
| 1593 | }
|
|---|
| 1594 |
|
|---|
| 1595 | /*!
|
|---|
| 1596 | \overload
|
|---|
| 1597 |
|
|---|
| 1598 | Assigns a deep copy of \a cstr, interpreted as a classic C
|
|---|
| 1599 | string, to this string. Returns a reference to this string.
|
|---|
| 1600 | */
|
|---|
| 1601 | QString &QString::operator=( const QCString& cstr )
|
|---|
| 1602 | {
|
|---|
| 1603 | return setAscii( cstr );
|
|---|
| 1604 | }
|
|---|
| 1605 |
|
|---|
| 1606 |
|
|---|
| 1607 | /*!
|
|---|
| 1608 | \overload
|
|---|
| 1609 |
|
|---|
| 1610 | Assigns a deep copy of \a str, interpreted as a classic C string
|
|---|
| 1611 | to this string and returns a reference to this string.
|
|---|
| 1612 |
|
|---|
| 1613 | If \a str is 0, then a null string is created.
|
|---|
| 1614 |
|
|---|
| 1615 | \sa isNull()
|
|---|
| 1616 | */
|
|---|
| 1617 | QString &QString::operator=( const char *str )
|
|---|
| 1618 | {
|
|---|
| 1619 | return setAscii(str);
|
|---|
| 1620 | }
|
|---|
| 1621 |
|
|---|
| 1622 |
|
|---|
| 1623 | /*!
|
|---|
| 1624 | \fn bool QString::isNull() const
|
|---|
| 1625 |
|
|---|
| 1626 | Returns TRUE if the string is null; otherwise returns FALSE. A
|
|---|
| 1627 | null string is always empty.
|
|---|
| 1628 |
|
|---|
| 1629 | \code
|
|---|
| 1630 | QString a; // a.unicode() == 0, a.length() == 0
|
|---|
| 1631 | a.isNull(); // TRUE, because a.unicode() == 0
|
|---|
| 1632 | a.isEmpty(); // TRUE, because a.length() == 0
|
|---|
| 1633 | \endcode
|
|---|
| 1634 |
|
|---|
| 1635 | \sa isEmpty(), length()
|
|---|
| 1636 | */
|
|---|
| 1637 |
|
|---|
| 1638 | /*!
|
|---|
| 1639 | \fn bool QString::isEmpty() const
|
|---|
| 1640 |
|
|---|
| 1641 | Returns TRUE if the string is empty, i.e. if length() == 0;
|
|---|
| 1642 | otherwise returns FALSE. Null strings are also empty.
|
|---|
| 1643 |
|
|---|
| 1644 | \code
|
|---|
| 1645 | QString a( "" );
|
|---|
| 1646 | a.isEmpty(); // TRUE
|
|---|
| 1647 | a.isNull(); // FALSE
|
|---|
| 1648 |
|
|---|
| 1649 | QString b;
|
|---|
| 1650 | b.isEmpty(); // TRUE
|
|---|
| 1651 | b.isNull(); // TRUE
|
|---|
| 1652 | \endcode
|
|---|
| 1653 |
|
|---|
| 1654 | \sa isNull(), length()
|
|---|
| 1655 | */
|
|---|
| 1656 |
|
|---|
| 1657 | /*!
|
|---|
| 1658 | \fn uint QString::length() const
|
|---|
| 1659 |
|
|---|
| 1660 | Returns the length of the string.
|
|---|
| 1661 |
|
|---|
| 1662 | Null strings and empty strings have zero length.
|
|---|
| 1663 |
|
|---|
| 1664 | \sa isNull(), isEmpty()
|
|---|
| 1665 | */
|
|---|
| 1666 |
|
|---|
| 1667 | /*!
|
|---|
| 1668 | If \a newLen is less than the length of the string, then the
|
|---|
| 1669 | string is truncated at position \a newLen. Otherwise nothing
|
|---|
| 1670 | happens.
|
|---|
| 1671 |
|
|---|
| 1672 | \code
|
|---|
| 1673 | QString s = "truncate me";
|
|---|
| 1674 | s.truncate( 5 ); // s == "trunc"
|
|---|
| 1675 | \endcode
|
|---|
| 1676 |
|
|---|
| 1677 | \sa setLength()
|
|---|
| 1678 | */
|
|---|
| 1679 |
|
|---|
| 1680 | void QString::truncate( uint newLen )
|
|---|
| 1681 | {
|
|---|
| 1682 | if ( newLen < d->len )
|
|---|
| 1683 | setLength( newLen );
|
|---|
| 1684 | }
|
|---|
| 1685 |
|
|---|
| 1686 | /*!
|
|---|
| 1687 | Ensures that at least \a newLen characters are allocated to the
|
|---|
| 1688 | string, and sets the length of the string to \a newLen. Any new
|
|---|
| 1689 | space allocated contains arbitrary data.
|
|---|
| 1690 |
|
|---|
| 1691 | \sa reserve(), truncate()
|
|---|
| 1692 | */
|
|---|
| 1693 | void QString::setLength( uint newLen )
|
|---|
| 1694 | {
|
|---|
| 1695 | if ( d->count != 1 || newLen > d->maxl ||
|
|---|
| 1696 | ( newLen * 4 < d->maxl && d->maxl > 4 ) ) {
|
|---|
| 1697 | // detach, grow or shrink
|
|---|
| 1698 | uint newMax = computeNewMax( newLen );
|
|---|
| 1699 | QChar* nd = QT_ALLOC_QCHAR_VEC( newMax );
|
|---|
| 1700 | if ( nd ) {
|
|---|
| 1701 | uint len = QMIN( d->len, newLen );
|
|---|
| 1702 | memcpy( nd, d->unicode, sizeof(QChar) * len );
|
|---|
| 1703 | deref();
|
|---|
| 1704 | d = new QStringData( nd, newLen, newMax );
|
|---|
| 1705 | }
|
|---|
| 1706 | } else {
|
|---|
| 1707 | d->len = newLen;
|
|---|
| 1708 | d->setDirty();
|
|---|
| 1709 | }
|
|---|
| 1710 | }
|
|---|
| 1711 |
|
|---|
| 1712 | /*!
|
|---|
| 1713 | \fn uint QString::capacity() const
|
|---|
| 1714 |
|
|---|
| 1715 | Returns the number of characters this string can hold
|
|---|
| 1716 | in the allocated memory.
|
|---|
| 1717 |
|
|---|
| 1718 | \sa reserve(), squeeze()
|
|---|
| 1719 | */
|
|---|
| 1720 |
|
|---|
| 1721 | /*!
|
|---|
| 1722 | Ensures that at least \a minCapacity characters are allocated to
|
|---|
| 1723 | the string.
|
|---|
| 1724 |
|
|---|
| 1725 | This function is useful for code that needs to build up a long
|
|---|
| 1726 | string and wants to avoid repeated reallocation. In this example,
|
|---|
| 1727 | we want to add to the string until some condition is true, and
|
|---|
| 1728 | we're fairly sure that size is big enough:
|
|---|
| 1729 | \code
|
|---|
| 1730 | QString result;
|
|---|
| 1731 | int len = 0;
|
|---|
| 1732 | result.reserve(maxLen);
|
|---|
| 1733 | while (...) {
|
|---|
| 1734 | result[len++] = ... // fill part of the space
|
|---|
| 1735 | }
|
|---|
| 1736 | result.squeeze();
|
|---|
| 1737 | \endcode
|
|---|
| 1738 |
|
|---|
| 1739 | If \e maxLen is an underestimate, the worst that will happen is
|
|---|
| 1740 | that the loop will slow down.
|
|---|
| 1741 |
|
|---|
| 1742 | If it is not possible to allocate enough memory, the string
|
|---|
| 1743 | remains unchanged.
|
|---|
| 1744 |
|
|---|
| 1745 | \sa capacity(), squeeze(), setLength()
|
|---|
| 1746 | */
|
|---|
| 1747 |
|
|---|
| 1748 | void QString::reserve( uint minCapacity )
|
|---|
| 1749 | {
|
|---|
| 1750 | if ( d->maxl < minCapacity ) {
|
|---|
| 1751 | QChar *nd = QT_ALLOC_QCHAR_VEC( minCapacity );
|
|---|
| 1752 | if ( nd ) {
|
|---|
| 1753 | uint len = d->len;
|
|---|
| 1754 | if ( len )
|
|---|
| 1755 | memcpy( nd, d->unicode, sizeof(QChar) * len );
|
|---|
| 1756 | deref();
|
|---|
| 1757 | d = new QStringData( nd, len, minCapacity );
|
|---|
| 1758 | }
|
|---|
| 1759 | }
|
|---|
| 1760 | }
|
|---|
| 1761 |
|
|---|
| 1762 |
|
|---|
| 1763 | /*!
|
|---|
| 1764 | Squeezes the string's capacity to the current content.
|
|---|
| 1765 |
|
|---|
| 1766 | \sa capacity(), reserve()
|
|---|
| 1767 | */
|
|---|
| 1768 | void QString::squeeze()
|
|---|
| 1769 | {
|
|---|
| 1770 | if ( d->maxl > d->len ) {
|
|---|
| 1771 | QChar *nd = QT_ALLOC_QCHAR_VEC( d->len );
|
|---|
| 1772 | if ( nd ) {
|
|---|
| 1773 | uint len = d->len;
|
|---|
| 1774 | if ( len )
|
|---|
| 1775 | memcpy( nd, d->unicode, sizeof(QChar) * len );
|
|---|
| 1776 | deref();
|
|---|
| 1777 | d = new QStringData( nd, len, len );
|
|---|
| 1778 | }
|
|---|
| 1779 | }
|
|---|
| 1780 | }
|
|---|
| 1781 |
|
|---|
| 1782 | /*!
|
|---|
| 1783 | \internal
|
|---|
| 1784 |
|
|---|
| 1785 | Like setLength, but doesn't shrink the allocated memory.
|
|---|
| 1786 | */
|
|---|
| 1787 | void QString::grow( uint newLen )
|
|---|
| 1788 | {
|
|---|
| 1789 | if ( d->count != 1 || newLen > d->maxl ) {
|
|---|
| 1790 | setLength( newLen );
|
|---|
| 1791 | } else {
|
|---|
| 1792 | d->len = newLen;
|
|---|
| 1793 | d->setDirty();
|
|---|
| 1794 | }
|
|---|
| 1795 | }
|
|---|
| 1796 |
|
|---|
| 1797 | struct ArgEscapeData
|
|---|
| 1798 | {
|
|---|
| 1799 | uint min_escape; // lowest escape sequence number
|
|---|
| 1800 | uint occurrences; // number of occurences of the lowest escape
|
|---|
| 1801 | // sequence number
|
|---|
| 1802 | uint locale_occurrences; // number of occurences of the lowest escape
|
|---|
| 1803 | // sequence number which contain 'L'
|
|---|
| 1804 | uint escape_len; // total length of escape sequences which will
|
|---|
| 1805 | // be replaced
|
|---|
| 1806 | };
|
|---|
| 1807 |
|
|---|
| 1808 | static ArgEscapeData findArgEscapes(const QString &s)
|
|---|
| 1809 | {
|
|---|
| 1810 | const QChar *uc_begin = s.unicode();
|
|---|
| 1811 | const QChar *uc_end = uc_begin + s.length();
|
|---|
| 1812 |
|
|---|
| 1813 | ArgEscapeData d;
|
|---|
| 1814 |
|
|---|
| 1815 | d.min_escape = 10;
|
|---|
| 1816 | d.occurrences = 0;
|
|---|
| 1817 | d.escape_len = 0;
|
|---|
| 1818 | d.locale_occurrences = 0;
|
|---|
| 1819 |
|
|---|
| 1820 | const QChar *c = uc_begin;
|
|---|
| 1821 | while (c != uc_end) {
|
|---|
| 1822 | while (c != uc_end && c->unicode() != '%')
|
|---|
| 1823 | ++c;
|
|---|
| 1824 |
|
|---|
| 1825 | if (c == uc_end || ++c == uc_end)
|
|---|
| 1826 | break;
|
|---|
| 1827 |
|
|---|
| 1828 | bool locale_arg = false;
|
|---|
| 1829 | if (c->unicode() == 'L') {
|
|---|
| 1830 | locale_arg = true;
|
|---|
| 1831 | if (++c == uc_end)
|
|---|
| 1832 | break;
|
|---|
| 1833 | }
|
|---|
| 1834 |
|
|---|
| 1835 | if (c->unicode() < '0' || c->unicode() > '9')
|
|---|
| 1836 | continue;
|
|---|
| 1837 |
|
|---|
| 1838 | uint escape = c->unicode() - '0';
|
|---|
| 1839 | ++c;
|
|---|
| 1840 |
|
|---|
| 1841 | if (escape > d.min_escape)
|
|---|
| 1842 | continue;
|
|---|
| 1843 |
|
|---|
| 1844 | if (escape < d.min_escape) {
|
|---|
| 1845 | d.min_escape = escape;
|
|---|
| 1846 | d.occurrences = 0;
|
|---|
| 1847 | d.escape_len = 0;
|
|---|
| 1848 | d.locale_occurrences = 0;
|
|---|
| 1849 | }
|
|---|
| 1850 |
|
|---|
| 1851 | #if QT_VERSION < 0x040000
|
|---|
| 1852 | // ### remove preprocessor in Qt 4.0
|
|---|
| 1853 | /* Since in Qt < 4.0 only the first instance is replaced,
|
|---|
| 1854 | escape_len should hold the length of only the first escape
|
|---|
| 1855 | sequence */
|
|---|
| 1856 | if (d.occurrences == 0)
|
|---|
| 1857 | #endif
|
|---|
| 1858 | {
|
|---|
| 1859 | ++d.occurrences;
|
|---|
| 1860 | if (locale_arg) {
|
|---|
| 1861 | ++d.locale_occurrences;
|
|---|
| 1862 | d.escape_len += 3;
|
|---|
| 1863 | }
|
|---|
| 1864 | else
|
|---|
| 1865 | d.escape_len += 2;
|
|---|
| 1866 | }
|
|---|
| 1867 | }
|
|---|
| 1868 |
|
|---|
| 1869 | return d;
|
|---|
| 1870 | }
|
|---|
| 1871 |
|
|---|
| 1872 | static QString replaceArgEscapes(const QString &s, const ArgEscapeData &d, int field_width,
|
|---|
| 1873 | const QString &arg, const QString &larg)
|
|---|
| 1874 | {
|
|---|
| 1875 | const QChar *uc_begin = s.unicode();
|
|---|
| 1876 | const QChar *uc_end = uc_begin + s.length();
|
|---|
| 1877 |
|
|---|
| 1878 | uint abs_field_width = QABS(field_width);
|
|---|
| 1879 | uint result_len = s.length()
|
|---|
| 1880 | - d.escape_len
|
|---|
| 1881 | + (d.occurrences - d.locale_occurrences)
|
|---|
| 1882 | *QMAX(abs_field_width, arg.length())
|
|---|
| 1883 | + d.locale_occurrences
|
|---|
| 1884 | *QMAX(abs_field_width, larg.length());
|
|---|
| 1885 |
|
|---|
| 1886 | QString result;
|
|---|
| 1887 | result.setLength(result_len);
|
|---|
| 1888 | QChar *result_buff = (QChar*) result.unicode();
|
|---|
| 1889 |
|
|---|
| 1890 | QChar *rc = result_buff;
|
|---|
| 1891 | const QChar *c = uc_begin;
|
|---|
| 1892 | uint repl_cnt = 0;
|
|---|
| 1893 | while (c != uc_end) {
|
|---|
| 1894 | /* We don't have to check if we run off the end of the string with c,
|
|---|
| 1895 | because as long as d.occurrences > 0 we KNOW there are valid escape
|
|---|
| 1896 | sequences. */
|
|---|
| 1897 |
|
|---|
| 1898 | const QChar *text_start = c;
|
|---|
| 1899 |
|
|---|
| 1900 | while (c->unicode() != '%')
|
|---|
| 1901 | ++c;
|
|---|
| 1902 |
|
|---|
| 1903 | const QChar *escape_start = c++;
|
|---|
| 1904 |
|
|---|
| 1905 | bool locale_arg = false;
|
|---|
| 1906 | if (c->unicode() == 'L') {
|
|---|
| 1907 | locale_arg = true;
|
|---|
| 1908 | ++c;
|
|---|
| 1909 | }
|
|---|
| 1910 |
|
|---|
| 1911 | if (c->unicode() != '0' + d.min_escape) {
|
|---|
| 1912 | memcpy(rc, text_start, (c - text_start)*sizeof(QChar));
|
|---|
| 1913 | rc += c - text_start;
|
|---|
| 1914 | }
|
|---|
| 1915 | else {
|
|---|
| 1916 | ++c;
|
|---|
| 1917 |
|
|---|
| 1918 | memcpy(rc, text_start, (escape_start - text_start)*sizeof(QChar));
|
|---|
| 1919 | rc += escape_start - text_start;
|
|---|
| 1920 |
|
|---|
| 1921 | uint pad_chars;
|
|---|
| 1922 | if (locale_arg)
|
|---|
| 1923 | pad_chars = QMAX(abs_field_width, larg.length()) - larg.length();
|
|---|
| 1924 | else
|
|---|
| 1925 | pad_chars = QMAX(abs_field_width, arg.length()) - arg.length();
|
|---|
| 1926 |
|
|---|
| 1927 | if (field_width > 0) { // left padded
|
|---|
| 1928 | for (uint i = 0; i < pad_chars; ++i)
|
|---|
| 1929 | (rc++)->unicode() = ' ';
|
|---|
| 1930 | }
|
|---|
| 1931 |
|
|---|
| 1932 | if (locale_arg) {
|
|---|
| 1933 | memcpy(rc, larg.unicode(), larg.length()*sizeof(QChar));
|
|---|
| 1934 | rc += larg.length();
|
|---|
| 1935 | }
|
|---|
| 1936 | else {
|
|---|
| 1937 | memcpy(rc, arg.unicode(), arg.length()*sizeof(QChar));
|
|---|
| 1938 | rc += arg.length();
|
|---|
| 1939 | }
|
|---|
| 1940 |
|
|---|
| 1941 | if (field_width < 0) { // right padded
|
|---|
| 1942 | for (uint i = 0; i < pad_chars; ++i)
|
|---|
| 1943 | (rc++)->unicode() = ' ';
|
|---|
| 1944 | }
|
|---|
| 1945 |
|
|---|
| 1946 | if (++repl_cnt == d.occurrences) {
|
|---|
| 1947 | memcpy(rc, c, (uc_end - c)*sizeof(QChar));
|
|---|
| 1948 | rc += uc_end - c;
|
|---|
| 1949 | Q_ASSERT(rc - result_buff == (int)result_len);
|
|---|
| 1950 | c = uc_end;
|
|---|
| 1951 | }
|
|---|
| 1952 | }
|
|---|
| 1953 | }
|
|---|
| 1954 |
|
|---|
| 1955 | return result;
|
|---|
| 1956 | }
|
|---|
| 1957 |
|
|---|
| 1958 | /*!
|
|---|
| 1959 | This function will return a string that replaces the lowest
|
|---|
| 1960 | numbered occurrence of \c %1, \c %2, ..., \c %9 with \a a.
|
|---|
| 1961 |
|
|---|
| 1962 | The \a fieldWidth value specifies the minimum amount of space that
|
|---|
| 1963 | \a a is padded to. A positive value will produce right-aligned
|
|---|
| 1964 | text, whereas a negative value will produce left-aligned text.
|
|---|
| 1965 |
|
|---|
| 1966 | The following example shows how we could create a 'status' string
|
|---|
| 1967 | when processing a list of files:
|
|---|
| 1968 | \code
|
|---|
| 1969 | QString status = QString( "Processing file %1 of %2: %3" )
|
|---|
| 1970 | .arg( i ) // current file's number
|
|---|
| 1971 | .arg( total ) // number of files to process
|
|---|
| 1972 | .arg( fileName ); // current file's name
|
|---|
| 1973 | \endcode
|
|---|
| 1974 |
|
|---|
| 1975 | It is generally fine to use filenames and numbers as we have done
|
|---|
| 1976 | in the example above. But note that using arg() to construct
|
|---|
| 1977 | natural language sentences does not usually translate well into
|
|---|
| 1978 | other languages because sentence structure and word order often
|
|---|
| 1979 | differ between languages.
|
|---|
| 1980 |
|
|---|
| 1981 | If there is no place marker (\c %1, \c %2, etc.), a warning
|
|---|
| 1982 | message (qWarning()) is output and the result is undefined.
|
|---|
| 1983 | */
|
|---|
| 1984 | QString QString::arg( const QString& a, int fieldWidth ) const
|
|---|
| 1985 | {
|
|---|
| 1986 | ArgEscapeData d = findArgEscapes(*this);
|
|---|
| 1987 |
|
|---|
| 1988 | if (d.occurrences == 0) {
|
|---|
| 1989 | qWarning( "QString::arg(): Argument missing: %s, %s", latin1(),
|
|---|
| 1990 | a.latin1() );
|
|---|
| 1991 | return *this;
|
|---|
| 1992 | }
|
|---|
| 1993 |
|
|---|
| 1994 | return replaceArgEscapes(*this, d, fieldWidth, a, a);
|
|---|
| 1995 | }
|
|---|
| 1996 |
|
|---|
| 1997 | /*!
|
|---|
| 1998 | \fn QString QString::arg( const QString& a1, const QString& a2 ) const
|
|---|
| 1999 |
|
|---|
| 2000 | \overload
|
|---|
| 2001 |
|
|---|
| 2002 | This is the same as str.arg(\a a1).arg(\a a2), except that
|
|---|
| 2003 | the strings are replaced in one pass. This can make a difference
|
|---|
| 2004 | if \a a1 contains e.g. \c{%1}:
|
|---|
| 2005 |
|
|---|
| 2006 | \code
|
|---|
| 2007 | QString str( "%1 %2" );
|
|---|
| 2008 | str.arg( "Hello", "world" ); // returns "Hello world"
|
|---|
| 2009 | str.arg( "Hello" ).arg( "world" ); // returns "Hello world"
|
|---|
| 2010 |
|
|---|
| 2011 | str.arg( "(%1)", "Hello" ); // returns "(%1) Hello"
|
|---|
| 2012 | str.arg( "(%1)" ).arg( "Hello" ); // returns "(Hello) %2"
|
|---|
| 2013 | \endcode
|
|---|
| 2014 | */
|
|---|
| 2015 |
|
|---|
| 2016 | /*!
|
|---|
| 2017 | \fn QString QString::arg( const QString& a1, const QString& a2,
|
|---|
| 2018 | const QString& a3 ) const
|
|---|
| 2019 | \overload
|
|---|
| 2020 |
|
|---|
| 2021 | This is the same as calling str.arg(\a a1).arg(\a a2).arg(\a a3),
|
|---|
| 2022 | except that the strings are replaced in one pass.
|
|---|
| 2023 | */
|
|---|
| 2024 |
|
|---|
| 2025 | /*!
|
|---|
| 2026 | \fn QString QString::arg( const QString& a1, const QString& a2,
|
|---|
| 2027 | const QString& a3, const QString& a4 ) const
|
|---|
| 2028 | \overload
|
|---|
| 2029 |
|
|---|
| 2030 | This is the same as calling
|
|---|
| 2031 | str.arg(\a a1).arg(\a a2).arg(\a a3).arg(\a a4),
|
|---|
| 2032 | except that the strings are replaced in one pass.
|
|---|
| 2033 | */
|
|---|
| 2034 |
|
|---|
| 2035 | /*!
|
|---|
| 2036 | \overload
|
|---|
| 2037 |
|
|---|
| 2038 | The \a fieldWidth value specifies the minimum amount of space that
|
|---|
| 2039 | \a a is padded to. A positive value will produce a right-aligned
|
|---|
| 2040 | number, whereas a negative value will produce a left-aligned
|
|---|
| 2041 | number.
|
|---|
| 2042 |
|
|---|
| 2043 | \a a is expressed in base \a base, which is 10 by default and must
|
|---|
| 2044 | be between 2 and 36.
|
|---|
| 2045 |
|
|---|
| 2046 | The '%' can be followed by an 'L', in which case the sequence is
|
|---|
| 2047 | replaced with a localized representation of \a a. The conversion uses
|
|---|
| 2048 | the default locale, set by QLocale::setDefaultLocale(). If no default
|
|---|
| 2049 | locale was specified, the "C" locale is used. The 'L' flag is ignored
|
|---|
| 2050 | if \a base is not 10.
|
|---|
| 2051 |
|
|---|
| 2052 | \code
|
|---|
| 2053 | QString str;
|
|---|
| 2054 | str = QString( "Decimal 63 is %1 in hexadecimal" )
|
|---|
| 2055 | .arg( 63, 0, 16 );
|
|---|
| 2056 | // str == "Decimal 63 is 3f in hexadecimal"
|
|---|
| 2057 |
|
|---|
| 2058 | QLocale::setDefaultLocale(QLocale::English, QLocale::UnitedStates);
|
|---|
| 2059 | str = QString( "%1 %L2 %L3" )
|
|---|
| 2060 | .arg( 12345 )
|
|---|
| 2061 | .arg( 12345 )
|
|---|
| 2062 | .arg( 12345, 0, 16 );
|
|---|
| 2063 | // str == "12345 12,345 3039"
|
|---|
| 2064 | \endcode
|
|---|
| 2065 | */
|
|---|
| 2066 | QString QString::arg( long a, int fieldWidth, int base ) const
|
|---|
| 2067 | {
|
|---|
| 2068 | return arg((Q_LLONG)a, fieldWidth, base);
|
|---|
| 2069 | }
|
|---|
| 2070 |
|
|---|
| 2071 | /*!
|
|---|
| 2072 | \overload
|
|---|
| 2073 |
|
|---|
| 2074 | \a a is expressed in base \a base, which is 10 by default and must
|
|---|
| 2075 | be between 2 and 36. If \a base is 10, the '%l' syntax can be used
|
|---|
| 2076 | to produce localized strings.
|
|---|
| 2077 | */
|
|---|
| 2078 | QString QString::arg( ulong a, int fieldWidth, int base ) const
|
|---|
| 2079 | {
|
|---|
| 2080 | return arg((Q_ULLONG)a, fieldWidth, base);
|
|---|
| 2081 | }
|
|---|
| 2082 |
|
|---|
| 2083 | /*!
|
|---|
| 2084 | \overload
|
|---|
| 2085 |
|
|---|
| 2086 | \a a is expressed in base \a base, which is 10 by default and must
|
|---|
| 2087 | be between 2 and 36. If \a base is 10, the '%l' syntax can be used
|
|---|
| 2088 | to produce localized strings.
|
|---|
| 2089 | */
|
|---|
| 2090 | QString QString::arg( Q_LLONG a, int fieldWidth, int base ) const
|
|---|
| 2091 | {
|
|---|
| 2092 | ArgEscapeData d = findArgEscapes(*this);
|
|---|
| 2093 |
|
|---|
| 2094 | if (d.occurrences == 0) {
|
|---|
| 2095 | qWarning( "QString::arg(): Argument missing: %s, %lld", latin1(),
|
|---|
| 2096 | a );
|
|---|
| 2097 | return *this;
|
|---|
| 2098 | }
|
|---|
| 2099 |
|
|---|
| 2100 | QString arg;
|
|---|
| 2101 | if (d.occurrences > d.locale_occurrences)
|
|---|
| 2102 | arg = number(a, base);
|
|---|
| 2103 |
|
|---|
| 2104 | QString locale_arg;
|
|---|
| 2105 | if (d.locale_occurrences > 0) {
|
|---|
| 2106 | QLocale locale;
|
|---|
| 2107 | locale_arg = locale.d->longLongToString(a, -1, base, -1, QLocalePrivate::ThousandsGroup);
|
|---|
| 2108 | }
|
|---|
| 2109 |
|
|---|
| 2110 | return replaceArgEscapes(*this, d, fieldWidth, arg, locale_arg);
|
|---|
| 2111 | }
|
|---|
| 2112 |
|
|---|
| 2113 | /*!
|
|---|
| 2114 | \overload
|
|---|
| 2115 |
|
|---|
| 2116 | \a a is expressed in base \a base, which is 10 by default and must
|
|---|
| 2117 | be between 2 and 36. If \a base is 10, the '%l' syntax can be used
|
|---|
| 2118 | to produce localized strings.
|
|---|
| 2119 | */
|
|---|
| 2120 | QString QString::arg( Q_ULLONG a, int fieldWidth, int base ) const
|
|---|
| 2121 | {
|
|---|
| 2122 | ArgEscapeData d = findArgEscapes(*this);
|
|---|
| 2123 |
|
|---|
| 2124 | if (d.occurrences == 0) {
|
|---|
| 2125 | qWarning( "QString::arg(): Argument missing: %s, %llu", latin1(),
|
|---|
| 2126 | a );
|
|---|
| 2127 | return *this;
|
|---|
| 2128 | }
|
|---|
| 2129 |
|
|---|
| 2130 | QString arg;
|
|---|
| 2131 | if (d.occurrences > d.locale_occurrences)
|
|---|
| 2132 | arg = number(a, base);
|
|---|
| 2133 |
|
|---|
| 2134 | QString locale_arg;
|
|---|
| 2135 | if (d.locale_occurrences > 0) {
|
|---|
| 2136 | QLocale locale;
|
|---|
| 2137 | locale_arg = locale.d->unsLongLongToString(a, -1, base, -1, QLocalePrivate::ThousandsGroup);
|
|---|
| 2138 | }
|
|---|
| 2139 |
|
|---|
| 2140 | return replaceArgEscapes(*this, d, fieldWidth, arg, locale_arg);
|
|---|
| 2141 | }
|
|---|
| 2142 |
|
|---|
| 2143 | /*!
|
|---|
| 2144 | \fn QString QString::arg( int a, int fieldWidth, int base ) const
|
|---|
| 2145 |
|
|---|
| 2146 | \overload
|
|---|
| 2147 |
|
|---|
| 2148 | \a a is expressed in base \a base, which is 10 by default and must
|
|---|
| 2149 | be between 2 and 36. If \a base is 10, the '%l' syntax can be used
|
|---|
| 2150 | to produce localized strings.
|
|---|
| 2151 | */
|
|---|
| 2152 |
|
|---|
| 2153 | /*!
|
|---|
| 2154 | \fn QString QString::arg( uint a, int fieldWidth, int base ) const
|
|---|
| 2155 |
|
|---|
| 2156 | \overload
|
|---|
| 2157 |
|
|---|
| 2158 | \a a is expressed in base \a base, which is 10 by default and must
|
|---|
| 2159 | be between 2 and 36. If \a base is 10, the '%l' syntax can be used
|
|---|
| 2160 | to produce localized strings.
|
|---|
| 2161 | */
|
|---|
| 2162 |
|
|---|
| 2163 | /*!
|
|---|
| 2164 | \fn QString QString::arg( short a, int fieldWidth, int base ) const
|
|---|
| 2165 |
|
|---|
| 2166 | \overload
|
|---|
| 2167 |
|
|---|
| 2168 | \a a is expressed in base \a base, which is 10 by default and must
|
|---|
| 2169 | be between 2 and 36. If \a base is 10, the '%l' syntax can be used
|
|---|
| 2170 | to produce localized strings.
|
|---|
| 2171 | */
|
|---|
| 2172 |
|
|---|
| 2173 | /*!
|
|---|
| 2174 | \fn QString QString::arg( ushort a, int fieldWidth, int base ) const
|
|---|
| 2175 |
|
|---|
| 2176 | \overload
|
|---|
| 2177 |
|
|---|
| 2178 | \a a is expressed in base \a base, which is 10 by default and must
|
|---|
| 2179 | be between 2 and 36. If \a base is 10, the '%l' syntax can be used
|
|---|
| 2180 | to produce localized strings.
|
|---|
| 2181 | */
|
|---|
| 2182 |
|
|---|
| 2183 |
|
|---|
| 2184 | /*!
|
|---|
| 2185 | \overload
|
|---|
| 2186 |
|
|---|
| 2187 | \a a is assumed to be in the Latin-1 character set.
|
|---|
| 2188 | */
|
|---|
| 2189 | QString QString::arg( char a, int fieldWidth ) const
|
|---|
| 2190 | {
|
|---|
| 2191 | QString c;
|
|---|
| 2192 | c += a;
|
|---|
| 2193 | return arg( c, fieldWidth );
|
|---|
| 2194 | }
|
|---|
| 2195 |
|
|---|
| 2196 | /*!
|
|---|
| 2197 | \overload
|
|---|
| 2198 | */
|
|---|
| 2199 | QString QString::arg( QChar a, int fieldWidth ) const
|
|---|
| 2200 | {
|
|---|
| 2201 | QString c;
|
|---|
| 2202 | c += a;
|
|---|
| 2203 | return arg( c, fieldWidth );
|
|---|
| 2204 | }
|
|---|
| 2205 |
|
|---|
| 2206 | /*!
|
|---|
| 2207 | \overload
|
|---|
| 2208 |
|
|---|
| 2209 | \target arg-formats
|
|---|
| 2210 |
|
|---|
| 2211 | Argument \a a is formatted according to the \a fmt format specified,
|
|---|
| 2212 | which is 'g' by default and can be any of the following:
|
|---|
| 2213 |
|
|---|
| 2214 | \table
|
|---|
| 2215 | \header \i Format \i Meaning
|
|---|
| 2216 | \row \i \c e \i format as [-]9.9e[+|-]999
|
|---|
| 2217 | \row \i \c E \i format as [-]9.9E[+|-]999
|
|---|
| 2218 | \row \i \c f \i format as [-]9.9
|
|---|
| 2219 | \row \i \c g \i use \c e or \c f format, whichever is the most concise
|
|---|
| 2220 | \row \i \c G \i use \c E or \c f format, whichever is the most concise
|
|---|
| 2221 | \endtable
|
|---|
| 2222 |
|
|---|
| 2223 | With 'e', 'E', and 'f', \a prec is the number of digits after the
|
|---|
| 2224 | decimal point. With 'g' and 'G', \a prec is the maximum number of
|
|---|
| 2225 | significant digits (trailing zeroes are omitted).
|
|---|
| 2226 |
|
|---|
| 2227 | \code
|
|---|
| 2228 | double d = 12.34;
|
|---|
| 2229 | QString ds = QString( "'E' format, precision 3, gives %1" )
|
|---|
| 2230 | .arg( d, 0, 'E', 3 );
|
|---|
| 2231 | // ds == "1.234E+001"
|
|---|
| 2232 | \endcode
|
|---|
| 2233 |
|
|---|
| 2234 | The '%l' syntax can be used to produce localized strings.
|
|---|
| 2235 | */
|
|---|
| 2236 | QString QString::arg( double a, int fieldWidth, char fmt, int prec ) const
|
|---|
| 2237 | {
|
|---|
| 2238 | ArgEscapeData d = findArgEscapes(*this);
|
|---|
| 2239 |
|
|---|
| 2240 | if (d.occurrences == 0) {
|
|---|
| 2241 | qWarning( "QString::arg(): Argument missing: %s, %g", latin1(),
|
|---|
| 2242 | a );
|
|---|
| 2243 | return *this;
|
|---|
| 2244 | }
|
|---|
| 2245 |
|
|---|
| 2246 | QString arg;
|
|---|
| 2247 | if (d.occurrences > d.locale_occurrences)
|
|---|
| 2248 | arg = number(a, fmt, prec);
|
|---|
| 2249 |
|
|---|
| 2250 | QString locale_arg;
|
|---|
| 2251 | if (d.locale_occurrences > 0) {
|
|---|
| 2252 | QLocale locale;
|
|---|
| 2253 |
|
|---|
| 2254 | QLocalePrivate::DoubleForm form = QLocalePrivate::DFDecimal;
|
|---|
| 2255 | uint flags = 0;
|
|---|
| 2256 |
|
|---|
| 2257 | if (qIsUpper(fmt))
|
|---|
| 2258 | flags = QLocalePrivate::CapitalEorX;
|
|---|
| 2259 | fmt = qToLower(fmt);
|
|---|
| 2260 |
|
|---|
| 2261 | switch (fmt) {
|
|---|
| 2262 | case 'f':
|
|---|
| 2263 | form = QLocalePrivate::DFDecimal;
|
|---|
| 2264 | break;
|
|---|
| 2265 | case 'e':
|
|---|
| 2266 | form = QLocalePrivate::DFExponent;
|
|---|
| 2267 | break;
|
|---|
| 2268 | case 'g':
|
|---|
| 2269 | form = QLocalePrivate::DFSignificantDigits;
|
|---|
| 2270 | break;
|
|---|
| 2271 | default:
|
|---|
| 2272 | #if defined(QT_CHECK_RANGE)
|
|---|
| 2273 | qWarning( "QString::setNum: Invalid format char '%c'", fmt );
|
|---|
| 2274 | #endif
|
|---|
| 2275 | break;
|
|---|
| 2276 | }
|
|---|
| 2277 |
|
|---|
| 2278 | flags |= QLocalePrivate::ThousandsGroup;
|
|---|
| 2279 |
|
|---|
| 2280 | locale_arg = locale.d->doubleToString(a, prec, form, -1, flags);
|
|---|
| 2281 | }
|
|---|
| 2282 |
|
|---|
| 2283 | return replaceArgEscapes(*this, d, fieldWidth, arg, locale_arg);
|
|---|
| 2284 | }
|
|---|
| 2285 |
|
|---|
| 2286 | QString QString::multiArg( int numArgs, const QString& a1, const QString& a2,
|
|---|
| 2287 | const QString& a3, const QString& a4 ) const
|
|---|
| 2288 | {
|
|---|
| 2289 | QString result;
|
|---|
| 2290 | union {
|
|---|
| 2291 | int digitUsed[10];
|
|---|
| 2292 | int argForDigit[10];
|
|---|
| 2293 | };
|
|---|
| 2294 | register const QChar *uc = d->unicode;
|
|---|
| 2295 | const QString *args[4];
|
|---|
| 2296 | const int len = (int) length();
|
|---|
| 2297 | const int end = len - 1;
|
|---|
| 2298 | int lastDigit = -1;
|
|---|
| 2299 | int i;
|
|---|
| 2300 |
|
|---|
| 2301 | memset( digitUsed, 0, sizeof(digitUsed) );
|
|---|
| 2302 | args[0] = &a1;
|
|---|
| 2303 | args[1] = &a2;
|
|---|
| 2304 | args[2] = &a3;
|
|---|
| 2305 | args[3] = &a4;
|
|---|
| 2306 |
|
|---|
| 2307 | for ( i = 0; i < end; i++ ) {
|
|---|
| 2308 | if ( uc[i] == '%' ) {
|
|---|
| 2309 | int digit = uc[i + 1].unicode() - '0';
|
|---|
| 2310 | if ( digit >= 0 && digit <= 9 )
|
|---|
| 2311 | digitUsed[digit]++;
|
|---|
| 2312 | }
|
|---|
| 2313 | }
|
|---|
| 2314 |
|
|---|
| 2315 | for ( i = 0; i < numArgs; i++ ) {
|
|---|
| 2316 | do {
|
|---|
| 2317 | ++lastDigit;
|
|---|
| 2318 | } while ( lastDigit < 10 && digitUsed[lastDigit] == 0 );
|
|---|
| 2319 |
|
|---|
| 2320 | if ( lastDigit == 10 ) {
|
|---|
| 2321 | qWarning( "QString::arg(): Argument missing: %s, %s",
|
|---|
| 2322 | latin1(), args[i]->latin1() );
|
|---|
| 2323 | numArgs = i;
|
|---|
| 2324 | lastDigit = 9;
|
|---|
| 2325 | break;
|
|---|
| 2326 | }
|
|---|
| 2327 | argForDigit[lastDigit] = i;
|
|---|
| 2328 | }
|
|---|
| 2329 |
|
|---|
| 2330 | i = 0;
|
|---|
| 2331 | while ( i < len ) {
|
|---|
| 2332 | if ( uc[i] == '%' && i != end ) {
|
|---|
| 2333 | int digit = uc[i + 1].unicode() - '0';
|
|---|
| 2334 | if ( digit >= 0 && digit <= lastDigit ) {
|
|---|
| 2335 | result += *args[argForDigit[digit]];
|
|---|
| 2336 | i += 2;
|
|---|
| 2337 | continue;
|
|---|
| 2338 | }
|
|---|
| 2339 | }
|
|---|
| 2340 | result += uc[i++];
|
|---|
| 2341 | }
|
|---|
| 2342 | return result;
|
|---|
| 2343 | }
|
|---|
| 2344 |
|
|---|
| 2345 |
|
|---|
| 2346 | /*!
|
|---|
| 2347 | Safely builds a formatted string from the format string \a cformat
|
|---|
| 2348 | and an arbitrary list of arguments. The format string supports all
|
|---|
| 2349 | the escape sequences of printf() in the standard C library.
|
|---|
| 2350 |
|
|---|
| 2351 | The %s escape sequence expects a utf8() encoded string. The format
|
|---|
| 2352 | string \e cformat is expected to be in latin1. If you need a
|
|---|
| 2353 | Unicode format string, use arg() instead. For typesafe string
|
|---|
| 2354 | building, with full Unicode support, you can use QTextOStream like
|
|---|
| 2355 | this:
|
|---|
| 2356 |
|
|---|
| 2357 | \code
|
|---|
| 2358 | QString str;
|
|---|
| 2359 | QString s = ...;
|
|---|
| 2360 | int x = ...;
|
|---|
| 2361 | QTextOStream( &str ) << s << " : " << x;
|
|---|
| 2362 | \endcode
|
|---|
| 2363 |
|
|---|
| 2364 | For \link QObject::tr() translations,\endlink especially if the
|
|---|
| 2365 | strings contains more than one escape sequence, you should
|
|---|
| 2366 | consider using the arg() function instead. This allows the order
|
|---|
| 2367 | of the replacements to be controlled by the translator, and has
|
|---|
| 2368 | Unicode support.
|
|---|
| 2369 |
|
|---|
| 2370 | The %lc escape sequence expects a unicode character of type ushort
|
|---|
| 2371 | (as returned by QChar::unicode()).
|
|---|
| 2372 | The %ls escape sequence expects a pointer to a zero-terminated
|
|---|
| 2373 | array of unicode characters of type ushort (as returned by
|
|---|
| 2374 | QString::ucs2()).
|
|---|
| 2375 |
|
|---|
| 2376 | \sa arg()
|
|---|
| 2377 | */
|
|---|
| 2378 |
|
|---|
| 2379 | #ifndef QT_NO_SPRINTF
|
|---|
| 2380 | QString &QString::sprintf( const char* cformat, ... )
|
|---|
| 2381 | {
|
|---|
| 2382 | QLocale locale(QLocale::C);
|
|---|
| 2383 |
|
|---|
| 2384 | va_list ap;
|
|---|
| 2385 | va_start( ap, cformat );
|
|---|
| 2386 |
|
|---|
| 2387 | if ( !cformat || !*cformat ) {
|
|---|
| 2388 | // Qt 1.x compat
|
|---|
| 2389 | *this = fromLatin1( "" );
|
|---|
| 2390 | return *this;
|
|---|
| 2391 | }
|
|---|
| 2392 |
|
|---|
| 2393 | // Parse cformat
|
|---|
| 2394 |
|
|---|
| 2395 | QString result;
|
|---|
| 2396 | const char *c = cformat;
|
|---|
| 2397 | for (;;) {
|
|---|
| 2398 | // Copy non-escape chars to result
|
|---|
| 2399 | while (*c != '\0' && *c != '%')
|
|---|
| 2400 | result.append(*c++);
|
|---|
| 2401 |
|
|---|
| 2402 | if (*c == '\0')
|
|---|
| 2403 | break;
|
|---|
| 2404 |
|
|---|
| 2405 | // Found '%'
|
|---|
| 2406 | const char *escape_start = c;
|
|---|
| 2407 | ++c;
|
|---|
| 2408 |
|
|---|
| 2409 | if (*c == '\0') {
|
|---|
| 2410 | result.append('%'); // a % at the end of the string - treat as non-escape text
|
|---|
| 2411 | break;
|
|---|
| 2412 | }
|
|---|
| 2413 | if (*c == '%') {
|
|---|
| 2414 | result.append('%'); // %%
|
|---|
| 2415 | ++c;
|
|---|
| 2416 | continue;
|
|---|
| 2417 | }
|
|---|
| 2418 |
|
|---|
| 2419 | // Parse flag characters
|
|---|
| 2420 | unsigned flags = 0;
|
|---|
| 2421 | bool no_more_flags = false;
|
|---|
| 2422 | do {
|
|---|
| 2423 | switch (*c) {
|
|---|
| 2424 | case '#': flags |= QLocalePrivate::Alternate; break;
|
|---|
| 2425 | case '0': flags |= QLocalePrivate::ZeroPadded; break;
|
|---|
| 2426 | case '-': flags |= QLocalePrivate::LeftAdjusted; break;
|
|---|
| 2427 | case ' ': flags |= QLocalePrivate::BlankBeforePositive; break;
|
|---|
| 2428 | case '+': flags |= QLocalePrivate::AlwaysShowSign; break;
|
|---|
| 2429 | case '\'': flags |= QLocalePrivate::ThousandsGroup; break;
|
|---|
| 2430 | default: no_more_flags = true; break;
|
|---|
| 2431 | }
|
|---|
| 2432 |
|
|---|
| 2433 | if (!no_more_flags)
|
|---|
| 2434 | ++c;
|
|---|
| 2435 | } while (!no_more_flags);
|
|---|
| 2436 |
|
|---|
| 2437 | if (*c == '\0') {
|
|---|
| 2438 | result.append(escape_start); // incomplete escape, treat as non-escape text
|
|---|
| 2439 | break;
|
|---|
| 2440 | }
|
|---|
| 2441 |
|
|---|
| 2442 | // Parse field width
|
|---|
| 2443 | int width = -1; // -1 means unspecified
|
|---|
| 2444 | if (qIsDigit(*c)) {
|
|---|
| 2445 | QString width_str;
|
|---|
| 2446 | while (*c != '\0' && qIsDigit(*c))
|
|---|
| 2447 | width_str.append(*c++);
|
|---|
| 2448 |
|
|---|
| 2449 | // can't be negative - started with a digit
|
|---|
| 2450 | // contains at least one digit
|
|---|
| 2451 | width = width_str.toInt();
|
|---|
| 2452 | }
|
|---|
| 2453 | else if (*c == '*') {
|
|---|
| 2454 | width = va_arg(ap, int);
|
|---|
| 2455 | if (width < 0)
|
|---|
| 2456 | width = -1; // treat all negative numbers as unspecified
|
|---|
| 2457 | ++c;
|
|---|
| 2458 | }
|
|---|
| 2459 |
|
|---|
| 2460 | if (*c == '\0') {
|
|---|
| 2461 | result.append(escape_start); // incomplete escape, treat as non-escape text
|
|---|
| 2462 | break;
|
|---|
| 2463 | }
|
|---|
| 2464 |
|
|---|
| 2465 | // Parse precision
|
|---|
| 2466 | int precision = -1; // -1 means unspecified
|
|---|
| 2467 | if (*c == '.') {
|
|---|
| 2468 | ++c;
|
|---|
| 2469 | if (qIsDigit(*c)) {
|
|---|
| 2470 | QString precision_str;
|
|---|
| 2471 | while (*c != '\0' && qIsDigit(*c))
|
|---|
| 2472 | precision_str.append(*c++);
|
|---|
| 2473 |
|
|---|
| 2474 | // can't be negative - started with a digit
|
|---|
| 2475 | // contains at least one digit
|
|---|
| 2476 | precision = precision_str.toInt();
|
|---|
| 2477 | }
|
|---|
| 2478 | else if (*c == '*') {
|
|---|
| 2479 | precision = va_arg(ap, int);
|
|---|
| 2480 | if (precision < 0)
|
|---|
| 2481 | precision = -1; // treat all negative numbers as unspecified
|
|---|
| 2482 | ++c;
|
|---|
| 2483 | }
|
|---|
| 2484 | }
|
|---|
| 2485 |
|
|---|
| 2486 | if (*c == '\0') {
|
|---|
| 2487 | result.append(escape_start); // incomplete escape, treat as non-escape text
|
|---|
| 2488 | break;
|
|---|
| 2489 | }
|
|---|
| 2490 |
|
|---|
| 2491 | // Parse the length modifier
|
|---|
| 2492 | enum LengthMod { lm_none, lm_hh, lm_h, lm_l, lm_ll, lm_L, lm_j, lm_z, lm_t };
|
|---|
| 2493 | LengthMod length_mod = lm_none;
|
|---|
| 2494 | switch (*c) {
|
|---|
| 2495 | case 'h':
|
|---|
| 2496 | ++c;
|
|---|
| 2497 | if (*c == 'h') {
|
|---|
| 2498 | length_mod = lm_hh;
|
|---|
| 2499 | ++c;
|
|---|
| 2500 | }
|
|---|
| 2501 | else
|
|---|
| 2502 | length_mod = lm_h;
|
|---|
| 2503 | break;
|
|---|
| 2504 |
|
|---|
| 2505 | case 'l':
|
|---|
| 2506 | ++c;
|
|---|
| 2507 | if (*c == 'l') {
|
|---|
| 2508 | length_mod = lm_ll;
|
|---|
| 2509 | ++c;
|
|---|
| 2510 | }
|
|---|
| 2511 | else
|
|---|
| 2512 | length_mod = lm_l;
|
|---|
| 2513 | break;
|
|---|
| 2514 |
|
|---|
| 2515 | case 'L':
|
|---|
| 2516 | ++c;
|
|---|
| 2517 | length_mod = lm_L;
|
|---|
| 2518 | break;
|
|---|
| 2519 |
|
|---|
| 2520 | case 'j':
|
|---|
| 2521 | ++c;
|
|---|
| 2522 | length_mod = lm_j;
|
|---|
| 2523 | break;
|
|---|
| 2524 |
|
|---|
| 2525 | case 'z':
|
|---|
| 2526 | case 'Z':
|
|---|
| 2527 | ++c;
|
|---|
| 2528 | length_mod = lm_z;
|
|---|
| 2529 | break;
|
|---|
| 2530 |
|
|---|
| 2531 | case 't':
|
|---|
| 2532 | ++c;
|
|---|
| 2533 | length_mod = lm_t;
|
|---|
| 2534 | break;
|
|---|
| 2535 |
|
|---|
| 2536 | default: break;
|
|---|
| 2537 | }
|
|---|
| 2538 |
|
|---|
| 2539 | if (*c == '\0') {
|
|---|
| 2540 | result.append(escape_start); // incomplete escape, treat as non-escape text
|
|---|
| 2541 | break;
|
|---|
| 2542 | }
|
|---|
| 2543 |
|
|---|
| 2544 | // Parse the conversion specifier and do the conversion
|
|---|
| 2545 | QString subst;
|
|---|
| 2546 | switch (*c) {
|
|---|
| 2547 | case 'd':
|
|---|
| 2548 | case 'i': {
|
|---|
| 2549 | Q_LLONG i;
|
|---|
| 2550 | switch (length_mod) {
|
|---|
| 2551 | case lm_none: i = va_arg(ap, int); break;
|
|---|
| 2552 | case lm_hh: i = va_arg(ap, int); break;
|
|---|
| 2553 | case lm_h: i = va_arg(ap, int); break;
|
|---|
| 2554 | case lm_l: i = va_arg(ap, long int); break;
|
|---|
| 2555 | case lm_ll: i = va_arg(ap, Q_LLONG); break;
|
|---|
| 2556 | case lm_j: i = va_arg(ap, long int); break;
|
|---|
| 2557 | case lm_z: i = va_arg(ap, size_t); break;
|
|---|
| 2558 | case lm_t: i = va_arg(ap, int); break;
|
|---|
| 2559 | default: i = 0; break;
|
|---|
| 2560 | }
|
|---|
| 2561 | subst = locale.d->longLongToString(i, precision, 10, width, flags);
|
|---|
| 2562 | ++c;
|
|---|
| 2563 | break;
|
|---|
| 2564 | }
|
|---|
| 2565 | case 'o':
|
|---|
| 2566 | case 'u':
|
|---|
| 2567 | case 'x':
|
|---|
| 2568 | case 'X': {
|
|---|
| 2569 | Q_ULLONG u;
|
|---|
| 2570 | switch (length_mod) {
|
|---|
| 2571 | case lm_none: u = va_arg(ap, unsigned int); break;
|
|---|
| 2572 | case lm_hh: u = va_arg(ap, unsigned int); break;
|
|---|
| 2573 | case lm_h: u = va_arg(ap, unsigned int); break;
|
|---|
| 2574 | case lm_l: u = va_arg(ap, unsigned long int); break;
|
|---|
| 2575 | case lm_ll: u = va_arg(ap, Q_ULLONG); break;
|
|---|
| 2576 | default: u = 0; break;
|
|---|
| 2577 | }
|
|---|
| 2578 |
|
|---|
| 2579 | if (qIsUpper(*c))
|
|---|
| 2580 | flags |= QLocalePrivate::CapitalEorX;
|
|---|
| 2581 |
|
|---|
| 2582 | int base = 10;
|
|---|
| 2583 | switch (qToLower(*c)) {
|
|---|
| 2584 | case 'o':
|
|---|
| 2585 | base = 8; break;
|
|---|
| 2586 | case 'u':
|
|---|
| 2587 | base = 10; break;
|
|---|
| 2588 | case 'x':
|
|---|
| 2589 | base = 16; break;
|
|---|
| 2590 | default: break;
|
|---|
| 2591 | }
|
|---|
| 2592 | subst = locale.d->unsLongLongToString(u, precision, base, width, flags);
|
|---|
| 2593 | ++c;
|
|---|
| 2594 | break;
|
|---|
| 2595 | }
|
|---|
| 2596 | case 'E':
|
|---|
| 2597 | case 'e':
|
|---|
| 2598 | case 'F':
|
|---|
| 2599 | case 'f':
|
|---|
| 2600 | case 'G':
|
|---|
| 2601 | case 'g':
|
|---|
| 2602 | case 'A':
|
|---|
| 2603 | case 'a': {
|
|---|
| 2604 | double d;
|
|---|
| 2605 | if (length_mod == lm_L)
|
|---|
| 2606 | d = va_arg(ap, long double); // not supported - converted to a double
|
|---|
| 2607 | else
|
|---|
| 2608 | d = va_arg(ap, double);
|
|---|
| 2609 |
|
|---|
| 2610 | if (qIsUpper(*c))
|
|---|
| 2611 | flags |= QLocalePrivate::CapitalEorX;
|
|---|
| 2612 |
|
|---|
| 2613 | QLocalePrivate::DoubleForm form = QLocalePrivate::DFDecimal;
|
|---|
| 2614 | switch (qToLower(*c)) {
|
|---|
| 2615 | case 'e': form = QLocalePrivate::DFExponent; break;
|
|---|
| 2616 | case 'a': // not supported - decimal form used instead
|
|---|
| 2617 | case 'f': form = QLocalePrivate::DFDecimal; break;
|
|---|
| 2618 | case 'g': form = QLocalePrivate::DFSignificantDigits; break;
|
|---|
| 2619 | default: break;
|
|---|
| 2620 | }
|
|---|
| 2621 | subst = locale.d->doubleToString(d, precision, form, width, flags);
|
|---|
| 2622 | ++c;
|
|---|
| 2623 | break;
|
|---|
| 2624 | }
|
|---|
| 2625 | case 'c': {
|
|---|
| 2626 | if (length_mod == lm_l)
|
|---|
| 2627 | subst = QChar((ushort) va_arg(ap, int));
|
|---|
| 2628 | else
|
|---|
| 2629 | subst = (uchar) va_arg(ap, int);
|
|---|
| 2630 | ++c;
|
|---|
| 2631 | break;
|
|---|
| 2632 | }
|
|---|
| 2633 | case 's': {
|
|---|
| 2634 | if (length_mod == lm_l) {
|
|---|
| 2635 | const ushort *buff = va_arg(ap, const ushort*);
|
|---|
| 2636 | const ushort *ch = buff;
|
|---|
| 2637 | while (*ch != 0)
|
|---|
| 2638 | ++ch;
|
|---|
| 2639 | subst.setUnicodeCodes(buff, ch - buff);
|
|---|
| 2640 | } else
|
|---|
| 2641 | subst = QString::fromUtf8(va_arg(ap, const char*));
|
|---|
| 2642 | if (precision != -1)
|
|---|
| 2643 | subst.truncate(precision);
|
|---|
| 2644 | ++c;
|
|---|
| 2645 | break;
|
|---|
| 2646 | }
|
|---|
| 2647 | case 'p': {
|
|---|
| 2648 | Q_ULLONG i;
|
|---|
| 2649 | #ifdef Q_OS_WIN64
|
|---|
| 2650 | i = (Q_ULLONG) va_arg(ap, void*);
|
|---|
| 2651 | #else
|
|---|
| 2652 | i = (Q_ULONG) va_arg(ap, void*);
|
|---|
| 2653 | #endif
|
|---|
| 2654 | flags |= QLocalePrivate::Alternate;
|
|---|
| 2655 | subst = locale.d->unsLongLongToString(i, precision, 16, width, flags);
|
|---|
| 2656 | ++c;
|
|---|
| 2657 | break;
|
|---|
| 2658 | }
|
|---|
| 2659 | case 'n':
|
|---|
| 2660 | switch (length_mod) {
|
|---|
| 2661 | case lm_hh: {
|
|---|
| 2662 | signed char *n = va_arg(ap, signed char*);
|
|---|
| 2663 | *n = result.length();
|
|---|
| 2664 | break;
|
|---|
| 2665 | }
|
|---|
| 2666 | case lm_h: {
|
|---|
| 2667 | short int *n = va_arg(ap, short int*);
|
|---|
| 2668 | *n = result.length();
|
|---|
| 2669 | break;
|
|---|
| 2670 | }
|
|---|
| 2671 | case lm_l: {
|
|---|
| 2672 | long int *n = va_arg(ap, long int*);
|
|---|
| 2673 | *n = result.length();
|
|---|
| 2674 | break;
|
|---|
| 2675 | }
|
|---|
| 2676 | case lm_ll: {
|
|---|
| 2677 | Q_LLONG *n = va_arg(ap, Q_LLONG*);
|
|---|
| 2678 | volatile uint tmp = result.length(); // egcs-2.91.66 gets internal
|
|---|
| 2679 | *n = tmp; // compiler error without volatile
|
|---|
| 2680 | break;
|
|---|
| 2681 | }
|
|---|
| 2682 | default: {
|
|---|
| 2683 | int *n = va_arg(ap, int*);
|
|---|
| 2684 | *n = result.length();
|
|---|
| 2685 | break;
|
|---|
| 2686 | }
|
|---|
| 2687 | }
|
|---|
| 2688 | ++c;
|
|---|
| 2689 | break;
|
|---|
| 2690 |
|
|---|
| 2691 | default: // bad escape, treat as non-escape text
|
|---|
| 2692 | for (const char *cc = escape_start; cc != c; ++cc)
|
|---|
| 2693 | result.append(*cc);
|
|---|
| 2694 | continue;
|
|---|
| 2695 | }
|
|---|
| 2696 |
|
|---|
| 2697 | if (flags & QLocalePrivate::LeftAdjusted)
|
|---|
| 2698 | result.append(subst.leftJustify(width));
|
|---|
| 2699 | else
|
|---|
| 2700 | result.append(subst.rightJustify(width));
|
|---|
| 2701 | }
|
|---|
| 2702 |
|
|---|
| 2703 | va_end(ap);
|
|---|
| 2704 | *this = result;
|
|---|
| 2705 |
|
|---|
| 2706 | return *this;
|
|---|
| 2707 | }
|
|---|
| 2708 | #endif
|
|---|
| 2709 |
|
|---|
| 2710 | /*!
|
|---|
| 2711 | Fills the string with \a len characters of value \a c, and returns
|
|---|
| 2712 | a reference to the string.
|
|---|
| 2713 |
|
|---|
| 2714 | If \a len is negative (the default), the current string length is
|
|---|
| 2715 | used.
|
|---|
| 2716 |
|
|---|
| 2717 | \code
|
|---|
| 2718 | QString str;
|
|---|
| 2719 | str.fill( 'g', 5 ); // string == "ggggg"
|
|---|
| 2720 | \endcode
|
|---|
| 2721 | */
|
|---|
| 2722 |
|
|---|
| 2723 | QString& QString::fill( QChar c, int len )
|
|---|
| 2724 | {
|
|---|
| 2725 | if ( len < 0 )
|
|---|
| 2726 | len = length();
|
|---|
| 2727 | if ( len == 0 ) {
|
|---|
| 2728 | *this = "";
|
|---|
| 2729 | } else {
|
|---|
| 2730 | deref();
|
|---|
| 2731 | QChar * nd = QT_ALLOC_QCHAR_VEC( len );
|
|---|
| 2732 | d = new QStringData(nd,len,len);
|
|---|
| 2733 | while (len--) *nd++ = c;
|
|---|
| 2734 | }
|
|---|
| 2735 | return *this;
|
|---|
| 2736 | }
|
|---|
| 2737 |
|
|---|
| 2738 |
|
|---|
| 2739 | /*!
|
|---|
| 2740 | \fn QString QString::copy() const
|
|---|
| 2741 |
|
|---|
| 2742 | \obsolete
|
|---|
| 2743 |
|
|---|
| 2744 | In Qt 2.0 and later, all calls to this function are needless. Just
|
|---|
| 2745 | remove them.
|
|---|
| 2746 | */
|
|---|
| 2747 |
|
|---|
| 2748 | /*!
|
|---|
| 2749 | \overload
|
|---|
| 2750 |
|
|---|
| 2751 | Finds the first occurrence of the character \a c, starting at
|
|---|
| 2752 | position \a index. If \a index is -1, the search starts at the
|
|---|
| 2753 | last character; if -2, at the next to last character and so on.
|
|---|
| 2754 | (See findRev() for searching backwards.)
|
|---|
| 2755 |
|
|---|
| 2756 | If \a cs is TRUE (the default), the search is case sensitive;
|
|---|
| 2757 | otherwise the search is case insensitive.
|
|---|
| 2758 |
|
|---|
| 2759 | Returns the position of \a c or -1 if \a c could not be found.
|
|---|
| 2760 | */
|
|---|
| 2761 |
|
|---|
| 2762 | int QString::find( QChar c, int index, bool cs ) const
|
|---|
| 2763 | {
|
|---|
| 2764 | const uint l = length();
|
|---|
| 2765 | if ( index < 0 )
|
|---|
| 2766 | index += l;
|
|---|
| 2767 | if ( (uint)index >= l )
|
|---|
| 2768 | return -1;
|
|---|
| 2769 | register const QChar *uc = unicode()+index;
|
|---|
| 2770 | const QChar *end = unicode() + l;
|
|---|
| 2771 | if ( cs ) {
|
|---|
| 2772 | while ( uc < end && *uc != c )
|
|---|
| 2773 | uc++;
|
|---|
| 2774 | } else {
|
|---|
| 2775 | c = ::lower( c );
|
|---|
| 2776 | while ( uc < end && ::lower( *uc ) != c )
|
|---|
| 2777 | uc++;
|
|---|
| 2778 | }
|
|---|
| 2779 | if ( uint(uc - unicode()) >= l )
|
|---|
| 2780 | return -1;
|
|---|
| 2781 | return (int)(uc - unicode());
|
|---|
| 2782 | }
|
|---|
| 2783 |
|
|---|
| 2784 | /* an implementation of the Boyer-Moore search algorithm
|
|---|
| 2785 | */
|
|---|
| 2786 |
|
|---|
| 2787 | /* initializes the skiptable to know haw far ahead we can skip on a wrong match
|
|---|
| 2788 | */
|
|---|
| 2789 | static void bm_init_skiptable( const QString &pattern, uint *skiptable, bool cs )
|
|---|
| 2790 | {
|
|---|
| 2791 | int i = 0;
|
|---|
| 2792 | register uint *st = skiptable;
|
|---|
| 2793 | int l = pattern.length();
|
|---|
| 2794 | while ( i++ < 0x100/8 ) {
|
|---|
| 2795 | *(st++) = l;
|
|---|
| 2796 | *(st++) = l;
|
|---|
| 2797 | *(st++) = l;
|
|---|
| 2798 | *(st++) = l;
|
|---|
| 2799 | *(st++) = l;
|
|---|
| 2800 | *(st++) = l;
|
|---|
| 2801 | *(st++) = l;
|
|---|
| 2802 | *(st++) = l;
|
|---|
| 2803 | }
|
|---|
| 2804 | const QChar *uc = pattern.unicode();
|
|---|
| 2805 | if ( cs ) {
|
|---|
| 2806 | while ( l-- ) {
|
|---|
| 2807 | skiptable[ uc->cell() ] = l;
|
|---|
| 2808 | uc++;
|
|---|
| 2809 | }
|
|---|
| 2810 | } else {
|
|---|
| 2811 | while ( l-- ) {
|
|---|
| 2812 | skiptable[ ::lower( *uc ).cell() ] = l;
|
|---|
| 2813 | uc++;
|
|---|
| 2814 | }
|
|---|
| 2815 | }
|
|---|
| 2816 | }
|
|---|
| 2817 |
|
|---|
| 2818 | static int bm_find( const QString &str, int index, const QString &pattern, uint *skiptable, bool cs )
|
|---|
| 2819 | {
|
|---|
| 2820 | const uint l = str.length();
|
|---|
| 2821 | if ( pattern.isEmpty() )
|
|---|
| 2822 | return index > (int)l ? -1 : index;
|
|---|
| 2823 |
|
|---|
| 2824 | const QChar *uc = str.unicode();
|
|---|
| 2825 | const QChar *puc = pattern.unicode();
|
|---|
| 2826 | const uint pl = pattern.length();
|
|---|
| 2827 | const uint pl_minus_one = pl - 1;
|
|---|
| 2828 |
|
|---|
| 2829 | register const QChar *current = uc + index + pl_minus_one;
|
|---|
| 2830 | const QChar *end = uc + l;
|
|---|
| 2831 | if ( cs ) {
|
|---|
| 2832 | while ( current < end ) {
|
|---|
| 2833 | uint skip = skiptable[ current->cell() ];
|
|---|
| 2834 | if ( !skip ) {
|
|---|
| 2835 | // possible match
|
|---|
| 2836 | while ( skip < pl ) {
|
|---|
| 2837 | if ( *(current - skip ) != puc[pl_minus_one-skip] )
|
|---|
| 2838 | break;
|
|---|
| 2839 | skip++;
|
|---|
| 2840 | }
|
|---|
| 2841 | if ( skip > pl_minus_one ) { // we have a match
|
|---|
| 2842 | return (current - uc) - skip + 1;
|
|---|
| 2843 | }
|
|---|
| 2844 | // in case we don't have a match we are a bit inefficient as we only skip by one
|
|---|
| 2845 | // when we have the non matching char in the string.
|
|---|
| 2846 | if ( skiptable[ (current-skip)->cell() ] == pl )
|
|---|
| 2847 | skip = pl - skip;
|
|---|
| 2848 | else
|
|---|
| 2849 | skip = 1;
|
|---|
| 2850 | }
|
|---|
| 2851 | current += skip;
|
|---|
| 2852 | }
|
|---|
| 2853 | } else {
|
|---|
| 2854 | while ( current < end ) {
|
|---|
| 2855 | uint skip = skiptable[ ::lower( *current ).cell() ];
|
|---|
| 2856 | if ( !skip ) {
|
|---|
| 2857 | // possible match
|
|---|
| 2858 | while ( skip < pl ) {
|
|---|
| 2859 | if ( ::lower( *(current - skip) ) != ::lower( puc[pl_minus_one-skip] ) )
|
|---|
| 2860 | break;
|
|---|
| 2861 | skip++;
|
|---|
| 2862 | }
|
|---|
| 2863 | if ( skip > pl_minus_one ) // we have a match
|
|---|
| 2864 | return (current - uc) - skip + 1;
|
|---|
| 2865 | // in case we don't have a match we are a bit inefficient as we only skip by one
|
|---|
| 2866 | // when we have the non matching char in the string.
|
|---|
| 2867 | if ( skiptable[ ::lower(*(current - skip)).cell() ] == pl )
|
|---|
| 2868 | skip = pl - skip;
|
|---|
| 2869 | else
|
|---|
| 2870 | skip = 1;
|
|---|
| 2871 | }
|
|---|
| 2872 | current += skip;
|
|---|
| 2873 | }
|
|---|
| 2874 | }
|
|---|
| 2875 | // not found
|
|---|
| 2876 | return -1;
|
|---|
| 2877 | }
|
|---|
| 2878 |
|
|---|
| 2879 |
|
|---|
| 2880 | #define REHASH( a ) \
|
|---|
| 2881 | if ( sl_minus_1 < sizeof(uint) * CHAR_BIT ) \
|
|---|
| 2882 | hashHaystack -= (a) << sl_minus_1; \
|
|---|
| 2883 | hashHaystack <<= 1
|
|---|
| 2884 |
|
|---|
| 2885 | /*!
|
|---|
| 2886 | \overload
|
|---|
| 2887 |
|
|---|
| 2888 | Finds the first occurrence of the string \a str, starting at
|
|---|
| 2889 | position \a index. If \a index is -1, the search starts at the
|
|---|
| 2890 | last character, if it is -2, at the next to last character and so
|
|---|
| 2891 | on. (See findRev() for searching backwards.)
|
|---|
| 2892 |
|
|---|
| 2893 | If \a cs is TRUE (the default), the search is case sensitive;
|
|---|
| 2894 | otherwise the search is case insensitive.
|
|---|
| 2895 |
|
|---|
| 2896 | Returns the position of \a str or -1 if \a str could not be found.
|
|---|
| 2897 | */
|
|---|
| 2898 |
|
|---|
| 2899 | int QString::find( const QString& str, int index, bool cs ) const
|
|---|
| 2900 | {
|
|---|
| 2901 | const uint l = length();
|
|---|
| 2902 | const uint sl = str.length();
|
|---|
| 2903 | if ( index < 0 )
|
|---|
| 2904 | index += l;
|
|---|
| 2905 | if ( sl + index > l )
|
|---|
| 2906 | return -1;
|
|---|
| 2907 | if ( !sl )
|
|---|
| 2908 | return index;
|
|---|
| 2909 | if (!l)
|
|---|
| 2910 | return -1;
|
|---|
| 2911 |
|
|---|
| 2912 | #if defined(Q_OS_MACX) && defined(QT_MACOSX_VERSION) && QT_MACOSX_VERSION >= 0x1020
|
|---|
| 2913 | if ( sl == 1 )
|
|---|
| 2914 | return find( *str.unicode(), index, cs );
|
|---|
| 2915 | #endif
|
|---|
| 2916 |
|
|---|
| 2917 | // we use the Boyer-Moore algorithm in cases where the overhead
|
|---|
| 2918 | // for the hash table should pay off, otherwise we use a simple
|
|---|
| 2919 | // hash function
|
|---|
| 2920 | if ( l > 500 && sl > 5 ) {
|
|---|
| 2921 | uint skiptable[0x100];
|
|---|
| 2922 | bm_init_skiptable( str, skiptable, cs );
|
|---|
| 2923 | return bm_find( *this, index, str, skiptable, cs );
|
|---|
| 2924 | }
|
|---|
| 2925 |
|
|---|
| 2926 | /*
|
|---|
| 2927 | We use some hashing for efficiency's sake. Instead of
|
|---|
| 2928 | comparing strings, we compare the hash value of str with that of
|
|---|
| 2929 | a part of this QString. Only if that matches, we call ucstrncmp
|
|---|
| 2930 | or ucstrnicmp.
|
|---|
| 2931 | */
|
|---|
| 2932 | const QChar* needle = str.unicode();
|
|---|
| 2933 | const QChar* haystack = unicode() + index;
|
|---|
| 2934 | const QChar* end = unicode() + (l-sl);
|
|---|
| 2935 | const uint sl_minus_1 = sl-1;
|
|---|
| 2936 | uint hashNeedle = 0, hashHaystack = 0, i;
|
|---|
| 2937 |
|
|---|
| 2938 | if ( cs ) {
|
|---|
| 2939 | for ( i = 0; i < sl; ++i ) {
|
|---|
| 2940 | hashNeedle = ((hashNeedle<<1) + needle[i].unicode() );
|
|---|
| 2941 | hashHaystack = ((hashHaystack<<1) + haystack[i].unicode() );
|
|---|
| 2942 | }
|
|---|
| 2943 | hashHaystack -= (haystack+sl_minus_1)->unicode();
|
|---|
| 2944 |
|
|---|
| 2945 | while ( haystack <= end ) {
|
|---|
| 2946 | hashHaystack += (haystack+sl_minus_1)->unicode();
|
|---|
| 2947 | if ( hashHaystack == hashNeedle
|
|---|
| 2948 | && ucstrncmp( needle, haystack, sl ) == 0 )
|
|---|
| 2949 | return haystack-unicode();
|
|---|
| 2950 |
|
|---|
| 2951 | REHASH( haystack->unicode() );
|
|---|
| 2952 | ++haystack;
|
|---|
| 2953 | }
|
|---|
| 2954 | } else {
|
|---|
| 2955 | for ( i = 0; i < sl; ++i ) {
|
|---|
| 2956 | hashNeedle = ((hashNeedle<<1) +
|
|---|
| 2957 | ::lower( needle[i].unicode() ).unicode() );
|
|---|
| 2958 | hashHaystack = ((hashHaystack<<1) +
|
|---|
| 2959 | ::lower( haystack[i].unicode() ).unicode() );
|
|---|
| 2960 | }
|
|---|
| 2961 |
|
|---|
| 2962 | hashHaystack -= ::lower(*(haystack+sl_minus_1)).unicode();
|
|---|
| 2963 | while ( haystack <= end ) {
|
|---|
| 2964 | hashHaystack += ::lower(*(haystack+sl_minus_1)).unicode();
|
|---|
| 2965 | if ( hashHaystack == hashNeedle
|
|---|
| 2966 | && ucstrnicmp( needle, haystack, sl ) == 0 )
|
|---|
| 2967 | return haystack-unicode();
|
|---|
| 2968 |
|
|---|
| 2969 | REHASH( ::lower(*haystack).unicode() );
|
|---|
| 2970 | ++haystack;
|
|---|
| 2971 | }
|
|---|
| 2972 | }
|
|---|
| 2973 | return -1;
|
|---|
| 2974 | }
|
|---|
| 2975 |
|
|---|
| 2976 | /*!
|
|---|
| 2977 | \fn int QString::findRev( const char* str, int index ) const
|
|---|
| 2978 |
|
|---|
| 2979 | Equivalent to findRev(QString(\a str), \a index).
|
|---|
| 2980 | */
|
|---|
| 2981 |
|
|---|
| 2982 | /*!
|
|---|
| 2983 | \fn int QString::find( const char* str, int index ) const
|
|---|
| 2984 |
|
|---|
| 2985 | \overload
|
|---|
| 2986 |
|
|---|
| 2987 | Equivalent to find(QString(\a str), \a index).
|
|---|
| 2988 | */
|
|---|
| 2989 |
|
|---|
| 2990 | /*!
|
|---|
| 2991 | \overload
|
|---|
| 2992 |
|
|---|
| 2993 | Finds the first occurrence of the character \a c, starting at
|
|---|
| 2994 | position \a index and searching backwards. If the index is -1, the
|
|---|
| 2995 | search starts at the last character, if it is -2, at the next to
|
|---|
| 2996 | last character and so on.
|
|---|
| 2997 |
|
|---|
| 2998 | Returns the position of \a c or -1 if \a c could not be found.
|
|---|
| 2999 |
|
|---|
| 3000 | If \a cs is TRUE (the default), the search is case sensitive;
|
|---|
| 3001 | otherwise the search is case insensitive.
|
|---|
| 3002 |
|
|---|
| 3003 | \code
|
|---|
| 3004 | QString string( "bananas" );
|
|---|
| 3005 | int i = string.findRev( 'a' ); // i == 5
|
|---|
| 3006 | \endcode
|
|---|
| 3007 | */
|
|---|
| 3008 |
|
|---|
| 3009 | int QString::findRev( QChar c, int index, bool cs ) const
|
|---|
| 3010 | {
|
|---|
| 3011 | #if defined(Q_OS_MACX) && defined(QT_MACOSX_VERSION) && QT_MACOSX_VERSION < 0x1020
|
|---|
| 3012 | return findRev( QString( c ), index, cs );
|
|---|
| 3013 | #else
|
|---|
| 3014 | const uint l = length();
|
|---|
| 3015 | if ( index < 0 )
|
|---|
| 3016 | index += l;
|
|---|
| 3017 | if ( (uint)index >= l )
|
|---|
| 3018 | return -1;
|
|---|
| 3019 | const QChar *end = unicode();
|
|---|
| 3020 | register const QChar *uc = end + index;
|
|---|
| 3021 | if ( cs ) {
|
|---|
| 3022 | while ( uc >= end && *uc != c )
|
|---|
| 3023 | uc--;
|
|---|
| 3024 | } else {
|
|---|
| 3025 | c = ::lower( c );
|
|---|
| 3026 | while ( uc >= end && ::lower( *uc ) != c )
|
|---|
| 3027 | uc--;
|
|---|
| 3028 | }
|
|---|
| 3029 | return uc - end;
|
|---|
| 3030 | #endif
|
|---|
| 3031 | }
|
|---|
| 3032 |
|
|---|
| 3033 | /*!
|
|---|
| 3034 | \overload
|
|---|
| 3035 |
|
|---|
| 3036 | Finds the first occurrence of the string \a str, starting at
|
|---|
| 3037 | position \a index and searching backwards. If the index is -1, the
|
|---|
| 3038 | search starts at the last character, if it is -2, at the next to
|
|---|
| 3039 | last character and so on.
|
|---|
| 3040 |
|
|---|
| 3041 | Returns the position of \a str or -1 if \a str could not be found.
|
|---|
| 3042 |
|
|---|
| 3043 | If \a cs is TRUE (the default), the search is case sensitive;
|
|---|
| 3044 | otherwise the search is case insensitive.
|
|---|
| 3045 |
|
|---|
| 3046 | \code
|
|---|
| 3047 | QString string("bananas");
|
|---|
| 3048 | int i = string.findRev( "ana" ); // i == 3
|
|---|
| 3049 | \endcode
|
|---|
| 3050 | */
|
|---|
| 3051 |
|
|---|
| 3052 | int QString::findRev( const QString& str, int index, bool cs ) const
|
|---|
| 3053 | {
|
|---|
| 3054 | /*
|
|---|
| 3055 | See QString::find() for explanations.
|
|---|
| 3056 | */
|
|---|
| 3057 | const uint l = length();
|
|---|
| 3058 | if ( index < 0 )
|
|---|
| 3059 | index += l;
|
|---|
| 3060 | const uint sl = str.length();
|
|---|
| 3061 | int delta = l-sl;
|
|---|
| 3062 | if ( index < 0 || index > (int)l || delta < 0 )
|
|---|
| 3063 | return -1;
|
|---|
| 3064 | if ( index > delta )
|
|---|
| 3065 | index = delta;
|
|---|
| 3066 |
|
|---|
| 3067 | #if defined(Q_OS_MACX) && defined(QT_MACOSX_VERSION) && QT_MACOSX_VERSION >= 0x1020
|
|---|
| 3068 | if ( sl == 1 )
|
|---|
| 3069 | return findRev( *str.unicode(), index, cs );
|
|---|
| 3070 | #endif
|
|---|
| 3071 |
|
|---|
| 3072 | const QChar* needle = str.unicode();
|
|---|
| 3073 | const QChar* haystack = unicode() + index;
|
|---|
| 3074 | const QChar* end = unicode();
|
|---|
| 3075 | const uint sl_minus_1 = sl-1;
|
|---|
| 3076 | const QChar* n = needle+sl_minus_1;
|
|---|
| 3077 | const QChar* h = haystack+sl_minus_1;
|
|---|
| 3078 | uint hashNeedle = 0, hashHaystack = 0, i;
|
|---|
| 3079 |
|
|---|
| 3080 | if ( cs ) {
|
|---|
| 3081 | for ( i = 0; i < sl; ++i ) {
|
|---|
| 3082 | hashNeedle = ((hashNeedle<<1) + (n-i)->unicode() );
|
|---|
| 3083 | hashHaystack = ((hashHaystack<<1) + (h-i)->unicode() );
|
|---|
| 3084 | }
|
|---|
| 3085 | hashHaystack -= haystack->unicode();
|
|---|
| 3086 |
|
|---|
| 3087 | while ( haystack >= end ) {
|
|---|
| 3088 | hashHaystack += haystack->unicode();
|
|---|
| 3089 | if ( hashHaystack == hashNeedle
|
|---|
| 3090 | && ucstrncmp( needle, haystack, sl ) == 0 )
|
|---|
| 3091 | return haystack-unicode();
|
|---|
| 3092 | --haystack;
|
|---|
| 3093 | REHASH( (haystack+sl)->unicode() );
|
|---|
| 3094 | }
|
|---|
| 3095 | } else {
|
|---|
| 3096 | for ( i = 0; i < sl; ++i ) {
|
|---|
| 3097 | hashNeedle = ((hashNeedle<<1)
|
|---|
| 3098 | + ::lower( (n-i)->unicode() ).unicode() );
|
|---|
| 3099 | hashHaystack = ((hashHaystack<<1)
|
|---|
| 3100 | + ::lower( (h-i)->unicode() ).unicode() );
|
|---|
| 3101 | }
|
|---|
| 3102 | hashHaystack -= ::lower(*haystack).unicode();
|
|---|
| 3103 |
|
|---|
| 3104 | while ( haystack >= end ) {
|
|---|
| 3105 | hashHaystack += ::lower(*haystack).unicode();
|
|---|
| 3106 | if ( hashHaystack == hashNeedle
|
|---|
| 3107 | && ucstrnicmp( needle, haystack, sl ) == 0 )
|
|---|
| 3108 | return haystack-unicode();
|
|---|
| 3109 | --haystack;
|
|---|
| 3110 | REHASH( ::lower(*(haystack+sl)).unicode() );
|
|---|
| 3111 | }
|
|---|
| 3112 | }
|
|---|
| 3113 | return -1;
|
|---|
| 3114 | }
|
|---|
| 3115 |
|
|---|
| 3116 | #undef REHASH
|
|---|
| 3117 |
|
|---|
| 3118 | /*!
|
|---|
| 3119 | \enum QString::SectionFlags
|
|---|
| 3120 |
|
|---|
| 3121 | \value SectionDefault Empty fields are counted, leading and
|
|---|
| 3122 | trailing separators are not included, and the separator is
|
|---|
| 3123 | compared case sensitively.
|
|---|
| 3124 |
|
|---|
| 3125 | \value SectionSkipEmpty Treat empty fields as if they don't exist,
|
|---|
| 3126 | i.e. they are not considered as far as \e start and \e end are
|
|---|
| 3127 | concerned.
|
|---|
| 3128 |
|
|---|
| 3129 | \value SectionIncludeLeadingSep Include the leading separator (if
|
|---|
| 3130 | any) in the result string.
|
|---|
| 3131 |
|
|---|
| 3132 | \value SectionIncludeTrailingSep Include the trailing separator
|
|---|
| 3133 | (if any) in the result string.
|
|---|
| 3134 |
|
|---|
| 3135 | \value SectionCaseInsensitiveSeps Compare the separator
|
|---|
| 3136 | case-insensitively.
|
|---|
| 3137 |
|
|---|
| 3138 | Any of the last four values can be OR-ed together to form a flag.
|
|---|
| 3139 |
|
|---|
| 3140 | \sa section()
|
|---|
| 3141 | */
|
|---|
| 3142 |
|
|---|
| 3143 | /*!
|
|---|
| 3144 | \fn QString QString::section( QChar sep, int start, int end = 0xffffffff, int flags = SectionDefault ) const
|
|---|
| 3145 |
|
|---|
| 3146 | This function returns a section of the string.
|
|---|
| 3147 |
|
|---|
| 3148 | This string is treated as a sequence of fields separated by the
|
|---|
| 3149 | character, \a sep. The returned string consists of the fields from
|
|---|
| 3150 | position \a start to position \a end inclusive. If \a end is not
|
|---|
| 3151 | specified, all fields from position \a start to the end of the
|
|---|
| 3152 | string are included. Fields are numbered 0, 1, 2, etc., counting
|
|---|
| 3153 | from the left, and -1, -2, etc., counting from right to left.
|
|---|
| 3154 |
|
|---|
| 3155 | The \a flags argument can be used to affect some aspects of the
|
|---|
| 3156 | function's behaviour, e.g. whether to be case sensitive, whether
|
|---|
| 3157 | to skip empty fields and how to deal with leading and trailing
|
|---|
| 3158 | separators; see \l{SectionFlags}.
|
|---|
| 3159 |
|
|---|
| 3160 | \code
|
|---|
| 3161 | QString csv( "forename,middlename,surname,phone" );
|
|---|
| 3162 | QString s = csv.section( ',', 2, 2 ); // s == "surname"
|
|---|
| 3163 |
|
|---|
| 3164 | QString path( "/usr/local/bin/myapp" ); // First field is empty
|
|---|
| 3165 | QString s = path.section( '/', 3, 4 ); // s == "bin/myapp"
|
|---|
| 3166 | QString s = path.section( '/', 3, 3, SectionSkipEmpty ); // s == "myapp"
|
|---|
| 3167 | \endcode
|
|---|
| 3168 |
|
|---|
| 3169 | If \a start or \a end is negative, we count fields from the right
|
|---|
| 3170 | of the string, the right-most field being -1, the one from
|
|---|
| 3171 | right-most field being -2, and so on.
|
|---|
| 3172 |
|
|---|
| 3173 | \code
|
|---|
| 3174 | QString csv( "forename,middlename,surname,phone" );
|
|---|
| 3175 | QString s = csv.section( ',', -3, -2 ); // s == "middlename,surname"
|
|---|
| 3176 |
|
|---|
| 3177 | QString path( "/usr/local/bin/myapp" ); // First field is empty
|
|---|
| 3178 | QString s = path.section( '/', -1 ); // s == "myapp"
|
|---|
| 3179 | \endcode
|
|---|
| 3180 |
|
|---|
| 3181 | \sa QStringList::split()
|
|---|
| 3182 | */
|
|---|
| 3183 |
|
|---|
| 3184 | /*!
|
|---|
| 3185 | \overload
|
|---|
| 3186 |
|
|---|
| 3187 | This function returns a section of the string.
|
|---|
| 3188 |
|
|---|
| 3189 | This string is treated as a sequence of fields separated by the
|
|---|
| 3190 | string, \a sep. The returned string consists of the fields from
|
|---|
| 3191 | position \a start to position \a end inclusive. If \a end is not
|
|---|
| 3192 | specified, all fields from position \a start to the end of the
|
|---|
| 3193 | string are included. Fields are numbered 0, 1, 2, etc., counting
|
|---|
| 3194 | from the left, and -1, -2, etc., counting from right to left.
|
|---|
| 3195 |
|
|---|
| 3196 | The \a flags argument can be used to affect some aspects of the
|
|---|
| 3197 | function's behaviour, e.g. whether to be case sensitive, whether
|
|---|
| 3198 | to skip empty fields and how to deal with leading and trailing
|
|---|
| 3199 | separators; see \l{SectionFlags}.
|
|---|
| 3200 |
|
|---|
| 3201 | \code
|
|---|
| 3202 | QString data( "forename**middlename**surname**phone" );
|
|---|
| 3203 | QString s = data.section( "**", 2, 2 ); // s == "surname"
|
|---|
| 3204 | \endcode
|
|---|
| 3205 |
|
|---|
| 3206 | If \a start or \a end is negative, we count fields from the right
|
|---|
| 3207 | of the string, the right-most field being -1, the one from
|
|---|
| 3208 | right-most field being -2, and so on.
|
|---|
| 3209 |
|
|---|
| 3210 | \code
|
|---|
| 3211 | QString data( "forename**middlename**surname**phone" );
|
|---|
| 3212 | QString s = data.section( "**", -3, -2 ); // s == "middlename**surname"
|
|---|
| 3213 | \endcode
|
|---|
| 3214 |
|
|---|
| 3215 | \sa QStringList::split()
|
|---|
| 3216 | */
|
|---|
| 3217 |
|
|---|
| 3218 | QString QString::section( const QString &sep, int start, int end, int flags ) const
|
|---|
| 3219 | {
|
|---|
| 3220 | QStringList sections = QStringList::split(sep, *this, TRUE);
|
|---|
| 3221 | if(sections.isEmpty())
|
|---|
| 3222 | return QString();
|
|---|
| 3223 | if(!(flags & SectionSkipEmpty)) {
|
|---|
| 3224 | if(start < 0)
|
|---|
| 3225 | start += sections.count();
|
|---|
| 3226 | if(end < 0)
|
|---|
| 3227 | end += sections.count();
|
|---|
| 3228 | } else {
|
|---|
| 3229 | int skip = 0;
|
|---|
| 3230 | for(QStringList::Iterator it = sections.begin(); it != sections.end(); ++it) {
|
|---|
| 3231 | if((*it).isEmpty())
|
|---|
| 3232 | skip++;
|
|---|
| 3233 | }
|
|---|
| 3234 | if(start < 0)
|
|---|
| 3235 | start += sections.count() - skip;
|
|---|
| 3236 | if(end < 0)
|
|---|
| 3237 | end += sections.count() - skip;
|
|---|
| 3238 | }
|
|---|
| 3239 | int x = 0, run = 0;
|
|---|
| 3240 | QString ret;
|
|---|
| 3241 | for(QStringList::Iterator it = sections.begin(); x <= end && it != sections.end(); ++it) {
|
|---|
| 3242 | if(x >= start) {
|
|---|
| 3243 | if((*it).isEmpty()) {
|
|---|
| 3244 | run++;
|
|---|
| 3245 | } else {
|
|---|
| 3246 | if(!ret.isEmpty() || !(flags & SectionSkipEmpty)) {
|
|---|
| 3247 | int i_end = run;
|
|---|
| 3248 | if(!ret.isEmpty() && !(flags & SectionIncludeTrailingSep))
|
|---|
| 3249 | i_end++;
|
|---|
| 3250 | if((flags & SectionIncludeLeadingSep) && it != sections.begin() && x == start)
|
|---|
| 3251 | i_end++;
|
|---|
| 3252 | for(int i = 0; i < i_end; i++)
|
|---|
| 3253 | ret += sep;
|
|---|
| 3254 | } else if((flags & SectionIncludeLeadingSep) && it != sections.begin()) {
|
|---|
| 3255 | ret += sep;
|
|---|
| 3256 | }
|
|---|
| 3257 | run = 0;
|
|---|
| 3258 | ret += (*it);
|
|---|
| 3259 | if((flags & SectionIncludeTrailingSep) && it != sections.end())
|
|---|
| 3260 | ret += sep;
|
|---|
| 3261 | }
|
|---|
| 3262 | }
|
|---|
| 3263 | if(!(*it).isEmpty() || !(flags & SectionSkipEmpty))
|
|---|
| 3264 | x++;
|
|---|
| 3265 | }
|
|---|
| 3266 | return ret;
|
|---|
| 3267 | }
|
|---|
| 3268 |
|
|---|
| 3269 | #ifndef QT_NO_REGEXP
|
|---|
| 3270 | class section_chunk {
|
|---|
| 3271 | public:
|
|---|
| 3272 | section_chunk(int l, QString s) { length = l; string = s; }
|
|---|
| 3273 | int length;
|
|---|
| 3274 | QString string;
|
|---|
| 3275 | };
|
|---|
| 3276 | /*!
|
|---|
| 3277 | \overload
|
|---|
| 3278 |
|
|---|
| 3279 | This function returns a section of the string.
|
|---|
| 3280 |
|
|---|
| 3281 | This string is treated as a sequence of fields separated by the
|
|---|
| 3282 | regular expression, \a reg. The returned string consists of the
|
|---|
| 3283 | fields from position \a start to position \a end inclusive. If \a
|
|---|
| 3284 | end is not specified, all fields from position \a start to the end
|
|---|
| 3285 | of the string are included. Fields are numbered 0, 1, 2, etc., counting
|
|---|
| 3286 | from the left, and -1, -2, etc., counting from right to left.
|
|---|
| 3287 |
|
|---|
| 3288 | The \a flags argument can be used to affect some aspects of the
|
|---|
| 3289 | function's behaviour, e.g. whether to be case sensitive, whether
|
|---|
| 3290 | to skip empty fields and how to deal with leading and trailing
|
|---|
| 3291 | separators; see \l{SectionFlags}.
|
|---|
| 3292 |
|
|---|
| 3293 | \code
|
|---|
| 3294 | QString line( "forename\tmiddlename surname \t \t phone" );
|
|---|
| 3295 | QRegExp sep( "\s+" );
|
|---|
| 3296 | QString s = line.section( sep, 2, 2 ); // s == "surname"
|
|---|
| 3297 | \endcode
|
|---|
| 3298 |
|
|---|
| 3299 | If \a start or \a end is negative, we count fields from the right
|
|---|
| 3300 | of the string, the right-most field being -1, the one from
|
|---|
| 3301 | right-most field being -2, and so on.
|
|---|
| 3302 |
|
|---|
| 3303 | \code
|
|---|
| 3304 | QString line( "forename\tmiddlename surname \t \t phone" );
|
|---|
| 3305 | QRegExp sep( "\\s+" );
|
|---|
| 3306 | QString s = line.section( sep, -3, -2 ); // s == "middlename surname"
|
|---|
| 3307 | \endcode
|
|---|
| 3308 |
|
|---|
| 3309 | \warning Using this QRegExp version is much more expensive than
|
|---|
| 3310 | the overloaded string and character versions.
|
|---|
| 3311 |
|
|---|
| 3312 | \sa QStringList::split() simplifyWhiteSpace()
|
|---|
| 3313 | */
|
|---|
| 3314 |
|
|---|
| 3315 | QString QString::section( const QRegExp ®, int start, int end, int flags ) const
|
|---|
| 3316 | {
|
|---|
| 3317 | const QChar *uc = unicode();
|
|---|
| 3318 | if(!uc)
|
|---|
| 3319 | return QString();
|
|---|
| 3320 |
|
|---|
| 3321 | QRegExp sep(reg);
|
|---|
| 3322 | sep.setCaseSensitive(!(flags & SectionCaseInsensitiveSeps));
|
|---|
| 3323 |
|
|---|
| 3324 | QPtrList<section_chunk> l;
|
|---|
| 3325 | l.setAutoDelete(TRUE);
|
|---|
| 3326 | int n = length(), m = 0, last_m = 0, last = 0, last_len = 0;
|
|---|
| 3327 |
|
|---|
| 3328 | while ( ( m = sep.search( *this, m ) ) != -1 ) {
|
|---|
| 3329 | l.append(new section_chunk(last_len, QString(uc + last_m, m - last_m)));
|
|---|
| 3330 | last_m = m;
|
|---|
| 3331 | last_len = sep.matchedLength();
|
|---|
| 3332 | if((m += sep.matchedLength()) >= n) {
|
|---|
| 3333 | last = 1;
|
|---|
| 3334 | break;
|
|---|
| 3335 | }
|
|---|
| 3336 | }
|
|---|
| 3337 | if(!last)
|
|---|
| 3338 | l.append(new section_chunk(last_len, QString(uc + last_m, n - last_m)));
|
|---|
| 3339 |
|
|---|
| 3340 | if(start < 0)
|
|---|
| 3341 | start = l.count() + start;
|
|---|
| 3342 | if(end == -1)
|
|---|
| 3343 | end = l.count();
|
|---|
| 3344 | else if(end < 0)
|
|---|
| 3345 | end = l.count() + end;
|
|---|
| 3346 |
|
|---|
| 3347 | int i = 0;
|
|---|
| 3348 | QString ret;
|
|---|
| 3349 | for ( section_chunk *chk=l.first(); chk; chk=l.next(), i++ ) {
|
|---|
| 3350 | if((flags & SectionSkipEmpty) && chk->length == (int)chk->string.length()) {
|
|---|
| 3351 | if(i <= start)
|
|---|
| 3352 | start++;
|
|---|
| 3353 | end++;
|
|---|
| 3354 | }
|
|---|
| 3355 | if(i == start) {
|
|---|
| 3356 | ret = (flags & SectionIncludeLeadingSep) ? chk->string : chk->string.mid(chk->length);
|
|---|
| 3357 | } else if(i > start) {
|
|---|
| 3358 | ret += chk->string;
|
|---|
| 3359 | }
|
|---|
| 3360 | if(i == end) {
|
|---|
| 3361 | if((chk=l.next()) && flags & SectionIncludeTrailingSep)
|
|---|
| 3362 | ret += chk->string.left(chk->length);
|
|---|
| 3363 | break;
|
|---|
| 3364 | }
|
|---|
| 3365 | }
|
|---|
| 3366 | return ret;
|
|---|
| 3367 | }
|
|---|
| 3368 | #endif
|
|---|
| 3369 |
|
|---|
| 3370 | /*!
|
|---|
| 3371 | \fn QString QString::section( char sep, int start, int end = 0xffffffff, int flags = SectionDefault ) const
|
|---|
| 3372 |
|
|---|
| 3373 | \overload
|
|---|
| 3374 | */
|
|---|
| 3375 |
|
|---|
| 3376 | /*!
|
|---|
| 3377 | \fn QString QString::section( const char *sep, int start, int end = 0xffffffff, int flags = SectionDefault ) const
|
|---|
| 3378 |
|
|---|
| 3379 | \overload
|
|---|
| 3380 | */
|
|---|
| 3381 |
|
|---|
| 3382 |
|
|---|
| 3383 | /*!
|
|---|
| 3384 | Returns the number of times the character \a c occurs in the
|
|---|
| 3385 | string.
|
|---|
| 3386 |
|
|---|
| 3387 | If \a cs is TRUE (the default), the search is case sensitive;
|
|---|
| 3388 | otherwise the search is case insensitive.
|
|---|
| 3389 |
|
|---|
| 3390 | \code
|
|---|
| 3391 | QString string( "Trolltech and Qt" );
|
|---|
| 3392 | int n = string.contains( 't', FALSE );
|
|---|
| 3393 | // n == 3
|
|---|
| 3394 | \endcode
|
|---|
| 3395 | */
|
|---|
| 3396 |
|
|---|
| 3397 | int QString::contains( QChar c, bool cs ) const
|
|---|
| 3398 | {
|
|---|
| 3399 | int count = 0;
|
|---|
| 3400 | const QChar *uc = unicode();
|
|---|
| 3401 | if ( !uc )
|
|---|
| 3402 | return 0;
|
|---|
| 3403 | int n = length();
|
|---|
| 3404 | if ( cs ) {
|
|---|
| 3405 | while ( n-- )
|
|---|
| 3406 | if ( *uc++ == c )
|
|---|
| 3407 | count++;
|
|---|
| 3408 | } else {
|
|---|
| 3409 | c = ::lower( c );
|
|---|
| 3410 | while ( n-- ) {
|
|---|
| 3411 | if ( ::lower( *uc ) == c )
|
|---|
| 3412 | count++;
|
|---|
| 3413 | uc++;
|
|---|
| 3414 | }
|
|---|
| 3415 | }
|
|---|
| 3416 | return count;
|
|---|
| 3417 | }
|
|---|
| 3418 |
|
|---|
| 3419 | /*!
|
|---|
| 3420 | \overload
|
|---|
| 3421 |
|
|---|
| 3422 | Returns the number of times the string \a str occurs in the string.
|
|---|
| 3423 |
|
|---|
| 3424 | If \a cs is TRUE (the default), the search is case sensitive;
|
|---|
| 3425 | otherwise the search is case insensitive.
|
|---|
| 3426 | */
|
|---|
| 3427 | int QString::contains( const char* str, bool cs ) const
|
|---|
| 3428 | {
|
|---|
| 3429 | return contains( QString(str), cs );
|
|---|
| 3430 | }
|
|---|
| 3431 |
|
|---|
| 3432 | /*!
|
|---|
| 3433 | \fn int QString::contains( char c, bool cs ) const
|
|---|
| 3434 |
|
|---|
| 3435 | \overload
|
|---|
| 3436 | */
|
|---|
| 3437 |
|
|---|
| 3438 | /*!
|
|---|
| 3439 | \fn int QString::find( char c, int index, bool cs ) const
|
|---|
| 3440 |
|
|---|
| 3441 | \overload
|
|---|
| 3442 |
|
|---|
| 3443 | Find character \a c starting from position \a index.
|
|---|
| 3444 |
|
|---|
| 3445 | If \a cs is TRUE (the default), the search is case sensitive;
|
|---|
| 3446 | otherwise the search is case insensitive.
|
|---|
| 3447 | */
|
|---|
| 3448 |
|
|---|
| 3449 | /*!
|
|---|
| 3450 | \fn int QString::findRev( char c, int index, bool cs ) const
|
|---|
| 3451 |
|
|---|
| 3452 | \overload
|
|---|
| 3453 |
|
|---|
| 3454 | Find character \a c starting from position \a index and working
|
|---|
| 3455 | backwards.
|
|---|
| 3456 |
|
|---|
| 3457 | If \a cs is TRUE (the default), the search is case sensitive;
|
|---|
| 3458 | otherwise the search is case insensitive.
|
|---|
| 3459 | */
|
|---|
| 3460 |
|
|---|
| 3461 | /*!
|
|---|
| 3462 | \overload
|
|---|
| 3463 |
|
|---|
| 3464 | Returns the number of times \a str occurs in the string.
|
|---|
| 3465 |
|
|---|
| 3466 | If \a cs is TRUE (the default), the search is case sensitive;
|
|---|
| 3467 | otherwise the search is case insensitive.
|
|---|
| 3468 |
|
|---|
| 3469 | This function counts overlapping strings, so in the example below,
|
|---|
| 3470 | there are two instances of "ana" in "bananas".
|
|---|
| 3471 |
|
|---|
| 3472 | \code
|
|---|
| 3473 | QString str( "bananas" );
|
|---|
| 3474 | int i = str.contains( "ana" ); // i == 2
|
|---|
| 3475 | \endcode
|
|---|
| 3476 |
|
|---|
| 3477 | \sa findRev()
|
|---|
| 3478 | */
|
|---|
| 3479 |
|
|---|
| 3480 | int QString::contains( const QString &str, bool cs ) const
|
|---|
| 3481 | {
|
|---|
| 3482 | if ( isNull() )
|
|---|
| 3483 | return 0;
|
|---|
| 3484 | int count = 0;
|
|---|
| 3485 | uint skiptable[0x100];
|
|---|
| 3486 | bm_init_skiptable( str, skiptable, cs );
|
|---|
| 3487 | int i = -1;
|
|---|
| 3488 | // use boyer-moore for the ultimate speed experience
|
|---|
| 3489 | while ( ( i = bm_find( *this, i + 1, str, skiptable, cs ) ) != -1 )
|
|---|
| 3490 | count++;
|
|---|
| 3491 | return count;
|
|---|
| 3492 | }
|
|---|
| 3493 |
|
|---|
| 3494 | /*!
|
|---|
| 3495 | Returns a substring that contains the \a len leftmost characters
|
|---|
| 3496 | of the string.
|
|---|
| 3497 |
|
|---|
| 3498 | The whole string is returned if \a len exceeds the length of the
|
|---|
| 3499 | string.
|
|---|
| 3500 |
|
|---|
| 3501 | \code
|
|---|
| 3502 | QString s = "Pineapple";
|
|---|
| 3503 | QString t = s.left( 4 ); // t == "Pine"
|
|---|
| 3504 | \endcode
|
|---|
| 3505 |
|
|---|
| 3506 | \sa right(), mid(), isEmpty()
|
|---|
| 3507 | */
|
|---|
| 3508 |
|
|---|
| 3509 | QString QString::left( uint len ) const
|
|---|
| 3510 | {
|
|---|
| 3511 | if ( isEmpty() ) {
|
|---|
| 3512 | return QString();
|
|---|
| 3513 | } else if ( len == 0 ) { // ## just for 1.x compat:
|
|---|
| 3514 | return fromLatin1( "" );
|
|---|
| 3515 | } else if ( len >= length() ) {
|
|---|
| 3516 | return *this;
|
|---|
| 3517 | } else {
|
|---|
| 3518 | QString s( len, TRUE );
|
|---|
| 3519 | memcpy( s.d->unicode, d->unicode, len * sizeof(QChar) );
|
|---|
| 3520 | s.d->len = len;
|
|---|
| 3521 | return s;
|
|---|
| 3522 | }
|
|---|
| 3523 | }
|
|---|
| 3524 |
|
|---|
| 3525 | /*!
|
|---|
| 3526 | Returns a string that contains the \a len rightmost characters of
|
|---|
| 3527 | the string.
|
|---|
| 3528 |
|
|---|
| 3529 | If \a len is greater than the length of the string then the whole
|
|---|
| 3530 | string is returned.
|
|---|
| 3531 |
|
|---|
| 3532 | \code
|
|---|
| 3533 | QString string( "Pineapple" );
|
|---|
| 3534 | QString t = string.right( 5 ); // t == "apple"
|
|---|
| 3535 | \endcode
|
|---|
| 3536 |
|
|---|
| 3537 | \sa left(), mid(), isEmpty()
|
|---|
| 3538 | */
|
|---|
| 3539 |
|
|---|
| 3540 | QString QString::right( uint len ) const
|
|---|
| 3541 | {
|
|---|
| 3542 | if ( isEmpty() ) {
|
|---|
| 3543 | return QString();
|
|---|
| 3544 | } else if ( len == 0 ) { // ## just for 1.x compat:
|
|---|
| 3545 | return fromLatin1( "" );
|
|---|
| 3546 | } else {
|
|---|
| 3547 | uint l = length();
|
|---|
| 3548 | if ( len >= l )
|
|---|
| 3549 | return *this;
|
|---|
| 3550 | QString s( len, TRUE );
|
|---|
| 3551 | memcpy( s.d->unicode, d->unicode+(l-len), len*sizeof(QChar) );
|
|---|
| 3552 | s.d->len = len;
|
|---|
| 3553 | return s;
|
|---|
| 3554 | }
|
|---|
| 3555 | }
|
|---|
| 3556 |
|
|---|
| 3557 | /*!
|
|---|
| 3558 | Returns a string that contains the \a len characters of this
|
|---|
| 3559 | string, starting at position \a index.
|
|---|
| 3560 |
|
|---|
| 3561 | Returns a null string if the string is empty or \a index is out of
|
|---|
| 3562 | range. Returns the whole string from \a index if \a index + \a len
|
|---|
| 3563 | exceeds the length of the string.
|
|---|
| 3564 |
|
|---|
| 3565 | \code
|
|---|
| 3566 | QString s( "Five pineapples" );
|
|---|
| 3567 | QString t = s.mid( 5, 4 ); // t == "pine"
|
|---|
| 3568 | \endcode
|
|---|
| 3569 |
|
|---|
| 3570 | \sa left(), right()
|
|---|
| 3571 | */
|
|---|
| 3572 |
|
|---|
| 3573 | QString QString::mid( uint index, uint len ) const
|
|---|
| 3574 | {
|
|---|
| 3575 | uint slen = length();
|
|---|
| 3576 | if ( isEmpty() || index >= slen ) {
|
|---|
| 3577 | return QString();
|
|---|
| 3578 | } else if ( len == 0 ) { // ## just for 1.x compat:
|
|---|
| 3579 | return fromLatin1( "" );
|
|---|
| 3580 | } else {
|
|---|
| 3581 | if ( len > slen-index )
|
|---|
| 3582 | len = slen - index;
|
|---|
| 3583 | if ( index == 0 && len == slen )
|
|---|
| 3584 | return *this;
|
|---|
| 3585 | register const QChar *p = unicode()+index;
|
|---|
| 3586 | QString s( len, TRUE );
|
|---|
| 3587 | memcpy( s.d->unicode, p, len * sizeof(QChar) );
|
|---|
| 3588 | s.d->len = len;
|
|---|
| 3589 | return s;
|
|---|
| 3590 | }
|
|---|
| 3591 | }
|
|---|
| 3592 |
|
|---|
| 3593 | /*!
|
|---|
| 3594 | Returns a string of length \a width that contains this string
|
|---|
| 3595 | padded by the \a fill character.
|
|---|
| 3596 |
|
|---|
| 3597 | If \a truncate is FALSE and the length of the string is more than
|
|---|
| 3598 | \a width, then the returned string is a copy of the string.
|
|---|
| 3599 |
|
|---|
| 3600 | If \a truncate is TRUE and the length of the string is more than
|
|---|
| 3601 | \a width, then any characters in a copy of the string after length
|
|---|
| 3602 | \a width are removed, and the copy is returned.
|
|---|
| 3603 |
|
|---|
| 3604 | \code
|
|---|
| 3605 | QString s( "apple" );
|
|---|
| 3606 | QString t = s.leftJustify( 8, '.' ); // t == "apple..."
|
|---|
| 3607 | \endcode
|
|---|
| 3608 |
|
|---|
| 3609 | \sa rightJustify()
|
|---|
| 3610 | */
|
|---|
| 3611 |
|
|---|
| 3612 | QString QString::leftJustify( uint width, QChar fill, bool truncate ) const
|
|---|
| 3613 | {
|
|---|
| 3614 | QString result;
|
|---|
| 3615 | int len = length();
|
|---|
| 3616 | int padlen = width - len;
|
|---|
| 3617 | if ( padlen > 0 ) {
|
|---|
| 3618 | result.setLength(len+padlen);
|
|---|
| 3619 | if ( len )
|
|---|
| 3620 | memcpy( result.d->unicode, unicode(), sizeof(QChar)*len );
|
|---|
| 3621 | QChar* uc = result.d->unicode + len;
|
|---|
| 3622 | while (padlen--)
|
|---|
| 3623 | *uc++ = fill;
|
|---|
| 3624 | } else {
|
|---|
| 3625 | if ( truncate )
|
|---|
| 3626 | result = left( width );
|
|---|
| 3627 | else
|
|---|
| 3628 | result = *this;
|
|---|
| 3629 | }
|
|---|
| 3630 | return result;
|
|---|
| 3631 | }
|
|---|
| 3632 |
|
|---|
| 3633 | /*!
|
|---|
| 3634 | Returns a string of length \a width that contains the \a fill
|
|---|
| 3635 | character followed by the string.
|
|---|
| 3636 |
|
|---|
| 3637 | If \a truncate is FALSE and the length of the string is more than
|
|---|
| 3638 | \a width, then the returned string is a copy of the string.
|
|---|
| 3639 |
|
|---|
| 3640 | If \a truncate is TRUE and the length of the string is more than
|
|---|
| 3641 | \a width, then the resulting string is truncated at position \a
|
|---|
| 3642 | width.
|
|---|
| 3643 |
|
|---|
| 3644 | \code
|
|---|
| 3645 | QString string( "apple" );
|
|---|
| 3646 | QString t = string.rightJustify( 8, '.' ); // t == "...apple"
|
|---|
| 3647 | \endcode
|
|---|
| 3648 |
|
|---|
| 3649 | \sa leftJustify()
|
|---|
| 3650 | */
|
|---|
| 3651 |
|
|---|
| 3652 | QString QString::rightJustify( uint width, QChar fill, bool truncate ) const
|
|---|
| 3653 | {
|
|---|
| 3654 | QString result;
|
|---|
| 3655 | int len = length();
|
|---|
| 3656 | int padlen = width - len;
|
|---|
| 3657 | if ( padlen > 0 ) {
|
|---|
| 3658 | result.setLength( len+padlen );
|
|---|
| 3659 | QChar* uc = result.d->unicode;
|
|---|
| 3660 | while (padlen--)
|
|---|
| 3661 | *uc++ = fill;
|
|---|
| 3662 | if ( len )
|
|---|
| 3663 | memcpy( uc, unicode(), sizeof(QChar)*len );
|
|---|
| 3664 | } else {
|
|---|
| 3665 | if ( truncate )
|
|---|
| 3666 | result = left( width );
|
|---|
| 3667 | else
|
|---|
| 3668 | result = *this;
|
|---|
| 3669 | }
|
|---|
| 3670 | return result;
|
|---|
| 3671 | }
|
|---|
| 3672 |
|
|---|
| 3673 | /*!
|
|---|
| 3674 | Returns a lowercase copy of the string.
|
|---|
| 3675 |
|
|---|
| 3676 | \code
|
|---|
| 3677 | QString string( "TROlltECH" );
|
|---|
| 3678 | str = string.lower(); // str == "trolltech"
|
|---|
| 3679 | \endcode
|
|---|
| 3680 |
|
|---|
| 3681 | \sa upper()
|
|---|
| 3682 | */
|
|---|
| 3683 |
|
|---|
| 3684 | QString QString::lower() const
|
|---|
| 3685 | {
|
|---|
| 3686 | int l = length();
|
|---|
| 3687 | register QChar *p = d->unicode;
|
|---|
| 3688 | while ( l ) {
|
|---|
| 3689 | if ( *p != ::lower(*p) ) {
|
|---|
| 3690 | QString s( *this );
|
|---|
| 3691 | s.real_detach();
|
|---|
| 3692 | p = s.d->unicode + ( p - d->unicode );
|
|---|
| 3693 | while ( l ) {
|
|---|
| 3694 | *p = ::lower( *p );
|
|---|
| 3695 | l--;
|
|---|
| 3696 | p++;
|
|---|
| 3697 | }
|
|---|
| 3698 | return s;
|
|---|
| 3699 | }
|
|---|
| 3700 | l--;
|
|---|
| 3701 | p++;
|
|---|
| 3702 | }
|
|---|
| 3703 | return *this;
|
|---|
| 3704 | }
|
|---|
| 3705 |
|
|---|
| 3706 | /*!
|
|---|
| 3707 | Returns an uppercase copy of the string.
|
|---|
| 3708 |
|
|---|
| 3709 | \code
|
|---|
| 3710 | QString string( "TeXt" );
|
|---|
| 3711 | str = string.upper(); // t == "TEXT"
|
|---|
| 3712 | \endcode
|
|---|
| 3713 |
|
|---|
| 3714 | \sa lower()
|
|---|
| 3715 | */
|
|---|
| 3716 |
|
|---|
| 3717 | QString QString::upper() const
|
|---|
| 3718 | {
|
|---|
| 3719 | int l = length();
|
|---|
| 3720 | register QChar *p = d->unicode;
|
|---|
| 3721 | while ( l ) {
|
|---|
| 3722 | if ( *p != ::upper(*p) ) {
|
|---|
| 3723 | QString s( *this );
|
|---|
| 3724 | s.real_detach();
|
|---|
| 3725 | p = s.d->unicode + ( p - d->unicode );
|
|---|
| 3726 | while ( l ) {
|
|---|
| 3727 | *p = ::upper( *p );
|
|---|
| 3728 | l--;
|
|---|
| 3729 | p++;
|
|---|
| 3730 | }
|
|---|
| 3731 | return s;
|
|---|
| 3732 | }
|
|---|
| 3733 | l--;
|
|---|
| 3734 | p++;
|
|---|
| 3735 | }
|
|---|
| 3736 | return *this;
|
|---|
| 3737 | }
|
|---|
| 3738 |
|
|---|
| 3739 |
|
|---|
| 3740 | /*!
|
|---|
| 3741 | Returns a string that has whitespace removed from the start and
|
|---|
| 3742 | the end.
|
|---|
| 3743 |
|
|---|
| 3744 | Whitespace means any character for which QChar::isSpace() returns
|
|---|
| 3745 | TRUE. This includes Unicode characters with decimal values 9
|
|---|
| 3746 | (TAB), 10 (LF), 11 (VT), 12 (FF), 13 (CR) and 32 (Space), and may
|
|---|
| 3747 | also include other Unicode characters.
|
|---|
| 3748 |
|
|---|
| 3749 | \code
|
|---|
| 3750 | QString string = " white space ";
|
|---|
| 3751 | QString s = string.stripWhiteSpace(); // s == "white space"
|
|---|
| 3752 | \endcode
|
|---|
| 3753 |
|
|---|
| 3754 | \sa simplifyWhiteSpace()
|
|---|
| 3755 | */
|
|---|
| 3756 |
|
|---|
| 3757 | QString QString::stripWhiteSpace() const
|
|---|
| 3758 | {
|
|---|
| 3759 | if ( isEmpty() ) // nothing to do
|
|---|
| 3760 | return *this;
|
|---|
| 3761 | register const QChar *s = unicode();
|
|---|
| 3762 | if ( !s->isSpace() && !s[length()-1].isSpace() )
|
|---|
| 3763 | return *this;
|
|---|
| 3764 |
|
|---|
| 3765 | int start = 0;
|
|---|
| 3766 | int end = length() - 1;
|
|---|
| 3767 | while ( start<=end && s[start].isSpace() ) // skip white space from start
|
|---|
| 3768 | start++;
|
|---|
| 3769 | if ( start <= end ) { // only white space
|
|---|
| 3770 | while ( end && s[end].isSpace() ) // skip white space from end
|
|---|
| 3771 | end--;
|
|---|
| 3772 | }
|
|---|
| 3773 | int l = end - start + 1;
|
|---|
| 3774 | if ( l <= 0 )
|
|---|
| 3775 | return QString::fromLatin1("");
|
|---|
| 3776 |
|
|---|
| 3777 | QString result( l, TRUE );
|
|---|
| 3778 | memcpy( result.d->unicode, &s[start], sizeof(QChar)*l );
|
|---|
| 3779 | result.d->len = l;
|
|---|
| 3780 | return result;
|
|---|
| 3781 | }
|
|---|
| 3782 |
|
|---|
| 3783 |
|
|---|
| 3784 | /*!
|
|---|
| 3785 | Returns a string that has whitespace removed from the start and
|
|---|
| 3786 | the end, and which has each sequence of internal whitespace
|
|---|
| 3787 | replaced with a single space.
|
|---|
| 3788 |
|
|---|
| 3789 | Whitespace means any character for which QChar::isSpace() returns
|
|---|
| 3790 | TRUE. This includes Unicode characters with decimal values 9
|
|---|
| 3791 | (TAB), 10 (LF), 11 (VT), 12 (FF), 13 (CR), and 32 (Space).
|
|---|
| 3792 |
|
|---|
| 3793 | \code
|
|---|
| 3794 | QString string = " lots\t of\nwhite space ";
|
|---|
| 3795 | QString t = string.simplifyWhiteSpace();
|
|---|
| 3796 | // t == "lots of white space"
|
|---|
| 3797 | \endcode
|
|---|
| 3798 |
|
|---|
| 3799 | \sa stripWhiteSpace()
|
|---|
| 3800 | */
|
|---|
| 3801 |
|
|---|
| 3802 | QString QString::simplifyWhiteSpace() const
|
|---|
| 3803 | {
|
|---|
| 3804 | if ( isEmpty() )
|
|---|
| 3805 | return *this;
|
|---|
| 3806 | QString result;
|
|---|
| 3807 | result.setLength( length() );
|
|---|
| 3808 | const QChar *from = unicode();
|
|---|
| 3809 | const QChar *fromend = from+length();
|
|---|
| 3810 | int outc=0;
|
|---|
| 3811 | QChar *to = result.d->unicode;
|
|---|
| 3812 | for (;;) {
|
|---|
| 3813 | while ( from!=fromend && from->isSpace() )
|
|---|
| 3814 | from++;
|
|---|
| 3815 | while ( from!=fromend && !from->isSpace() )
|
|---|
| 3816 | to[outc++] = *from++;
|
|---|
| 3817 | if ( from!=fromend )
|
|---|
| 3818 | to[outc++] = ' ';
|
|---|
| 3819 | else
|
|---|
| 3820 | break;
|
|---|
| 3821 | }
|
|---|
| 3822 | if ( outc > 0 && to[outc-1] == ' ' )
|
|---|
| 3823 | outc--;
|
|---|
| 3824 | result.truncate( outc );
|
|---|
| 3825 | return result;
|
|---|
| 3826 | }
|
|---|
| 3827 |
|
|---|
| 3828 |
|
|---|
| 3829 | /*!
|
|---|
| 3830 | Inserts \a s into the string at position \a index.
|
|---|
| 3831 |
|
|---|
| 3832 | If \a index is beyond the end of the string, the string is
|
|---|
| 3833 | extended with spaces to length \a index and \a s is then appended
|
|---|
| 3834 | and returns a reference to the string.
|
|---|
| 3835 |
|
|---|
| 3836 | \code
|
|---|
| 3837 | QString string( "I like fish" );
|
|---|
| 3838 | str = string.insert( 2, "don't " );
|
|---|
| 3839 | // str == "I don't like fish"
|
|---|
| 3840 | \endcode
|
|---|
| 3841 |
|
|---|
| 3842 | \sa remove(), replace()
|
|---|
| 3843 | */
|
|---|
| 3844 |
|
|---|
| 3845 | QString &QString::insert( uint index, const QString &s )
|
|---|
| 3846 | {
|
|---|
| 3847 | // the sub function takes care of &s == this case.
|
|---|
| 3848 | return insert( index, s.unicode(), s.length() );
|
|---|
| 3849 | }
|
|---|
| 3850 |
|
|---|
| 3851 | /*! \fn QString &QString::insert( uint index, const QByteArray &s )
|
|---|
| 3852 | \overload
|
|---|
| 3853 |
|
|---|
| 3854 | Inserts \a s into the string at position \a index and returns
|
|---|
| 3855 | a reference to the string.
|
|---|
| 3856 | */
|
|---|
| 3857 |
|
|---|
| 3858 | /*! \fn QString &QString::insert( uint index, const char *s )
|
|---|
| 3859 | \overload
|
|---|
| 3860 |
|
|---|
| 3861 | Inserts \a s into the string at position \a index and returns
|
|---|
| 3862 | a reference to the string.
|
|---|
| 3863 | */
|
|---|
| 3864 |
|
|---|
| 3865 | #ifndef QT_NO_CAST_ASCII
|
|---|
| 3866 | QString &QString::insertHelper( uint index, const char *s, uint len )
|
|---|
| 3867 | {
|
|---|
| 3868 | if ( s ) {
|
|---|
| 3869 | #ifndef QT_NO_TEXTCODEC
|
|---|
| 3870 | if ( QTextCodec::codecForCStrings() )
|
|---|
| 3871 | return insert( index, fromAscii( s, len ) );
|
|---|
| 3872 | #endif
|
|---|
| 3873 | if ( len == UINT_MAX )
|
|---|
| 3874 | len = strlen( s );
|
|---|
| 3875 | if ( len == 0 )
|
|---|
| 3876 | return *this;
|
|---|
| 3877 |
|
|---|
| 3878 | uint olen = length();
|
|---|
| 3879 | int nlen = olen + len;
|
|---|
| 3880 |
|
|---|
| 3881 | if ( index >= olen ) { // insert after end of string
|
|---|
| 3882 | grow( len + index );
|
|---|
| 3883 | int n = index - olen;
|
|---|
| 3884 | QChar* uc = d->unicode + olen;
|
|---|
| 3885 | while ( n-- )
|
|---|
| 3886 | *uc++ = ' ';
|
|---|
| 3887 |
|
|---|
| 3888 | uc = d->unicode + index;
|
|---|
| 3889 | while ( len-- )
|
|---|
| 3890 | *uc++ = *s++;
|
|---|
| 3891 | } else { // normal insert
|
|---|
| 3892 | grow( nlen );
|
|---|
| 3893 | memmove( d->unicode + index + len, unicode() + index,
|
|---|
| 3894 | sizeof(QChar) * (olen - index) );
|
|---|
| 3895 |
|
|---|
| 3896 | QChar* uc = d->unicode + index;
|
|---|
| 3897 | while ( len-- )
|
|---|
| 3898 | *uc++ = *s++;
|
|---|
| 3899 | }
|
|---|
| 3900 | }
|
|---|
| 3901 | return *this;
|
|---|
| 3902 | }
|
|---|
| 3903 | #endif
|
|---|
| 3904 |
|
|---|
| 3905 | /*!
|
|---|
| 3906 | \overload
|
|---|
| 3907 |
|
|---|
| 3908 | Inserts the first \a len characters in \a s into the string at
|
|---|
| 3909 | position \a index and returns a reference to the string.
|
|---|
| 3910 | */
|
|---|
| 3911 |
|
|---|
| 3912 | QString &QString::insert( uint index, const QChar* s, uint len )
|
|---|
| 3913 | {
|
|---|
| 3914 | if ( len == 0 )
|
|---|
| 3915 | return *this;
|
|---|
| 3916 | uint olen = length();
|
|---|
| 3917 | int nlen = olen + len;
|
|---|
| 3918 |
|
|---|
| 3919 | if ( s >= d->unicode && (uint)(s - d->unicode) < d->maxl ) {
|
|---|
| 3920 | // Part of me - take a copy.
|
|---|
| 3921 | QChar *tmp = QT_ALLOC_QCHAR_VEC( len );
|
|---|
| 3922 | memcpy(tmp,s,len*sizeof(QChar));
|
|---|
| 3923 | insert(index,tmp,len);
|
|---|
| 3924 | QT_DELETE_QCHAR_VEC( tmp );
|
|---|
| 3925 | return *this;
|
|---|
| 3926 | }
|
|---|
| 3927 |
|
|---|
| 3928 | if ( index >= olen ) { // insert after end of string
|
|---|
| 3929 | grow( len + index );
|
|---|
| 3930 | int n = index - olen;
|
|---|
| 3931 | QChar* uc = d->unicode+olen;
|
|---|
| 3932 | while (n--)
|
|---|
| 3933 | *uc++ = ' ';
|
|---|
| 3934 | memcpy( d->unicode+index, s, sizeof(QChar)*len );
|
|---|
| 3935 | } else { // normal insert
|
|---|
| 3936 | grow( nlen );
|
|---|
| 3937 | memmove( d->unicode + index + len, unicode() + index,
|
|---|
| 3938 | sizeof(QChar) * (olen - index) );
|
|---|
| 3939 | memcpy( d->unicode + index, s, sizeof(QChar) * len );
|
|---|
| 3940 | }
|
|---|
| 3941 | return *this;
|
|---|
| 3942 | }
|
|---|
| 3943 |
|
|---|
| 3944 | /*!
|
|---|
| 3945 | \overload
|
|---|
| 3946 |
|
|---|
| 3947 | Insert \a c into the string at position \a index and returns a
|
|---|
| 3948 | reference to the string.
|
|---|
| 3949 |
|
|---|
| 3950 | If \a index is beyond the end of the string, the string is
|
|---|
| 3951 | extended with spaces (ASCII 32) to length \a index and \a c is
|
|---|
| 3952 | then appended.
|
|---|
| 3953 | */
|
|---|
| 3954 |
|
|---|
| 3955 | QString &QString::insert( uint index, QChar c ) // insert char
|
|---|
| 3956 | {
|
|---|
| 3957 | QString s( c );
|
|---|
| 3958 | return insert( index, s );
|
|---|
| 3959 | }
|
|---|
| 3960 |
|
|---|
| 3961 | /*!
|
|---|
| 3962 | \fn QString& QString::insert( uint index, char c )
|
|---|
| 3963 |
|
|---|
| 3964 | \overload
|
|---|
| 3965 |
|
|---|
| 3966 | Insert character \a c at position \a index.
|
|---|
| 3967 | */
|
|---|
| 3968 |
|
|---|
| 3969 | /*!
|
|---|
| 3970 | \fn QString &QString::prepend( const QString &s )
|
|---|
| 3971 |
|
|---|
| 3972 | Inserts \a s at the beginning of the string and returns a
|
|---|
| 3973 | reference to the string.
|
|---|
| 3974 |
|
|---|
| 3975 | Equivalent to insert(0, \a s).
|
|---|
| 3976 |
|
|---|
| 3977 | \code
|
|---|
| 3978 | QString string = "42";
|
|---|
| 3979 | string.prepend( "The answer is " );
|
|---|
| 3980 | // string == "The answer is 42"
|
|---|
| 3981 | \endcode
|
|---|
| 3982 |
|
|---|
| 3983 | \sa insert()
|
|---|
| 3984 | */
|
|---|
| 3985 |
|
|---|
| 3986 | /*!
|
|---|
| 3987 | \fn QString& QString::prepend( char ch )
|
|---|
| 3988 |
|
|---|
| 3989 | \overload
|
|---|
| 3990 |
|
|---|
| 3991 | Inserts \a ch at the beginning of the string and returns a
|
|---|
| 3992 | reference to the string.
|
|---|
| 3993 |
|
|---|
| 3994 | Equivalent to insert(0, \a ch).
|
|---|
| 3995 |
|
|---|
| 3996 | \sa insert()
|
|---|
| 3997 | */
|
|---|
| 3998 |
|
|---|
| 3999 | /*!
|
|---|
| 4000 | \fn QString& QString::prepend( QChar ch )
|
|---|
| 4001 |
|
|---|
| 4002 | \overload
|
|---|
| 4003 |
|
|---|
| 4004 | Inserts \a ch at the beginning of the string and returns a
|
|---|
| 4005 | reference to the string.
|
|---|
| 4006 |
|
|---|
| 4007 | Equivalent to insert(0, \a ch).
|
|---|
| 4008 |
|
|---|
| 4009 | \sa insert()
|
|---|
| 4010 | */
|
|---|
| 4011 |
|
|---|
| 4012 | /*! \fn QString& QString::prepend( const QByteArray &s )
|
|---|
| 4013 | \overload
|
|---|
| 4014 |
|
|---|
| 4015 | Inserts \a s at the beginning of the string and returns a reference to the string.
|
|---|
| 4016 |
|
|---|
| 4017 | Equivalent to insert(0, \a s).
|
|---|
| 4018 |
|
|---|
| 4019 | \sa insert()
|
|---|
| 4020 | */
|
|---|
| 4021 |
|
|---|
| 4022 | /*! \fn QString& QString::prepend( const std::string &s )
|
|---|
| 4023 | \overload
|
|---|
| 4024 |
|
|---|
| 4025 | Inserts \a s at the beginning of the string and returns a reference to the string.
|
|---|
| 4026 |
|
|---|
| 4027 | Equivalent to insert(0, \a s).
|
|---|
| 4028 |
|
|---|
| 4029 | \sa insert()
|
|---|
| 4030 | */
|
|---|
| 4031 |
|
|---|
| 4032 | /*!
|
|---|
| 4033 | \overload
|
|---|
| 4034 |
|
|---|
| 4035 | Inserts \a s at the beginning of the string and returns a reference to the string.
|
|---|
| 4036 |
|
|---|
| 4037 | Equivalent to insert(0, \a s).
|
|---|
| 4038 |
|
|---|
| 4039 | \sa insert()
|
|---|
| 4040 | */
|
|---|
| 4041 | QString &QString::prepend( const char *s )
|
|---|
| 4042 | {
|
|---|
| 4043 | return insert( 0, QString(s) );
|
|---|
| 4044 | }
|
|---|
| 4045 |
|
|---|
| 4046 | /*!
|
|---|
| 4047 | Removes \a len characters from the string starting at position \a
|
|---|
| 4048 | index, and returns a reference to the string.
|
|---|
| 4049 |
|
|---|
| 4050 | If \a index is beyond the length of the string, nothing happens.
|
|---|
| 4051 | If \a index is within the string, but \a index + \a len is beyond
|
|---|
| 4052 | the end of the string, the string is truncated at position \a
|
|---|
| 4053 | index.
|
|---|
| 4054 |
|
|---|
| 4055 | \code
|
|---|
| 4056 | QString string( "Montreal" );
|
|---|
| 4057 | string.remove( 1, 4 ); // string == "Meal"
|
|---|
| 4058 | \endcode
|
|---|
| 4059 |
|
|---|
| 4060 | \sa insert(), replace()
|
|---|
| 4061 | */
|
|---|
| 4062 |
|
|---|
| 4063 | QString &QString::remove( uint index, uint len )
|
|---|
| 4064 | {
|
|---|
| 4065 | uint olen = length();
|
|---|
| 4066 | if ( index >= olen ) {
|
|---|
| 4067 | // range problems
|
|---|
| 4068 | } else if ( index + len >= olen ) { // index ok
|
|---|
| 4069 | setLength( index );
|
|---|
| 4070 | } else if ( len != 0 ) {
|
|---|
| 4071 | real_detach();
|
|---|
| 4072 | memmove( d->unicode+index, d->unicode+index+len,
|
|---|
| 4073 | sizeof(QChar)*(olen-index-len) );
|
|---|
| 4074 | setLength( olen-len );
|
|---|
| 4075 | }
|
|---|
| 4076 | return *this;
|
|---|
| 4077 | }
|
|---|
| 4078 |
|
|---|
| 4079 | /*! \overload
|
|---|
| 4080 |
|
|---|
| 4081 | Removes every occurrence of the character \a c in the string.
|
|---|
| 4082 | Returns a reference to the string.
|
|---|
| 4083 |
|
|---|
| 4084 | This is the same as replace(\a c, "").
|
|---|
| 4085 | */
|
|---|
| 4086 | QString &QString::remove( QChar c )
|
|---|
| 4087 | {
|
|---|
| 4088 | int i = 0;
|
|---|
| 4089 | while ( i < (int) length() ) {
|
|---|
| 4090 | if ( constref(i) == c ) {
|
|---|
| 4091 | remove( i, 1 );
|
|---|
| 4092 | } else {
|
|---|
| 4093 | i++;
|
|---|
| 4094 | }
|
|---|
| 4095 | }
|
|---|
| 4096 | return *this;
|
|---|
| 4097 | }
|
|---|
| 4098 |
|
|---|
| 4099 | /*! \overload
|
|---|
| 4100 |
|
|---|
| 4101 | \fn QString &QString::remove( char c )
|
|---|
| 4102 |
|
|---|
| 4103 | Removes every occurrence of the character \a c in the string.
|
|---|
| 4104 | Returns a reference to the string.
|
|---|
| 4105 |
|
|---|
| 4106 | This is the same as replace(\a c, "").
|
|---|
| 4107 | */
|
|---|
| 4108 |
|
|---|
| 4109 | /*! \overload
|
|---|
| 4110 |
|
|---|
| 4111 | Removes every occurrence of \a str in the string. Returns a
|
|---|
| 4112 | reference to the string.
|
|---|
| 4113 |
|
|---|
| 4114 | If \a cs is TRUE (the default), the search is case sensitive;
|
|---|
| 4115 | otherwise the search is case insensitive.
|
|---|
| 4116 |
|
|---|
| 4117 | This is the same as replace(\a str, "", \a cs).
|
|---|
| 4118 | */
|
|---|
| 4119 | QString &QString::remove( const QString & str, bool cs )
|
|---|
| 4120 | {
|
|---|
| 4121 | if ( str.isEmpty() ) {
|
|---|
| 4122 | if ( isNull() )
|
|---|
| 4123 | real_detach();
|
|---|
| 4124 | } else {
|
|---|
| 4125 | int index = 0;
|
|---|
| 4126 | while ( (index = find(str, index, cs)) != -1 )
|
|---|
| 4127 | remove( index, str.length() );
|
|---|
| 4128 | }
|
|---|
| 4129 | return *this;
|
|---|
| 4130 | }
|
|---|
| 4131 |
|
|---|
| 4132 | QString &QString::remove( const QString & str )
|
|---|
| 4133 | {
|
|---|
| 4134 | return remove( str, TRUE );
|
|---|
| 4135 | }
|
|---|
| 4136 |
|
|---|
| 4137 | /*! \overload
|
|---|
| 4138 |
|
|---|
| 4139 | Replaces every occurrence of \a c1 with the char \a c2. Returns a
|
|---|
| 4140 | reference to the string.
|
|---|
| 4141 | */
|
|---|
| 4142 | QString &QString::replace( QChar c1, QChar c2 )
|
|---|
| 4143 | {
|
|---|
| 4144 | if ( isEmpty() )
|
|---|
| 4145 | return *this;
|
|---|
| 4146 |
|
|---|
| 4147 | real_detach();
|
|---|
| 4148 | uint i = 0;
|
|---|
| 4149 | while ( i < d->len ) {
|
|---|
| 4150 | if ( d->unicode[i] == c1 )
|
|---|
| 4151 | d->unicode[i] = c2;
|
|---|
| 4152 | i++;
|
|---|
| 4153 | }
|
|---|
| 4154 | return *this;
|
|---|
| 4155 | }
|
|---|
| 4156 |
|
|---|
| 4157 | #ifndef QT_NO_REGEXP_CAPTURE
|
|---|
| 4158 |
|
|---|
| 4159 | /*! \overload
|
|---|
| 4160 |
|
|---|
| 4161 | Removes every occurrence of the regular expression \a rx in the
|
|---|
| 4162 | string. Returns a reference to the string.
|
|---|
| 4163 |
|
|---|
| 4164 | This is the same as replace(\a rx, "").
|
|---|
| 4165 | */
|
|---|
| 4166 |
|
|---|
| 4167 | QString &QString::remove( const QRegExp & rx )
|
|---|
| 4168 | {
|
|---|
| 4169 | return replace( rx, QString::null );
|
|---|
| 4170 | }
|
|---|
| 4171 |
|
|---|
| 4172 | #endif
|
|---|
| 4173 |
|
|---|
| 4174 | /*!
|
|---|
| 4175 | \overload
|
|---|
| 4176 |
|
|---|
| 4177 | Removes every occurrence of \a str in the string. Returns a
|
|---|
| 4178 | reference to the string.
|
|---|
| 4179 | */
|
|---|
| 4180 | QString &QString::remove( const char *str )
|
|---|
| 4181 | {
|
|---|
| 4182 | return remove( QString::fromAscii(str), TRUE );
|
|---|
| 4183 | }
|
|---|
| 4184 |
|
|---|
| 4185 | /*!
|
|---|
| 4186 | Replaces \a len characters from the string with \a s, starting at
|
|---|
| 4187 | position \a index, and returns a reference to the string.
|
|---|
| 4188 |
|
|---|
| 4189 | If \a index is beyond the length of the string, nothing is deleted
|
|---|
| 4190 | and \a s is appended at the end of the string. If \a index is
|
|---|
| 4191 | valid, but \a index + \a len is beyond the end of the string,
|
|---|
| 4192 | the string is truncated at position \a index, then \a s is
|
|---|
| 4193 | appended at the end.
|
|---|
| 4194 |
|
|---|
| 4195 | \code
|
|---|
| 4196 | QString string( "Say yes!" );
|
|---|
| 4197 | string = string.replace( 4, 3, "NO" );
|
|---|
| 4198 | // string == "Say NO!"
|
|---|
| 4199 | \endcode
|
|---|
| 4200 |
|
|---|
| 4201 | \sa insert(), remove()
|
|---|
| 4202 | */
|
|---|
| 4203 |
|
|---|
| 4204 | QString &QString::replace( uint index, uint len, const QString &s )
|
|---|
| 4205 | {
|
|---|
| 4206 | return replace( index, len, s.unicode(), s.length() );
|
|---|
| 4207 | }
|
|---|
| 4208 |
|
|---|
| 4209 | /*! \overload
|
|---|
| 4210 |
|
|---|
| 4211 | This is the same as replace(\a index, \a len, QString(\a c)).
|
|---|
| 4212 | */
|
|---|
| 4213 | QString &QString::replace( uint index, uint len, QChar c )
|
|---|
| 4214 | {
|
|---|
| 4215 | return replace( index, len, &c, 1 );
|
|---|
| 4216 | }
|
|---|
| 4217 |
|
|---|
| 4218 | /*! \overload
|
|---|
| 4219 | \fn QString &QString::replace( uint index, uint len, char c )
|
|---|
| 4220 |
|
|---|
| 4221 | This is the same as replace(\a index, \a len, QChar(\a c)).
|
|---|
| 4222 | */
|
|---|
| 4223 |
|
|---|
| 4224 | /*!
|
|---|
| 4225 | \overload
|
|---|
| 4226 |
|
|---|
| 4227 | Replaces \a len characters with \a slen characters of QChar data
|
|---|
| 4228 | from \a s, starting at position \a index, and returns a reference
|
|---|
| 4229 | to the string.
|
|---|
| 4230 |
|
|---|
| 4231 | \sa insert(), remove()
|
|---|
| 4232 | */
|
|---|
| 4233 |
|
|---|
| 4234 | QString &QString::replace( uint index, uint len, const QChar* s, uint slen )
|
|---|
| 4235 | {
|
|---|
| 4236 | real_detach();
|
|---|
| 4237 | if ( len == slen && index + len <= length() ) {
|
|---|
| 4238 | // Optimized common case: replace without size change
|
|---|
| 4239 | memcpy( d->unicode+index, s, len * sizeof(QChar) );
|
|---|
| 4240 | } else if ( s >= d->unicode && (uint)(s - d->unicode) < d->maxl ) {
|
|---|
| 4241 | // Part of me - take a copy.
|
|---|
| 4242 | QChar *tmp = QT_ALLOC_QCHAR_VEC( slen );
|
|---|
| 4243 | memcpy( tmp, s, slen * sizeof(QChar) );
|
|---|
| 4244 | replace( index, len, tmp, slen );
|
|---|
| 4245 | QT_DELETE_QCHAR_VEC( tmp );
|
|---|
| 4246 | } else {
|
|---|
| 4247 | remove( index, len );
|
|---|
| 4248 | insert( index, s, slen );
|
|---|
| 4249 | }
|
|---|
| 4250 | return *this;
|
|---|
| 4251 | }
|
|---|
| 4252 |
|
|---|
| 4253 | /*! \overload
|
|---|
| 4254 |
|
|---|
| 4255 | Replaces every occurrence of the character \a c in the string
|
|---|
| 4256 | with \a after. Returns a reference to the string.
|
|---|
| 4257 |
|
|---|
| 4258 | If \a cs is TRUE (the default), the search is case sensitive;
|
|---|
| 4259 | otherwise the search is case insensitive.
|
|---|
| 4260 |
|
|---|
| 4261 | Example:
|
|---|
| 4262 | \code
|
|---|
| 4263 | QString s = "a,b,c";
|
|---|
| 4264 | s.replace( QChar(','), " or " );
|
|---|
| 4265 | // s == "a or b or c"
|
|---|
| 4266 | \endcode
|
|---|
| 4267 | */
|
|---|
| 4268 | QString &QString::replace( QChar c, const QString & after, bool cs )
|
|---|
| 4269 | {
|
|---|
| 4270 | return replace( QString( c ), after, cs );
|
|---|
| 4271 | }
|
|---|
| 4272 |
|
|---|
| 4273 | QString &QString::replace( QChar c, const QString & after )
|
|---|
| 4274 | {
|
|---|
| 4275 | return replace( QString( c ), after, TRUE );
|
|---|
| 4276 | }
|
|---|
| 4277 |
|
|---|
| 4278 | /*! \overload
|
|---|
| 4279 | \fn QString &QString::replace( char c, const QString & after, bool cs )
|
|---|
| 4280 |
|
|---|
| 4281 | Replaces every occurrence of the character \a c in the string
|
|---|
| 4282 | with \a after. Returns a reference to the string.
|
|---|
| 4283 |
|
|---|
| 4284 | If \a cs is TRUE (the default), the search is case sensitive;
|
|---|
| 4285 | otherwise the search is case insensitive.
|
|---|
| 4286 | */
|
|---|
| 4287 |
|
|---|
| 4288 | /*! \overload
|
|---|
| 4289 |
|
|---|
| 4290 | Replaces every occurrence of the string \a before in the string
|
|---|
| 4291 | with the string \a after. Returns a reference to the string.
|
|---|
| 4292 |
|
|---|
| 4293 | If \a cs is TRUE (the default), the search is case sensitive;
|
|---|
| 4294 | otherwise the search is case insensitive.
|
|---|
| 4295 |
|
|---|
| 4296 | Example:
|
|---|
| 4297 | \code
|
|---|
| 4298 | QString s = "Greek is Greek";
|
|---|
| 4299 | s.replace( "Greek", "English" );
|
|---|
| 4300 | // s == "English is English"
|
|---|
| 4301 | \endcode
|
|---|
| 4302 | */
|
|---|
| 4303 | QString &QString::replace( const QString & before, const QString & after,
|
|---|
| 4304 | bool cs )
|
|---|
| 4305 | {
|
|---|
| 4306 | if ( isEmpty() ) {
|
|---|
| 4307 | if ( !before.isEmpty() )
|
|---|
| 4308 | return *this;
|
|---|
| 4309 | } else {
|
|---|
| 4310 | if ( cs && before == after )
|
|---|
| 4311 | return *this;
|
|---|
| 4312 | }
|
|---|
| 4313 |
|
|---|
| 4314 | real_detach();
|
|---|
| 4315 |
|
|---|
| 4316 | int index = 0;
|
|---|
| 4317 | uint skiptable[256];
|
|---|
| 4318 | bm_init_skiptable( before, skiptable, cs );
|
|---|
| 4319 | const int bl = before.length();
|
|---|
| 4320 | const int al = after.length();
|
|---|
| 4321 |
|
|---|
| 4322 | if ( bl == al ) {
|
|---|
| 4323 | if ( bl ) {
|
|---|
| 4324 | const QChar *auc = after.unicode();
|
|---|
| 4325 | while ( (index = bm_find(*this, index, before, skiptable, cs) ) != -1 ) {
|
|---|
| 4326 | memcpy( d->unicode + index, auc, al * sizeof(QChar) );
|
|---|
| 4327 | index += bl;
|
|---|
| 4328 | }
|
|---|
| 4329 | }
|
|---|
| 4330 | } else if ( al < bl ) {
|
|---|
| 4331 | const QChar *auc = after.unicode();
|
|---|
| 4332 | uint to = 0;
|
|---|
| 4333 | uint movestart = 0;
|
|---|
| 4334 | uint num = 0;
|
|---|
| 4335 | while ( (index = bm_find(*this, index, before, skiptable, cs)) != -1 ) {
|
|---|
| 4336 | if ( num ) {
|
|---|
| 4337 | int msize = index - movestart;
|
|---|
| 4338 | if ( msize > 0 ) {
|
|---|
| 4339 | memmove( d->unicode + to, d->unicode + movestart, msize*sizeof(QChar) );
|
|---|
| 4340 | to += msize;
|
|---|
| 4341 | }
|
|---|
| 4342 | } else {
|
|---|
| 4343 | to = index;
|
|---|
| 4344 | }
|
|---|
| 4345 | if ( al ) {
|
|---|
| 4346 | memcpy( d->unicode+to, auc, al*sizeof(QChar) );
|
|---|
| 4347 | to += al;
|
|---|
| 4348 | }
|
|---|
| 4349 | index += bl;
|
|---|
| 4350 | movestart = index;
|
|---|
| 4351 | num++;
|
|---|
| 4352 | }
|
|---|
| 4353 | if ( num ) {
|
|---|
| 4354 | int msize = d->len - movestart;
|
|---|
| 4355 | if ( msize > 0 )
|
|---|
| 4356 | memmove( d->unicode + to, d->unicode + movestart, msize*sizeof(QChar) );
|
|---|
| 4357 | setLength( d->len - num*(bl-al) );
|
|---|
| 4358 | }
|
|---|
| 4359 | } else {
|
|---|
| 4360 | // the most complex case. We don't want to loose performance by doing repeated
|
|---|
| 4361 | // copies and reallocs of the string.
|
|---|
| 4362 | while ( index != -1 ) {
|
|---|
| 4363 | uint indices[4096];
|
|---|
| 4364 | uint pos = 0;
|
|---|
| 4365 | while ( pos < 4095 ) {
|
|---|
| 4366 | index = bm_find( *this, index, before, skiptable, cs );
|
|---|
| 4367 | if ( index == -1 )
|
|---|
| 4368 | break;
|
|---|
| 4369 | indices[pos++] = index;
|
|---|
| 4370 | index += bl;
|
|---|
| 4371 | // avoid infinite loop
|
|---|
| 4372 | if ( !bl )
|
|---|
| 4373 | index++;
|
|---|
| 4374 | }
|
|---|
| 4375 | if ( !pos )
|
|---|
| 4376 | break;
|
|---|
| 4377 |
|
|---|
| 4378 | // we have a table of replacement positions, use them for fast replacing
|
|---|
| 4379 | int adjust = pos*(al-bl);
|
|---|
| 4380 | // index has to be adjusted in case we get back into the loop above.
|
|---|
| 4381 | if ( index != -1 )
|
|---|
| 4382 | index += adjust;
|
|---|
| 4383 | uint newlen = d->len + adjust;
|
|---|
| 4384 | int moveend = d->len;
|
|---|
| 4385 | if ( newlen > d->len )
|
|---|
| 4386 | setLength( newlen );
|
|---|
| 4387 |
|
|---|
| 4388 | while ( pos ) {
|
|---|
| 4389 | pos--;
|
|---|
| 4390 | int movestart = indices[pos] + bl;
|
|---|
| 4391 | int insertstart = indices[pos] + pos*(al-bl);
|
|---|
| 4392 | int moveto = insertstart + al;
|
|---|
| 4393 | memmove( d->unicode + moveto, d->unicode + movestart, (moveend - movestart)*sizeof(QChar) );
|
|---|
| 4394 | memcpy( d->unicode + insertstart, after.unicode(), al*sizeof(QChar) );
|
|---|
| 4395 | moveend = movestart-bl;
|
|---|
| 4396 | }
|
|---|
| 4397 | }
|
|---|
| 4398 | }
|
|---|
| 4399 | return *this;
|
|---|
| 4400 | }
|
|---|
| 4401 |
|
|---|
| 4402 | QString &QString::replace( const QString & before, const QString & after )
|
|---|
| 4403 | {
|
|---|
| 4404 | return replace( before, after, TRUE );
|
|---|
| 4405 | }
|
|---|
| 4406 |
|
|---|
| 4407 | #ifndef QT_NO_REGEXP_CAPTURE
|
|---|
| 4408 | /*! \overload
|
|---|
| 4409 |
|
|---|
| 4410 | Replaces every occurrence of the regexp \a rx in the string with
|
|---|
| 4411 | \a after. Returns a reference to the string. For example:
|
|---|
| 4412 | \code
|
|---|
| 4413 | QString s = "banana";
|
|---|
| 4414 | s.replace( QRegExp("an"), "" );
|
|---|
| 4415 | // s == "ba"
|
|---|
| 4416 | \endcode
|
|---|
| 4417 |
|
|---|
| 4418 | For regexps containing \link qregexp.html#capturing-text capturing
|
|---|
| 4419 | parentheses \endlink, occurrences of <b>\\1</b>, <b>\\2</b>, ...,
|
|---|
| 4420 | in \a after are replaced with \a{rx}.cap(1), cap(2), ...
|
|---|
| 4421 |
|
|---|
| 4422 | \code
|
|---|
| 4423 | QString t = "A <i>bon mot</i>.";
|
|---|
| 4424 | t.replace( QRegExp("<i>([^<]*)</i>"), "\\emph{\\1}" );
|
|---|
| 4425 | // t == "A \\emph{bon mot}."
|
|---|
| 4426 | \endcode
|
|---|
| 4427 |
|
|---|
| 4428 | \sa find(), findRev(), QRegExp::cap()
|
|---|
| 4429 | */
|
|---|
| 4430 |
|
|---|
| 4431 | QString &QString::replace( const QRegExp &rx, const QString &after )
|
|---|
| 4432 | {
|
|---|
| 4433 | QRegExp rx2 = rx;
|
|---|
| 4434 |
|
|---|
| 4435 | if ( isEmpty() && rx2.search(*this) == -1 )
|
|---|
| 4436 | return *this;
|
|---|
| 4437 |
|
|---|
| 4438 | real_detach();
|
|---|
| 4439 |
|
|---|
| 4440 | int index = 0;
|
|---|
| 4441 | int numCaptures = rx2.numCaptures();
|
|---|
| 4442 | int al = after.length();
|
|---|
| 4443 | QRegExp::CaretMode caretMode = QRegExp::CaretAtZero;
|
|---|
| 4444 |
|
|---|
| 4445 | if ( numCaptures > 0 ) {
|
|---|
| 4446 | if ( numCaptures > 9 )
|
|---|
| 4447 | numCaptures = 9;
|
|---|
| 4448 |
|
|---|
| 4449 | const QChar *uc = after.unicode();
|
|---|
| 4450 | int numBackRefs = 0;
|
|---|
| 4451 |
|
|---|
| 4452 | for ( int i = 0; i < al - 1; i++ ) {
|
|---|
| 4453 | if ( uc[i] == '\\' ) {
|
|---|
| 4454 | int no = uc[i + 1].digitValue();
|
|---|
| 4455 | if ( no > 0 && no <= numCaptures )
|
|---|
| 4456 | numBackRefs++;
|
|---|
| 4457 | }
|
|---|
| 4458 | }
|
|---|
| 4459 |
|
|---|
| 4460 | /*
|
|---|
| 4461 | This is the harder case where we have back-references.
|
|---|
| 4462 | We don't try to optimize it.
|
|---|
| 4463 | */
|
|---|
| 4464 | if ( numBackRefs > 0 ) {
|
|---|
| 4465 | int *capturePositions = new int[numBackRefs];
|
|---|
| 4466 | int *captureNumbers = new int[numBackRefs];
|
|---|
| 4467 | int j = 0;
|
|---|
| 4468 |
|
|---|
| 4469 | for ( int i = 0; i < al - 1; i++ ) {
|
|---|
| 4470 | if ( uc[i] == '\\' ) {
|
|---|
| 4471 | int no = uc[i + 1].digitValue();
|
|---|
| 4472 | if ( no > 0 && no <= numCaptures ) {
|
|---|
| 4473 | capturePositions[j] = i;
|
|---|
| 4474 | captureNumbers[j] = no;
|
|---|
| 4475 | j++;
|
|---|
| 4476 | }
|
|---|
| 4477 | }
|
|---|
| 4478 | }
|
|---|
| 4479 |
|
|---|
| 4480 | while ( index <= (int)length() ) {
|
|---|
| 4481 | index = rx2.search( *this, index, caretMode );
|
|---|
| 4482 | if ( index == -1 )
|
|---|
| 4483 | break;
|
|---|
| 4484 |
|
|---|
| 4485 | QString after2 = after;
|
|---|
| 4486 | for ( j = numBackRefs - 1; j >= 0; j-- )
|
|---|
| 4487 | after2.replace( capturePositions[j], 2,
|
|---|
| 4488 | rx2.cap(captureNumbers[j]) );
|
|---|
| 4489 |
|
|---|
| 4490 | replace( index, rx2.matchedLength(), after2 );
|
|---|
| 4491 | index += after2.length();
|
|---|
| 4492 |
|
|---|
| 4493 | if ( rx2.matchedLength() == 0 ) {
|
|---|
| 4494 | // avoid infinite loop on 0-length matches (e.g., [a-z]*)
|
|---|
| 4495 | index++;
|
|---|
| 4496 | }
|
|---|
| 4497 | caretMode = QRegExp::CaretWontMatch;
|
|---|
| 4498 | }
|
|---|
| 4499 | delete[] capturePositions;
|
|---|
| 4500 | delete[] captureNumbers;
|
|---|
| 4501 | return *this;
|
|---|
| 4502 | }
|
|---|
| 4503 | }
|
|---|
| 4504 |
|
|---|
| 4505 | /*
|
|---|
| 4506 | This is the simple and optimized case where we don't have
|
|---|
| 4507 | back-references.
|
|---|
| 4508 | */
|
|---|
| 4509 | while ( index != -1 ) {
|
|---|
| 4510 | struct {
|
|---|
| 4511 | int pos;
|
|---|
| 4512 | int length;
|
|---|
| 4513 | } replacements[2048];
|
|---|
| 4514 |
|
|---|
| 4515 | uint pos = 0;
|
|---|
| 4516 | int adjust = 0;
|
|---|
| 4517 | while ( pos < 2047 ) {
|
|---|
| 4518 | index = rx2.search( *this, index, caretMode );
|
|---|
| 4519 | if ( index == -1 )
|
|---|
| 4520 | break;
|
|---|
| 4521 | int ml = rx2.matchedLength();
|
|---|
| 4522 | replacements[pos].pos = index;
|
|---|
| 4523 | replacements[pos++].length = ml;
|
|---|
| 4524 | index += ml;
|
|---|
| 4525 | adjust += al - ml;
|
|---|
| 4526 | // avoid infinite loop
|
|---|
| 4527 | if ( !ml )
|
|---|
| 4528 | index++;
|
|---|
| 4529 | }
|
|---|
| 4530 | if ( !pos )
|
|---|
| 4531 | break;
|
|---|
| 4532 | replacements[pos].pos = d->len;
|
|---|
| 4533 | uint newlen = d->len + adjust;
|
|---|
| 4534 |
|
|---|
| 4535 | // to continue searching at the right position after we did
|
|---|
| 4536 | // the first round of replacements
|
|---|
| 4537 | if ( index != -1 )
|
|---|
| 4538 | index += adjust;
|
|---|
| 4539 | QChar *newuc = QT_ALLOC_QCHAR_VEC( newlen + 1 );
|
|---|
| 4540 | QChar *uc = newuc;
|
|---|
| 4541 | int copystart = 0;
|
|---|
| 4542 | uint i = 0;
|
|---|
| 4543 | while ( i < pos ) {
|
|---|
| 4544 | int copyend = replacements[i].pos;
|
|---|
| 4545 | int size = copyend - copystart;
|
|---|
| 4546 | memcpy( uc, d->unicode + copystart, size * sizeof(QChar) );
|
|---|
| 4547 | uc += size;
|
|---|
| 4548 | memcpy( uc, after.unicode(), al * sizeof(QChar) );
|
|---|
| 4549 | uc += al;
|
|---|
| 4550 | copystart = copyend + replacements[i].length;
|
|---|
| 4551 | i++;
|
|---|
| 4552 | }
|
|---|
| 4553 | memcpy( uc, d->unicode + copystart,
|
|---|
| 4554 | (d->len - copystart) * sizeof(QChar) );
|
|---|
| 4555 | QT_DELETE_QCHAR_VEC( d->unicode );
|
|---|
| 4556 | d->unicode = newuc;
|
|---|
| 4557 | d->len = newlen;
|
|---|
| 4558 | d->maxl = newlen + 1;
|
|---|
| 4559 | d->setDirty();
|
|---|
| 4560 | caretMode = QRegExp::CaretWontMatch;
|
|---|
| 4561 | }
|
|---|
| 4562 | return *this;
|
|---|
| 4563 | }
|
|---|
| 4564 | #endif
|
|---|
| 4565 |
|
|---|
| 4566 | #ifndef QT_NO_REGEXP
|
|---|
| 4567 | /*!
|
|---|
| 4568 | Finds the first match of the regular expression \a rx, starting
|
|---|
| 4569 | from position \a index. If \a index is -1, the search starts at
|
|---|
| 4570 | the last character; if -2, at the next to last character and so
|
|---|
| 4571 | on. (See findRev() for searching backwards.)
|
|---|
| 4572 |
|
|---|
| 4573 | Returns the position of the first match of \a rx or -1 if no match
|
|---|
| 4574 | was found.
|
|---|
| 4575 |
|
|---|
| 4576 | \code
|
|---|
| 4577 | QString string( "bananas" );
|
|---|
| 4578 | int i = string.find( QRegExp("an"), 0 ); // i == 1
|
|---|
| 4579 | \endcode
|
|---|
| 4580 |
|
|---|
| 4581 | \sa findRev() replace() contains()
|
|---|
| 4582 | */
|
|---|
| 4583 |
|
|---|
| 4584 | int QString::find( const QRegExp &rx, int index ) const
|
|---|
| 4585 | {
|
|---|
| 4586 | return rx.search( *this, index );
|
|---|
| 4587 | }
|
|---|
| 4588 |
|
|---|
| 4589 | /*!
|
|---|
| 4590 | \overload
|
|---|
| 4591 |
|
|---|
| 4592 | Finds the first match of the regexp \a rx, starting at position \a
|
|---|
| 4593 | index and searching backwards. If the index is -1, the search
|
|---|
| 4594 | starts at the last character, if it is -2, at the next to last
|
|---|
| 4595 | character and so on. (See findRev() for searching backwards.)
|
|---|
| 4596 |
|
|---|
| 4597 | Returns the position of the match or -1 if no match was found.
|
|---|
| 4598 |
|
|---|
| 4599 | \code
|
|---|
| 4600 | QString string( "bananas" );
|
|---|
| 4601 | int i = string.findRev( QRegExp("an") ); // i == 3
|
|---|
| 4602 | \endcode
|
|---|
| 4603 |
|
|---|
| 4604 | \sa find()
|
|---|
| 4605 | */
|
|---|
| 4606 |
|
|---|
| 4607 | int QString::findRev( const QRegExp &rx, int index ) const
|
|---|
| 4608 | {
|
|---|
| 4609 | return rx.searchRev( *this, index );
|
|---|
| 4610 | }
|
|---|
| 4611 |
|
|---|
| 4612 | /*!
|
|---|
| 4613 | \overload
|
|---|
| 4614 |
|
|---|
| 4615 | Returns the number of times the regexp, \a rx, matches in the
|
|---|
| 4616 | string.
|
|---|
| 4617 |
|
|---|
| 4618 | This function counts overlapping matches, so in the example below,
|
|---|
| 4619 | there are four instances of "ana" or "ama".
|
|---|
| 4620 |
|
|---|
| 4621 | \code
|
|---|
| 4622 | QString str = "banana and panama";
|
|---|
| 4623 | QRegExp rxp = QRegExp( "a[nm]a", TRUE, FALSE );
|
|---|
| 4624 | int i = str.contains( rxp ); // i == 4
|
|---|
| 4625 | \endcode
|
|---|
| 4626 |
|
|---|
| 4627 | \sa find() findRev()
|
|---|
| 4628 | */
|
|---|
| 4629 |
|
|---|
| 4630 | int QString::contains( const QRegExp &rx ) const
|
|---|
| 4631 | {
|
|---|
| 4632 | int count = 0;
|
|---|
| 4633 | int index = -1;
|
|---|
| 4634 | int len = length();
|
|---|
| 4635 | while ( index < len - 1 ) { // count overlapping matches
|
|---|
| 4636 | index = rx.search( *this, index + 1 );
|
|---|
| 4637 | if ( index == -1 )
|
|---|
| 4638 | break;
|
|---|
| 4639 | count++;
|
|---|
| 4640 | }
|
|---|
| 4641 | return count;
|
|---|
| 4642 | }
|
|---|
| 4643 |
|
|---|
| 4644 | #endif //QT_NO_REGEXP
|
|---|
| 4645 |
|
|---|
| 4646 | /*!
|
|---|
| 4647 | Returns the string converted to a \c long using base \a
|
|---|
| 4648 | base, which is 10 by default and must be between 2 and 36 or 0. If
|
|---|
| 4649 | \a base is 0, the base is determined automatically using the
|
|---|
| 4650 | following rules: If the string begins with "0x", it is assumed to
|
|---|
| 4651 | be hexadecimal; if it begins with "0", it is assumed to be octal;
|
|---|
| 4652 | otherwise it is assumed to be decimal.
|
|---|
| 4653 |
|
|---|
| 4654 | Returns 0 if the conversion fails.
|
|---|
| 4655 |
|
|---|
| 4656 | If \a ok is not 0: if a conversion error occurs, \a *ok is set to
|
|---|
| 4657 | FALSE; otherwise \a *ok is set to TRUE.
|
|---|
| 4658 |
|
|---|
| 4659 | \sa number()
|
|---|
| 4660 | */
|
|---|
| 4661 |
|
|---|
| 4662 | long QString::toLong( bool *ok, int base ) const
|
|---|
| 4663 | {
|
|---|
| 4664 | Q_LLONG v = toLongLong( ok, base );
|
|---|
| 4665 | if ( v < LONG_MIN || v > LONG_MAX ) {
|
|---|
| 4666 | if ( ok )
|
|---|
| 4667 | *ok = FALSE;
|
|---|
| 4668 | v = 0;
|
|---|
| 4669 | }
|
|---|
| 4670 | return long(v);
|
|---|
| 4671 | }
|
|---|
| 4672 |
|
|---|
| 4673 | /*!
|
|---|
| 4674 | Returns the string converted to a \c {long long} using base \a
|
|---|
| 4675 | base, which is 10 by default and must be between 2 and 36 or 0. If
|
|---|
| 4676 | \a base is 0, the base is determined automatically using the
|
|---|
| 4677 | following rules: If the string begins with "0x", it is assumed to
|
|---|
| 4678 | be hexadecimal; if it begins with "0", it is assumed to be octal;
|
|---|
| 4679 | otherwise it is assumed to be decimal.
|
|---|
| 4680 |
|
|---|
| 4681 | Returns 0 if the conversion fails.
|
|---|
| 4682 |
|
|---|
| 4683 | If \a ok is not 0: if a conversion error occurs, \a *ok is set to
|
|---|
| 4684 | FALSE; otherwise \a *ok is set to TRUE.
|
|---|
| 4685 |
|
|---|
| 4686 | \sa number()
|
|---|
| 4687 | */
|
|---|
| 4688 |
|
|---|
| 4689 | Q_LLONG QString::toLongLong( bool *ok, int base ) const
|
|---|
| 4690 | {
|
|---|
| 4691 | #if defined(QT_CHECK_RANGE)
|
|---|
| 4692 | if ( base != 0 && (base < 2 || base > 36) ) {
|
|---|
| 4693 | qWarning( "QString::toLongLong: Invalid base (%d)", base );
|
|---|
| 4694 | base = 10;
|
|---|
| 4695 | }
|
|---|
| 4696 | #endif
|
|---|
| 4697 |
|
|---|
| 4698 | QLocale locale(QLocale::C);
|
|---|
| 4699 | return locale.d->stringToLongLong(*this, base, ok);
|
|---|
| 4700 | }
|
|---|
| 4701 |
|
|---|
| 4702 | /*!
|
|---|
| 4703 | Returns the string converted to an \c {unsigned long} using base \a
|
|---|
| 4704 | base, which is 10 by default and must be between 2 and 36 or 0. If
|
|---|
| 4705 | \a base is 0, the base is determined automatically using the
|
|---|
| 4706 | following rules: If the string begins with "0x", it is assumed to
|
|---|
| 4707 | be hexadecimal; if it begins with "0", it is assumed to be octal;
|
|---|
| 4708 | otherwise it is assumed to be decimal.
|
|---|
| 4709 |
|
|---|
| 4710 | Returns 0 if the conversion fails.
|
|---|
| 4711 |
|
|---|
| 4712 | If \a ok is not 0: if a conversion error occurs, \a *ok is set to
|
|---|
| 4713 | FALSE; otherwise \a *ok is set to TRUE.
|
|---|
| 4714 |
|
|---|
| 4715 | \sa number()
|
|---|
| 4716 | */
|
|---|
| 4717 |
|
|---|
| 4718 | ulong QString::toULong( bool *ok, int base ) const
|
|---|
| 4719 | {
|
|---|
| 4720 | Q_ULLONG v = toULongLong( ok, base );
|
|---|
| 4721 | if ( v > ULONG_MAX ) {
|
|---|
| 4722 | if ( ok )
|
|---|
| 4723 | *ok = FALSE;
|
|---|
| 4724 | v = 0;
|
|---|
| 4725 | }
|
|---|
| 4726 | return ulong(v);
|
|---|
| 4727 | }
|
|---|
| 4728 |
|
|---|
| 4729 | /*!
|
|---|
| 4730 | Returns the string converted to an \c {unsigned long long} using base \a
|
|---|
| 4731 | base, which is 10 by default and must be between 2 and 36 or 0. If
|
|---|
| 4732 | \a base is 0, the base is determined automatically using the
|
|---|
| 4733 | following rules: If the string begins with "0x", it is assumed to
|
|---|
| 4734 | be hexadecimal; if it begins with "0", it is assumed to be octal;
|
|---|
| 4735 | otherwise it is assumed to be decimal.
|
|---|
| 4736 |
|
|---|
| 4737 | Returns 0 if the conversion fails.
|
|---|
| 4738 |
|
|---|
| 4739 | If \a ok is not 0: if a conversion error occurs, \a *ok is set to
|
|---|
| 4740 | FALSE; otherwise \a *ok is set to TRUE.
|
|---|
| 4741 |
|
|---|
| 4742 | \sa number()
|
|---|
| 4743 | */
|
|---|
| 4744 |
|
|---|
| 4745 | Q_ULLONG QString::toULongLong( bool *ok, int base ) const
|
|---|
| 4746 | {
|
|---|
| 4747 | #if defined(QT_CHECK_RANGE)
|
|---|
| 4748 | if ( base != 0 && (base < 2 || base > 36) ) {
|
|---|
| 4749 | qWarning( "QString::toULongLong: Invalid base %d", base );
|
|---|
| 4750 | base = 10;
|
|---|
| 4751 | }
|
|---|
| 4752 | #endif
|
|---|
| 4753 |
|
|---|
| 4754 | QLocale locale(QLocale::C);
|
|---|
| 4755 | return locale.d->stringToUnsLongLong(*this, base, ok);
|
|---|
| 4756 | }
|
|---|
| 4757 |
|
|---|
| 4758 | /*!
|
|---|
| 4759 | Returns the string converted to a \c short using base \a
|
|---|
| 4760 | base, which is 10 by default and must be between 2 and 36 or 0. If
|
|---|
| 4761 | \a base is 0, the base is determined automatically using the
|
|---|
| 4762 | following rules: If the string begins with "0x", it is assumed to
|
|---|
| 4763 | be hexadecimal; if it begins with "0", it is assumed to be octal;
|
|---|
| 4764 | otherwise it is assumed to be decimal.
|
|---|
| 4765 |
|
|---|
| 4766 | Returns 0 if the conversion fails.
|
|---|
| 4767 |
|
|---|
| 4768 | If \a ok is not 0: if a conversion error occurs, \a *ok is set to
|
|---|
| 4769 | FALSE; otherwise \a *ok is set to TRUE.
|
|---|
| 4770 |
|
|---|
| 4771 | \sa number()
|
|---|
| 4772 | */
|
|---|
| 4773 |
|
|---|
| 4774 |
|
|---|
| 4775 | short QString::toShort( bool *ok, int base ) const
|
|---|
| 4776 | {
|
|---|
| 4777 | Q_LLONG v = toLongLong( ok, base );
|
|---|
| 4778 | if ( v < SHRT_MIN || v > SHRT_MAX ) {
|
|---|
| 4779 | if ( ok )
|
|---|
| 4780 | *ok = FALSE;
|
|---|
| 4781 | v = 0;
|
|---|
| 4782 | }
|
|---|
| 4783 | return (short)v;
|
|---|
| 4784 | }
|
|---|
| 4785 |
|
|---|
| 4786 | /*!
|
|---|
| 4787 | Returns the string converted to an \c {unsigned short} using base \a
|
|---|
| 4788 | base, which is 10 by default and must be between 2 and 36 or 0. If
|
|---|
| 4789 | \a base is 0, the base is determined automatically using the
|
|---|
| 4790 | following rules: If the string begins with "0x", it is assumed to
|
|---|
| 4791 | be hexadecimal; if it begins with "0", it is assumed to be octal;
|
|---|
| 4792 | otherwise it is assumed to be decimal.
|
|---|
| 4793 |
|
|---|
| 4794 | Returns 0 if the conversion fails.
|
|---|
| 4795 |
|
|---|
| 4796 | If \a ok is not 0: if a conversion error occurs, \a *ok is set to
|
|---|
| 4797 | FALSE; otherwise \a *ok is set to TRUE.
|
|---|
| 4798 |
|
|---|
| 4799 | \sa number()
|
|---|
| 4800 | */
|
|---|
| 4801 |
|
|---|
| 4802 | ushort QString::toUShort( bool *ok, int base ) const
|
|---|
| 4803 | {
|
|---|
| 4804 | Q_ULLONG v = toULongLong( ok, base );
|
|---|
| 4805 | if ( v > USHRT_MAX ) {
|
|---|
| 4806 | if ( ok )
|
|---|
| 4807 | *ok = FALSE;
|
|---|
| 4808 | v = 0;
|
|---|
| 4809 | }
|
|---|
| 4810 | return (ushort)v;
|
|---|
| 4811 | }
|
|---|
| 4812 |
|
|---|
| 4813 |
|
|---|
| 4814 | /*!
|
|---|
| 4815 | Returns the string converted to an \c int using base \a
|
|---|
| 4816 | base, which is 10 by default and must be between 2 and 36 or 0. If
|
|---|
| 4817 | \a base is 0, the base is determined automatically using the
|
|---|
| 4818 | following rules: If the string begins with "0x", it is assumed to
|
|---|
| 4819 | be hexadecimal; if it begins with "0", it is assumed to be octal;
|
|---|
| 4820 | otherwise it is assumed to be decimal.
|
|---|
| 4821 |
|
|---|
| 4822 | Returns 0 if the conversion fails.
|
|---|
| 4823 |
|
|---|
| 4824 | If \a ok is not 0: if a conversion error occurs, \a *ok is set to
|
|---|
| 4825 | FALSE; otherwise \a *ok is set to TRUE.
|
|---|
| 4826 |
|
|---|
| 4827 | \code
|
|---|
| 4828 | QString str( "FF" );
|
|---|
| 4829 | bool ok;
|
|---|
| 4830 | int hex = str.toInt( &ok, 16 ); // hex == 255, ok == TRUE
|
|---|
| 4831 | int dec = str.toInt( &ok, 10 ); // dec == 0, ok == FALSE
|
|---|
| 4832 | \endcode
|
|---|
| 4833 |
|
|---|
| 4834 | \sa number()
|
|---|
| 4835 | */
|
|---|
| 4836 |
|
|---|
| 4837 | int QString::toInt( bool *ok, int base ) const
|
|---|
| 4838 | {
|
|---|
| 4839 | Q_LLONG v = toLongLong( ok, base );
|
|---|
| 4840 | if ( v < INT_MIN || v > INT_MAX ) {
|
|---|
| 4841 | if ( ok )
|
|---|
| 4842 | *ok = FALSE;
|
|---|
| 4843 | v = 0;
|
|---|
| 4844 | }
|
|---|
| 4845 | return (int)v;
|
|---|
| 4846 | }
|
|---|
| 4847 |
|
|---|
| 4848 | /*!
|
|---|
| 4849 | Returns the string converted to an \c {unsigned int} using base \a
|
|---|
| 4850 | base, which is 10 by default and must be between 2 and 36 or 0. If
|
|---|
| 4851 | \a base is 0, the base is determined automatically using the
|
|---|
| 4852 | following rules: If the string begins with "0x", it is assumed to
|
|---|
| 4853 | be hexadecimal; if it begins with "0", it is assumed to be octal;
|
|---|
| 4854 | otherwise it is assumed to be decimal.
|
|---|
| 4855 |
|
|---|
| 4856 | Returns 0 if the conversion fails.
|
|---|
| 4857 |
|
|---|
| 4858 | If \a ok is not 0: if a conversion error occurs, \a *ok is set to
|
|---|
| 4859 | FALSE; otherwise \a *ok is set to TRUE.
|
|---|
| 4860 |
|
|---|
| 4861 | \sa number()
|
|---|
| 4862 | */
|
|---|
| 4863 |
|
|---|
| 4864 | uint QString::toUInt( bool *ok, int base ) const
|
|---|
| 4865 | {
|
|---|
| 4866 | Q_ULLONG v = toULongLong( ok, base );
|
|---|
| 4867 | if ( v > UINT_MAX ) {
|
|---|
| 4868 | if ( ok )
|
|---|
| 4869 | *ok = FALSE;
|
|---|
| 4870 | v = 0;
|
|---|
| 4871 | }
|
|---|
| 4872 | return (uint)v;
|
|---|
| 4873 | }
|
|---|
| 4874 |
|
|---|
| 4875 | /*!
|
|---|
| 4876 | Returns the string converted to a \c double value.
|
|---|
| 4877 |
|
|---|
| 4878 | Returns 0.0 if the conversion fails.
|
|---|
| 4879 |
|
|---|
| 4880 | If \a ok is not 0: if a conversion error occurs, \a *ok is set to
|
|---|
| 4881 | FALSE; otherwise \a *ok is set to TRUE.
|
|---|
| 4882 |
|
|---|
| 4883 | \code
|
|---|
| 4884 | QString string( "1234.56" );
|
|---|
| 4885 | double a = string.toDouble(); // a == 1234.56
|
|---|
| 4886 | \endcode
|
|---|
| 4887 |
|
|---|
| 4888 | \sa number()
|
|---|
| 4889 | */
|
|---|
| 4890 |
|
|---|
| 4891 | double QString::toDouble( bool *ok ) const
|
|---|
| 4892 | {
|
|---|
| 4893 | QLocale locale(QLocale::C);
|
|---|
| 4894 | return locale.d->stringToDouble(*this, ok);
|
|---|
| 4895 | }
|
|---|
| 4896 |
|
|---|
| 4897 | /*!
|
|---|
| 4898 | Returns the string converted to a \c float value.
|
|---|
| 4899 |
|
|---|
| 4900 | Returns 0.0 if the conversion fails.
|
|---|
| 4901 |
|
|---|
| 4902 | If \a ok is not 0: if a conversion error occurs, \a *ok is set to
|
|---|
| 4903 | FALSE; otherwise \a *ok is set to TRUE.
|
|---|
| 4904 |
|
|---|
| 4905 | \sa number()
|
|---|
| 4906 | */
|
|---|
| 4907 |
|
|---|
| 4908 | float QString::toFloat( bool *ok ) const
|
|---|
| 4909 | {
|
|---|
| 4910 | return (float)toDouble( ok );
|
|---|
| 4911 | }
|
|---|
| 4912 |
|
|---|
| 4913 | /*!
|
|---|
| 4914 | Sets the string to the printed value of \a n in base \a base and
|
|---|
| 4915 | returns a reference to the string.
|
|---|
| 4916 |
|
|---|
| 4917 | The base is 10 by default and must be between 2 and 36.
|
|---|
| 4918 |
|
|---|
| 4919 | \code
|
|---|
| 4920 | QString string;
|
|---|
| 4921 | string = string.setNum( 1234 ); // string == "1234"
|
|---|
| 4922 | \endcode
|
|---|
| 4923 | */
|
|---|
| 4924 |
|
|---|
| 4925 | QString &QString::setNum( Q_LLONG n, int base )
|
|---|
| 4926 | {
|
|---|
| 4927 | #if defined(QT_CHECK_RANGE)
|
|---|
| 4928 | if ( base < 2 || base > 36 ) {
|
|---|
| 4929 | qWarning( "QString::setNum: Invalid base %d", base );
|
|---|
| 4930 | base = 10;
|
|---|
| 4931 | }
|
|---|
| 4932 | #endif
|
|---|
| 4933 | QLocale locale(QLocale::C);
|
|---|
| 4934 | *this = locale.d->longLongToString(n, -1, base);
|
|---|
| 4935 | return *this;
|
|---|
| 4936 | }
|
|---|
| 4937 |
|
|---|
| 4938 | /*!
|
|---|
| 4939 | \overload
|
|---|
| 4940 |
|
|---|
| 4941 | Sets the string to the printed value of \a n in base \a base and
|
|---|
| 4942 | returns a reference to the string.
|
|---|
| 4943 |
|
|---|
| 4944 | The base is 10 by default and must be between 2 and 36.
|
|---|
| 4945 | */
|
|---|
| 4946 |
|
|---|
| 4947 | QString &QString::setNum( Q_ULLONG n, int base )
|
|---|
| 4948 | {
|
|---|
| 4949 | #if defined(QT_CHECK_RANGE)
|
|---|
| 4950 | if ( base < 2 || base > 36 ) {
|
|---|
| 4951 | qWarning( "QString::setNum: Invalid base %d", base );
|
|---|
| 4952 | base = 10;
|
|---|
| 4953 | }
|
|---|
| 4954 | #endif
|
|---|
| 4955 | QLocale locale(QLocale::C);
|
|---|
| 4956 | *this = locale.d->unsLongLongToString(n, -1, base);
|
|---|
| 4957 | return *this;
|
|---|
| 4958 | }
|
|---|
| 4959 |
|
|---|
| 4960 | /*!
|
|---|
| 4961 | \fn QString &QString::setNum( long n, int base )
|
|---|
| 4962 |
|
|---|
| 4963 | \overload
|
|---|
| 4964 | */
|
|---|
| 4965 | // ### 4.0: inline
|
|---|
| 4966 | QString &QString::setNum( long n, int base )
|
|---|
| 4967 | {
|
|---|
| 4968 | return setNum( (Q_LLONG)n, base );
|
|---|
| 4969 | }
|
|---|
| 4970 |
|
|---|
| 4971 | /*!
|
|---|
| 4972 | \fn QString &QString::setNum( ulong n, int base )
|
|---|
| 4973 |
|
|---|
| 4974 | \overload
|
|---|
| 4975 | */
|
|---|
| 4976 | // ### 4.0: inline
|
|---|
| 4977 | QString &QString::setNum( ulong n, int base )
|
|---|
| 4978 | {
|
|---|
| 4979 | return setNum( (Q_ULLONG)n, base );
|
|---|
| 4980 | }
|
|---|
| 4981 |
|
|---|
| 4982 | /*!
|
|---|
| 4983 | \fn QString &QString::setNum( int n, int base )
|
|---|
| 4984 |
|
|---|
| 4985 | \overload
|
|---|
| 4986 |
|
|---|
| 4987 | Sets the string to the printed value of \a n in base \a base and
|
|---|
| 4988 | returns a reference to the string.
|
|---|
| 4989 |
|
|---|
| 4990 | The base is 10 by default and must be between 2 and 36.
|
|---|
| 4991 | */
|
|---|
| 4992 |
|
|---|
| 4993 | /*!
|
|---|
| 4994 | \fn QString &QString::setNum( uint n, int base )
|
|---|
| 4995 |
|
|---|
| 4996 | \overload
|
|---|
| 4997 |
|
|---|
| 4998 | Sets the string to the printed value of \a n in base \a base and
|
|---|
| 4999 | returns a reference to the string.
|
|---|
| 5000 |
|
|---|
| 5001 | The base is 10 by default and must be between 2 and 36.
|
|---|
| 5002 | */
|
|---|
| 5003 |
|
|---|
| 5004 | /*!
|
|---|
| 5005 | \fn QString &QString::setNum( short n, int base )
|
|---|
| 5006 |
|
|---|
| 5007 | \overload
|
|---|
| 5008 |
|
|---|
| 5009 | Sets the string to the printed value of \a n in base \a base and
|
|---|
| 5010 | returns a reference to the string.
|
|---|
| 5011 |
|
|---|
| 5012 | The base is 10 by default and must be between 2 and 36.
|
|---|
| 5013 | */
|
|---|
| 5014 |
|
|---|
| 5015 | /*!
|
|---|
| 5016 | \fn QString &QString::setNum( ushort n, int base )
|
|---|
| 5017 |
|
|---|
| 5018 | \overload
|
|---|
| 5019 |
|
|---|
| 5020 | Sets the string to the printed value of \a n in base \a base and
|
|---|
| 5021 | returns a reference to the string.
|
|---|
| 5022 |
|
|---|
| 5023 | The base is 10 by default and must be between 2 and 36.
|
|---|
| 5024 | */
|
|---|
| 5025 |
|
|---|
| 5026 | /*!
|
|---|
| 5027 | \overload
|
|---|
| 5028 |
|
|---|
| 5029 | Sets the string to the printed value of \a n, formatted in format
|
|---|
| 5030 | \a f with precision \a prec, and returns a reference to the
|
|---|
| 5031 | string.
|
|---|
| 5032 |
|
|---|
| 5033 | The format \a f can be 'f', 'F', 'e', 'E', 'g' or 'G'. See \link
|
|---|
| 5034 | #arg-formats arg \endlink() for an explanation of the formats.
|
|---|
| 5035 | */
|
|---|
| 5036 |
|
|---|
| 5037 | QString &QString::setNum( double n, char f, int prec )
|
|---|
| 5038 | {
|
|---|
| 5039 | QLocalePrivate::DoubleForm form = QLocalePrivate::DFDecimal;
|
|---|
| 5040 | uint flags = 0;
|
|---|
| 5041 |
|
|---|
| 5042 | if (qIsUpper(f))
|
|---|
| 5043 | flags = QLocalePrivate::CapitalEorX;
|
|---|
| 5044 | f = qToLower(f);
|
|---|
| 5045 |
|
|---|
| 5046 | switch (f) {
|
|---|
| 5047 | case 'f':
|
|---|
| 5048 | form = QLocalePrivate::DFDecimal;
|
|---|
| 5049 | break;
|
|---|
| 5050 | case 'e':
|
|---|
| 5051 | form = QLocalePrivate::DFExponent;
|
|---|
| 5052 | break;
|
|---|
| 5053 | case 'g':
|
|---|
| 5054 | form = QLocalePrivate::DFSignificantDigits;
|
|---|
| 5055 | break;
|
|---|
| 5056 | default:
|
|---|
| 5057 | #if defined(QT_CHECK_RANGE)
|
|---|
| 5058 | qWarning( "QString::setNum: Invalid format char '%c'", f );
|
|---|
| 5059 | #endif
|
|---|
| 5060 | break;
|
|---|
| 5061 | }
|
|---|
| 5062 |
|
|---|
| 5063 | QLocale locale(QLocale::C);
|
|---|
| 5064 | *this = locale.d->doubleToString(n, prec, form, -1, flags);
|
|---|
| 5065 | return *this;
|
|---|
| 5066 | }
|
|---|
| 5067 |
|
|---|
| 5068 | /*!
|
|---|
| 5069 | \fn QString &QString::setNum( float n, char f, int prec )
|
|---|
| 5070 |
|
|---|
| 5071 | \overload
|
|---|
| 5072 |
|
|---|
| 5073 | Sets the string to the printed value of \a n, formatted in format
|
|---|
| 5074 | \a f with precision \a prec, and returns a reference to the
|
|---|
| 5075 | string.
|
|---|
| 5076 |
|
|---|
| 5077 | The format \a f can be 'f', 'F', 'e', 'E', 'g' or 'G'. See \link
|
|---|
| 5078 | #arg-formats arg \endlink() for an explanation of the formats.
|
|---|
| 5079 | */
|
|---|
| 5080 |
|
|---|
| 5081 |
|
|---|
| 5082 | /*!
|
|---|
| 5083 | A convenience function that returns a string equivalent of the
|
|---|
| 5084 | number \a n to base \a base, which is 10 by default and must be
|
|---|
| 5085 | between 2 and 36.
|
|---|
| 5086 |
|
|---|
| 5087 | \code
|
|---|
| 5088 | long a = 63;
|
|---|
| 5089 | QString str = QString::number( a, 16 ); // str == "3f"
|
|---|
| 5090 | QString str = QString::number( a, 16 ).upper(); // str == "3F"
|
|---|
| 5091 | \endcode
|
|---|
| 5092 |
|
|---|
| 5093 | \sa setNum()
|
|---|
| 5094 | */
|
|---|
| 5095 | QString QString::number( long n, int base )
|
|---|
| 5096 | {
|
|---|
| 5097 | QString s;
|
|---|
| 5098 | s.setNum( n, base );
|
|---|
| 5099 | return s;
|
|---|
| 5100 | }
|
|---|
| 5101 |
|
|---|
| 5102 | /*!
|
|---|
| 5103 | \overload
|
|---|
| 5104 |
|
|---|
| 5105 | \sa setNum()
|
|---|
| 5106 | */
|
|---|
| 5107 | QString QString::number( ulong n, int base )
|
|---|
| 5108 | {
|
|---|
| 5109 | QString s;
|
|---|
| 5110 | s.setNum( n, base );
|
|---|
| 5111 | return s;
|
|---|
| 5112 | }
|
|---|
| 5113 |
|
|---|
| 5114 | /*!
|
|---|
| 5115 | \overload
|
|---|
| 5116 |
|
|---|
| 5117 | \sa setNum()
|
|---|
| 5118 | */
|
|---|
| 5119 | QString QString::number( Q_LLONG n, int base )
|
|---|
| 5120 | {
|
|---|
| 5121 | QString s;
|
|---|
| 5122 | s.setNum( n, base );
|
|---|
| 5123 | return s;
|
|---|
| 5124 | }
|
|---|
| 5125 |
|
|---|
| 5126 | /*!
|
|---|
| 5127 | \overload
|
|---|
| 5128 |
|
|---|
| 5129 | \sa setNum()
|
|---|
| 5130 | */
|
|---|
| 5131 | QString QString::number( Q_ULLONG n, int base )
|
|---|
| 5132 | {
|
|---|
| 5133 | QString s;
|
|---|
| 5134 | s.setNum( n, base );
|
|---|
| 5135 | return s;
|
|---|
| 5136 | }
|
|---|
| 5137 |
|
|---|
| 5138 | /*!
|
|---|
| 5139 | \overload
|
|---|
| 5140 |
|
|---|
| 5141 | \sa setNum()
|
|---|
| 5142 | */
|
|---|
| 5143 | QString QString::number( int n, int base )
|
|---|
| 5144 | {
|
|---|
| 5145 | QString s;
|
|---|
| 5146 | s.setNum( n, base );
|
|---|
| 5147 | return s;
|
|---|
| 5148 | }
|
|---|
| 5149 |
|
|---|
| 5150 | /*!
|
|---|
| 5151 | \overload
|
|---|
| 5152 |
|
|---|
| 5153 | A convenience factory function that returns a string
|
|---|
| 5154 | representation of the number \a n to the base \a base, which is 10
|
|---|
| 5155 | by default and must be between 2 and 36.
|
|---|
| 5156 |
|
|---|
| 5157 | \sa setNum()
|
|---|
| 5158 | */
|
|---|
| 5159 | QString QString::number( uint n, int base )
|
|---|
| 5160 | {
|
|---|
| 5161 | QString s;
|
|---|
| 5162 | s.setNum( n, base );
|
|---|
| 5163 | return s;
|
|---|
| 5164 | }
|
|---|
| 5165 |
|
|---|
| 5166 | /*!
|
|---|
| 5167 | \overload
|
|---|
| 5168 |
|
|---|
| 5169 | Argument \a n is formatted according to the \a f format specified,
|
|---|
| 5170 | which is \c g by default, and can be any of the following:
|
|---|
| 5171 |
|
|---|
| 5172 | \table
|
|---|
| 5173 | \header \i Format \i Meaning
|
|---|
| 5174 | \row \i \c e \i format as [-]9.9e[+|-]999
|
|---|
| 5175 | \row \i \c E \i format as [-]9.9E[+|-]999
|
|---|
| 5176 | \row \i \c f \i format as [-]9.9
|
|---|
| 5177 | \row \i \c g \i use \c e or \c f format, whichever is the most concise
|
|---|
| 5178 | \row \i \c G \i use \c E or \c f format, whichever is the most concise
|
|---|
| 5179 | \endtable
|
|---|
| 5180 |
|
|---|
| 5181 | With 'e', 'E', and 'f', \a prec is the number of digits after the
|
|---|
| 5182 | decimal point. With 'g' and 'G', \a prec is the maximum number of
|
|---|
| 5183 | significant digits (trailing zeroes are omitted).
|
|---|
| 5184 |
|
|---|
| 5185 | \code
|
|---|
| 5186 | double d = 12.34;
|
|---|
| 5187 | QString ds = QString( "'E' format, precision 3, gives %1" )
|
|---|
| 5188 | .arg( d, 0, 'E', 3 );
|
|---|
| 5189 | // ds == "1.234E+001"
|
|---|
| 5190 | \endcode
|
|---|
| 5191 |
|
|---|
| 5192 | \sa setNum()
|
|---|
| 5193 | */
|
|---|
| 5194 | QString QString::number( double n, char f, int prec )
|
|---|
| 5195 | {
|
|---|
| 5196 | QString s;
|
|---|
| 5197 | s.setNum( n, f, prec );
|
|---|
| 5198 | return s;
|
|---|
| 5199 | }
|
|---|
| 5200 |
|
|---|
| 5201 |
|
|---|
| 5202 | /*! \obsolete
|
|---|
| 5203 |
|
|---|
| 5204 | Sets the character at position \a index to \a c and expands the
|
|---|
| 5205 | string if necessary, filling with spaces.
|
|---|
| 5206 |
|
|---|
| 5207 | This method is redundant in Qt 3.x, because operator[] will expand
|
|---|
| 5208 | the string as necessary.
|
|---|
| 5209 | */
|
|---|
| 5210 |
|
|---|
| 5211 | void QString::setExpand( uint index, QChar c )
|
|---|
| 5212 | {
|
|---|
| 5213 | int spaces = index - d->len;
|
|---|
| 5214 | at(index) = c;
|
|---|
| 5215 | while (spaces-->0)
|
|---|
| 5216 | d->unicode[--index]=' ';
|
|---|
| 5217 | }
|
|---|
| 5218 |
|
|---|
| 5219 |
|
|---|
| 5220 | /*!
|
|---|
| 5221 | \fn const char* QString::data() const
|
|---|
| 5222 |
|
|---|
| 5223 | \obsolete
|
|---|
| 5224 |
|
|---|
| 5225 | Returns a pointer to a '\0'-terminated classic C string.
|
|---|
| 5226 |
|
|---|
| 5227 | In Qt 1.x, this returned a char* allowing direct manipulation of the
|
|---|
| 5228 | string as a sequence of bytes. In Qt 2.x where QString is a Unicode
|
|---|
| 5229 | string, char* conversion constructs a temporary string, and hence
|
|---|
| 5230 | direct character operations are meaningless.
|
|---|
| 5231 | */
|
|---|
| 5232 |
|
|---|
| 5233 | /*!
|
|---|
| 5234 | \fn bool QString::operator!() const
|
|---|
| 5235 |
|
|---|
| 5236 | Returns TRUE if this is a null string; otherwise returns FALSE.
|
|---|
| 5237 |
|
|---|
| 5238 | \code
|
|---|
| 5239 | QString name = getName();
|
|---|
| 5240 | if ( !name )
|
|---|
| 5241 | name = "Rodney";
|
|---|
| 5242 | \endcode
|
|---|
| 5243 |
|
|---|
| 5244 | Note that if you say
|
|---|
| 5245 |
|
|---|
| 5246 | \code
|
|---|
| 5247 | QString name = getName();
|
|---|
| 5248 | if ( name )
|
|---|
| 5249 | doSomethingWith(name);
|
|---|
| 5250 | \endcode
|
|---|
| 5251 |
|
|---|
| 5252 | It will call "operator const char*()", which is inefficent; you
|
|---|
| 5253 | may wish to define the macro \c QT_NO_ASCII_CAST when writing code
|
|---|
| 5254 | which you wish to remain Unicode-clean.
|
|---|
| 5255 |
|
|---|
| 5256 | When you want the above semantics, use:
|
|---|
| 5257 |
|
|---|
| 5258 | \code
|
|---|
| 5259 | QString name = getName();
|
|---|
| 5260 | if ( !name.isNull() )
|
|---|
| 5261 | doSomethingWith(name);
|
|---|
| 5262 | \endcode
|
|---|
| 5263 |
|
|---|
| 5264 | \sa isEmpty()
|
|---|
| 5265 | */
|
|---|
| 5266 |
|
|---|
| 5267 |
|
|---|
| 5268 | /*!
|
|---|
| 5269 | \fn QString& QString::append( const QString& str )
|
|---|
| 5270 |
|
|---|
| 5271 | Appends \a str to the string and returns a reference to the
|
|---|
| 5272 | result.
|
|---|
| 5273 |
|
|---|
| 5274 | \code
|
|---|
| 5275 | string = "Test";
|
|---|
| 5276 | string.append( "ing" ); // string == "Testing"
|
|---|
| 5277 | \endcode
|
|---|
| 5278 |
|
|---|
| 5279 | Equivalent to operator+=().
|
|---|
| 5280 | */
|
|---|
| 5281 |
|
|---|
| 5282 | /*!
|
|---|
| 5283 | \fn QString& QString::append( char ch )
|
|---|
| 5284 |
|
|---|
| 5285 | \overload
|
|---|
| 5286 |
|
|---|
| 5287 | Appends character \a ch to the string and returns a reference to
|
|---|
| 5288 | the result.
|
|---|
| 5289 |
|
|---|
| 5290 | Equivalent to operator+=().
|
|---|
| 5291 | */
|
|---|
| 5292 |
|
|---|
| 5293 | /*!
|
|---|
| 5294 | \fn QString& QString::append( QChar ch )
|
|---|
| 5295 |
|
|---|
| 5296 | \overload
|
|---|
| 5297 |
|
|---|
| 5298 | Appends character \a ch to the string and returns a reference to
|
|---|
| 5299 | the result.
|
|---|
| 5300 |
|
|---|
| 5301 | Equivalent to operator+=().
|
|---|
| 5302 | */
|
|---|
| 5303 |
|
|---|
| 5304 | /*! \fn QString& QString::append( const QByteArray &str )
|
|---|
| 5305 | \overload
|
|---|
| 5306 |
|
|---|
| 5307 | Appends \a str to the string and returns a reference to the result.
|
|---|
| 5308 |
|
|---|
| 5309 | Equivalent to operator+=().
|
|---|
| 5310 | */
|
|---|
| 5311 |
|
|---|
| 5312 | /*! \fn QString& QString::append( const std::string &str )
|
|---|
| 5313 | \overload
|
|---|
| 5314 |
|
|---|
| 5315 | Appends \a str to the string and returns a reference to the result.
|
|---|
| 5316 |
|
|---|
| 5317 | Equivalent to operator+=().
|
|---|
| 5318 | */
|
|---|
| 5319 |
|
|---|
| 5320 | /*! \fn QString& QString::append( const char *str )
|
|---|
| 5321 | \overload
|
|---|
| 5322 |
|
|---|
| 5323 | Appends \a str to the string and returns a reference to the result.
|
|---|
| 5324 |
|
|---|
| 5325 | Equivalent to operator+=().
|
|---|
| 5326 | */
|
|---|
| 5327 |
|
|---|
| 5328 | /*!
|
|---|
| 5329 | Appends \a str to the string and returns a reference to the string.
|
|---|
| 5330 | */
|
|---|
| 5331 | QString& QString::operator+=( const QString &str )
|
|---|
| 5332 | {
|
|---|
| 5333 | uint len1 = length();
|
|---|
| 5334 | uint len2 = str.length();
|
|---|
| 5335 | if ( len2 ) {
|
|---|
| 5336 | if ( isEmpty() ) {
|
|---|
| 5337 | operator=( str );
|
|---|
| 5338 | } else {
|
|---|
| 5339 | grow( len1+len2 );
|
|---|
| 5340 | memcpy( d->unicode+len1, str.unicode(), sizeof(QChar)*len2 );
|
|---|
| 5341 | }
|
|---|
| 5342 | } else if ( isNull() && !str.isNull() ) { // ## just for 1.x compat:
|
|---|
| 5343 | *this = fromLatin1( "" );
|
|---|
| 5344 | }
|
|---|
| 5345 | return *this;
|
|---|
| 5346 | }
|
|---|
| 5347 |
|
|---|
| 5348 | #ifndef QT_NO_CAST_ASCII
|
|---|
| 5349 | QString &QString::operatorPlusEqHelper( const char *s, uint len2 )
|
|---|
| 5350 | {
|
|---|
| 5351 | if ( s ) {
|
|---|
| 5352 | #ifndef QT_NO_TEXTCODEC
|
|---|
| 5353 | if ( QTextCodec::codecForCStrings() )
|
|---|
| 5354 | return operator+=( fromAscii( s, len2 ) );
|
|---|
| 5355 | #endif
|
|---|
| 5356 |
|
|---|
| 5357 | uint len1 = length();
|
|---|
| 5358 | if ( len2 == UINT_MAX )
|
|---|
| 5359 | len2 = strlen( s );
|
|---|
| 5360 | if ( len2 ) {
|
|---|
| 5361 | grow( len1 + len2 );
|
|---|
| 5362 | QChar* uc = d->unicode + len1;
|
|---|
| 5363 | while ( len2-- )
|
|---|
| 5364 | *uc++ = *s++;
|
|---|
| 5365 | } else if ( isNull() ) { // ## just for 1.x compat:
|
|---|
| 5366 | *this = fromLatin1( "" );
|
|---|
| 5367 | }
|
|---|
| 5368 | }
|
|---|
| 5369 | return *this;
|
|---|
| 5370 | }
|
|---|
| 5371 | #endif
|
|---|
| 5372 |
|
|---|
| 5373 | /*!
|
|---|
| 5374 | \overload
|
|---|
| 5375 |
|
|---|
| 5376 | Appends \a str to the string and returns a reference to the string.
|
|---|
| 5377 | */
|
|---|
| 5378 | #ifndef QT_NO_CAST_ASCII
|
|---|
| 5379 | QString& QString::operator+=( const char *str )
|
|---|
| 5380 | {
|
|---|
| 5381 | // ### Qt 4: make this function inline
|
|---|
| 5382 | return operatorPlusEqHelper( str );
|
|---|
| 5383 | }
|
|---|
| 5384 | #endif
|
|---|
| 5385 |
|
|---|
| 5386 | /*! \overload
|
|---|
| 5387 |
|
|---|
| 5388 | Appends \a c to the string and returns a reference to the string.
|
|---|
| 5389 | */
|
|---|
| 5390 |
|
|---|
| 5391 | QString &QString::operator+=( QChar c )
|
|---|
| 5392 | {
|
|---|
| 5393 | grow( length()+1 );
|
|---|
| 5394 | d->unicode[length()-1] = c;
|
|---|
| 5395 | return *this;
|
|---|
| 5396 | }
|
|---|
| 5397 |
|
|---|
| 5398 | /*!
|
|---|
| 5399 | \overload
|
|---|
| 5400 |
|
|---|
| 5401 | Appends \a c to the string and returns a reference to the string.
|
|---|
| 5402 | */
|
|---|
| 5403 |
|
|---|
| 5404 | QString &QString::operator+=( char c )
|
|---|
| 5405 | {
|
|---|
| 5406 | #ifndef QT_NO_TEXTCODEC
|
|---|
| 5407 | if ( QTextCodec::codecForCStrings() )
|
|---|
| 5408 | return operator+=( fromAscii( &c, 1 ) );
|
|---|
| 5409 | #endif
|
|---|
| 5410 | grow( length()+1 );
|
|---|
| 5411 | d->unicode[length()-1] = c;
|
|---|
| 5412 | return *this;
|
|---|
| 5413 | }
|
|---|
| 5414 |
|
|---|
| 5415 | /*!
|
|---|
| 5416 | \fn QString &QString::operator+=( const QByteArray &str )
|
|---|
| 5417 | \overload
|
|---|
| 5418 |
|
|---|
| 5419 | Appends \a str to the string and returns a reference to the string.
|
|---|
| 5420 | */
|
|---|
| 5421 |
|
|---|
| 5422 | /*!
|
|---|
| 5423 | \fn QString &QString::operator+=( const std::string &str )
|
|---|
| 5424 | \overload
|
|---|
| 5425 |
|
|---|
| 5426 | Appends \a str to the string and returns a reference to the string.
|
|---|
| 5427 | */
|
|---|
| 5428 |
|
|---|
| 5429 | /*!
|
|---|
| 5430 | \fn char QChar::latin1() const
|
|---|
| 5431 |
|
|---|
| 5432 | Returns the Latin-1 value of this character, or 0 if it
|
|---|
| 5433 | cannot be represented in Latin-1.
|
|---|
| 5434 | */
|
|---|
| 5435 |
|
|---|
| 5436 |
|
|---|
| 5437 | /*!
|
|---|
| 5438 | Returns a Latin-1 representation of the string. The
|
|---|
| 5439 | returned value is undefined if the string contains non-Latin-1
|
|---|
| 5440 | characters. If you want to convert strings into formats other than
|
|---|
| 5441 | Unicode, see the QTextCodec classes.
|
|---|
| 5442 |
|
|---|
| 5443 | This function is mainly useful for boot-strapping legacy code to
|
|---|
| 5444 | use Unicode.
|
|---|
| 5445 |
|
|---|
| 5446 | The result remains valid so long as one unmodified copy of the
|
|---|
| 5447 | source string exists.
|
|---|
| 5448 |
|
|---|
| 5449 | \sa fromLatin1(), ascii(), utf8(), local8Bit()
|
|---|
| 5450 | */
|
|---|
| 5451 | const char* QString::latin1() const
|
|---|
| 5452 | {
|
|---|
| 5453 | if ( !d->ascii || !d->islatin1 ) {
|
|---|
| 5454 | delete [] d->ascii;
|
|---|
| 5455 | d->ascii = unicodeToLatin1( d->unicode, d->len );
|
|---|
| 5456 | d->islatin1 = TRUE;
|
|---|
| 5457 | }
|
|---|
| 5458 | return d->ascii;
|
|---|
| 5459 | }
|
|---|
| 5460 |
|
|---|
| 5461 | /*!
|
|---|
| 5462 | Returns an 8-bit ASCII representation of the string.
|
|---|
| 5463 |
|
|---|
| 5464 | If a codec has been set using QTextCodec::codecForCStrings(),
|
|---|
| 5465 | it is used to convert Unicode to 8-bit char. Otherwise, this function
|
|---|
| 5466 | does the same as latin1().
|
|---|
| 5467 |
|
|---|
| 5468 | \sa fromAscii(), latin1(), utf8(), local8Bit()
|
|---|
| 5469 | */
|
|---|
| 5470 | const char* QString::ascii() const
|
|---|
| 5471 | {
|
|---|
| 5472 | #ifndef QT_NO_TEXTCODEC
|
|---|
| 5473 | if ( QTextCodec::codecForCStrings() ) {
|
|---|
| 5474 | if ( !d->ascii || d->islatin1 ) {
|
|---|
| 5475 | delete [] d->ascii;
|
|---|
| 5476 | if (d->unicode) {
|
|---|
| 5477 | QCString s = QTextCodec::codecForCStrings()->fromUnicode( *this );
|
|---|
| 5478 | s.detach();
|
|---|
| 5479 | d->ascii = s.data();
|
|---|
| 5480 | s.resetRawData( s.data(), s.size() ); // we have stolen the data
|
|---|
| 5481 | } else {
|
|---|
| 5482 | d->ascii = 0;
|
|---|
| 5483 | }
|
|---|
| 5484 | d->islatin1 = FALSE;
|
|---|
| 5485 | }
|
|---|
| 5486 | return d->ascii;
|
|---|
| 5487 | }
|
|---|
| 5488 | #endif // QT_NO_TEXTCODEC
|
|---|
| 5489 | return latin1();
|
|---|
| 5490 | }
|
|---|
| 5491 |
|
|---|
| 5492 | /*!
|
|---|
| 5493 | Returns the string encoded in UTF-8 format.
|
|---|
| 5494 |
|
|---|
| 5495 | See QTextCodec for more diverse coding/decoding of Unicode strings.
|
|---|
| 5496 |
|
|---|
| 5497 | \sa fromUtf8(), ascii(), latin1(), local8Bit()
|
|---|
| 5498 | */
|
|---|
| 5499 | QCString QString::utf8() const
|
|---|
| 5500 | {
|
|---|
| 5501 | int l = length();
|
|---|
| 5502 | int rlen = l*3+1;
|
|---|
| 5503 | QCString rstr(rlen);
|
|---|
| 5504 | uchar* cursor = (uchar*)rstr.data();
|
|---|
| 5505 | const QChar *ch = d->unicode;
|
|---|
| 5506 | for (int i=0; i < l; i++) {
|
|---|
| 5507 | uint u = ch->unicode();
|
|---|
| 5508 | if ( u < 0x80 ) {
|
|---|
| 5509 | *cursor++ = (uchar)u;
|
|---|
| 5510 | } else {
|
|---|
| 5511 | if ( u < 0x0800 ) {
|
|---|
| 5512 | *cursor++ = 0xc0 | ((uchar) (u >> 6));
|
|---|
| 5513 | } else {
|
|---|
| 5514 | if (u >= 0xd800 && u < 0xdc00 && i < l-1) {
|
|---|
| 5515 | unsigned short low = ch[1].unicode();
|
|---|
| 5516 | if (low >= 0xdc00 && low < 0xe000) {
|
|---|
| 5517 | ++ch;
|
|---|
| 5518 | ++i;
|
|---|
| 5519 | u = (u - 0xd800)*0x400 + (low - 0xdc00) + 0x10000;
|
|---|
| 5520 | }
|
|---|
| 5521 | }
|
|---|
| 5522 | if (u > 0xffff) {
|
|---|
| 5523 | // if people are working in utf8, but strings are encoded in eg. latin1, the resulting
|
|---|
| 5524 | // name might be invalid utf8. This and the corresponding code in fromUtf8 takes care
|
|---|
| 5525 | // we can handle this without loosing information. This can happen with latin filenames
|
|---|
| 5526 | // and a utf8 locale under Unix.
|
|---|
| 5527 | if (u > 0x10fe00 && u < 0x10ff00) {
|
|---|
| 5528 | *cursor++ = (u - 0x10fe00);
|
|---|
| 5529 | ++ch;
|
|---|
| 5530 | continue;
|
|---|
| 5531 | } else {
|
|---|
| 5532 | *cursor++ = 0xf0 | ((uchar) (u >> 18));
|
|---|
| 5533 | *cursor++ = 0x80 | ( ((uchar) (u >> 12)) & 0x3f);
|
|---|
| 5534 | }
|
|---|
| 5535 | } else {
|
|---|
| 5536 | *cursor++ = 0xe0 | ((uchar) (u >> 12));
|
|---|
| 5537 | }
|
|---|
| 5538 | *cursor++ = 0x80 | ( ((uchar) (u >> 6)) & 0x3f);
|
|---|
| 5539 | }
|
|---|
| 5540 | *cursor++ = 0x80 | ((uchar) (u&0x3f));
|
|---|
| 5541 | }
|
|---|
| 5542 | ++ch;
|
|---|
| 5543 | }
|
|---|
| 5544 | rstr.truncate( cursor - (uchar*)rstr.data() );
|
|---|
| 5545 | return rstr;
|
|---|
| 5546 | }
|
|---|
| 5547 |
|
|---|
| 5548 | /*!
|
|---|
| 5549 | Returns the Unicode string decoded from the first \a len
|
|---|
| 5550 | bytes of \a utf8, ignoring the rest of \a utf8. If \a len is
|
|---|
| 5551 | -1 then the length of \a utf8 is used. If \a len is bigger than
|
|---|
| 5552 | the length of \a utf8 then it will use the length of \a utf8.
|
|---|
| 5553 |
|
|---|
| 5554 | \code
|
|---|
| 5555 | QString str = QString::fromUtf8( "123456789", 5 );
|
|---|
| 5556 | // str == "12345"
|
|---|
| 5557 | \endcode
|
|---|
| 5558 |
|
|---|
| 5559 | See QTextCodec for more diverse coding/decoding of Unicode strings.
|
|---|
| 5560 | */
|
|---|
| 5561 | QString QString::fromUtf8( const char* utf8, int len )
|
|---|
| 5562 | {
|
|---|
| 5563 | if ( !utf8 )
|
|---|
| 5564 | return QString::null;
|
|---|
| 5565 |
|
|---|
| 5566 | if ( len < 0 )
|
|---|
| 5567 | len = strlen( utf8 );
|
|---|
| 5568 | QString result;
|
|---|
| 5569 | result.setLength( len*2 ); // worst case
|
|---|
| 5570 | QChar *qch = (QChar *)result.unicode();
|
|---|
| 5571 | uint uc = 0;
|
|---|
| 5572 | int need = 0;
|
|---|
| 5573 | int error = -1;
|
|---|
| 5574 | uchar ch;
|
|---|
| 5575 | for (int i=0; i<len; i++) {
|
|---|
| 5576 | ch = utf8[i];
|
|---|
| 5577 | if (need) {
|
|---|
| 5578 | if ( (ch&0xc0) == 0x80 ) {
|
|---|
| 5579 | uc = (uc << 6) | (ch & 0x3f);
|
|---|
| 5580 | need--;
|
|---|
| 5581 | if ( !need ) {
|
|---|
| 5582 | if (uc > 0xffff) {
|
|---|
| 5583 | // surrogate pair
|
|---|
| 5584 | uc -= 0x10000;
|
|---|
| 5585 | unsigned short high = uc/0x400 + 0xd800;
|
|---|
| 5586 | unsigned short low = uc%0x400 + 0xdc00;
|
|---|
| 5587 | *qch++ = QChar(high);
|
|---|
| 5588 | *qch++ = QChar(low);
|
|---|
| 5589 | } else {
|
|---|
| 5590 | *qch++ = uc;
|
|---|
| 5591 | }
|
|---|
| 5592 | }
|
|---|
| 5593 | } else {
|
|---|
| 5594 | // See QString::utf8() for explanation.
|
|---|
| 5595 | //
|
|---|
| 5596 | // The surrogate below corresponds to a Unicode value of (0x10fe00+ch) which
|
|---|
| 5597 | // is in one of the private use areas of Unicode.
|
|---|
| 5598 | i = error;
|
|---|
| 5599 | *qch++ = QChar(0xdbff);
|
|---|
| 5600 | *qch++ = QChar(0xde00+((uchar)utf8[i]));
|
|---|
| 5601 | need = 0;
|
|---|
| 5602 | }
|
|---|
| 5603 | } else {
|
|---|
| 5604 | if ( ch < 128 ) {
|
|---|
| 5605 | *qch++ = ch;
|
|---|
| 5606 | } else if ((ch & 0xe0) == 0xc0) {
|
|---|
| 5607 | uc = ch & 0x1f;
|
|---|
| 5608 | need = 1;
|
|---|
| 5609 | error = i;
|
|---|
| 5610 | } else if ((ch & 0xf0) == 0xe0) {
|
|---|
| 5611 | uc = ch & 0x0f;
|
|---|
| 5612 | need = 2;
|
|---|
| 5613 | error = i;
|
|---|
| 5614 | } else if ((ch&0xf8) == 0xf0) {
|
|---|
| 5615 | uc = ch & 0x07;
|
|---|
| 5616 | need = 3;
|
|---|
| 5617 | error = i;
|
|---|
| 5618 | } else {
|
|---|
| 5619 | // Error
|
|---|
| 5620 | *qch++ = QChar(0xdbff);
|
|---|
| 5621 | *qch++ = QChar(0xde00+((uchar)utf8[i]));
|
|---|
| 5622 | }
|
|---|
| 5623 | }
|
|---|
| 5624 | }
|
|---|
| 5625 | if (need) {
|
|---|
| 5626 | // we have some invalid characters remaining we need to add to the string
|
|---|
| 5627 | for (int i = error; i < len; ++i) {
|
|---|
| 5628 | *qch++ = QChar(0xdbff);
|
|---|
| 5629 | *qch++ = QChar(0xde00+((uchar)utf8[i]));
|
|---|
| 5630 | }
|
|---|
| 5631 | }
|
|---|
| 5632 |
|
|---|
| 5633 | result.truncate( qch - result.unicode() );
|
|---|
| 5634 | return result;
|
|---|
| 5635 | }
|
|---|
| 5636 |
|
|---|
| 5637 | /*!
|
|---|
| 5638 | Returns the Unicode string decoded from the first \a len
|
|---|
| 5639 | bytes of \a ascii, ignoring the rest of \a ascii. If \a len
|
|---|
| 5640 | is -1 then the length of \a ascii is used. If \a len is bigger
|
|---|
| 5641 | than the length of \a ascii then it will use the length of \a
|
|---|
| 5642 | ascii.
|
|---|
| 5643 |
|
|---|
| 5644 | If a codec has been set using QTextCodec::codecForCStrings(),
|
|---|
| 5645 | it is used to convert Unicode to 8-bit char. Otherwise, this function
|
|---|
| 5646 | does the same as fromLatin1().
|
|---|
| 5647 |
|
|---|
| 5648 | This is the same as the QString(const char*) constructor, but you
|
|---|
| 5649 | can make that constructor invisible if you compile with the define
|
|---|
| 5650 | \c QT_NO_CAST_ASCII, in which case you can explicitly create a
|
|---|
| 5651 | QString from 8-bit ASCII text using this function.
|
|---|
| 5652 |
|
|---|
| 5653 | \code
|
|---|
| 5654 | QString str = QString::fromAscii( "123456789", 5 );
|
|---|
| 5655 | // str == "12345"
|
|---|
| 5656 | \endcode
|
|---|
| 5657 | */
|
|---|
| 5658 | QString QString::fromAscii( const char* ascii, int len )
|
|---|
| 5659 | {
|
|---|
| 5660 | #ifndef QT_NO_TEXTCODEC
|
|---|
| 5661 | if ( QTextCodec::codecForCStrings() ) {
|
|---|
| 5662 | if ( !ascii )
|
|---|
| 5663 | return QString::null;
|
|---|
| 5664 | if ( len < 0 )
|
|---|
| 5665 | len = strlen( ascii );
|
|---|
| 5666 | if ( len == 0 || *ascii == '\0' )
|
|---|
| 5667 | return QString::fromLatin1( "" );
|
|---|
| 5668 | return QTextCodec::codecForCStrings()->toUnicode( ascii, len );
|
|---|
| 5669 | }
|
|---|
| 5670 | #endif
|
|---|
| 5671 | return fromLatin1( ascii, len );
|
|---|
| 5672 | }
|
|---|
| 5673 |
|
|---|
| 5674 |
|
|---|
| 5675 | /*!
|
|---|
| 5676 | Returns the Unicode string decoded from the first \a len
|
|---|
| 5677 | bytes of \a chars, ignoring the rest of \a chars. If \a len
|
|---|
| 5678 | is -1 then the length of \a chars is used. If \a len is bigger
|
|---|
| 5679 | than the length of \a chars then it will use the length of \a
|
|---|
| 5680 | chars.
|
|---|
| 5681 |
|
|---|
| 5682 | \sa fromAscii()
|
|---|
| 5683 | */
|
|---|
| 5684 | QString QString::fromLatin1( const char* chars, int len )
|
|---|
| 5685 | {
|
|---|
| 5686 | uint l;
|
|---|
| 5687 | QChar *uc;
|
|---|
| 5688 | if ( len < 0 )
|
|---|
| 5689 | len = -1;
|
|---|
| 5690 | uc = internalLatin1ToUnicode( chars, &l, len );
|
|---|
| 5691 | return QString( new QStringData(uc, l, l), TRUE );
|
|---|
| 5692 | }
|
|---|
| 5693 |
|
|---|
| 5694 | /*!
|
|---|
| 5695 | \fn const QChar* QString::unicode() const
|
|---|
| 5696 |
|
|---|
| 5697 | Returns the Unicode representation of the string. The result
|
|---|
| 5698 | remains valid until the string is modified.
|
|---|
| 5699 | */
|
|---|
| 5700 |
|
|---|
| 5701 | /*!
|
|---|
| 5702 | Returns the string encoded in a locale-specific format. On X11,
|
|---|
| 5703 | this is the QTextCodec::codecForLocale(). On Windows, it is a
|
|---|
| 5704 | system-defined encoding. On Mac OS X, this always uses UTF-8 as
|
|---|
| 5705 | the encoding.
|
|---|
| 5706 |
|
|---|
| 5707 | See QTextCodec for more diverse coding/decoding of Unicode
|
|---|
| 5708 | strings.
|
|---|
| 5709 |
|
|---|
| 5710 | \sa fromLocal8Bit(), ascii(), latin1(), utf8()
|
|---|
| 5711 | */
|
|---|
| 5712 |
|
|---|
| 5713 | QCString QString::local8Bit() const
|
|---|
| 5714 | {
|
|---|
| 5715 | #ifdef QT_NO_TEXTCODEC
|
|---|
| 5716 | return latin1();
|
|---|
| 5717 | #else
|
|---|
| 5718 | #ifdef Q_WS_X11
|
|---|
| 5719 | QTextCodec* codec = QTextCodec::codecForLocale();
|
|---|
| 5720 | return codec
|
|---|
| 5721 | ? codec->fromUnicode(*this)
|
|---|
| 5722 | : QCString(latin1());
|
|---|
| 5723 | #endif
|
|---|
| 5724 | #if defined( Q_WS_MACX )
|
|---|
| 5725 | return utf8();
|
|---|
| 5726 | #endif
|
|---|
| 5727 | #if defined( Q_WS_MAC9 )
|
|---|
| 5728 | return QCString(latin1()); //I'm evil..
|
|---|
| 5729 | #endif
|
|---|
| 5730 | #ifdef Q_WS_WIN
|
|---|
| 5731 | return isNull() ? QCString("") : qt_winQString2MB( *this );
|
|---|
| 5732 | #endif
|
|---|
| 5733 | #ifdef Q_WS_QWS
|
|---|
| 5734 | return utf8(); // ### if there is any 8 bit format supported?
|
|---|
| 5735 | #endif
|
|---|
| 5736 | #endif
|
|---|
| 5737 | }
|
|---|
| 5738 |
|
|---|
| 5739 | /*!
|
|---|
| 5740 | Returns the Unicode string decoded from the first \a len
|
|---|
| 5741 | bytes of \a local8Bit, ignoring the rest of \a local8Bit. If
|
|---|
| 5742 | \a len is -1 then the length of \a local8Bit is used. If \a len is
|
|---|
| 5743 | bigger than the length of \a local8Bit then it will use the length
|
|---|
| 5744 | of \a local8Bit.
|
|---|
| 5745 |
|
|---|
| 5746 | \code
|
|---|
| 5747 | QString str = QString::fromLocal8Bit( "123456789", 5 );
|
|---|
| 5748 | // str == "12345"
|
|---|
| 5749 | \endcode
|
|---|
| 5750 |
|
|---|
| 5751 | \a local8Bit is assumed to be encoded in a locale-specific format.
|
|---|
| 5752 |
|
|---|
| 5753 | See QTextCodec for more diverse coding/decoding of Unicode strings.
|
|---|
| 5754 | */
|
|---|
| 5755 | QString QString::fromLocal8Bit( const char* local8Bit, int len )
|
|---|
| 5756 | {
|
|---|
| 5757 | #ifdef QT_NO_TEXTCODEC
|
|---|
| 5758 | return fromLatin1( local8Bit, len );
|
|---|
| 5759 | #else
|
|---|
| 5760 |
|
|---|
| 5761 | if ( !local8Bit )
|
|---|
| 5762 | return QString::null;
|
|---|
| 5763 | #ifdef Q_WS_X11
|
|---|
| 5764 | QTextCodec* codec = QTextCodec::codecForLocale();
|
|---|
| 5765 | if ( len < 0 )
|
|---|
| 5766 | len = strlen( local8Bit );
|
|---|
| 5767 | return codec
|
|---|
| 5768 | ? codec->toUnicode( local8Bit, len )
|
|---|
| 5769 | : fromLatin1( local8Bit, len );
|
|---|
| 5770 | #endif
|
|---|
| 5771 | #if defined( Q_WS_MAC )
|
|---|
| 5772 | return fromUtf8(local8Bit,len);
|
|---|
| 5773 | #endif
|
|---|
| 5774 | // Should this be OS_WIN32?
|
|---|
| 5775 | #ifdef Q_WS_WIN
|
|---|
| 5776 | if ( len >= 0 ) {
|
|---|
| 5777 | QCString s(local8Bit,len+1);
|
|---|
| 5778 | return qt_winMB2QString(s);
|
|---|
| 5779 | }
|
|---|
| 5780 | return qt_winMB2QString( local8Bit );
|
|---|
| 5781 | #endif
|
|---|
| 5782 | #ifdef Q_WS_QWS
|
|---|
| 5783 | return fromUtf8(local8Bit,len);
|
|---|
| 5784 | #endif
|
|---|
| 5785 | #endif // QT_NO_TEXTCODEC
|
|---|
| 5786 | }
|
|---|
| 5787 |
|
|---|
| 5788 | /*!
|
|---|
| 5789 | \fn QString::operator const char *() const
|
|---|
| 5790 |
|
|---|
| 5791 | Returns latin1(). Be sure to see the warnings documented in the
|
|---|
| 5792 | latin1() function. Note that for new code which you wish to be
|
|---|
| 5793 | strictly Unicode-clean, you can define the macro \c
|
|---|
| 5794 | QT_NO_ASCII_CAST when compiling your code to hide this function so
|
|---|
| 5795 | that automatic casts are not done. This has the added advantage
|
|---|
| 5796 | that you catch the programming error described in operator!().
|
|---|
| 5797 | */
|
|---|
| 5798 |
|
|---|
| 5799 | /*!
|
|---|
| 5800 | \fn QString::operator std::string() const
|
|---|
| 5801 |
|
|---|
| 5802 | Returns ascii().
|
|---|
| 5803 | */
|
|---|
| 5804 |
|
|---|
| 5805 | /*!
|
|---|
| 5806 | Returns the QString as a zero terminated array of unsigned shorts
|
|---|
| 5807 | if the string is not null; otherwise returns zero.
|
|---|
| 5808 |
|
|---|
| 5809 | The result remains valid so long as one unmodified
|
|---|
| 5810 | copy of the source string exists.
|
|---|
| 5811 | */
|
|---|
| 5812 | const unsigned short *QString::ucs2() const
|
|---|
| 5813 | {
|
|---|
| 5814 | if ( ! d->unicode )
|
|---|
| 5815 | return 0;
|
|---|
| 5816 | unsigned int len = d->len;
|
|---|
| 5817 | if ( d->maxl < len + 1 ) {
|
|---|
| 5818 | // detach, grow or shrink
|
|---|
| 5819 | uint newMax = computeNewMax( len + 1 );
|
|---|
| 5820 | QChar* nd = QT_ALLOC_QCHAR_VEC( newMax );
|
|---|
| 5821 | if ( nd ) {
|
|---|
| 5822 | if ( d->unicode )
|
|---|
| 5823 | memcpy( nd, d->unicode, sizeof(QChar)*len );
|
|---|
| 5824 | ((QString *)this)->deref();
|
|---|
| 5825 | ((QString *)this)->d = new QStringData( nd, len, newMax );
|
|---|
| 5826 | }
|
|---|
| 5827 | }
|
|---|
| 5828 | d->unicode[len] = 0;
|
|---|
| 5829 | return (unsigned short *) d->unicode;
|
|---|
| 5830 | }
|
|---|
| 5831 |
|
|---|
| 5832 | /*!
|
|---|
| 5833 | Constructs a string that is a deep copy of \a str, interpreted as a
|
|---|
| 5834 | UCS2 encoded, zero terminated, Unicode string.
|
|---|
| 5835 |
|
|---|
| 5836 | If \a str is 0, then a null string is created.
|
|---|
| 5837 |
|
|---|
| 5838 | \sa isNull()
|
|---|
| 5839 | */
|
|---|
| 5840 | QString QString::fromUcs2( const unsigned short *str )
|
|---|
| 5841 | {
|
|---|
| 5842 | if ( !str ) {
|
|---|
| 5843 | return QString::null;
|
|---|
| 5844 | } else {
|
|---|
| 5845 | int length = 0;
|
|---|
| 5846 | while ( str[length] != 0 )
|
|---|
| 5847 | length++;
|
|---|
| 5848 | QChar* uc = QT_ALLOC_QCHAR_VEC( length );
|
|---|
| 5849 | memcpy( uc, str, length*sizeof(QChar) );
|
|---|
| 5850 | return QString( new QStringData( uc, length, length ), TRUE );
|
|---|
| 5851 | }
|
|---|
| 5852 | }
|
|---|
| 5853 |
|
|---|
| 5854 | /*!
|
|---|
| 5855 | \fn QChar QString::at( uint ) const
|
|---|
| 5856 |
|
|---|
| 5857 | Returns the character at index \a i, or 0 if \a i is beyond the
|
|---|
| 5858 | length of the string.
|
|---|
| 5859 |
|
|---|
| 5860 | \code
|
|---|
| 5861 | const QString string( "abcdefgh" );
|
|---|
| 5862 | QChar ch = string.at( 4 );
|
|---|
| 5863 | // ch == 'e'
|
|---|
| 5864 | \endcode
|
|---|
| 5865 |
|
|---|
| 5866 | If the QString is not const (i.e. const QString) or const& (i.e.
|
|---|
| 5867 | const QString &), then the non-const overload of at() will be used
|
|---|
| 5868 | instead.
|
|---|
| 5869 | */
|
|---|
| 5870 |
|
|---|
| 5871 | /*!
|
|---|
| 5872 | \fn QChar QString::constref(uint i) const
|
|---|
| 5873 |
|
|---|
| 5874 | Returns the QChar at index \a i by value.
|
|---|
| 5875 |
|
|---|
| 5876 | Equivalent to at(\a i).
|
|---|
| 5877 |
|
|---|
| 5878 | \sa ref()
|
|---|
| 5879 | */
|
|---|
| 5880 |
|
|---|
| 5881 | /*!
|
|---|
| 5882 | \fn QChar& QString::ref(uint i)
|
|---|
| 5883 |
|
|---|
| 5884 | Returns the QChar at index \a i by reference, expanding the string
|
|---|
| 5885 | with QChar::null if necessary. The resulting reference can be
|
|---|
| 5886 | assigned to, or otherwise used immediately, but becomes invalid
|
|---|
| 5887 | once furher modifications are made to the string.
|
|---|
| 5888 |
|
|---|
| 5889 | \code
|
|---|
| 5890 | QString string("ABCDEF");
|
|---|
| 5891 | QChar ch = string.ref( 3 ); // ch == 'D'
|
|---|
| 5892 | \endcode
|
|---|
| 5893 |
|
|---|
| 5894 | \sa constref()
|
|---|
| 5895 | */
|
|---|
| 5896 |
|
|---|
| 5897 | /*!
|
|---|
| 5898 | \fn QChar QString::operator[]( int ) const
|
|---|
| 5899 |
|
|---|
| 5900 | Returns the character at index \a i, or QChar::null if \a i is
|
|---|
| 5901 | beyond the length of the string.
|
|---|
| 5902 |
|
|---|
| 5903 | If the QString is not const (i.e., const QString) or const\&
|
|---|
| 5904 | (i.e., const QString\&), then the non-const overload of operator[]
|
|---|
| 5905 | will be used instead.
|
|---|
| 5906 | */
|
|---|
| 5907 |
|
|---|
| 5908 | /*!
|
|---|
| 5909 | \fn QCharRef QString::operator[]( int )
|
|---|
| 5910 |
|
|---|
| 5911 | \overload
|
|---|
| 5912 |
|
|---|
| 5913 | The function returns a reference to the character at index \a i.
|
|---|
| 5914 | The resulting reference can then be assigned to, or used
|
|---|
| 5915 | immediately, but it will become invalid once further modifications
|
|---|
| 5916 | are made to the original string.
|
|---|
| 5917 |
|
|---|
| 5918 | If \a i is beyond the length of the string then the string is
|
|---|
| 5919 | expanded with QChar::nulls, so that the QCharRef references a
|
|---|
| 5920 | valid (null) character in the string.
|
|---|
| 5921 |
|
|---|
| 5922 | The QCharRef internal class can be used much like a constant
|
|---|
| 5923 | QChar, but if you assign to it, you change the original string
|
|---|
| 5924 | (which will detach itself because of QString's copy-on-write
|
|---|
| 5925 | semantics). You will get compilation errors if you try to use the
|
|---|
| 5926 | result as anything but a QChar.
|
|---|
| 5927 | */
|
|---|
| 5928 |
|
|---|
| 5929 | /*!
|
|---|
| 5930 | \fn QCharRef QString::at( uint i )
|
|---|
| 5931 |
|
|---|
| 5932 | \overload
|
|---|
| 5933 |
|
|---|
| 5934 | The function returns a reference to the character at index \a i.
|
|---|
| 5935 | The resulting reference can then be assigned to, or used
|
|---|
| 5936 | immediately, but it will become invalid once further modifications
|
|---|
| 5937 | are made to the original string.
|
|---|
| 5938 |
|
|---|
| 5939 | If \a i is beyond the length of the string then the string is
|
|---|
| 5940 | expanded with QChar::null.
|
|---|
| 5941 | */
|
|---|
| 5942 |
|
|---|
| 5943 | /*
|
|---|
| 5944 | Internal chunk of code to handle the
|
|---|
| 5945 | uncommon cases of at() above.
|
|---|
| 5946 | */
|
|---|
| 5947 | void QString::subat( uint i )
|
|---|
| 5948 | {
|
|---|
| 5949 | uint olen = d->len;
|
|---|
| 5950 | if ( i >= olen ) {
|
|---|
| 5951 | setLength( i+1 ); // i is index; i+1 is needed length
|
|---|
| 5952 | for ( uint j=olen; j<=i; j++ )
|
|---|
| 5953 | d->unicode[j] = QChar::null;
|
|---|
| 5954 | } else {
|
|---|
| 5955 | // Just be sure to detach
|
|---|
| 5956 | real_detach();
|
|---|
| 5957 | }
|
|---|
| 5958 | }
|
|---|
| 5959 |
|
|---|
| 5960 |
|
|---|
| 5961 | /*!
|
|---|
| 5962 | Resizes the string to \a len characters and copies \a unicode into
|
|---|
| 5963 | the string. If \a unicode is 0, nothing is copied, but the
|
|---|
| 5964 | string is still resized to \a len. If \a len is zero, then the
|
|---|
| 5965 | string becomes a \link isNull() null\endlink string.
|
|---|
| 5966 |
|
|---|
| 5967 | \sa setLatin1(), isNull()
|
|---|
| 5968 | */
|
|---|
| 5969 |
|
|---|
| 5970 | QString& QString::setUnicode( const QChar *unicode, uint len )
|
|---|
| 5971 | {
|
|---|
| 5972 | if ( len == 0 ) { // set to null string
|
|---|
| 5973 | if ( d != shared_null ) { // beware of nullstring being set to nullstring
|
|---|
| 5974 | deref();
|
|---|
| 5975 | d = shared_null ? shared_null : makeSharedNull();
|
|---|
| 5976 | d->ref();
|
|---|
| 5977 | }
|
|---|
| 5978 | } else if ( d->count != 1 || len > d->maxl ||
|
|---|
| 5979 | ( len * 4 < d->maxl && d->maxl > 4 ) ) {
|
|---|
| 5980 | // detach, grown or shrink
|
|---|
| 5981 | uint newMax = computeNewMax( len );
|
|---|
| 5982 | QChar* nd = QT_ALLOC_QCHAR_VEC( newMax );
|
|---|
| 5983 | if ( unicode )
|
|---|
| 5984 | memcpy( nd, unicode, sizeof(QChar)*len );
|
|---|
| 5985 | deref();
|
|---|
| 5986 | d = new QStringData( nd, len, newMax );
|
|---|
| 5987 | } else {
|
|---|
| 5988 | d->len = len;
|
|---|
| 5989 | d->setDirty();
|
|---|
| 5990 | if ( unicode )
|
|---|
| 5991 | memcpy( d->unicode, unicode, sizeof(QChar)*len );
|
|---|
| 5992 | }
|
|---|
| 5993 | return *this;
|
|---|
| 5994 | }
|
|---|
| 5995 |
|
|---|
| 5996 | /*!
|
|---|
| 5997 | Resizes the string to \a len characters and copies \a
|
|---|
| 5998 | unicode_as_ushorts into the string (on some X11 client platforms
|
|---|
| 5999 | this will involve a byte-swapping pass).
|
|---|
| 6000 |
|
|---|
| 6001 | If \a unicode_as_ushorts is 0, nothing is copied, but the string
|
|---|
| 6002 | is still resized to \a len. If \a len is zero, the string becomes
|
|---|
| 6003 | a \link isNull() null\endlink string.
|
|---|
| 6004 |
|
|---|
| 6005 | \sa setLatin1(), isNull()
|
|---|
| 6006 | */
|
|---|
| 6007 | QString& QString::setUnicodeCodes( const ushort* unicode_as_ushorts, uint len )
|
|---|
| 6008 | {
|
|---|
| 6009 | return setUnicode((const QChar*)unicode_as_ushorts, len);
|
|---|
| 6010 | }
|
|---|
| 6011 |
|
|---|
| 6012 |
|
|---|
| 6013 | /*!
|
|---|
| 6014 | Sets this string to \a str, interpreted as a classic 8-bit ASCII C
|
|---|
| 6015 | string. If \a len is -1 (the default), then it is set to
|
|---|
| 6016 | strlen(str).
|
|---|
| 6017 |
|
|---|
| 6018 | If \a str is 0 a null string is created. If \a str is "", an empty
|
|---|
| 6019 | string is created.
|
|---|
| 6020 |
|
|---|
| 6021 | \sa isNull(), isEmpty()
|
|---|
| 6022 | */
|
|---|
| 6023 |
|
|---|
| 6024 | QString &QString::setAscii( const char *str, int len )
|
|---|
| 6025 | {
|
|---|
| 6026 | #ifndef QT_NO_TEXTCODEC
|
|---|
| 6027 | if ( QTextCodec::codecForCStrings() ) {
|
|---|
| 6028 | *this = QString::fromAscii( str, len );
|
|---|
| 6029 | return *this;
|
|---|
| 6030 | }
|
|---|
| 6031 | #endif // QT_NO_TEXTCODEC
|
|---|
| 6032 | return setLatin1( str, len );
|
|---|
| 6033 | }
|
|---|
| 6034 |
|
|---|
| 6035 | /*!
|
|---|
| 6036 | Sets this string to \a str, interpreted as a classic Latin-1 C
|
|---|
| 6037 | string. If \a len is -1 (the default), then it is set to
|
|---|
| 6038 | strlen(str).
|
|---|
| 6039 |
|
|---|
| 6040 | If \a str is 0 a null string is created. If \a str is "", an empty
|
|---|
| 6041 | string is created.
|
|---|
| 6042 |
|
|---|
| 6043 | \sa isNull(), isEmpty()
|
|---|
| 6044 | */
|
|---|
| 6045 |
|
|---|
| 6046 | QString &QString::setLatin1( const char *str, int len )
|
|---|
| 6047 | {
|
|---|
| 6048 | if ( str == 0 )
|
|---|
| 6049 | return setUnicode(0,0);
|
|---|
| 6050 | if ( len < 0 )
|
|---|
| 6051 | len = strlen( str );
|
|---|
| 6052 | if ( len == 0 ) { // won't make a null string
|
|---|
| 6053 | *this = QString::fromLatin1( "" );
|
|---|
| 6054 | } else {
|
|---|
| 6055 | setUnicode( 0, len ); // resize but not copy
|
|---|
| 6056 | QChar *p = d->unicode;
|
|---|
| 6057 | while ( len-- )
|
|---|
| 6058 | *p++ = *str++;
|
|---|
| 6059 | }
|
|---|
| 6060 | return *this;
|
|---|
| 6061 | }
|
|---|
| 6062 |
|
|---|
| 6063 | /*! \internal
|
|---|
| 6064 | */
|
|---|
| 6065 | void QString::checkSimpleText() const
|
|---|
| 6066 | {
|
|---|
| 6067 | QChar *p = d->unicode;
|
|---|
| 6068 | QChar *end = p + d->len;
|
|---|
| 6069 | while ( p < end ) {
|
|---|
| 6070 | ushort uc = p->unicode();
|
|---|
| 6071 | // sort out regions of complex text formatting
|
|---|
| 6072 | if ( uc > 0x058f && ( uc < 0x1100 || uc > 0xfb0f ) ) {
|
|---|
| 6073 | d->issimpletext = FALSE;
|
|---|
| 6074 | return;
|
|---|
| 6075 | }
|
|---|
| 6076 | p++;
|
|---|
| 6077 | }
|
|---|
| 6078 | d->issimpletext = TRUE;
|
|---|
| 6079 | }
|
|---|
| 6080 |
|
|---|
| 6081 | /*! \fn bool QString::simpleText() const
|
|---|
| 6082 | \internal
|
|---|
| 6083 | */
|
|---|
| 6084 |
|
|---|
| 6085 | /*! \internal
|
|---|
| 6086 | */
|
|---|
| 6087 | bool QString::isRightToLeft() const
|
|---|
| 6088 | {
|
|---|
| 6089 | int len = length();
|
|---|
| 6090 | QChar *p = d->unicode;
|
|---|
| 6091 | while ( len-- ) {
|
|---|
| 6092 | switch( ::direction( *p ) )
|
|---|
| 6093 | {
|
|---|
| 6094 | case QChar::DirL:
|
|---|
| 6095 | case QChar::DirLRO:
|
|---|
| 6096 | case QChar::DirLRE:
|
|---|
| 6097 | return FALSE;
|
|---|
| 6098 | case QChar::DirR:
|
|---|
| 6099 | case QChar::DirAL:
|
|---|
| 6100 | case QChar::DirRLO:
|
|---|
| 6101 | case QChar::DirRLE:
|
|---|
| 6102 | return TRUE;
|
|---|
| 6103 | default:
|
|---|
| 6104 | break;
|
|---|
| 6105 | }
|
|---|
| 6106 | ++p;
|
|---|
| 6107 | }
|
|---|
| 6108 | return FALSE;
|
|---|
| 6109 | }
|
|---|
| 6110 |
|
|---|
| 6111 |
|
|---|
| 6112 | /*!
|
|---|
| 6113 | \fn int QString::compare( const QString & s1, const QString & s2 )
|
|---|
| 6114 |
|
|---|
| 6115 | Lexically compares \a s1 with \a s2 and returns an integer less
|
|---|
| 6116 | than, equal to, or greater than zero if \a s1 is less than, equal
|
|---|
| 6117 | to, or greater than \a s2.
|
|---|
| 6118 |
|
|---|
| 6119 | The comparison is based exclusively on the numeric Unicode values
|
|---|
| 6120 | of the characters and is very fast, but is not what a human would
|
|---|
| 6121 | expect. Consider sorting user-interface strings with
|
|---|
| 6122 | QString::localeAwareCompare().
|
|---|
| 6123 |
|
|---|
| 6124 | \code
|
|---|
| 6125 | int a = QString::compare( "def", "abc" ); // a > 0
|
|---|
| 6126 | int b = QString::compare( "abc", "def" ); // b < 0
|
|---|
| 6127 | int c = QString::compare( "abc", "abc" ); // c == 0
|
|---|
| 6128 | \endcode
|
|---|
| 6129 | */
|
|---|
| 6130 |
|
|---|
| 6131 | /*!
|
|---|
| 6132 | \overload
|
|---|
| 6133 |
|
|---|
| 6134 | Lexically compares this string with \a s and returns an integer
|
|---|
| 6135 | less than, equal to, or greater than zero if it is less than, equal
|
|---|
| 6136 | to, or greater than \a s.
|
|---|
| 6137 | */
|
|---|
| 6138 | int QString::compare( const QString& s ) const
|
|---|
| 6139 | {
|
|---|
| 6140 | return ucstrcmp( *this, s );
|
|---|
| 6141 | }
|
|---|
| 6142 |
|
|---|
| 6143 | /*!
|
|---|
| 6144 | \fn int QString::localeAwareCompare( const QString & s1, const QString & s2 )
|
|---|
| 6145 |
|
|---|
| 6146 | Compares \a s1 with \a s2 and returns an integer less than, equal
|
|---|
| 6147 | to, or greater than zero if \a s1 is less than, equal to, or
|
|---|
| 6148 | greater than \a s2.
|
|---|
| 6149 |
|
|---|
| 6150 | The comparison is performed in a locale- and also
|
|---|
| 6151 | platform-dependent manner. Use this function to present sorted
|
|---|
| 6152 | lists of strings to the user.
|
|---|
| 6153 |
|
|---|
| 6154 | \sa QString::compare() QTextCodec::locale()
|
|---|
| 6155 | */
|
|---|
| 6156 |
|
|---|
| 6157 | /*!
|
|---|
| 6158 | \overload
|
|---|
| 6159 |
|
|---|
| 6160 | Compares this string with \a s.
|
|---|
| 6161 | */
|
|---|
| 6162 |
|
|---|
| 6163 | #if !defined(CSTR_LESS_THAN)
|
|---|
| 6164 | #define CSTR_LESS_THAN 1
|
|---|
| 6165 | #define CSTR_EQUAL 2
|
|---|
| 6166 | #define CSTR_GREATER_THAN 3
|
|---|
| 6167 | #endif
|
|---|
| 6168 |
|
|---|
| 6169 | int QString::localeAwareCompare( const QString& s ) const
|
|---|
| 6170 | {
|
|---|
| 6171 | // do the right thing for null and empty
|
|---|
| 6172 | if ( isEmpty() || s.isEmpty() )
|
|---|
| 6173 | return compare( s );
|
|---|
| 6174 |
|
|---|
| 6175 | #if defined(Q_WS_WIN)
|
|---|
| 6176 | int res;
|
|---|
| 6177 | QT_WA( {
|
|---|
| 6178 | const TCHAR* s1 = (TCHAR*)ucs2();
|
|---|
| 6179 | const TCHAR* s2 = (TCHAR*)s.ucs2();
|
|---|
| 6180 | res = CompareStringW( LOCALE_USER_DEFAULT, 0, s1, length(), s2, s.length() );
|
|---|
| 6181 | } , {
|
|---|
| 6182 | QCString s1 = local8Bit();
|
|---|
| 6183 | QCString s2 = s.local8Bit();
|
|---|
| 6184 | res = CompareStringA( LOCALE_USER_DEFAULT, 0, s1.data(), s1.length(), s2.data(), s2.length() );
|
|---|
| 6185 | } );
|
|---|
| 6186 |
|
|---|
| 6187 | switch ( res ) {
|
|---|
| 6188 | case CSTR_LESS_THAN:
|
|---|
| 6189 | return -1;
|
|---|
| 6190 | case CSTR_GREATER_THAN:
|
|---|
| 6191 | return 1;
|
|---|
| 6192 | default:
|
|---|
| 6193 | return 0;
|
|---|
| 6194 | }
|
|---|
| 6195 | #elif defined(Q_WS_MACX)
|
|---|
| 6196 | int delta = 0;
|
|---|
| 6197 | #if !defined(QT_NO_TEXTCODEC)
|
|---|
| 6198 | QTextCodec *codec = QTextCodec::codecForLocale();
|
|---|
| 6199 | if (codec)
|
|---|
| 6200 | delta = strcoll(codec->fromUnicode(*this), codec->fromUnicode(s));
|
|---|
| 6201 | if (delta == 0)
|
|---|
| 6202 | #endif
|
|---|
| 6203 | delta = ucstrcmp(*this, s);
|
|---|
| 6204 | return delta;
|
|---|
| 6205 | #elif defined(Q_WS_X11)
|
|---|
| 6206 | // declared in <string.h>
|
|---|
| 6207 | int delta = strcoll( local8Bit(), s.local8Bit() );
|
|---|
| 6208 | if ( delta == 0 )
|
|---|
| 6209 | delta = ucstrcmp( *this, s );
|
|---|
| 6210 | return delta;
|
|---|
| 6211 | #else
|
|---|
| 6212 | return ucstrcmp( *this, s );
|
|---|
| 6213 | #endif
|
|---|
| 6214 | }
|
|---|
| 6215 |
|
|---|
| 6216 | bool operator==( const QString &s1, const QString &s2 )
|
|---|
| 6217 | {
|
|---|
| 6218 | if ( s1.unicode() == s2.unicode() )
|
|---|
| 6219 | return TRUE;
|
|---|
| 6220 | return (s1.length() == s2.length()) && s1.isNull() == s2.isNull() &&
|
|---|
| 6221 | (memcmp((char*)s1.unicode(),(char*)s2.unicode(),
|
|---|
| 6222 | s1.length()*sizeof(QChar)) == 0 );
|
|---|
| 6223 | }
|
|---|
| 6224 |
|
|---|
| 6225 | bool operator!=( const QString &s1, const QString &s2 )
|
|---|
| 6226 | { return !(s1==s2); }
|
|---|
| 6227 |
|
|---|
| 6228 | bool operator<( const QString &s1, const QString &s2 )
|
|---|
| 6229 | { return ucstrcmp(s1,s2) < 0; }
|
|---|
| 6230 |
|
|---|
| 6231 | bool operator<=( const QString &s1, const QString &s2 )
|
|---|
| 6232 | { return ucstrcmp(s1,s2) <= 0; }
|
|---|
| 6233 |
|
|---|
| 6234 | bool operator>( const QString &s1, const QString &s2 )
|
|---|
| 6235 | { return ucstrcmp(s1,s2) > 0; }
|
|---|
| 6236 |
|
|---|
| 6237 | bool operator>=( const QString &s1, const QString &s2 )
|
|---|
| 6238 | { return ucstrcmp(s1,s2) >= 0; }
|
|---|
| 6239 |
|
|---|
| 6240 |
|
|---|
| 6241 | bool operator==( const QString &s1, const char *s2 )
|
|---|
| 6242 | {
|
|---|
| 6243 | if ( !s2 )
|
|---|
| 6244 | return s1.isNull();
|
|---|
| 6245 |
|
|---|
| 6246 | int len = s1.length();
|
|---|
| 6247 | const QChar *uc = s1.unicode();
|
|---|
| 6248 | while ( len ) {
|
|---|
| 6249 | if ( !(*s2) || uc->unicode() != (uchar) *s2 )
|
|---|
| 6250 | return FALSE;
|
|---|
| 6251 | ++uc;
|
|---|
| 6252 | ++s2;
|
|---|
| 6253 | --len;
|
|---|
| 6254 | }
|
|---|
| 6255 | return !*s2;
|
|---|
| 6256 | }
|
|---|
| 6257 |
|
|---|
| 6258 | bool operator==( const char *s1, const QString &s2 )
|
|---|
| 6259 | { return (s2 == s1); }
|
|---|
| 6260 |
|
|---|
| 6261 | bool operator!=( const QString &s1, const char *s2 )
|
|---|
| 6262 | { return !(s1==s2); }
|
|---|
| 6263 |
|
|---|
| 6264 | bool operator!=( const char *s1, const QString &s2 )
|
|---|
| 6265 | { return !(s1==s2); }
|
|---|
| 6266 |
|
|---|
| 6267 | bool operator<( const QString &s1, const char *s2 )
|
|---|
| 6268 | { return ucstrcmp(s1,s2) < 0; }
|
|---|
| 6269 |
|
|---|
| 6270 | bool operator<( const char *s1, const QString &s2 )
|
|---|
| 6271 | { return ucstrcmp(s1,s2) < 0; }
|
|---|
| 6272 |
|
|---|
| 6273 | bool operator<=( const QString &s1, const char *s2 )
|
|---|
| 6274 | { return ucstrcmp(s1,s2) <= 0; }
|
|---|
| 6275 |
|
|---|
| 6276 | bool operator<=( const char *s1, const QString &s2 )
|
|---|
| 6277 | { return ucstrcmp(s1,s2) <= 0; }
|
|---|
| 6278 |
|
|---|
| 6279 | bool operator>( const QString &s1, const char *s2 )
|
|---|
| 6280 | { return ucstrcmp(s1,s2) > 0; }
|
|---|
| 6281 |
|
|---|
| 6282 | bool operator>( const char *s1, const QString &s2 )
|
|---|
| 6283 | { return ucstrcmp(s1,s2) > 0; }
|
|---|
| 6284 |
|
|---|
| 6285 | bool operator>=( const QString &s1, const char *s2 )
|
|---|
| 6286 | { return ucstrcmp(s1,s2) >= 0; }
|
|---|
| 6287 |
|
|---|
| 6288 | bool operator>=( const char *s1, const QString &s2 )
|
|---|
| 6289 | { return ucstrcmp(s1,s2) >= 0; }
|
|---|
| 6290 |
|
|---|
| 6291 |
|
|---|
| 6292 | /*****************************************************************************
|
|---|
| 6293 | Documentation for QString related functions
|
|---|
| 6294 | *****************************************************************************/
|
|---|
| 6295 |
|
|---|
| 6296 | /*!
|
|---|
| 6297 | \fn bool operator==( const QString &s1, const QString &s2 )
|
|---|
| 6298 |
|
|---|
| 6299 | \relates QString
|
|---|
| 6300 |
|
|---|
| 6301 | Returns TRUE if \a s1 is equal to \a s2; otherwise returns FALSE.
|
|---|
| 6302 | Note that a null string is not equal to a not-null empty string.
|
|---|
| 6303 |
|
|---|
| 6304 | Equivalent to compare(\a s1, \a s2) != 0.
|
|---|
| 6305 |
|
|---|
| 6306 | \sa isNull(), isEmpty()
|
|---|
| 6307 | */
|
|---|
| 6308 |
|
|---|
| 6309 | /*!
|
|---|
| 6310 | \fn bool operator==( const QString &s1, const char *s2 )
|
|---|
| 6311 |
|
|---|
| 6312 | \overload
|
|---|
| 6313 | \relates QString
|
|---|
| 6314 |
|
|---|
| 6315 | Returns TRUE if \a s1 is equal to \a s2; otherwise returns FALSE.
|
|---|
| 6316 | Note that a null string is not equal to a not-null empty string.
|
|---|
| 6317 |
|
|---|
| 6318 | Equivalent to compare(\a s1, \a s2) == 0.
|
|---|
| 6319 |
|
|---|
| 6320 | \sa isNull(), isEmpty()
|
|---|
| 6321 | */
|
|---|
| 6322 |
|
|---|
| 6323 | /*!
|
|---|
| 6324 | \fn bool operator==( const char *s1, const QString &s2 )
|
|---|
| 6325 |
|
|---|
| 6326 | \overload
|
|---|
| 6327 | \relates QString
|
|---|
| 6328 |
|
|---|
| 6329 | Returns TRUE if \a s1 is equal to \a s2; otherwise returns FALSE.
|
|---|
| 6330 | Note that a null string is not equal to a not-null empty string.
|
|---|
| 6331 |
|
|---|
| 6332 | Equivalent to compare(\a s1, \a s2) == 0.
|
|---|
| 6333 |
|
|---|
| 6334 | \sa isNull(), isEmpty()
|
|---|
| 6335 | */
|
|---|
| 6336 |
|
|---|
| 6337 | /*!
|
|---|
| 6338 | \fn bool operator!=( const QString &s1, const QString &s2 )
|
|---|
| 6339 |
|
|---|
| 6340 | \relates QString
|
|---|
| 6341 |
|
|---|
| 6342 | Returns TRUE if \a s1 is not equal to \a s2; otherwise returns FALSE.
|
|---|
| 6343 | Note that a null string is not equal to a not-null empty string.
|
|---|
| 6344 |
|
|---|
| 6345 | Equivalent to compare(\a s1, \a s2) != 0.
|
|---|
| 6346 |
|
|---|
| 6347 | \sa isNull(), isEmpty()
|
|---|
| 6348 | */
|
|---|
| 6349 |
|
|---|
| 6350 | /*!
|
|---|
| 6351 | \fn bool operator!=( const QString &s1, const char *s2 )
|
|---|
| 6352 |
|
|---|
| 6353 | \overload
|
|---|
| 6354 | \relates QString
|
|---|
| 6355 |
|
|---|
| 6356 | Returns TRUE if \a s1 is not equal to \a s2; otherwise returns FALSE.
|
|---|
| 6357 | Note that a null string is not equal to a not-null empty string.
|
|---|
| 6358 |
|
|---|
| 6359 | Equivalent to compare(\a s1, \a s2) != 0.
|
|---|
| 6360 |
|
|---|
| 6361 | \sa isNull(), isEmpty()
|
|---|
| 6362 | */
|
|---|
| 6363 |
|
|---|
| 6364 | /*!
|
|---|
| 6365 | \fn bool operator!=( const char *s1, const QString &s2 )
|
|---|
| 6366 |
|
|---|
| 6367 | \overload
|
|---|
| 6368 | \relates QString
|
|---|
| 6369 |
|
|---|
| 6370 | Returns TRUE if \a s1 is not equal to \a s2; otherwise returns FALSE.
|
|---|
| 6371 | Note that a null string is not equal to a not-null empty string.
|
|---|
| 6372 |
|
|---|
| 6373 | Equivalent to compare(\a s1, \a s2) != 0.
|
|---|
| 6374 |
|
|---|
| 6375 | \sa isNull(), isEmpty()
|
|---|
| 6376 | */
|
|---|
| 6377 |
|
|---|
| 6378 | /*!
|
|---|
| 6379 | \fn bool operator<( const QString &s1, const char *s2 )
|
|---|
| 6380 |
|
|---|
| 6381 | \relates QString
|
|---|
| 6382 |
|
|---|
| 6383 | Returns TRUE if \a s1 is lexically less than \a s2; otherwise returns FALSE.
|
|---|
| 6384 | The comparison is case sensitive.
|
|---|
| 6385 |
|
|---|
| 6386 | Equivalent to compare(\a s1, \a s2) \< 0.
|
|---|
| 6387 | */
|
|---|
| 6388 |
|
|---|
| 6389 | /*!
|
|---|
| 6390 | \fn bool operator<( const char *s1, const QString &s2 )
|
|---|
| 6391 |
|
|---|
| 6392 | \overload
|
|---|
| 6393 | \relates QString
|
|---|
| 6394 |
|
|---|
| 6395 | Returns TRUE if \a s1 is lexically less than \a s2; otherwise returns FALSE.
|
|---|
| 6396 | The comparison is case sensitive.
|
|---|
| 6397 |
|
|---|
| 6398 | Equivalent to compare(\a s1, \a s2) \< 0.
|
|---|
| 6399 | */
|
|---|
| 6400 |
|
|---|
| 6401 | /*!
|
|---|
| 6402 | \fn bool operator<=( const QString &s1, const char *s2 )
|
|---|
| 6403 |
|
|---|
| 6404 | \relates QString
|
|---|
| 6405 |
|
|---|
| 6406 | Returns TRUE if \a s1 is lexically less than or equal to \a s2;
|
|---|
| 6407 | otherwise returns FALSE.
|
|---|
| 6408 | The comparison is case sensitive.
|
|---|
| 6409 | Note that a null string is not equal to a not-null empty string.
|
|---|
| 6410 |
|
|---|
| 6411 | Equivalent to compare(\a s1,\a s2) \<= 0.
|
|---|
| 6412 |
|
|---|
| 6413 | \sa isNull(), isEmpty()
|
|---|
| 6414 | */
|
|---|
| 6415 |
|
|---|
| 6416 | /*!
|
|---|
| 6417 | \fn bool operator<=( const char *s1, const QString &s2 )
|
|---|
| 6418 |
|
|---|
| 6419 | \overload
|
|---|
| 6420 | \relates QString
|
|---|
| 6421 |
|
|---|
| 6422 | Returns TRUE if \a s1 is lexically less than or equal to \a s2;
|
|---|
| 6423 | otherwise returns FALSE.
|
|---|
| 6424 | The comparison is case sensitive.
|
|---|
| 6425 | Note that a null string is not equal to a not-null empty string.
|
|---|
| 6426 |
|
|---|
| 6427 | Equivalent to compare(\a s1, \a s2) \<= 0.
|
|---|
| 6428 |
|
|---|
| 6429 | \sa isNull(), isEmpty()
|
|---|
| 6430 | */
|
|---|
| 6431 |
|
|---|
| 6432 | /*!
|
|---|
| 6433 | \fn bool operator>( const QString &s1, const char *s2 )
|
|---|
| 6434 |
|
|---|
| 6435 | \relates QString
|
|---|
| 6436 |
|
|---|
| 6437 | Returns TRUE if \a s1 is lexically greater than \a s2; otherwise
|
|---|
| 6438 | returns FALSE.
|
|---|
| 6439 | The comparison is case sensitive.
|
|---|
| 6440 |
|
|---|
| 6441 | Equivalent to compare(\a s1, \a s2) \> 0.
|
|---|
| 6442 | */
|
|---|
| 6443 |
|
|---|
| 6444 | /*!
|
|---|
| 6445 | \fn bool operator>( const char *s1, const QString &s2 )
|
|---|
| 6446 |
|
|---|
| 6447 | \overload
|
|---|
| 6448 | \relates QString
|
|---|
| 6449 |
|
|---|
| 6450 | Returns TRUE if \a s1 is lexically greater than \a s2; otherwise
|
|---|
| 6451 | returns FALSE.
|
|---|
| 6452 | The comparison is case sensitive.
|
|---|
| 6453 |
|
|---|
| 6454 | Equivalent to compare(\a s1, \a s2) \> 0.
|
|---|
| 6455 | */
|
|---|
| 6456 |
|
|---|
| 6457 | /*!
|
|---|
| 6458 | \fn bool operator>=( const QString &s1, const char *s2 )
|
|---|
| 6459 |
|
|---|
| 6460 | \relates QString
|
|---|
| 6461 |
|
|---|
| 6462 | Returns TRUE if \a s1 is lexically greater than or equal to \a s2;
|
|---|
| 6463 | otherwise returns FALSE.
|
|---|
| 6464 | The comparison is case sensitive.
|
|---|
| 6465 | Note that a null string is not equal to a not-null empty string.
|
|---|
| 6466 |
|
|---|
| 6467 | Equivalent to compare(\a s1, \a s2) \>= 0.
|
|---|
| 6468 |
|
|---|
| 6469 | \sa isNull(), isEmpty()
|
|---|
| 6470 | */
|
|---|
| 6471 |
|
|---|
| 6472 | /*!
|
|---|
| 6473 | \fn bool operator>=( const char *s1, const QString &s2 )
|
|---|
| 6474 |
|
|---|
| 6475 | \overload
|
|---|
| 6476 | \relates QString
|
|---|
| 6477 |
|
|---|
| 6478 | Returns TRUE if \a s1 is lexically greater than or equal to \a s2;
|
|---|
| 6479 | otherwise returns FALSE.
|
|---|
| 6480 | The comparison is case sensitive.
|
|---|
| 6481 | Note that a null string is not equal to a not-null empty string.
|
|---|
| 6482 |
|
|---|
| 6483 | Equivalent to compare(\a s1, \a s2) \>= 0.
|
|---|
| 6484 |
|
|---|
| 6485 | \sa isNull(), isEmpty()
|
|---|
| 6486 | */
|
|---|
| 6487 |
|
|---|
| 6488 | /*!
|
|---|
| 6489 | \fn const QString operator+( const QString &s1, const QString &s2 )
|
|---|
| 6490 |
|
|---|
| 6491 | \relates QString
|
|---|
| 6492 |
|
|---|
| 6493 | Returns a string which is the result of concatenating the string
|
|---|
| 6494 | \a s1 and the string \a s2.
|
|---|
| 6495 |
|
|---|
| 6496 | Equivalent to \a {s1}.append(\a s2).
|
|---|
| 6497 | */
|
|---|
| 6498 |
|
|---|
| 6499 | /*!
|
|---|
| 6500 | \fn const QString operator+( const QString &s1, const char *s2 )
|
|---|
| 6501 |
|
|---|
| 6502 | \overload
|
|---|
| 6503 | \relates QString
|
|---|
| 6504 |
|
|---|
| 6505 | Returns a string which is the result of concatenating the string
|
|---|
| 6506 | \a s1 and character \a s2.
|
|---|
| 6507 |
|
|---|
| 6508 | Equivalent to \a {s1}.append(\a s2).
|
|---|
| 6509 | */
|
|---|
| 6510 |
|
|---|
| 6511 | /*!
|
|---|
| 6512 | \fn const QString operator+( const char *s1, const QString &s2 )
|
|---|
| 6513 |
|
|---|
| 6514 | \overload
|
|---|
| 6515 | \relates QString
|
|---|
| 6516 |
|
|---|
| 6517 | Returns a string which is the result of concatenating the
|
|---|
| 6518 | character \a s1 and string \a s2.
|
|---|
| 6519 | */
|
|---|
| 6520 |
|
|---|
| 6521 | /*!
|
|---|
| 6522 | \fn const QString operator+( const QString &s, char c )
|
|---|
| 6523 |
|
|---|
| 6524 | \overload
|
|---|
| 6525 | \relates QString
|
|---|
| 6526 |
|
|---|
| 6527 | Returns a string which is the result of concatenating the string
|
|---|
| 6528 | \a s and character \a c.
|
|---|
| 6529 |
|
|---|
| 6530 | Equivalent to \a {s}.append(\a c).
|
|---|
| 6531 | */
|
|---|
| 6532 |
|
|---|
| 6533 | /*!
|
|---|
| 6534 | \fn const QString operator+( char c, const QString &s )
|
|---|
| 6535 |
|
|---|
| 6536 | \overload
|
|---|
| 6537 | \relates QString
|
|---|
| 6538 |
|
|---|
| 6539 | Returns a string which is the result of concatenating the
|
|---|
| 6540 | character \a c and string \a s.
|
|---|
| 6541 |
|
|---|
| 6542 | Equivalent to \a {s}.prepend(\a c).
|
|---|
| 6543 | */
|
|---|
| 6544 |
|
|---|
| 6545 |
|
|---|
| 6546 | /*****************************************************************************
|
|---|
| 6547 | QString stream functions
|
|---|
| 6548 | *****************************************************************************/
|
|---|
| 6549 | #ifndef QT_NO_DATASTREAM
|
|---|
| 6550 | /*!
|
|---|
| 6551 | \relates QString
|
|---|
| 6552 |
|
|---|
| 6553 | Writes the string \a str to the stream \a s.
|
|---|
| 6554 |
|
|---|
| 6555 | See also \link datastreamformat.html Format of the QDataStream operators \endlink
|
|---|
| 6556 | */
|
|---|
| 6557 |
|
|---|
| 6558 | QDataStream &operator<<( QDataStream &s, const QString &str )
|
|---|
| 6559 | {
|
|---|
| 6560 | if ( s.version() == 1 ) {
|
|---|
| 6561 | QCString l( str.latin1() );
|
|---|
| 6562 | s << l;
|
|---|
| 6563 | }
|
|---|
| 6564 | else {
|
|---|
| 6565 | int byteOrder = s.byteOrder();
|
|---|
| 6566 | const QChar* ub = str.unicode();
|
|---|
| 6567 | if ( ub || s.version() < 3 ) {
|
|---|
| 6568 | static const uint auto_size = 1024;
|
|---|
| 6569 | char t[auto_size];
|
|---|
| 6570 | char *b;
|
|---|
| 6571 | if ( str.length()*sizeof(QChar) > auto_size ) {
|
|---|
| 6572 | b = new char[str.length()*sizeof(QChar)];
|
|---|
| 6573 | } else {
|
|---|
| 6574 | b = t;
|
|---|
| 6575 | }
|
|---|
| 6576 | int l = str.length();
|
|---|
| 6577 | char *c=b;
|
|---|
| 6578 | while ( l-- ) {
|
|---|
| 6579 | if ( byteOrder == QDataStream::BigEndian ) {
|
|---|
| 6580 | *c++ = (char)ub->row();
|
|---|
| 6581 | *c++ = (char)ub->cell();
|
|---|
| 6582 | } else {
|
|---|
| 6583 | *c++ = (char)ub->cell();
|
|---|
| 6584 | *c++ = (char)ub->row();
|
|---|
| 6585 | }
|
|---|
| 6586 | ub++;
|
|---|
| 6587 | }
|
|---|
| 6588 | s.writeBytes( b, sizeof(QChar)*str.length() );
|
|---|
| 6589 | if ( str.length()*sizeof(QChar) > auto_size )
|
|---|
| 6590 | delete [] b;
|
|---|
| 6591 | } else {
|
|---|
| 6592 | // write null marker
|
|---|
| 6593 | s << (Q_UINT32)0xffffffff;
|
|---|
| 6594 | }
|
|---|
| 6595 | }
|
|---|
| 6596 | return s;
|
|---|
| 6597 | }
|
|---|
| 6598 |
|
|---|
| 6599 | /*!
|
|---|
| 6600 | \relates QString
|
|---|
| 6601 |
|
|---|
| 6602 | Reads a string from the stream \a s into string \a str.
|
|---|
| 6603 |
|
|---|
| 6604 | See also \link datastreamformat.html Format of the QDataStream operators \endlink
|
|---|
| 6605 | */
|
|---|
| 6606 |
|
|---|
| 6607 | QDataStream &operator>>( QDataStream &s, QString &str )
|
|---|
| 6608 | {
|
|---|
| 6609 | #ifdef QT_QSTRING_UCS_4
|
|---|
| 6610 | #if defined(Q_CC_GNU)
|
|---|
| 6611 | #warning "operator>> not working properly"
|
|---|
| 6612 | #endif
|
|---|
| 6613 | #endif
|
|---|
| 6614 | if ( s.version() == 1 ) {
|
|---|
| 6615 | QCString l;
|
|---|
| 6616 | s >> l;
|
|---|
| 6617 | str = QString( l );
|
|---|
| 6618 | }
|
|---|
| 6619 | else {
|
|---|
| 6620 | Q_UINT32 bytes;
|
|---|
| 6621 | s >> bytes; // read size of string
|
|---|
| 6622 | if ( bytes == 0xffffffff ) { // null string
|
|---|
| 6623 | str = QString::null;
|
|---|
| 6624 | } else if ( bytes > 0 ) { // not empty
|
|---|
| 6625 | int byteOrder = s.byteOrder();
|
|---|
| 6626 | str.setLength( bytes/2 );
|
|---|
| 6627 | QChar* ch = str.d->unicode;
|
|---|
| 6628 | static const uint auto_size = 1024;
|
|---|
| 6629 | char t[auto_size];
|
|---|
| 6630 | char *b;
|
|---|
| 6631 | if ( bytes > auto_size ) {
|
|---|
| 6632 | b = new char[bytes];
|
|---|
| 6633 | } else {
|
|---|
| 6634 | b = t;
|
|---|
| 6635 | }
|
|---|
| 6636 | s.readRawBytes( b, bytes );
|
|---|
| 6637 | int bt = bytes/2;
|
|---|
| 6638 | char *oldb = b;
|
|---|
| 6639 | while ( bt-- ) {
|
|---|
| 6640 | if ( byteOrder == QDataStream::BigEndian )
|
|---|
| 6641 | *ch++ = (ushort) (((ushort)b[0])<<8) | (uchar)b[1];
|
|---|
| 6642 | else
|
|---|
| 6643 | *ch++ = (ushort) (((ushort)b[1])<<8) | (uchar)b[0];
|
|---|
| 6644 | b += 2;
|
|---|
| 6645 | }
|
|---|
| 6646 | if ( bytes > auto_size )
|
|---|
| 6647 | delete [] oldb;
|
|---|
| 6648 | } else {
|
|---|
| 6649 | str = "";
|
|---|
| 6650 | }
|
|---|
| 6651 | }
|
|---|
| 6652 | return s;
|
|---|
| 6653 | }
|
|---|
| 6654 | #endif // QT_NO_DATASTREAM
|
|---|
| 6655 |
|
|---|
| 6656 | /*****************************************************************************
|
|---|
| 6657 | QConstString member functions
|
|---|
| 6658 | *****************************************************************************/
|
|---|
| 6659 |
|
|---|
| 6660 | /*!
|
|---|
| 6661 | \class QConstString qstring.h
|
|---|
| 6662 | \reentrant
|
|---|
| 6663 | \ingroup text
|
|---|
| 6664 | \brief The QConstString class provides string objects using constant Unicode data.
|
|---|
| 6665 |
|
|---|
| 6666 | In order to minimize copying, highly optimized applications can
|
|---|
| 6667 | use QConstString to provide a QString-compatible object from
|
|---|
| 6668 | existing Unicode data. It is then the programmer's responsibility
|
|---|
| 6669 | to ensure that the Unicode data exists for the entire lifetime of
|
|---|
| 6670 | the QConstString object.
|
|---|
| 6671 |
|
|---|
| 6672 | A QConstString is created with the QConstString constructor. The
|
|---|
| 6673 | string held by the object can be obtained by calling string().
|
|---|
| 6674 | */
|
|---|
| 6675 |
|
|---|
| 6676 | /*!
|
|---|
| 6677 | Constructs a QConstString that uses the first \a length Unicode
|
|---|
| 6678 | characters in the array \a unicode. Any attempt to modify copies
|
|---|
| 6679 | of the string will cause it to create a copy of the data, thus it
|
|---|
| 6680 | remains forever unmodified.
|
|---|
| 6681 |
|
|---|
| 6682 | The data in \a unicode is not copied. The caller must be able to
|
|---|
| 6683 | guarantee that \a unicode will not be deleted or modified.
|
|---|
| 6684 | */
|
|---|
| 6685 | QConstString::QConstString( const QChar* unicode, uint length ) :
|
|---|
| 6686 | QString( new QStringData( (QChar*)unicode, length, length ), TRUE )
|
|---|
| 6687 | {
|
|---|
| 6688 | }
|
|---|
| 6689 |
|
|---|
| 6690 | /*!
|
|---|
| 6691 | Destroys the QConstString, creating a copy of the data if other
|
|---|
| 6692 | strings are still using it.
|
|---|
| 6693 | */
|
|---|
| 6694 | QConstString::~QConstString()
|
|---|
| 6695 | {
|
|---|
| 6696 | if ( d->count > 1 ) {
|
|---|
| 6697 | QChar* cp = QT_ALLOC_QCHAR_VEC( d->len );
|
|---|
| 6698 | memcpy( cp, d->unicode, d->len*sizeof(QChar) );
|
|---|
| 6699 | d->unicode = cp;
|
|---|
| 6700 | } else {
|
|---|
| 6701 | d->unicode = 0;
|
|---|
| 6702 | }
|
|---|
| 6703 |
|
|---|
| 6704 | // The original d->unicode is now unlinked.
|
|---|
| 6705 | }
|
|---|
| 6706 |
|
|---|
| 6707 | /*!
|
|---|
| 6708 | \fn const QString& QConstString::string() const
|
|---|
| 6709 |
|
|---|
| 6710 | Returns a constant string referencing the data passed during
|
|---|
| 6711 | construction.
|
|---|
| 6712 | */
|
|---|
| 6713 |
|
|---|
| 6714 | /*!
|
|---|
| 6715 | Returns TRUE if the string starts with \a s; otherwise returns
|
|---|
| 6716 | FALSE.
|
|---|
| 6717 |
|
|---|
| 6718 | If \a cs is TRUE (the default), the search is case sensitive;
|
|---|
| 6719 | otherwise the search is case insensitive.
|
|---|
| 6720 |
|
|---|
| 6721 | \code
|
|---|
| 6722 | QString str( "Bananas" );
|
|---|
| 6723 | str.startsWith( "Ban" ); // returns TRUE
|
|---|
| 6724 | str.startsWith( "Car" ); // returns FALSE
|
|---|
| 6725 | \endcode
|
|---|
| 6726 |
|
|---|
| 6727 | \sa endsWith()
|
|---|
| 6728 | */
|
|---|
| 6729 | bool QString::startsWith( const QString& s, bool cs ) const
|
|---|
| 6730 | {
|
|---|
| 6731 | if ( isNull() )
|
|---|
| 6732 | return s.isNull();
|
|---|
| 6733 | if ( s.length() > length() )
|
|---|
| 6734 | return FALSE;
|
|---|
| 6735 | if ( cs ) {
|
|---|
| 6736 | for ( int i = 0; i < (int) s.length(); i++ ) {
|
|---|
| 6737 | if ( d->unicode[i] != s[i] )
|
|---|
| 6738 | return FALSE;
|
|---|
| 6739 | }
|
|---|
| 6740 | } else {
|
|---|
| 6741 | for ( int i = 0; i < (int) s.length(); i++ ) {
|
|---|
| 6742 | if ( d->unicode[i].lower() != s[i].lower() )
|
|---|
| 6743 | return FALSE;
|
|---|
| 6744 | }
|
|---|
| 6745 | }
|
|---|
| 6746 | return TRUE;
|
|---|
| 6747 | }
|
|---|
| 6748 |
|
|---|
| 6749 | bool QString::startsWith( const QString& s ) const
|
|---|
| 6750 | {
|
|---|
| 6751 | return startsWith( s, TRUE );
|
|---|
| 6752 | }
|
|---|
| 6753 |
|
|---|
| 6754 | /*!
|
|---|
| 6755 | Returns TRUE if the string ends with \a s; otherwise returns
|
|---|
| 6756 | FALSE.
|
|---|
| 6757 |
|
|---|
| 6758 | If \a cs is TRUE (the default), the search is case sensitive;
|
|---|
| 6759 | otherwise the search is case insensitive.
|
|---|
| 6760 |
|
|---|
| 6761 | \code
|
|---|
| 6762 | QString str( "Bananas" );
|
|---|
| 6763 | str.endsWith( "anas" ); // returns TRUE
|
|---|
| 6764 | str.endsWith( "pple" ); // returns FALSE
|
|---|
| 6765 | \endcode
|
|---|
| 6766 |
|
|---|
| 6767 | \sa startsWith()
|
|---|
| 6768 | */
|
|---|
| 6769 | bool QString::endsWith( const QString& s, bool cs ) const
|
|---|
| 6770 | {
|
|---|
| 6771 | if ( isNull() )
|
|---|
| 6772 | return s.isNull();
|
|---|
| 6773 | int pos = length() - s.length();
|
|---|
| 6774 | if ( pos < 0 )
|
|---|
| 6775 | return FALSE;
|
|---|
| 6776 | if ( cs ) {
|
|---|
| 6777 | for ( int i = 0; i < (int) s.length(); i++ ) {
|
|---|
| 6778 | if ( d->unicode[pos + i] != s[i] )
|
|---|
| 6779 | return FALSE;
|
|---|
| 6780 | }
|
|---|
| 6781 | } else {
|
|---|
| 6782 | for ( int i = 0; i < (int) s.length(); i++ ) {
|
|---|
| 6783 | if ( d->unicode[pos + i].lower() != s[i].lower() )
|
|---|
| 6784 | return FALSE;
|
|---|
| 6785 | }
|
|---|
| 6786 | }
|
|---|
| 6787 | return TRUE;
|
|---|
| 6788 | }
|
|---|
| 6789 |
|
|---|
| 6790 | bool QString::endsWith( const QString& s ) const
|
|---|
| 6791 | {
|
|---|
| 6792 | return endsWith( s, TRUE );
|
|---|
| 6793 | }
|
|---|
| 6794 |
|
|---|
| 6795 | /*! \fn void QString::detach()
|
|---|
| 6796 | If the string does not share its data with another QString instance,
|
|---|
| 6797 | nothing happens; otherwise the function creates a new, unique copy of
|
|---|
| 6798 | this string. This function is called whenever the string is modified. The
|
|---|
| 6799 | implicit sharing mechanism is implemented this way.
|
|---|
| 6800 | */
|
|---|
| 6801 |
|
|---|
| 6802 | #if defined(Q_OS_WIN32)
|
|---|
| 6803 |
|
|---|
| 6804 | #include <windows.h>
|
|---|
| 6805 |
|
|---|
| 6806 | /*!
|
|---|
| 6807 | \obsolete
|
|---|
| 6808 |
|
|---|
| 6809 | Returns a static Windows TCHAR* from a QString, adding NUL if \a
|
|---|
| 6810 | addnul is TRUE.
|
|---|
| 6811 |
|
|---|
| 6812 | The lifetime of the return value is until the next call to this function,
|
|---|
| 6813 | or until the last copy of str is deleted, whatever comes first.
|
|---|
| 6814 |
|
|---|
| 6815 | Use ucs2() instead.
|
|---|
| 6816 | */
|
|---|
| 6817 | const void* qt_winTchar(const QString& str, bool)
|
|---|
| 6818 | {
|
|---|
| 6819 | // So that the return value lives long enough.
|
|---|
| 6820 | static QString str_cache;
|
|---|
| 6821 | str_cache = str;
|
|---|
| 6822 | #ifdef UNICODE
|
|---|
| 6823 | return str_cache.ucs2();
|
|---|
| 6824 | #else
|
|---|
| 6825 | return str_cache.latin1();
|
|---|
| 6826 | #endif
|
|---|
| 6827 | }
|
|---|
| 6828 |
|
|---|
| 6829 | /*!
|
|---|
| 6830 | Makes a new '\0'-terminated Windows TCHAR* from a QString.
|
|---|
| 6831 | */
|
|---|
| 6832 | void* qt_winTchar_new(const QString& str)
|
|---|
| 6833 | {
|
|---|
| 6834 | if ( str.isNull() )
|
|---|
| 6835 | return 0;
|
|---|
| 6836 | int l = str.length()+1;
|
|---|
| 6837 | TCHAR *tc = new TCHAR[ l ];
|
|---|
| 6838 | #ifdef UNICODE
|
|---|
| 6839 | memcpy( tc, str.ucs2(), sizeof(TCHAR)*l );
|
|---|
| 6840 | #else
|
|---|
| 6841 | memcpy( tc, str.latin1(), sizeof(TCHAR)*l );
|
|---|
| 6842 | #endif
|
|---|
| 6843 | return tc;
|
|---|
| 6844 | }
|
|---|
| 6845 |
|
|---|
| 6846 | /*!
|
|---|
| 6847 | Makes a QString from a Windows TCHAR*.
|
|---|
| 6848 | */
|
|---|
| 6849 | QString qt_winQString(void* tc)
|
|---|
| 6850 | {
|
|---|
| 6851 | #ifdef UNICODE
|
|---|
| 6852 | return QString::fromUcs2( (ushort*)tc );
|
|---|
| 6853 | #else
|
|---|
| 6854 | return QString::fromLatin1( (TCHAR *)tc );
|
|---|
| 6855 | #endif
|
|---|
| 6856 | }
|
|---|
| 6857 |
|
|---|
| 6858 | QCString qt_winQString2MB( const QString& s, int uclen )
|
|---|
| 6859 | {
|
|---|
| 6860 | if ( uclen < 0 )
|
|---|
| 6861 | uclen = s.length();
|
|---|
| 6862 | if ( s.isNull() )
|
|---|
| 6863 | return QCString();
|
|---|
| 6864 | if ( uclen == 0 )
|
|---|
| 6865 | return QCString("");
|
|---|
| 6866 | BOOL used_def;
|
|---|
| 6867 | QCString mb(4096);
|
|---|
| 6868 | int len;
|
|---|
| 6869 | while ( !(len=WideCharToMultiByte(CP_ACP, 0, (const WCHAR*)s.unicode(), uclen,
|
|---|
| 6870 | mb.data(), mb.size()-1, 0, &used_def)) )
|
|---|
| 6871 | {
|
|---|
| 6872 | int r = GetLastError();
|
|---|
| 6873 | if ( r == ERROR_INSUFFICIENT_BUFFER ) {
|
|---|
| 6874 | mb.resize(1+WideCharToMultiByte( CP_ACP, 0,
|
|---|
| 6875 | (const WCHAR*)s.unicode(), uclen,
|
|---|
| 6876 | 0, 0, 0, &used_def));
|
|---|
| 6877 | // and try again...
|
|---|
| 6878 | } else {
|
|---|
| 6879 | #ifndef QT_NO_DEBUG
|
|---|
| 6880 | // Fail.
|
|---|
| 6881 | qWarning("WideCharToMultiByte cannot convert multibyte text (error %d): %s (UTF8)",
|
|---|
| 6882 | r, s.utf8().data());
|
|---|
| 6883 | #endif
|
|---|
| 6884 | break;
|
|---|
| 6885 | }
|
|---|
| 6886 | }
|
|---|
| 6887 | mb[len]='\0';
|
|---|
| 6888 | return mb;
|
|---|
| 6889 | }
|
|---|
| 6890 |
|
|---|
| 6891 | // WATCH OUT: mblen must include the NUL (or just use -1)
|
|---|
| 6892 | QString qt_winMB2QString( const char* mb, int mblen )
|
|---|
| 6893 | {
|
|---|
| 6894 | if ( !mb || !mblen )
|
|---|
| 6895 | return QString::null;
|
|---|
| 6896 | const int wclen_auto = 4096;
|
|---|
| 6897 | WCHAR wc_auto[wclen_auto];
|
|---|
| 6898 | int wclen = wclen_auto;
|
|---|
| 6899 | WCHAR *wc = wc_auto;
|
|---|
| 6900 | int len;
|
|---|
| 6901 | while ( !(len=MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED,
|
|---|
| 6902 | mb, mblen, wc, wclen )) )
|
|---|
| 6903 | {
|
|---|
| 6904 | int r = GetLastError();
|
|---|
| 6905 | if ( r == ERROR_INSUFFICIENT_BUFFER ) {
|
|---|
| 6906 | if ( wc != wc_auto ) {
|
|---|
| 6907 | qWarning("Size changed in MultiByteToWideChar");
|
|---|
| 6908 | break;
|
|---|
| 6909 | } else {
|
|---|
| 6910 | wclen = MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED,
|
|---|
| 6911 | mb, mblen, 0, 0 );
|
|---|
| 6912 | wc = new WCHAR[wclen];
|
|---|
| 6913 | // and try again...
|
|---|
| 6914 | }
|
|---|
| 6915 | } else {
|
|---|
| 6916 | // Fail.
|
|---|
| 6917 | qWarning("MultiByteToWideChar cannot convert multibyte text");
|
|---|
| 6918 | break;
|
|---|
| 6919 | }
|
|---|
| 6920 | }
|
|---|
| 6921 | if ( len <= 0 )
|
|---|
| 6922 | return QString::null;
|
|---|
| 6923 | QString s( (QChar*)wc, len - 1 ); // len - 1: we don't want terminator
|
|---|
| 6924 | if ( wc != wc_auto )
|
|---|
| 6925 | delete [] wc;
|
|---|
| 6926 | return s;
|
|---|
| 6927 | }
|
|---|
| 6928 |
|
|---|
| 6929 | #endif // Q_OS_WIN32
|
|---|