home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / qt3_emx.zip / include / qstring.h < prev    next >
C/C++ Source or Header  |  2001-10-11  |  29KB  |  907 lines

  1. /****************************************************************************
  2. ** $Id$
  3. **
  4. ** Definition of the QString class, and related Unicode
  5. ** functions.
  6. **
  7. ** Created : 920609
  8. **
  9. ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
  10. **
  11. ** This file is part of the tools module of the Qt GUI Toolkit.
  12. **
  13. ** This file may be distributed under the terms of the Q Public License
  14. ** as defined by Trolltech AS of Norway and appearing in the file
  15. ** LICENSE.QPL included in the packaging of this file.
  16. **
  17. ** This file may be distributed and/or modified under the terms of the
  18. ** GNU General Public License version 2 as published by the Free Software
  19. ** Foundation and appearing in the file LICENSE.GPL included in the
  20. ** packaging of this file.
  21. **
  22. ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
  23. ** licenses may use this file in accordance with the Qt Commercial License
  24. ** Agreement provided with the Software.
  25. **
  26. ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  27. ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  28. **
  29. ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
  30. **   information about Qt Commercial License Agreements.
  31. ** See http://www.trolltech.com/qpl/ for QPL licensing information.
  32. ** See http://www.trolltech.com/gpl/ for GPL licensing information.
  33. **
  34. ** Contact info@trolltech.com if any conditions of this licensing are
  35. ** not clear to you.
  36. **
  37. **********************************************************************/
  38.  
  39. #ifndef QSTRING_H
  40. #define QSTRING_H
  41.  
  42. #ifndef QT_H
  43. #include "qcstring.h"
  44. #endif // QT_H
  45.  
  46.  
  47. /*****************************************************************************
  48.   QString class
  49.  *****************************************************************************/
  50.  
  51. class QRegExp;
  52. class QString;
  53. class QCharRef;
  54.  
  55. class Q_EXPORT QChar {
  56. public:
  57.     QChar();
  58.     QChar( char c );
  59.     QChar( uchar c );
  60.     QChar( uchar c, uchar r );
  61.     QChar( const QChar& c );
  62.     QChar( ushort rc );
  63.     QChar( short rc );
  64.     QChar( uint rc );
  65.     QChar( int rc );
  66.  
  67.     QT_STATIC_CONST QChar null;            // 0000
  68.     QT_STATIC_CONST QChar replacement;     // FFFD
  69.     QT_STATIC_CONST QChar byteOrderMark;     // FEFF
  70.     QT_STATIC_CONST QChar byteOrderSwapped;     // FFFE
  71.     QT_STATIC_CONST QChar nbsp;            // 00A0
  72.  
  73.     // Unicode information
  74.  
  75.     enum Category
  76.     {
  77.         NoCategory,
  78.  
  79.         Mark_NonSpacing,          //   Mn
  80.         Mark_SpacingCombining,    //   Mc
  81.         Mark_Enclosing,           //   Me
  82.  
  83.         Number_DecimalDigit,      //   Nd
  84.         Number_Letter,            //   Nl
  85.         Number_Other,             //   No
  86.  
  87.         Separator_Space,          //   Zs
  88.         Separator_Line,           //   Zl
  89.         Separator_Paragraph,      //   Zp
  90.  
  91.         Other_Control,            //   Cc
  92.         Other_Format,             //   Cf
  93.         Other_Surrogate,          //   Cs
  94.         Other_PrivateUse,         //   Co
  95.         Other_NotAssigned,        //   Cn
  96.  
  97.         Letter_Uppercase,         //   Lu
  98.         Letter_Lowercase,         //   Ll
  99.         Letter_Titlecase,         //   Lt
  100.         Letter_Modifier,          //   Lm
  101.         Letter_Other,             //   Lo
  102.  
  103.         Punctuation_Connector,    //   Pc
  104.         Punctuation_Dash,         //   Pd
  105.         Punctuation_Dask = Punctuation_Dash, // oops
  106.         Punctuation_Open,         //   Ps
  107.         Punctuation_Close,        //   Pe
  108.         Punctuation_InitialQuote, //   Pi
  109.         Punctuation_FinalQuote,   //   Pf
  110.         Punctuation_Other,        //   Po
  111.  
  112.         Symbol_Math,              //   Sm
  113.         Symbol_Currency,          //   Sc
  114.         Symbol_Modifier,          //   Sk
  115.         Symbol_Other              //   So
  116.     };
  117.  
  118.     enum Direction
  119.     {
  120.         DirL, DirR, DirEN, DirES, DirET, DirAN, DirCS, DirB, DirS, DirWS, DirON,
  121.         DirLRE, DirLRO, DirAL, DirRLE, DirRLO, DirPDF, DirNSM, DirBN
  122.     };
  123.  
  124.     enum Decomposition
  125.     {
  126.         Single, Canonical, Font, NoBreak, Initial, Medial,
  127.         Final, Isolated, Circle, Super, Sub, Vertical,
  128.         Wide, Narrow, Small, Square, Compat, Fraction
  129.     };
  130.  
  131.     enum Joining
  132.     {
  133.         OtherJoining, Dual, Right, Center
  134.     };
  135.  
  136.     enum CombiningClass
  137.     {
  138.         Combining_BelowLeftAttached       = 200,
  139.         Combining_BelowAttached           = 202,
  140.         Combining_BelowRightAttached      = 204,
  141.         Combining_LeftAttached            = 208,
  142.         Combining_RightAttached           = 210,
  143.         Combining_AboveLeftAttached       = 212,
  144.         Combining_AboveAttached           = 214,
  145.         Combining_AboveRightAttached      = 216,
  146.  
  147.         Combining_BelowLeft               = 218,
  148.         Combining_Below                   = 220,
  149.         Combining_BelowRight              = 222,
  150.         Combining_Left                    = 224,
  151.         Combining_Right                   = 226,
  152.         Combining_AboveLeft               = 228,
  153.         Combining_Above                   = 230,
  154.         Combining_AboveRight              = 232,
  155.  
  156.         Combining_DoubleBelow             = 233,
  157.         Combining_DoubleAbove             = 234,
  158.         Combining_IotaSubscript           = 240
  159.     };
  160.  
  161.     // ****** WHEN ADDING FUNCTIONS, CONSIDER ADDING TO QCharRef TOO
  162.  
  163.     int digitValue() const;
  164.     QChar lower() const;
  165.     QChar upper() const;
  166.  
  167.     Category category() const;
  168.     Direction direction() const;
  169.     Joining joining() const;
  170.     bool mirrored() const;
  171.     QChar mirroredChar() const;
  172.     const QString &decomposition() const;
  173.     Decomposition decompositionTag() const;
  174.     unsigned char combiningClass() const;
  175.  
  176.     char latin1() const { return ucs > 0xff ? 0 : (char) ucs; }
  177.     ushort unicode() const { return ucs; }
  178.     ushort &unicode() { return ucs; }
  179. #ifndef QT_NO_CAST_ASCII
  180.     // like all ifdef'd code this is undocumented
  181.     operator char() const { return latin1(); }
  182. #endif
  183.  
  184.     bool isNull() const { return unicode()==0; }
  185.     bool isPrint() const;
  186.     bool isPunct() const;
  187.     bool isSpace() const;
  188.     bool isMark() const;
  189.     bool isLetter() const;
  190.     bool isNumber() const;
  191.     bool isLetterOrNumber() const;
  192.     bool isDigit() const;
  193.     bool isSymbol() const;
  194.  
  195.     uchar cell() const { return ((uchar) ucs & 0xff); }
  196.     uchar row() const { return ((uchar) (ucs>>8)&0xff); }
  197.     void setCell( uchar cell ) { ucs = (ucs & 0xff00) + cell; }
  198.     void setRow( uchar row ) { ucs = (((ushort) row)<<8) + (ucs&0xff); }
  199.  
  200. #ifndef QT_NO_COMPAT
  201.     static bool networkOrdered() { return FALSE; }
  202. #endif
  203.  
  204.     friend inline bool operator==( char ch, QChar c );
  205.     friend inline bool operator==( QChar c, char ch );
  206.     friend inline bool operator==( QChar c1, QChar c2 );
  207.     friend inline bool operator!=( QChar c1, QChar c2 );
  208.     friend inline bool operator!=( char ch, QChar c );
  209.     friend inline bool operator!=( QChar c, char ch );
  210.     friend inline bool operator<=( QChar c, char ch );
  211.     friend inline bool operator<=( char ch, QChar c );
  212.     friend inline bool operator<=( QChar c1, QChar c2 );
  213.  
  214. private:
  215.     ushort ucs;
  216. #if defined(QT_QSTRING_UCS_4)
  217.     ushort grp;
  218. #endif
  219. } Q_PACKED;
  220.  
  221. inline QChar::QChar()
  222. {
  223.     ucs = 0;
  224. #ifdef QT_QSTRING_UCS_4
  225.     grp = 0;
  226. #endif
  227. }
  228. inline QChar::QChar( char c )
  229. {
  230.     ucs = (uchar)c;
  231. #ifdef QT_QSTRING_UCS_4
  232.     grp = 0;
  233. #endif
  234. }
  235. inline QChar::QChar( uchar c )
  236. {
  237.     ucs = c;
  238. #ifdef QT_QSTRING_UCS_4
  239.     grp = 0;
  240. #endif
  241. }
  242. inline QChar::QChar( uchar c, uchar r )
  243. {
  244.     ucs = (r << 8) | c;
  245. #ifdef QT_QSTRING_UCS_4
  246.     grp = 0;
  247. #endif
  248. }
  249. inline QChar::QChar( const QChar& c )
  250. {
  251.     ucs = c.ucs;
  252. #ifdef QT_QSTRING_UCS_4
  253.     grp = c.grp;
  254. #endif
  255. }
  256.  
  257. inline QChar::QChar( ushort rc )
  258. {
  259.     ucs = rc;
  260. #ifdef QT_QSTRING_UCS_4
  261.     grp = 0;
  262. #endif
  263. }
  264. inline QChar::QChar( short rc )
  265. {
  266.     ucs = (ushort) rc;
  267. #ifdef QT_QSTRING_UCS_4
  268.     grp = 0;
  269. #endif
  270. }
  271. inline QChar::QChar( uint rc )
  272. {
  273.     ucs = (ushort ) (rc & 0xffff);
  274. #ifdef QT_QSTRING_UCS_4
  275.     grp = (ushort) ((rc >> 16) & 0xffff);
  276. #endif
  277. }
  278. inline QChar::QChar( int rc )
  279. {
  280.     ucs = (ushort) (rc & 0xffff);
  281. #ifdef QT_QSTRING_UCS_4
  282.     grp = (ushort) ((rc >> 16) & 0xffff);
  283. #endif
  284. }
  285.  
  286.  
  287. inline bool operator==( char ch, QChar c )
  288. {
  289.     return ((uchar) ch) == c.ucs;
  290. }
  291.  
  292. inline bool operator==( QChar c, char ch )
  293. {
  294.     return ((uchar) ch) == c.ucs;
  295. }
  296.  
  297. inline bool operator==( QChar c1, QChar c2 )
  298. {
  299.     return c1.ucs == c2.ucs;
  300. }
  301.  
  302. inline bool operator!=( QChar c1, QChar c2 )
  303. {
  304.     return c1.ucs != c2.ucs;
  305. }
  306.  
  307. inline bool operator!=( char ch, QChar c )
  308. {
  309.     return ((uchar)ch) != c.ucs;
  310. }
  311.  
  312. inline bool operator!=( QChar c, char ch )
  313. {
  314.     return ((uchar) ch) != c.ucs;
  315. }
  316.  
  317. inline bool operator<=( QChar c, char ch )
  318. {
  319.     return c.ucs <= ((uchar) ch);
  320. }
  321.  
  322. inline bool operator<=( char ch, QChar c )
  323. {
  324.     return ((uchar) ch) <= c.ucs;
  325. }
  326.  
  327. inline bool operator<=( QChar c1, QChar c2 )
  328. {
  329.     return c1.ucs <= c2.ucs;
  330. }
  331.  
  332. inline bool operator>=( QChar c, char ch ) { return ch <= c; }
  333. inline bool operator>=( char ch, QChar c ) { return c <= ch; }
  334. inline bool operator>=( QChar c1, QChar c2 ) { return c2 <= c1; }
  335. inline bool operator<( QChar c, char ch ) { return !(ch<=c); }
  336. inline bool operator<( char ch, QChar c ) { return !(c<=ch); }
  337. inline bool operator<( QChar c1, QChar c2 ) { return !(c2<=c1); }
  338. inline bool operator>( QChar c, char ch ) { return !(ch>=c); }
  339. inline bool operator>( char ch, QChar c ) { return !(c>=ch); }
  340. inline bool operator>( QChar c1, QChar c2 ) { return !(c2>=c1); }
  341.  
  342. // internal
  343. struct Q_EXPORT QStringData : public QShared {
  344.     QStringData() :
  345.         unicode(0), ascii(0), len(0), simpletext(1), maxl(0), dirty(0) { ref(); }
  346.     QStringData(QChar *u, uint l, uint m) :
  347.         unicode(u), ascii(0), len(l), simpletext(1), maxl(m), dirty(1) { }
  348.  
  349.     ~QStringData() { if ( unicode ) delete[] ((char*)unicode);
  350.                      if ( ascii ) delete[] ascii; }
  351.  
  352.     void deleteSelf();
  353.     QChar *unicode;
  354.     char *ascii;
  355.     void setDirty() {
  356.     if ( ascii ) {
  357.         delete [] ascii;
  358.         ascii = 0;
  359.     }
  360.     dirty = 1;
  361.     }
  362. #ifdef Q_OS_MAC9
  363.     uint len;
  364. #else
  365.     uint len : 30;
  366. #endif
  367.     uint simpletext : 1;
  368. #ifdef Q_OS_MAC9
  369.     uint maxl;
  370. #else
  371.     uint maxl : 30;
  372. #endif
  373.     uint dirty : 1;
  374. };
  375.  
  376.  
  377. class Q_EXPORT QString
  378. {
  379. public:
  380.     QString();                                  // make null string
  381.     QString( QChar );                           // one-char string
  382.     QString( const QString & );                 // impl-shared copy
  383.     QString( const QByteArray& );               // deep copy
  384.     QString( const QChar* unicode, uint length ); // deep copy
  385. #ifndef QT_NO_CAST_ASCII
  386.     QString( const char *str );                 // deep copy
  387. #endif
  388.     ~QString();
  389.  
  390.     QString    &operator=( const QString & );   // impl-shared copy
  391. #ifndef QT_NO_CAST_ASCII
  392.     QString    &operator=( const char * );      // deep copy
  393. #endif
  394.     QString    &operator=( const QCString& );   // deep copy
  395.     QString    &operator=( QChar c );
  396.     QString    &operator=( char c );
  397.  
  398.     QT_STATIC_CONST QString null;
  399.  
  400.     bool        isNull()        const;
  401.     bool        isEmpty()       const;
  402.     uint        length()        const;
  403.     void        truncate( uint pos );
  404.  
  405.     QString &   fill( QChar c, int len = -1 );
  406.  
  407.     QString     copy()  const;
  408.  
  409.     QString arg( long a, int fieldwidth=0, int base=10 ) const;
  410.     QString arg( ulong a, int fieldwidth=0, int base=10 ) const;
  411.     QString arg( int a, int fieldwidth=0, int base=10 ) const;
  412.     QString arg( uint a, int fieldwidth=0, int base=10 ) const;
  413.     QString arg( short a, int fieldwidth=0, int base=10 ) const;
  414.     QString arg( ushort a, int fieldwidth=0, int base=10 ) const;
  415.     QString arg( char a, int fieldwidth=0 ) const;
  416.     QString arg( QChar a, int fieldwidth=0 ) const;
  417.     QString arg( const QString& a, int fieldwidth=0 ) const;
  418.     QString arg( double a, int fieldwidth=0, char fmt='g', int prec=-1 ) const;
  419.  
  420. #ifndef QT_NO_SPRINTF
  421.     QString    &sprintf( const char* format, ... )
  422. #if defined(Q_CC_GNU) && !defined(__INSURE__)
  423.         __attribute__ ((format (printf, 2, 3)))
  424. #endif
  425.         ;
  426. #endif
  427.  
  428.     int         find( QChar c, int index=0, bool cs=TRUE ) const;
  429.     int         find( char c, int index=0, bool cs=TRUE ) const;
  430.     int         find( const QString &str, int index=0, bool cs=TRUE ) const;
  431. #ifndef QT_NO_REGEXP
  432.     int         find( const QRegExp &, int index=0 ) const;
  433. #endif
  434. #ifndef QT_NO_CAST_ASCII
  435.     int         find( const char* str, int index=0 ) const;
  436. #endif
  437.     int         findRev( QChar c, int index=-1, bool cs=TRUE) const;
  438.     int         findRev( char c, int index=-1, bool cs=TRUE) const;
  439.     int         findRev( const QString &str, int index=-1, bool cs=TRUE) const;
  440. #ifndef QT_NO_REGEXP
  441.     int         findRev( const QRegExp &, int index=-1 ) const;
  442. #endif
  443. #ifndef QT_NO_CAST_ASCII
  444.     int         findRev( const char* str, int index=-1 ) const;
  445. #endif
  446.     int         contains( QChar c, bool cs=TRUE ) const;
  447.     int         contains( char c, bool cs=TRUE ) const
  448.                     { return contains(QChar(c), cs); }
  449. #ifndef QT_NO_CAST_ASCII
  450.     int         contains( const char* str, bool cs=TRUE ) const;
  451. #endif
  452.     int         contains( const QString &str, bool cs=TRUE ) const;
  453. #ifndef QT_NO_REGEXP
  454.     int         contains( const QRegExp & ) const;
  455. #endif
  456.  
  457.     enum SectionFlags {
  458.     SectionDefault             = 0x00,
  459.     SectionSkipEmpty           = 0x01,
  460.     SectionIncludeLeadingSep   = 0x02,
  461.     SectionIncludeTrailingSep  = 0x04,
  462.     SectionCaseInsensitiveSeps = 0x08
  463.     };
  464.     QString     section( QChar sep, int start, int end = 0xffffffff, int flags = SectionDefault ) const;
  465.     QString     section( char sep, int start, int end = 0xffffffff, int flags = SectionDefault ) const;
  466. #ifndef QT_NO_CAST_ASCII
  467.     QString      section( const char *in_sep, int start, int end = 0xffffffff, int flags = SectionDefault ) const;
  468. #endif
  469.     QString     section( const QString &in_sep, int start, int end = 0xffffffff, int flags = SectionDefault ) const;
  470. #ifndef QT_NO_REGEXP
  471.     QString     section( const QRegExp ®, int start, int end = 0xffffffff, int flags = SectionDefault ) const;
  472. #endif
  473.  
  474.     QString     left( uint len )  const;
  475.     QString     right( uint len ) const;
  476.     QString     mid( uint index, uint len=0xffffffff) const;
  477.  
  478.     QString     leftJustify( uint width, QChar fill=' ', bool trunc=FALSE)const;
  479.     QString     rightJustify( uint width, QChar fill=' ',bool trunc=FALSE)const;
  480.  
  481.     QString     lower() const;
  482.     QString     upper() const;
  483.  
  484.     QString     stripWhiteSpace()       const;
  485.     QString     simplifyWhiteSpace()    const;
  486.  
  487.     QString    &insert( uint index, const QString & );
  488.     QString    &insert( uint index, const QChar*, uint len );
  489.     QString    &insert( uint index, QChar );
  490.     QString    &insert( uint index, char c ) { return insert(index,QChar(c)); }
  491.     QString    &append( char );
  492.     QString    &append( QChar );
  493.     QString    &append( const QString & );
  494.     QString    &prepend( char );
  495.     QString    &prepend( QChar );
  496.     QString    &prepend( const QString & );
  497.     QString    &remove( uint index, uint len );
  498.     QString    &replace( uint index, uint len, const QString & );
  499.     QString    &replace( uint index, uint len, const QChar*, uint clen );
  500. #ifndef QT_NO_REGEXP
  501.     QString    &replace( const QRegExp &, const QString & );
  502. #endif
  503.     short       toShort( bool *ok=0, int base=10 )      const;
  504.     ushort      toUShort( bool *ok=0, int base=10 )     const;
  505.     int         toInt( bool *ok=0, int base=10 )        const;
  506.     uint        toUInt( bool *ok=0, int base=10 )       const;
  507.     long        toLong( bool *ok=0, int base=10 )       const;
  508.     ulong       toULong( bool *ok=0, int base=10 )      const;
  509.     float       toFloat( bool *ok=0 )   const;
  510.     double      toDouble( bool *ok=0 )  const;
  511.  
  512.     QString    &setNum( short, int base=10 );
  513.     QString    &setNum( ushort, int base=10 );
  514.     QString    &setNum( int, int base=10 );
  515.     QString    &setNum( uint, int base=10 );
  516.     QString    &setNum( long, int base=10 );
  517.     QString    &setNum( ulong, int base=10 );
  518.     QString    &setNum( float, char f='g', int prec=6 );
  519.     QString    &setNum( double, char f='g', int prec=6 );
  520.  
  521.     static QString number( long, int base=10 );
  522.     static QString number( ulong, int base=10);
  523.     static QString number( int, int base=10 );
  524.     static QString number( uint, int base=10);
  525.     static QString number( double, char f='g', int prec=6 );
  526.  
  527.     void        setExpand( uint index, QChar c );
  528.  
  529.     QString    &operator+=( const QString &str );
  530.     QString    &operator+=( QChar c );
  531.     QString    &operator+=( char c );
  532.  
  533.     QChar at( uint i ) const
  534.         { return i < d->len ? d->unicode[i] : QChar::null; }
  535.     QChar operator[]( int i ) const { return at((uint)i); }
  536.     QCharRef at( uint i );
  537.     QCharRef operator[]( int i );
  538.  
  539.     QChar constref(uint i) const
  540.         { return at(i); }
  541.     QChar& ref(uint i)
  542.         { // Optimized for easy-inlining by simple compilers.
  543.             if ( d->count != 1 || i >= d->len )
  544.                 subat( i );
  545.             d->setDirty();
  546.             return d->unicode[i];
  547.         }
  548.  
  549.     const QChar* unicode() const { return d->unicode; }
  550.     const char* ascii() const { return latin1(); }
  551.     const char* latin1() const;
  552.     static QString fromLatin1(const char*, int len=-1);
  553. #ifndef QT_NO_TEXTCODEC
  554.     QCString utf8() const;
  555.     static QString fromUtf8(const char*, int len=-1);
  556. #endif
  557.     QCString local8Bit() const;
  558.     static QString fromLocal8Bit(const char*, int len=-1);
  559.     bool operator!() const;
  560. #ifndef QT_NO_ASCII_CAST
  561.     operator const char *() const { return latin1(); }
  562. #endif
  563.  
  564.     QString &setUnicode( const QChar* unicode, uint len );
  565.     QString &setUnicodeCodes( const ushort* unicode_as_ushorts, uint len );
  566.     QString &setLatin1( const char*, int len=-1 );
  567.  
  568.     int compare( const QString& s ) const;
  569.     static int compare( const QString& s1, const QString& s2 )
  570.     { return s1.compare( s2 ); }
  571.  
  572.     int localeAwareCompare( const QString& s ) const;
  573.     static int localeAwareCompare( const QString& s1, const QString& s2 )
  574.     { return s1.localeAwareCompare( s2 ); }
  575.  
  576. #ifndef QT_NO_DATASTREAM
  577.     friend Q_EXPORT QDataStream &operator>>( QDataStream &, QString & );
  578. #endif
  579.  
  580.     void compose();
  581.  
  582. #ifndef QT_NO_COMPAT
  583.     const char* data() const { return latin1(); }
  584. #endif
  585.  
  586.     bool startsWith( const QString& ) const;
  587.     bool endsWith( const QString& ) const;
  588.  
  589.     void setLength( uint newLength );
  590.  
  591.     bool simpleText() const { if ( d->dirty ) checkSimpleText(); return (bool)d->simpletext; }
  592.     bool isRightToLeft() const;
  593.  
  594. private:
  595.     /*
  596.       In Qt 1.0 and 2.0, users could write
  597.       str.replace(QString("foo"), QString("bar")) and "foo" was
  598.       automatically converted into a regexp.
  599.  
  600.       In Qt 3.0, this gives a compiler error. Users have to write
  601.       QRegExp("foo"). In Qt 3.1 or 4.0, we will move replace() to the
  602.       public API.
  603.     */
  604.     QString& replace( const QString &, const QString & ) { return *this; }
  605.  
  606.     QString( int size, bool /* dummy */ );    // allocate size incl. \0
  607.  
  608.     void deref();
  609.     void real_detach();
  610.     void subat( uint );
  611.     bool findArg(int& pos, int& len) const;
  612.  
  613.     void checkSimpleText() const;
  614.  
  615.     static QChar* asciiToUnicode( const char*, uint * len, uint maxlen=(uint)-1 );
  616.     static QChar* asciiToUnicode( const QByteArray&, uint * len );
  617.     static char* unicodeToAscii( const QChar*, uint len );
  618.  
  619.     QStringData *d;
  620.     static QStringData* shared_null;
  621.     static QStringData* makeSharedNull();
  622.  
  623.     friend class QConstString;
  624.     QString( QStringData* dd, bool /* dummy */ ) : d(dd) { }
  625. };
  626.  
  627. class Q_EXPORT QCharRef {
  628.     friend class QString;
  629.     QString& s;
  630.     uint p;
  631.     QCharRef(QString* str, uint pos) : s(*str), p(pos) { }
  632.  
  633. public:
  634.     // most QChar operations repeated here...
  635.  
  636.     // all this is not documented: We just say "like QChar" and let it be.
  637. #ifndef Q_QDOC
  638.     ushort unicode() const { return s.constref(p).unicode(); }
  639.     char latin1() const { return s.constref(p).latin1(); }
  640.  
  641.     // An operator= for each QChar cast constructor...
  642.     QCharRef operator=(char c ) { s.ref(p)=c; return *this; }
  643.     QCharRef operator=(uchar c ) { s.ref(p)=c; return *this; }
  644.     QCharRef operator=(QChar c ) { s.ref(p)=c; return *this; }
  645.     QCharRef operator=(const QCharRef& c ) { s.ref(p)=c.unicode(); return *this; }
  646.     QCharRef operator=(ushort rc ) { s.ref(p)=rc; return *this; }
  647.     QCharRef operator=(short rc ) { s.ref(p)=rc; return *this; }
  648.     QCharRef operator=(uint rc ) { s.ref(p)=rc; return *this; }
  649.     QCharRef operator=(int rc ) { s.ref(p)=rc; return *this; }
  650.  
  651.     operator QChar () const { return s.constref(p); }
  652.  
  653.     // each function...
  654.     bool isNull() const { return unicode()==0; }
  655.     bool isPrint() const { return s.constref(p).isPrint(); }
  656.     bool isPunct() const { return s.constref(p).isPunct(); }
  657.     bool isSpace() const { return s.constref(p).isSpace(); }
  658.     bool isMark() const { return s.constref(p).isMark(); }
  659.     bool isLetter() const { return s.constref(p).isLetter(); }
  660.     bool isNumber() const { return s.constref(p).isNumber(); }
  661.     bool isLetterOrNumber() { return s.constref(p).isLetterOrNumber(); }
  662.     bool isDigit() const { return s.constref(p).isDigit(); }
  663.  
  664.     int digitValue() const { return s.constref(p).digitValue(); }
  665.     QChar lower() const { return s.constref(p).lower(); }
  666.     QChar upper() const { return s.constref(p).upper(); }
  667.  
  668.     QChar::Category category() const { return s.constref(p).category(); }
  669.     QChar::Direction direction() const { return s.constref(p).direction(); }
  670.     QChar::Joining joining() const { return s.constref(p).joining(); }
  671.     bool mirrored() const { return s.constref(p).mirrored(); }
  672.     QChar mirroredChar() const { return s.constref(p).mirroredChar(); }
  673.     const QString &decomposition() const { return s.constref(p).decomposition(); }
  674.     QChar::Decomposition decompositionTag() const { return s.constref(p).decompositionTag(); }
  675.     unsigned char combiningClass() const { return s.constref(p).combiningClass(); }
  676.  
  677.     // Not the non-const ones of these.
  678.     uchar cell() const { return s.constref(p).cell(); }
  679.     uchar row() const { return s.constref(p).row(); }
  680. #endif
  681. };
  682.  
  683. inline QCharRef QString::at( uint i ) { return QCharRef(this,i); }
  684. inline QCharRef QString::operator[]( int i ) { return at((uint)i); }
  685.  
  686.  
  687. class Q_EXPORT QConstString : private QString {
  688. public:
  689.     QConstString( const QChar* unicode, uint length );
  690.     ~QConstString();
  691.     const QString& string() const { return *this; }
  692. };
  693.  
  694.  
  695. /*****************************************************************************
  696.   QString stream functions
  697.  *****************************************************************************/
  698. #ifndef QT_NO_DATASTREAM
  699. Q_EXPORT QDataStream &operator<<( QDataStream &, const QString & );
  700. Q_EXPORT QDataStream &operator>>( QDataStream &, QString & );
  701. #endif
  702.  
  703. /*****************************************************************************
  704.   QString inline functions
  705.  *****************************************************************************/
  706.  
  707. // These two move code into makeSharedNull() and deletesData()
  708. // to improve cache-coherence (and reduce code bloat), while
  709. // keeping the common cases fast.
  710. //
  711. // No safe way to pre-init shared_null on ALL compilers/linkers.
  712. inline QString::QString() :
  713.     d(shared_null ? shared_null : makeSharedNull())
  714. {
  715.     d->ref();
  716. }
  717. //
  718. inline QString::~QString()
  719. {
  720.     if ( d->deref() ) {
  721.         if ( d == shared_null )
  722.             shared_null = 0;
  723.         d->deleteSelf();
  724.     }
  725. }
  726.  
  727. inline QString QString::section( QChar sep, int start, int end, int flags ) const
  728. { return section(QString(sep), start, end, flags); }
  729.  
  730. inline QString QString::section( char sep, int start, int end, int flags ) const
  731. { return section(QChar(sep), start, end, flags); }
  732.  
  733. #ifndef QT_NO_CAST_ASCII
  734. inline QString QString::section( const char *in_sep, int start, int end, int flags ) const
  735. { return section(QString(in_sep), start, end, flags); }
  736. #endif
  737.  
  738. inline QString &QString::operator=( QChar c )
  739. { return *this = QString(c); }
  740.  
  741. inline QString &QString::operator=( char c )
  742. { return *this = QString(QChar(c)); }
  743.  
  744. inline bool QString::isNull() const
  745. { return unicode() == 0; }
  746.  
  747. inline bool QString::operator!() const
  748. { return isNull(); }
  749.  
  750. inline uint QString::length() const
  751. { return d->len; }
  752.  
  753. inline bool QString::isEmpty() const
  754. { return length() == 0; }
  755.  
  756. inline QString QString::copy() const
  757. { return QString( *this ); }
  758.  
  759. inline QString &QString::prepend( const QString & s )
  760. { return insert(0,s); }
  761.  
  762. inline QString &QString::prepend( QChar c )
  763. { return insert(0,c); }
  764.  
  765. inline QString &QString::prepend( char c )
  766. { return insert(0,c); }
  767.  
  768. inline QString &QString::append( const QString & s )
  769. { return operator+=(s); }
  770.  
  771. inline QString &QString::append( QChar c )
  772. { return operator+=(c); }
  773.  
  774. inline QString &QString::append( char c )
  775. { return operator+=(c); }
  776.  
  777. inline QString &QString::setNum( short n, int base )
  778. { return setNum((long)n, base); }
  779.  
  780. inline QString &QString::setNum( ushort n, int base )
  781. { return setNum((ulong)n, base); }
  782.  
  783. inline QString &QString::setNum( int n, int base )
  784. { return setNum((long)n, base); }
  785.  
  786. inline QString &QString::setNum( uint n, int base )
  787. { return setNum((ulong)n, base); }
  788.  
  789. inline QString &QString::setNum( float n, char f, int prec )
  790. { return setNum((double)n,f,prec); }
  791.  
  792. inline QString QString::arg(int a, int fieldwidth, int base) const
  793. { return arg((long)a, fieldwidth, base); }
  794.  
  795. inline QString QString::arg(uint a, int fieldwidth, int base) const
  796. { return arg((ulong)a, fieldwidth, base); }
  797.  
  798. inline QString QString::arg(short a, int fieldwidth, int base) const
  799. { return arg((long)a, fieldwidth, base); }
  800.  
  801. inline QString QString::arg(ushort a, int fieldwidth, int base) const
  802. { return arg((ulong)a, fieldwidth, base); }
  803.  
  804. inline int QString::find( char c, int index, bool cs ) const
  805. { return find(QChar(c), index, cs); }
  806.  
  807. inline int QString::findRev( char c, int index, bool cs) const
  808. { return findRev( QChar(c), index, cs ); }
  809.  
  810.  
  811. #ifndef QT_NO_CAST_ASCII
  812. inline int QString::find( const char* str, int index ) const
  813. { return find(QString::fromLatin1(str), index); }
  814.  
  815. inline int QString::findRev( const char* str, int index ) const
  816. { return findRev(QString::fromLatin1(str), index); }
  817. #endif
  818.  
  819.  
  820. /*****************************************************************************
  821.   QString non-member operators
  822.  *****************************************************************************/
  823.  
  824. Q_EXPORT bool operator!=( const QString &s1, const QString &s2 );
  825. Q_EXPORT bool operator<( const QString &s1, const QString &s2 );
  826. Q_EXPORT bool operator<=( const QString &s1, const QString &s2 );
  827. Q_EXPORT bool operator==( const QString &s1, const QString &s2 );
  828. Q_EXPORT bool operator>( const QString &s1, const QString &s2 );
  829. Q_EXPORT bool operator>=( const QString &s1, const QString &s2 );
  830. #ifndef QT_NO_CAST_ASCII
  831. Q_EXPORT bool operator!=( const QString &s1, const char *s2 );
  832. Q_EXPORT bool operator<( const QString &s1, const char *s2 );
  833. Q_EXPORT bool operator<=( const QString &s1, const char *s2 );
  834. Q_EXPORT bool operator==( const QString &s1, const char *s2 );
  835. Q_EXPORT bool operator>( const QString &s1, const char *s2 );
  836. Q_EXPORT bool operator>=( const QString &s1, const char *s2 );
  837. Q_EXPORT bool operator!=( const char *s1, const QString &s2 );
  838. Q_EXPORT bool operator<( const char *s1, const QString &s2 );
  839. Q_EXPORT bool operator<=( const char *s1, const QString &s2 );
  840. Q_EXPORT bool operator==( const char *s1, const QString &s2 );
  841. //Q_EXPORT bool operator>( const char *s1, const QString &s2 ); // MSVC++
  842. Q_EXPORT bool operator>=( const char *s1, const QString &s2 );
  843. #endif
  844.  
  845. Q_EXPORT inline const QString operator+( const QString &s1, const QString &s2 )
  846. {
  847.     QString tmp( s1 );
  848.     tmp += s2;
  849.     return tmp;
  850. }
  851.  
  852. #ifndef QT_NO_CAST_ASCII
  853. Q_EXPORT inline const QString operator+( const QString &s1, const char *s2 )
  854. {
  855.     QString tmp( s1 );
  856.     tmp += QString::fromLatin1(s2);
  857.     return tmp;
  858. }
  859.  
  860. Q_EXPORT inline const QString operator+( const char *s1, const QString &s2 )
  861. {
  862.     QString tmp = QString::fromLatin1( s1 );
  863.     tmp += s2;
  864.     return tmp;
  865. }
  866. #endif
  867.  
  868. Q_EXPORT inline const QString operator+( const QString &s1, QChar c2 )
  869. {
  870.     QString tmp( s1 );
  871.     tmp += c2;
  872.     return tmp;
  873. }
  874.  
  875. Q_EXPORT inline const QString operator+( const QString &s1, char c2 )
  876. {
  877.     QString tmp( s1 );
  878.     tmp += c2;
  879.     return tmp;
  880. }
  881.  
  882. Q_EXPORT inline const QString operator+( QChar c1, const QString &s2 )
  883. {
  884.     QString tmp;
  885.     tmp += c1;
  886.     tmp += s2;
  887.     return tmp;
  888. }
  889.  
  890. Q_EXPORT inline const QString operator+( char c1, const QString &s2 )
  891. {
  892.     QString tmp;
  893.     tmp += c1;
  894.     tmp += s2;
  895.     return tmp;
  896. }
  897.  
  898. #if defined(Q_OS_WIN32)
  899. extern Q_EXPORT QString qt_winQString(void*);
  900. extern Q_EXPORT const void* qt_winTchar(const QString& str, bool addnul);
  901. extern Q_EXPORT void* qt_winTchar_new(const QString& str);
  902. extern Q_EXPORT QCString qt_winQString2MB( const QString& s, int len=-1 );
  903. extern Q_EXPORT QString qt_winMB2QString( const char* mb, int len=-1 );
  904. #endif
  905.  
  906. #endif // QSTRING_H
  907.