Fix for UBSan build
[platform/upstream/doxygen.git] / qtools / scstring.h
1 /******************************************************************************
2  *
3  * Copyright (C) 1997-2004 by Dimitri van Heesch.
4  *
5  * Permission to use, copy, modify, and distribute this software and its
6  * documentation under the terms of the GNU General Public License is hereby 
7  * granted. No representations are made about the suitability of this software 
8  * for any purpose. It is provided "as is" without express or implied warranty.
9  * See the GNU General Public License for more details.
10  *
11  * Documents produced by Doxygen are derivative works derived from the
12  * input used in their production; they are not affected by this license.
13  *
14  */
15
16 #ifndef SCSTRING_H
17 #define SCSTRING_H
18
19 #include <stdlib.h>
20
21 class QRegExp;
22
23 /** This is an alternative implementation of QCString. It provides basically
24  *  the same functions but uses less memory for administration. This class
25  *  is just a wrapper around a plain C string requiring only 4 bytes "overhead".
26  *  QCString features sharing of data and stores the string length, but 
27  *  requires 4 + 12 bytes for this (even for the empty string). As doxygen 
28  *  uses a LOT of string during a run it saves a lot of memory to use a 
29  *  more memory efficient implementation at the cost of relatively low
30  *  runtime overhead.
31  */
32 class SCString 
33 {
34 public:
35     SCString() : m_data(0) {}           // make null string
36     SCString( const SCString &s );
37     SCString( int size );
38     SCString( const char *str );
39     SCString( const char *str, uint maxlen );
40     ~SCString();
41
42     SCString    &operator=( const SCString &s );// deep copy
43     SCString    &operator=( const char *str );  // deep copy
44
45     bool        isNull()        const;
46     bool        isEmpty()       const;
47     uint        length()        const;
48     uint        size()          const { return m_data ? length()+1 : 0; }
49     char *      data()          const { return m_data; }
50     bool        resize( uint newlen );
51     bool        truncate( uint pos );
52     bool        fill( char c, int len = -1 );
53
54     SCString    copy()  const;
55
56     SCString    &sprintf( const char *format, ... );
57
58     int         find( char c, int index=0, bool cs=TRUE ) const;
59     int         find( const char *str, int index=0, bool cs=TRUE ) const;
60     int         find( const QRegExp &, int index=0 ) const;
61     int         find( const QCString &str, int index, bool cs ) const;
62     int         findRev( char c, int index=-1, bool cs=TRUE) const;
63     int         findRev( const char *str, int index=-1, bool cs=TRUE) const;
64     int         findRev( const QRegExp &, int index=-1 ) const;
65     int         contains( char c, bool cs=TRUE ) const;
66     int         contains( const char *str, bool cs=TRUE ) const;
67     int         contains( const QRegExp & ) const;
68     bool        stripPrefix(const char *prefix);
69
70     SCString    left( uint len )  const;
71     SCString    right( uint len ) const;
72     SCString    mid( uint index, uint len=0xffffffff) const;
73
74     SCString    lower() const;
75     SCString    upper() const;
76
77     SCString    stripWhiteSpace()       const;
78     SCString    simplifyWhiteSpace()    const;
79
80     SCString    &assign( const char *str );
81     SCString    &insert( uint index, const char * );
82     SCString    &insert( uint index, char );
83     SCString    &append( const char *s );
84     SCString    &prepend( const char *s );
85     SCString    &remove( uint index, uint len );
86     SCString    &replace( uint index, uint len, const char * );
87     SCString    &replace( const QRegExp &, const char * );
88
89     short       toShort( bool *ok=0 )   const;
90     ushort      toUShort( bool *ok=0 )  const;
91     int         toInt( bool *ok=0 )     const;
92     uint        toUInt( bool *ok=0 )    const;
93     long        toLong( bool *ok=0 )    const;
94     ulong       toULong( bool *ok=0 )   const;
95
96     SCString    &setNum( short );
97     SCString    &setNum( ushort );
98     SCString    &setNum( int );
99     SCString    &setNum( uint );
100     SCString    &setNum( long );
101     SCString    &setNum( ulong );
102     QCString    &setNum( float, char f='g', int prec=6 );
103     QCString    &setNum( double, char f='g', int prec=6 );
104
105                 operator const char *() const;
106     SCString    &operator+=( const char *str );
107     SCString    &operator+=( char c );
108     char &at( uint index ) const;
109     char &operator[]( int i ) const { return at(i); }
110     
111   private:
112     static void msg_index( uint );
113     void duplicate( const SCString &s );
114     void duplicate( const char *str);
115     SCString &duplicate( const char *str, int);
116
117     char *      m_data;
118 };
119
120 inline char &SCString::at( uint index ) const
121 {
122   return m_data[index];
123 }
124
125 inline void SCString::duplicate( const SCString &s )
126 {
127   if (!s.isEmpty()) 
128   {
129     uint l = strlen(s.data());
130     m_data = (char *)malloc(l+1);
131     if (m_data) memcpy(m_data,s.data(),l+1);
132   }
133   else 
134     m_data=0; 
135 }
136 inline void SCString::duplicate( const char *str)
137 {
138   if (str && str[0]!='\0') 
139   {
140     uint l = strlen(str);
141     m_data = (char *)malloc(l+1);
142     if (m_data) memcpy(m_data,str,l+1);
143   }
144   else 
145     m_data=0;
146 }
147 inline SCString &SCString::duplicate( const char *str, int)
148 {
149   if (m_data) free(m_data);
150   duplicate(str);
151   return *this;
152 }
153
154 #endif
155