|
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 findRev( char c, int index=-1, bool cs=TRUE) const; |
|
62 int findRev( const char *str, int index=-1, bool cs=TRUE) const; |
|
63 int findRev( const QRegExp &, int index=-1 ) const; |
|
64 int contains( char c, bool cs=TRUE ) const; |
|
65 int contains( const char *str, bool cs=TRUE ) const; |
|
66 int contains( const QRegExp & ) const; |
|
67 bool stripPrefix(const char *prefix); |
|
68 |
|
69 SCString left( uint len ) const; |
|
70 SCString right( uint len ) const; |
|
71 SCString mid( uint index, uint len=0xffffffff) const; |
|
72 |
|
73 SCString lower() const; |
|
74 SCString upper() const; |
|
75 |
|
76 SCString stripWhiteSpace() const; |
|
77 SCString simplifyWhiteSpace() const; |
|
78 |
|
79 SCString &assign( const char *str ); |
|
80 SCString &insert( uint index, const char * ); |
|
81 SCString &insert( uint index, char ); |
|
82 SCString &append( const char *s ); |
|
83 SCString &prepend( const char *s ); |
|
84 SCString &remove( uint index, uint len ); |
|
85 SCString &replace( uint index, uint len, const char * ); |
|
86 SCString &replace( const QRegExp &, const char * ); |
|
87 |
|
88 short toShort( bool *ok=0 ) const; |
|
89 ushort toUShort( bool *ok=0 ) const; |
|
90 int toInt( bool *ok=0 ) const; |
|
91 uint toUInt( bool *ok=0 ) const; |
|
92 long toLong( bool *ok=0 ) const; |
|
93 ulong toULong( bool *ok=0 ) const; |
|
94 |
|
95 SCString &setNum( short ); |
|
96 SCString &setNum( ushort ); |
|
97 SCString &setNum( int ); |
|
98 SCString &setNum( uint ); |
|
99 SCString &setNum( long ); |
|
100 SCString &setNum( ulong ); |
|
101 QCString &setNum( float, char f='g', int prec=6 ); |
|
102 QCString &setNum( double, char f='g', int prec=6 ); |
|
103 |
|
104 operator const char *() const; |
|
105 SCString &operator+=( const char *str ); |
|
106 SCString &operator+=( char c ); |
|
107 char &at( uint index ) const; |
|
108 char &operator[]( int i ) const { return at(i); } |
|
109 |
|
110 private: |
|
111 static void msg_index( uint ); |
|
112 void duplicate( const SCString &s ); |
|
113 void duplicate( const char *str); |
|
114 SCString &duplicate( const char *str, int); |
|
115 |
|
116 char * m_data; |
|
117 }; |
|
118 |
|
119 inline char &SCString::at( uint index ) const |
|
120 { |
|
121 return m_data[index]; |
|
122 } |
|
123 |
|
124 inline void SCString::duplicate( const SCString &s ) |
|
125 { |
|
126 if (!s.isEmpty()) |
|
127 { |
|
128 uint l = strlen(s.data()); |
|
129 m_data = (char *)malloc(l+1); |
|
130 if (m_data) memcpy(m_data,s.data(),l+1); |
|
131 } |
|
132 else |
|
133 m_data=0; |
|
134 } |
|
135 inline void SCString::duplicate( const char *str) |
|
136 { |
|
137 if (str && str[0]!='\0') |
|
138 { |
|
139 uint l = strlen(str); |
|
140 m_data = (char *)malloc(l+1); |
|
141 if (m_data) memcpy(m_data,str,l+1); |
|
142 } |
|
143 else |
|
144 m_data=0; |
|
145 } |
|
146 inline SCString &SCString::duplicate( const char *str, int) |
|
147 { |
|
148 if (m_data) free(m_data); |
|
149 duplicate(str); |
|
150 return *this; |
|
151 } |
|
152 |
|
153 #endif |
|
154 |