WvStreams
wvstring.h
1/* -*- Mode: C++ -*-
2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
4 *
5 * Implementation of a simple and efficient printable-string class.
6 *
7 * It leaves out many of the notational conveniences provided by other
8 * string classes, because they waste too much CPU time and space.
9 * It does the one thing really missing from char* strings, that is,
10 * dynamic buffer management.
11 *
12 * The 'str' member is the actual (char*) string. You should never
13 * need to access it directly.
14 */
15#ifndef __WVSTRING_H
16#define __WVSTRING_H
17
18#include <string.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <string> // no code is actually used from here
22
23
24/* 1 byte for terminating NUL */
25#define WVSTRING_EXTRA 1
26
27
28#define __WVS_F(n) WvStringParm __wvs_##n
29#define __WVS_FORM(n) WvStringParm __wvs_##n = WvFastString::null
30#define WVSTRING_FORMAT_DECL WvStringParm __wvs_format, \
31 WvStringParm __wvs_a0, \
32 __WVS_FORM( a1), __WVS_FORM( a2), __WVS_FORM( a3), \
33 __WVS_FORM( a4), __WVS_FORM( a5), __WVS_FORM( a6), \
34 __WVS_FORM( a7), __WVS_FORM( a8), __WVS_FORM( a9), \
35 __WVS_FORM(a10), __WVS_FORM(a11), __WVS_FORM(a12), \
36 __WVS_FORM(a13), __WVS_FORM(a14), __WVS_FORM(a15), \
37 __WVS_FORM(a16), __WVS_FORM(a17), __WVS_FORM(a18), \
38 __WVS_FORM(a19)
39#define WVSTRING_FORMAT_DEFN WvStringParm __wvs_format, \
40 WvStringParm __wvs_a0, \
41 __WVS_F( a1), __WVS_F( a2), __WVS_F( a3), \
42 __WVS_F( a4), __WVS_F( a5), __WVS_F( a6), \
43 __WVS_F( a7), __WVS_F( a8), __WVS_F( a9), \
44 __WVS_F(a10), __WVS_F(a11), __WVS_F(a12), \
45 __WVS_F(a13), __WVS_F(a14), __WVS_F(a15), \
46 __WVS_F(a16), __WVS_F(a17), __WVS_F(a18), \
47 __WVS_F(a19)
48#define WVSTRING_FORMAT_CALL __wvs_format, __wvs_a0, \
49 __wvs_a1, __wvs_a2, __wvs_a3, __wvs_a4, __wvs_a5, \
50 __wvs_a6, __wvs_a7, __wvs_a8, __wvs_a9, __wvs_a10, \
51 __wvs_a11, __wvs_a12, __wvs_a13, __wvs_a14, __wvs_a15, \
52 __wvs_a16, __wvs_a17, __wvs_a18, __wvs_a19
53
54struct WvStringBuf;
55class WvFastString;
56class WvString;
57class QString; // for operator QString()
58class QCString;
59
60// all WvFastString objects are const - they should _only_ be created
61// automatically by automatic typecasting in parameter passing. So let's
62// create a handy alias.
63typedef const WvFastString & WvStringParm;
64
65
66
68{
69 size_t size; // string length - if zero, use strlen!!
70 unsigned links; // number of WvStrings using this buf.
71 char data[1]; // optional room for extra string data
72};
73
74
75// the _actual_ space taken by a WvStringBuf, without the data[] array
76// (which is variable-sized, not really 1 byte)
77#define WVSTRINGBUF_SIZE(s) (s->data - (char *)s)
78
94{
95 friend class WvString; // so WvString can access members of _other_ objects
96
97protected:
98 WvStringBuf *buf;
99 char *str;
100
101 // WvStringBuf used for char* strings that have not been cloned.
102 static WvStringBuf nullbuf;
103
104public:
105 // a null string, converted to char* as "(nil)"
106 static const WvFastString null;
107
114 WvFastString();
115 void setsize(size_t i);
116
121 WvFastString offset(size_t i) const;
122
129 WvFastString(const WvFastString &s);
130 WvFastString(const WvString &s);
131
138 WvFastString(const char *_str);
139
144 WvFastString(const QString &s);
145 WvFastString(const QCString &s);
146
151 inline WvFastString(const std::string &s);
152
157 WvFastString(short i);
158 WvFastString(unsigned short i);
159 WvFastString(int i);
160 WvFastString(unsigned int i);
161 WvFastString(long i);
162 WvFastString(unsigned long i);
163 WvFastString(long long i);
164 WvFastString(unsigned long long i);
165 WvFastString(double i);
166
168 static void do_format(WvFastString &output, const char *format,
169 const WvFastString * const *a);
170
171
188 WvFastString(WVSTRING_FORMAT_DECL)
189 {
190 const WvFastString *x[20];
191
192 x[ 0] = (&__wvs_a0 != &null)? &__wvs_a0 : 0;
193 x[ 1] = (&__wvs_a1 != &null)? &__wvs_a1 : 0;
194 x[ 2] = (&__wvs_a2 != &null)? &__wvs_a2 : 0;
195 x[ 3] = (&__wvs_a3 != &null)? &__wvs_a3 : 0;
196 x[ 4] = (&__wvs_a4 != &null)? &__wvs_a4 : 0;
197 x[ 5] = (&__wvs_a5 != &null)? &__wvs_a5 : 0;
198 x[ 6] = (&__wvs_a6 != &null)? &__wvs_a6 : 0;
199 x[ 7] = (&__wvs_a7 != &null)? &__wvs_a7 : 0;
200 x[ 8] = (&__wvs_a8 != &null)? &__wvs_a8 : 0;
201 x[ 9] = (&__wvs_a9 != &null)? &__wvs_a9 : 0;
202 x[10] = (&__wvs_a10 != &null)? &__wvs_a10 : 0;
203 x[11] = (&__wvs_a11 != &null)? &__wvs_a11 : 0;
204 x[12] = (&__wvs_a12 != &null)? &__wvs_a12 : 0;
205 x[13] = (&__wvs_a13 != &null)? &__wvs_a13 : 0;
206 x[14] = (&__wvs_a14 != &null)? &__wvs_a14 : 0;
207 x[15] = (&__wvs_a15 != &null)? &__wvs_a15 : 0;
208 x[16] = (&__wvs_a16 != &null)? &__wvs_a16 : 0;
209 x[17] = (&__wvs_a17 != &null)? &__wvs_a17 : 0;
210 x[18] = (&__wvs_a18 != &null)? &__wvs_a18 : 0;
211 x[19] = (&__wvs_a19 != &null)? &__wvs_a19 : 0;
212
213 link(&nullbuf, NULL);
214 do_format(*this, __wvs_format.str, x);
215 }
216
218
219 /*
220 * Figure out the length of this string. ==0 if NULL or empty.
221 */
222 size_t len() const;
223
224protected:
225 void construct(const char *_str);
226
227 // this doesn't exist - it's just here to keep it from being auto-created
228 // by stupid C++.
229 WvFastString &operator= (const WvFastString &s2);
230
231 // connect/disconnect ourselves from a WvStringBuf.
232 void link(WvStringBuf *_buf, const char *_str);
233 void unlink();
234
235 // allocate new space for buffers - needed only by the (int i) constructor,
236 // for now.
237 WvStringBuf *alloc(size_t size);
238 void newbuf(size_t size);
239
240public:
241 // string comparison
242 bool operator== (WvStringParm s2) const;
243 bool operator!= (WvStringParm s2) const;
244 bool operator< (WvStringParm s2) const;
245 bool operator== (const char *s2) const;
246 bool operator!= (const char *s2) const;
247 bool operator< (const char *s2) const;
248
250 bool operator! () const;
251
252 // pointer arithmetic
253 const char *operator+ (int i) const
254 { return str + i; }
255 const char *operator- (int i) const
256 { return str - i; }
257
259 operator const char*() const
260 { return str; }
261
267 const char *cstr() const
268 { return str; }
269
274 operator QString() const;
275
280 //inline operator std::string() const;
281
286 int num() const
287 { return str ? atoi(str) : 0; }
288
290 bool isnull() const
291 { return str == NULL; }
292
294 const WvFastString &ifnull(WvStringParm defval) const
295 { return isnull() ? defval : *this; }
296};
297
298
329class WvString : public WvFastString
330{
331public:
332 // an empty string, converted to char* as ""
333 static const WvString empty;
334
335 WvString() {} // nothing special needed
336 WvString(short i) : WvFastString(i) { } // nothing special
337 WvString(unsigned short i) : WvFastString(i) { } // nothing special
338 WvString(int i) : WvFastString(i) { } // nothing special
339 WvString(unsigned int i) : WvFastString(i) { } // nothing special
340 WvString(long i) : WvFastString(i) { } // nothing special
341 WvString(unsigned long i) : WvFastString(i) { } // nothing special
342 WvString(long long i) : WvFastString(i) { } // nothing special
343 WvString(unsigned long long i) : WvFastString(i) { } // nothing special
344 WvString(double i) : WvFastString(i) { } // nothing special
345
353 { copy_constructor(s); }
354 WvString(const WvFastString &s)
355 { copy_constructor(s); }
356
363 WvString(const char *_str);
364
369 WvString(const QString &);
370 WvString(const QCString &);
371
376 inline WvString(const std::string &s);
377
378 WvString(WVSTRING_FORMAT_DECL) : WvFastString(WVSTRING_FORMAT_CALL)
379 { }
380
381 WvString &append(WvStringParm s);
382 WvString &append(WVSTRING_FORMAT_DECL)
383 { return append(WvString(WVSTRING_FORMAT_CALL)); }
384
385 WvString &operator= (int i);
386 WvString &operator= (const WvFastString &s2);
387 WvString &operator= (const char *s2)
388 { return *this = WvFastString(s2); }
389
391 WvString &unique();
392
394 bool is_unique() const;
395
397 char *edit()
398 { return unique().str; }
399
400protected:
401 void copy_constructor(const WvFastString &s);
402 inline void construct(const char *_str)
403 {
404 link(&nullbuf, _str);
405
406 // apenwarr (2002/04/24): from now on, all WvString objects are created
407 // with unique(), so you should _never_ have to call it explicitly. We
408 // still can (and should!) use fast parameter passing via WvFastString.
409 unique();
410 }
411};
412
413
421{
422public:
424 { }
425 WvFastString *operator -> ()
426 { return this; }
427};
428
429
430inline bool operator== (const char *s1, WvStringParm s2)
431{
432 return s2 == s1;
433}
434
435
436inline bool operator!= (const char *s1, WvStringParm s2)
437{
438 return s2 != s1;
439}
440
441#endif // __WVSTRING_H
A WvFastString acts exactly like a WvString, but can take (const char *) strings without needing to a...
Definition wvstring.h:94
WvFastString(WVSTRING_FORMAT_DECL)
Now, you're probably thinking to yourself: Boy, does this ever look ridiculous.
Definition wvstring.h:188
const WvFastString & ifnull(WvStringParm defval) const
returns either this string, or, if isnull(), the given string.
Definition wvstring.h:294
int num() const
Return a stdc++ string with the contents of this string.
Definition wvstring.h:286
WvFastString()
Create an empty, NULL string.
Definition wvstring.cc:33
bool operator!() const
the not operator is 'true' if string is empty
Definition wvstring.cc:428
bool isnull() const
returns true if this string is null
Definition wvstring.h:290
const char * cstr() const
return a (const char *) for this string.
Definition wvstring.h:267
static void do_format(WvFastString &output, const char *format, const WvFastString *const *a)
when this is called, we assume output.str == NULL; it will be filled.
Definition wvstring.cc:497
WvFastString offset(size_t i) const
Returns a copy of string pointed i bytes into this.
Definition wvstring.cc:79
A ridiculous class needed because UniConf::operator->() needs to return a pointer,...
Definition wvstring.h:421
WvString is an implementation of a simple and efficient printable-string class.
Definition wvstring.h:330
WvString(const WvString &s)
Magic copy constructor for "fast" char* strings.
Definition wvstring.h:352
WvString & unique()
make the buf and str pointers owned only by this WvString.
Definition wvstring.cc:306
char * edit()
make the string editable, and return a non-const (char*)
Definition wvstring.h:397
bool is_unique() const
returns true if this string is already unique()
Definition wvstring.cc:320