summaryrefslogtreecommitdiffstats
path: root/qtools/qcstring.h
blob: 717045588aae71a94ef77d6615f91964c72f404b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
/****************************************************************************
**
** Copyright (C) 1997-2004 by Dimitri van Heesch.
**
** Permission to use, copy, modify, and distribute this software and its
** documentation under the terms of the GNU General Public License is hereby
** granted. No representations are made about the suitability of this software
** for any purpose. It is provided "as is" without express or implied warranty.
** See the GNU General Public License for more details.
**
** Note: this is a reimplementation of the qcstring.h that came with
** an Qt version 2.2.3. For short strings it stores the string data inside
** the object. For long strings it uses a separate array with reference counting.
**
**********************************************************************/

#ifndef QCSTRING_H
#define QCSTRING_H

#ifndef QT_H
#include "qarray.h"
#endif // QT_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#if !defined(_OS_WIN32_) || defined(__MINGW32__)
#include <stdint.h>
#endif

#if defined(_OS_SUN_) && defined(_CC_GNU_)
#include <strings.h>
#endif

#include <assert.h>

class QGString;

/*****************************************************************************
  Fixes and workarounds for some platforms
 *****************************************************************************/

#if defined(_OS_HPUX_)
// HP-UX has badly defined strstr() etc.
// ### fix in 3.0: change hack_* to qt_hack_*
// by the way HP-UX is probably right, the standard has evolved and
// we'll have to adapt to it
inline char *hack_strstr( const char *s1, const char *s2 )
{ return (char *)strstr(s1, s2); }
inline char *hack_strchr( const char *s, int c )
{ return (char *)strchr(s, c); }
inline char *hack_strrchr( const char *s, int c )
{ return (char *)strrchr(s, c); }
#define strstr(s1,s2)	hack_strstr((s1),(s2))
#define strchr(s,c)	hack_strchr((s),(c))
#define strrchr(s,c)	hack_strrchr((s),(c))
#endif

/*****************************************************************************
  Safe and portable C string functions; extensions to standard string.h
 *****************************************************************************/

Q_EXPORT void *qmemmove( void *dst, const void *src, uint len );

#if defined(_OS_SUN_) || defined(_CC_OC_)
#define memmove(s1,s2,n) qmemmove((s1),(s2),(n))
#endif

#if defined(_OS_WIN32_)
#define qsnprintf _snprintf
#else
#define qsnprintf snprintf
#endif

Q_EXPORT char *qstrdup( const char * );

Q_EXPORT inline uint cstrlen( const char *str )
{ return (uint)strlen(str); }

Q_EXPORT inline uint qstrlen( const char *str )
{ return str ? (uint)strlen(str) : 0; }

Q_EXPORT inline char *cstrcpy( char *dst, const char *src )
{ return strcpy(dst,src); }

Q_EXPORT inline char *qstrcpy( char *dst, const char *src )
{ return src ? strcpy(dst, src) : 0; }

Q_EXPORT char * qstrncpy(char *src,const char *dst, uint len);

Q_EXPORT inline int cstrcmp( const char *str1, const char *str2 )
{ return strcmp(str1,str2); }

Q_EXPORT inline int qstrcmp( const char *str1, const char *str2 )
{ return (str1 && str2) ? strcmp(str1,str2) : (int)((intptr_t)str2 - (intptr_t)str1); }

Q_EXPORT inline int cstrncmp( const char *str1, const char *str2, uint len )
{ return strncmp(str1,str2,len); }

Q_EXPORT inline int qstrncmp( const char *str1, const char *str2, uint len )
{ return (str1 && str2) ? strncmp(str1,str2,len) :
			  (int)((intptr_t)str2 - (intptr_t)str1); }

Q_EXPORT int qstricmp( const char *str1, const char *str2 );

Q_EXPORT int qstrnicmp( const char *str1, const char *str2, uint len );

/*****************************************************************************
  QByteArray class
 *****************************************************************************/

#if defined(Q_TEMPLATEDLL)
template class Q_EXPORT QArray<char>;
#endif
typedef QArray<char> QByteArray;

/*****************************************************************************
  QByteArray stream functions
 *****************************************************************************/
#ifndef QT_NO_DATASTREAM
Q_EXPORT QDataStream &operator<<( QDataStream &, const QByteArray & );
Q_EXPORT QDataStream &operator>>( QDataStream &, QByteArray & );
#endif

class QRegExp;

/** This is an alternative implementation of QCString. It provides basically
 *  the same functions but uses reference counting and copy on write.
 */
class QCString
{
public:
    /** creates an empty string */
    QCString()
    {
    }

    /** destroys the string */
   ~QCString()
    {
    }

    /** makes a copy of a string. */
    QCString( const QCString &s ) : m_rep(s.m_rep)
    {
    }

    /** creates a string with room for size characters
     *  @param[in] size the number of character to allocate (including the 0-terminator)
     */
    QCString( int size ) : m_rep(size)
    {
    }

    /** creates a string from a plain C string.
     *  @param[in] str A zero terminated C string. When 0 an empty string is created.
     */
    QCString( const char *str ) : m_rep(str)
    {
    }

    /** creates a string from \a str and copies over the first \a maxlen characters. */
    QCString( const char *str, uint maxlen ) : m_rep(str,maxlen)
    {
    }

    /** replaces the contents by that of string \a s. */
    QCString &operator=( const QCString &s )
    {
      m_rep = s.m_rep;
      return *this;
    }

    /** replaces the contents by that of C string \a str. */
    QCString &operator=( const char *str)
    {
      m_rep = str;
      return *this;
    }

    /** Returns TRUE iff the string is empty. Equivalent to isEmpty(). */
    bool isNull() const
    {
      return m_rep.isEmpty();
    }

    /** Returns TRUE iff the string is empty */
    bool isEmpty() const
    {
      return m_rep.isEmpty();
    }

    /** Returns the length of the string, excluding the 0-terminator. Equivalent to size(). */
    uint length() const
    {
      return m_rep.length();
    }

    /** Returns the length of the string, excluding the 0-terminator. */
    uint size() const
    {
      return m_rep.length();
    }

    /** Returns a pointer to the contents of the string in the form of a 0-terminated C string */
    char *data() const
    {
      return m_rep.data();
    }

    /** Resizes the string to hold \a newlen characters
     *  (this value should include the 0-terminator). If the string is enlarged the contents will
     *  be left unmodified.
     */
    bool resize( uint newlen )
    {
      m_rep.resize(newlen);
      return TRUE;
    }

    /** Truncates the string at position \a pos. */
    bool truncate( uint pos )
    {
      return resize(pos+1);
    }

    /** Fills a string with a predefined character
     *  @param[in] c the character used to fill the string with.
     *  @param[in] len the number of character to fill. Use -1 to fill the whole string.
     *  @note the string will be resized to contain \a len characters. The contents of the
     *  string will be lost.
     */
    bool fill( char c, int len = -1 )
    {
      m_rep.fill(c,len);
      return TRUE;
    }

    /** Returns a deep copy of the string. */
    QCString copy() const
    {
      if (length()==0) return QCString();
      QCString cs(length()+1);
      memcpy(cs.data(),data(),length());
      return cs;
    }

    QCString &sprintf( const char *format, ... );
    int	find( char c, int index=0, bool cs=TRUE ) const;
    int	find( const char *str, int index=0, bool cs=TRUE ) const;
    int find( const QCString &str, int index=0, bool cs=TRUE ) const;
    int	find( const QRegExp &rx, int index=0 ) const;
    int	findRev( char c, int index=-1, bool cs=TRUE) const;
    int	findRev( const char *str, int index=-1, bool cs=TRUE) const;
    int	findRev( const QRegExp &rx, int index=-1 ) const;
    int	contains( char c, bool cs=TRUE ) const;
    int	contains( const char *str, bool cs=TRUE ) const;
    int contains( const QRegExp &rx ) const;
    bool stripPrefix(const char *prefix);
    QCString left( uint len ) const;
    QCString right( uint len ) const;
    QCString mid( uint index, uint len=0xffffffff) const;
    QCString lower() const;
    QCString upper() const;
    QCString stripWhiteSpace() const;
    QCString simplifyWhiteSpace() const;
    QCString &assign( const char *str );
    QCString &insert( uint index, const char *s );
    QCString &insert( uint index, char c);
    QCString &append( const char *s );
    QCString &prepend( const char *s );
    QCString &remove( uint index, uint len );
    QCString &replace( uint index, uint len, const char *s);
    QCString &replace( const QRegExp &rx, const char *str );
    short toShort( bool *ok=0 ) const;
    ushort toUShort( bool *ok=0 ) const;
    int	toInt( bool *ok=0 ) const;
    uint toUInt( bool *ok=0 ) const;
    long toLong( bool *ok=0 ) const;
    ulong toULong( bool *ok=0 )	const;
    QCString &setNum(short n);
    QCString &setNum(ushort n);
    QCString &setNum(int n);
    QCString &setNum(uint n);
    QCString &setNum(long n);
    QCString &setNum(ulong n);

    /** Converts the string to a plain C string */
    operator const char *() const
    {
      return (const char *)data();
    }

    /** Appends string \a str to this string and returns a reference to the result. */
    QCString &operator+=( const char *str )
    {
      if (!str) return *this;
      int len1 = length();
      int len2 = strlen(str);
      resize(len1+len2+1);
      memcpy(data()+len1,str,len2);
      return *this;
    }

    /** Appends character \a c to this string and returns a reference to the result. */
    QCString &operator+=( char c )
    {
      int len = length();
      resize(len+2);
      data()[len]=c;
      return *this;
    }

    /** Returns a reference to the character at index \a i. */
    char &at( uint i) const
    {
      return m_rep.at(i);
    }

    /** Indexing operator. Equavalent to at(). */
    char &operator[]( int i ) const
    {
      return m_rep.at((uint)i);
    }

  private:

    struct LSData;

    // long string representation
    struct LongStringRep
    {
      uchar isShort : 1;  // should be shared with ShortStringRep
      uchar : 7;
      LSData *d;
    };

#define SHORT_STR_CAPACITY ((int)sizeof(LongStringRep)-1)
#define SHORT_STR_MAX_LEN (SHORT_STR_CAPACITY-1)

    // short string representation
    struct ShortStringRep
    {
      uchar isShort : 1;  // should be shared with LongStringRep
      uchar len     : 7;
      char str[SHORT_STR_CAPACITY]; // size including 0-terminator
    };

    // ref counting string header
    struct LSHeader
    {
      int           len;      // length of string without 0 terminator
      int           refCount; // -1=leaked, 0=one ref & non-cost, n>0, n+1 refs, const
    };
    // ref counting string data and methods
    struct LSData : public LSHeader
    {
      char *toStr()
      {
        return (char*)(this+1); // string data starts after the header
      }

      // creates a LSData item with room for size bytes (which includes the 0 terminator!)
      // if size is zero, an empty string will be created.
      static LSData *create(int size)
      {
        LSData *data;
        data = (LSData*)malloc(sizeof(LSHeader)+size);
        data->len = size-1;
        data->refCount = 0;
        data->toStr()[size-1] = 0;
        return data;
      }
      // remove out reference to the data. Frees memory if no more users
      void dispose()
      {
        if (--refCount<0) free(this);
      }

      // resizes LSData so it can hold size bytes (which includes the 0 terminator!)
      // Since this is for long strings only, size should be > SHORT_STR_CAPACITY
      static LSData *resize(LSData *d,int size)
      {
        if (d->len>0 && d->refCount==0) // non-const, non-empty
        {
          d = (LSData*)realloc(d,sizeof(LSHeader)+size);
          d->len = size-1;
          d->toStr()[size-1] = 0;
          return d;
        }
        else // need to make a copy
        {
          LSData *newData = LSData::create(size);
          int len = d->len;
          if (len>=size) len=size-1;
          memcpy(newData->toStr(),d->toStr(),len);
          newData->toStr()[len]=0;
          d->dispose();
          return newData;
        }
      }
    };

    class StringRep
    {
      public:
        StringRep()
        {
          u.s.isShort=TRUE;
          u.s.len=0;
        }
       ~StringRep()
        {
          if (!u.s.isShort)
          {
            u.l.d->dispose();
          }
        }
        StringRep(const StringRep &s)
        {
          if (&s!=this)
          {
            u = s.u;
            if (!u.s.isShort)
            {
              u.l.d->refCount++;
            }
          }
        }
        StringRep(int size)
        {
          u.s.isShort = size<=SHORT_STR_CAPACITY;
          if (u.s.isShort) // init short string
          {
            if (size>0)
            {
              u.s.len = size-1;
              u.s.str[size-1]='\0';
            }
            else
            {
              u.s.len = 0;
            }
          }
          else // int long string
          {
            u.l.d = LSData::create(size);
          }
        }
        StringRep(const char *str)
        {
          if (str)
          {
            int len = strlen(str);
            u.s.isShort = len<=SHORT_STR_MAX_LEN;
            if (u.s.isShort)
            {
              u.s.len = len;
              memcpy(u.s.str,str,len+1);
            }
            else
            {
              u.l.d = LSData::create(len+1);
              memcpy(u.l.d->toStr(),str,u.l.d->len);
            }
          }
          else // create empty string
          {
            u.s.isShort=TRUE;
            u.s.len=0;
          }
        }
        StringRep( const char *str, uint maxlen )
        {
          if (str && maxlen>0)
          {
            uint len=strlen(str);
            if (len>maxlen) len=maxlen;
            u.s.isShort = len<=SHORT_STR_MAX_LEN;
            if (u.s.isShort)
            {
              u.s.len = len;
              memcpy(u.s.str,str,len);
              u.s.str[len]='\0';
            }
            else
            {
              u.l.d = LSData::create(len+1);
              memcpy(u.l.d->toStr(),str,len);
            }
          }
          else // create empty string
          {
            u.s.isShort=TRUE;
            u.s.len=0;
          }
        }
        StringRep &operator=(const StringRep &s)
        {
          if (!u.s.isShort)
          {
            u.l.d->dispose();
          }
          if (s.u.s.isShort) // copy by value
          {
            u.s = s.u.s;
          }
          else // copy by reference
          {
            u.l.isShort=FALSE;
            u.l.d = s.u.l.d;
            u.l.d->refCount++;
          }
          return *this;
        }
        StringRep &operator=(const char *str)
        {
          if (!u.s.isShort)
          {
            u.l.d->dispose();
          }
          if (str)
          {
            int len = strlen(str);
            u.s.isShort = len<=SHORT_STR_MAX_LEN;
            if (u.s.isShort)
            {
              u.s.len = len;
              memcpy(u.s.str,str,len+1);
            }
            else
            {
              u.l.d = LSData::create(len+1);
              memcpy(u.l.d->toStr(),str,u.l.d->len);
            }
          }
          else
          {
            u.s.isShort=TRUE;
            u.s.len=0;
          }
          return *this;
        }
        bool isEmpty() const
        {
          return u.s.isShort && u.s.len==0;
        }
        uint length() const
        {
          return u.s.isShort ? u.s.len : u.l.d->len;
        }
        char *data() const
        {
          if (u.s.isShort)
          {
            return u.s.len==0 ? 0 : (char*)u.s.str;
          }
          else
          {
            return u.l.d->len==0 ? 0 : u.l.d->toStr();
          }
        }
        char &at(int i) const
        {
          if (u.s.isShort)
          {
            return (char&)u.s.str[i];
          }
          else
          {
            return u.l.d->toStr()[i];
          }
        }
        bool resize( uint newlen )
        {
          if (u.s.isShort && newlen<=SHORT_STR_CAPACITY) // resize short string
          {
            if (newlen>0)
            {
              u.s.len = newlen-1;
              u.s.str[newlen-1]='\0';
            }
            else // string becomes empty
            {
              u.s.len = 0;
            }
          }
          else if (u.s.isShort) // turn short string into long string
          {
            StringRep tmp = *this;
            u.s.isShort=FALSE;
            u.l.d = LSData::create(newlen);
            if (tmp.u.s.len>0)
            {
              memcpy(u.l.d->toStr(),tmp.u.s.str,tmp.u.s.len+1);
            }
            else
            {
              u.l.d->toStr()[0]='\0';
            }
          }
          else if (!u.s.isShort && newlen<=SHORT_STR_CAPACITY) // turn long string into short string
          {
            if (newlen>0)
            {
              StringRep tmp(newlen); // copy short part into tmp buffer
              memcpy(tmp.u.s.str,u.l.d->toStr(),newlen-1);
              tmp.u.s.str[newlen-1]='\0';
              u.l.d->dispose();
              u.s = tmp.u.s;
            }
            else
            {
              u.l.d->dispose();
              u.s.isShort=TRUE;
              u.s.len=0;
            }
          }
          else // resize long string
          {
            u.l.d = u.l.d->resize(u.l.d,newlen);
          }
          return TRUE;
        }
        bool fill( char c, int len )
        {
          if (len<0) len=length();
          if (len!=(int)length())
          {
            if (len>0)
            {
              resize(len+1);
            }
            else
            {
              if (!u.s.isShort)
              {
                u.l.d->dispose();
              }
              u.s.isShort=TRUE;
              u.s.len=0;
            }
          }
          if (len>0)
          {
            memset(data(),c,len);
          }
          return TRUE;
        }
      private:
        union
        {
          ShortStringRep s;
          LongStringRep  l;
        } u;
    };
    StringRep m_rep;
};

/*****************************************************************************
  QCString stream functions
 *****************************************************************************/
#ifndef QT_NO_DATASTREAM
Q_EXPORT QDataStream &operator<<( QDataStream &, const QCString & );
Q_EXPORT QDataStream &operator>>( QDataStream &, QCString & );
#endif

/*****************************************************************************
  QCString non-member operators
 *****************************************************************************/

Q_EXPORT inline bool operator==( const QCString &s1, const QCString &s2 )
{ return qstrcmp(s1.data(),s2.data()) == 0; }

Q_EXPORT inline bool operator==( const QCString &s1, const char *s2 )
{ return qstrcmp(s1.data(),s2) == 0; }

Q_EXPORT inline bool operator==( const char *s1, const QCString &s2 )
{ return qstrcmp(s1,s2.data()) == 0; }

Q_EXPORT inline bool operator!=( const QCString &s1, const QCString &s2 )
{ return qstrcmp(s1.data(),s2.data()) != 0; }

Q_EXPORT inline bool operator!=( const QCString &s1, const char *s2 )
{ return qstrcmp(s1.data(),s2) != 0; }

Q_EXPORT inline bool operator!=( const char *s1, const QCString &s2 )
{ return qstrcmp(s1,s2.data()) != 0; }

Q_EXPORT inline bool operator<( const QCString &s1, const QCString& s2 )
{ return qstrcmp(s1.data(),s2.data()) < 0; }

Q_EXPORT inline bool operator<( const QCString &s1, const char *s2 )
{ return qstrcmp(s1.data(),s2) < 0; }

Q_EXPORT inline bool operator<( const char *s1, const QCString &s2 )
{ return qstrcmp(s1,s2.data()) < 0; }

Q_EXPORT inline bool operator<=( const QCString &s1, const char *s2 )
{ return qstrcmp(s1.data(),s2) <= 0; }

Q_EXPORT inline bool operator<=( const char *s1, const QCString &s2 )
{ return qstrcmp(s1,s2.data()) <= 0; }

Q_EXPORT inline bool operator>( const QCString &s1, const char *s2 )
{ return qstrcmp(s1.data(),s2) > 0; }

Q_EXPORT inline bool operator>( const char *s1, const QCString &s2 )
{ return qstrcmp(s1,s2.data()) > 0; }

Q_EXPORT inline bool operator>=( const QCString &s1, const char *s2 )
{ return qstrcmp(s1.data(),s2) >= 0; }

Q_EXPORT inline bool operator>=( const char *s1, const QCString &s2 )
{ return qstrcmp(s1,s2.data()) >= 0; }

Q_EXPORT inline QCString operator+( const QCString &s1, const QCString &s2 )
{
    QCString tmp(s1);
    tmp += s2;
    return tmp;
}


inline QCString operator+( const QCString &s1, const QGString &s2 );
inline QCString operator+( const QGString &s1, const QCString &s2 );


Q_EXPORT inline QCString operator+( const QCString &s1, const char *s2 )
{
    QCString tmp(s1);
    tmp += s2;
    return tmp;
}

Q_EXPORT inline QCString operator+( const char *s1, const QCString &s2 )
{
    QCString tmp(s1);
    tmp += s2;
    return tmp;
}

Q_EXPORT inline QCString operator+( const QCString &s1, char c2 )
{
    QCString tmp( s1.data() );
    tmp += c2;
    return tmp;
}

Q_EXPORT inline QCString operator+( char c1, const QCString &s2 )
{
    QCString tmp;
    tmp += c1;
    tmp += s2;
    return tmp;
}

inline const char *qPrint(const char *s)
{
  if (s) return s; else return "";
}

inline const char *qPrint(const QCString &s)
{
  if (!s.isEmpty()) return s.data(); else return "";
}

#endif // QCSTRING_H