aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
tinystr.h
Go to the documentation of this file.
1/*
2www.sourceforge.net/projects/tinyxml
3Original file by Yves Berquin.
4
5This software is provided 'as-is', without any express or implied
6warranty. In no event will the authors be held liable for any
7damages arising from the use of this software.
8
9Permission is granted to anyone to use this software for any
10purpose, including commercial applications, and to alter it and
11redistribute it freely, subject to the following restrictions:
12
131. The origin of this software must not be misrepresented; you must
14not claim that you wrote the original software. If you use this
15software in a product, an acknowledgment in the product documentation
16would be appreciated but is not required.
17
182. Altered source versions must be plainly marked as such, and
19must not be misrepresented as being the original software.
20
213. This notice may not be removed or altered from any source
22distribution.
23*/
24
25/*
26 * THIS FILE WAS ALTERED BY Tyge Lovset, 7. April 2005.
27 *
28 * - completely rewritten. compact, clean, and fast implementation.
29 * - sizeof(TiXmlString) = pointer size (4 bytes on 32-bit systems)
30 * - fixed reserve() to work as per specification.
31 * - fixed buggy compares operator==(), operator<(), and operator>()
32 * - fixed operator+=() to take a const ref argument, following spec.
33 * - added "copy" constructor with length, and most compare operators.
34 * - added replace(), clear(), size(), capacity(), operator+().
35 */
36
37#ifndef TIXML_USE_STL
39#ifndef TIXML_STRING_INCLUDED
40#define TIXML_STRING_INCLUDED
41
42#include <assert.h>
43#include <string.h>
44
45/* The support for explicit isn't that universal, and it isn't really
46 required - it is used to check that the TiXmlString class isn't incorrectly
47 used. Be nice to old compilers and macro it here:
48*/
49#if defined(_MSC_VER) && (_MSC_VER >= 1200)
50// Microsoft visual studio, version 6 and higher.
51#define TIXML_EXPLICIT explicit
52#elif defined( __GNUC__) && ( __GNUC__ >= 3)
53// GCC version 3 and higher.s
54#define TIXML_EXPLICIT explicit
55#else
56#define TIXML_EXPLICIT
57#endif
58
59/*
60 TiXmlString is an emulation of a subset of the std::string template.
61 Its purpose is to allow compiling TinyXML on compilers with no or poor STL
62 support.
63 Only the member functions relevant to the TinyXML project have been
64 implemented.
65 The buffer allocation is made by a simplistic power of 2 like mechanism : if
66 we
67 increase
68 a string and there's no more room, we allocate a buffer twice as big as we
69 need.
70*/
72 public:
73 // The size type used
74 typedef size_t size_type;
75
76 // Error value for find primitive
77 static const size_type npos; // = -1;
78
79 // TiXmlString empty constructor
82
83 // TiXmlString copy constructor
85 : rep_(0) {
86 init(copy.length());
87 memcpy(start(), copy.data(), length());
88 }
89
90 // TiXmlString constructor, based on a string
91 TIXML_EXPLICIT TiXmlString(const char* copy)
92 : rep_(0) {
93 init(static_cast< size_type >(strlen(copy)));
94 memcpy(start(), copy, length());
95 }
96
97 // TiXmlString constructor, based on a string
98 TIXML_EXPLICIT TiXmlString(const char* str, size_type len)
99 : rep_(0) {
100 init(len);
101 memcpy(start(), str, len);
102 }
103
104 // TiXmlString destructor
106
107 // = operator
108 TiXmlString& operator=(const char* copy) {
109 return assign(copy, (size_type)strlen(copy));
110 }
111
112 // = operator
114 return assign(copy.start(), copy.length());
115 }
116
117 // += operator. Maps to append
118 TiXmlString& operator+=(const char* suffix) {
119 return append(suffix, static_cast< size_type >(strlen(suffix)));
120 }
121
122 // += operator. Maps to append
123 TiXmlString& operator+=(char single) { return append(&single, 1); }
124
125 // += operator. Maps to append
127 return append(suffix.data(), suffix.length());
128 }
129
130 // Convert a TiXmlString into a null-terminated char *
131 const char* c_str() const { return rep_->str; }
132
133 // Convert a TiXmlString into a char * (need not be null terminated).
134 const char* data() const { return rep_->str; }
135
136 // Return the length of a TiXmlString
137 size_type length() const { return rep_->size; }
138
139 // Alias for length()
140 size_type size() const { return rep_->size; }
141
142 // Checks if a TiXmlString is empty
143 bool empty() const { return rep_->size == 0; }
144
145 // Return capacity of string
146 size_type capacity() const { return rep_->capacity; }
147
148 // single char extraction
149 const char& at(size_type index) const {
150 assert(index < length());
151 return rep_->str[index];
152 }
153
154 // [] operator
155 char& operator[](size_type index) const {
156 assert(index < length());
157 return rep_->str[index];
158 }
159
160 // find a char in a string. Return TiXmlString::npos if not found
161 size_type find(char lookup) const { return find(lookup, 0); }
162
163 // find a char in a string from an offset. Return TiXmlString::npos if not
164 // found
165 size_type find(char tofind, size_type offset) const {
166 if (offset >= length()) return npos;
167
168 for (const char* p = c_str() + offset; *p != '\0'; ++p) {
169 if (*p == tofind) return static_cast< size_type >(p - c_str());
170 }
171
172 return npos;
173 }
174
175 void clear() {
176 // Lee:
177 // The original was just too strange, though correct:
178 // TiXmlString().replace(*this);
179 // Instead use the quit & re-init:
180 quit();
181 init(0, 0);
182 }
183
184 /* Function to reserve a big amount of data when we know we'll need it. Be
185 aware
186 that this
187 function DOES NOT clear the content of the TiXmlString if any exists.
188 */
189 void reserve(size_type cap);
190
191 TiXmlString& assign(const char* str, size_type len);
192
193 TiXmlString& append(const char* str, size_type len);
194
195 void swap(TiXmlString& other) {
196 Rep* r = rep_;
197 rep_ = other.rep_;
198 other.rep_ = r;
199 }
200
201 private:
202 void init(size_type sz) { init(sz, sz); }
203 void set_size(size_type sz) { rep_->str[rep_->size = sz] = '\0'; }
204 char* start() const { return rep_->str; }
205 char* finish() const { return rep_->str + rep_->size; }
206
207 struct Rep {
209 char str[1];
210 };
211
212 void init(size_type sz, size_type cap) {
213 if (cap) {
214 // Lee: the original form:
215 // rep_ = static_cast<Rep*>(operator new(sizeof(Rep) + cap));
216 // doesn't work in some cases of new being overloaded. Switching
217 // to the normal allocation, although use an 'int' for systems
218 // that are overly picky about structure alignment.
219 const size_type bytesNeeded = sizeof(Rep) + cap;
220 const size_type intsNeeded = (bytesNeeded + sizeof(int) - 1) / sizeof(int);
221 rep_ = reinterpret_cast< Rep* >(new int[intsNeeded]);
222
223 rep_->str[rep_->size = sz] = '\0';
224 rep_->capacity = cap;
225 } else {
226 rep_ = &nullrep_;
227 }
228 }
229
230 void quit() {
231 if (rep_ != &nullrep_) {
232 // The rep_ is really an array of ints. (see the allocator, above).
233 // Cast it back before delete, so the compiler won't incorrectly call
234 // destructors.
235 delete[](reinterpret_cast< int* >(rep_));
236 }
237 }
238
240 static Rep nullrep_;
241};
242
243inline bool operator==(const TiXmlString& a, const TiXmlString& b) {
244 return (a.length() == b.length()) // optimization on some platforms
245 && (strcmp(a.c_str(), b.c_str()) == 0); // actual compare
246}
247inline bool operator<(const TiXmlString& a, const TiXmlString& b) {
248 return strcmp(a.c_str(), b.c_str()) < 0;
249}
250
251inline bool operator!=(const TiXmlString& a, const TiXmlString& b) {
252 return !(a == b);
253}
254inline bool operator>(const TiXmlString& a, const TiXmlString& b) { return b < a; }
255inline bool operator<=(const TiXmlString& a, const TiXmlString& b) {
256 return !(b < a);
257}
258inline bool operator>=(const TiXmlString& a, const TiXmlString& b) {
259 return !(a < b);
260}
261
262inline bool operator==(const TiXmlString& a, const char* b) {
263 return strcmp(a.c_str(), b) == 0;
264}
265inline bool operator==(const char* a, const TiXmlString& b) { return b == a; }
266inline bool operator!=(const TiXmlString& a, const char* b) { return !(a == b); }
267inline bool operator!=(const char* a, const TiXmlString& b) { return !(b == a); }
268
269TiXmlString operator+(const TiXmlString& a, const TiXmlString& b);
270TiXmlString operator+(const TiXmlString& a, const char* b);
271TiXmlString operator+(const char* a, const TiXmlString& b);
272
273/*
274 TiXmlOutStream is an emulation of std::ostream. It is based on TiXmlString.
275 Only the operators that we need for TinyXML have been developped.
276*/
278 public:
279 // TiXmlOutStream << operator.
281 *this += in;
282 return *this;
283 }
284
285 // TiXmlOutStream << operator.
286 TiXmlOutStream& operator<<(const char* in) {
287 *this += in;
288 return *this;
289 }
290};
291
292#endif // TIXML_STRING_INCLUDED
293#endif // TIXML_USE_STL
TiXmlOutStream & operator<<(const char *in)
Definition tinystr.h:286
TiXmlOutStream & operator<<(const TiXmlString &in)
Definition tinystr.h:280
size_type capacity() const
Definition tinystr.h:146
TiXmlString & operator=(const TiXmlString &copy)
Definition tinystr.h:113
const char * data() const
Definition tinystr.h:134
size_type find(char lookup) const
Definition tinystr.h:161
size_type find(char tofind, size_type offset) const
Definition tinystr.h:165
bool empty() const
Definition tinystr.h:143
TiXmlString()
Definition tinystr.h:80
size_type size() const
Definition tinystr.h:140
TIXML_EXPLICIT TiXmlString(const char *str, size_type len)
Definition tinystr.h:98
void set_size(size_type sz)
Definition tinystr.h:203
size_type length() const
Definition tinystr.h:137
void init(size_type sz)
Definition tinystr.h:202
const char & at(size_type index) const
Definition tinystr.h:149
TiXmlString & operator+=(const char *suffix)
Definition tinystr.h:118
TiXmlString & operator+=(const TiXmlString &suffix)
Definition tinystr.h:126
char * start() const
Definition tinystr.h:204
const char * c_str() const
Definition tinystr.h:131
void reserve(size_type cap)
Definition tinystr.cpp:40
static const size_type npos
Definition tinystr.h:77
TiXmlString & operator+=(char single)
Definition tinystr.h:123
void swap(TiXmlString &other)
Definition tinystr.h:195
TIXML_EXPLICIT TiXmlString(const char *copy)
Definition tinystr.h:91
TiXmlString & operator=(const char *copy)
Definition tinystr.h:108
void quit()
Definition tinystr.h:230
void clear()
Definition tinystr.h:175
size_t size_type
Definition tinystr.h:74
TiXmlString & assign(const char *str, size_type len)
Definition tinystr.cpp:49
Rep * rep_
Definition tinystr.h:239
TiXmlString(const TiXmlString &copy)
Definition tinystr.h:84
TiXmlString & append(const char *str, size_type len)
Definition tinystr.cpp:65
char * finish() const
Definition tinystr.h:205
char & operator[](size_type index) const
Definition tinystr.h:155
void init(size_type sz, size_type cap)
Definition tinystr.h:212
static Rep nullrep_
Definition tinystr.h:38
size_type size
Definition tinystr.h:208
size_type capacity
Definition tinystr.h:208
bool operator>(const TiXmlString &a, const TiXmlString &b)
Definition tinystr.h:254
bool operator<(const TiXmlString &a, const TiXmlString &b)
Definition tinystr.h:247
TiXmlString operator+(const TiXmlString &a, const TiXmlString &b)
Definition tinystr.cpp:77
bool operator==(const TiXmlString &a, const TiXmlString &b)
Definition tinystr.h:243
bool operator<=(const TiXmlString &a, const TiXmlString &b)
Definition tinystr.h:255
bool operator!=(const TiXmlString &a, const TiXmlString &b)
Definition tinystr.h:251
bool operator>=(const TiXmlString &a, const TiXmlString &b)
Definition tinystr.h:258
#define TIXML_EXPLICIT
Definition tinystr.h:56