Debian Codegraph @

src/ITGSend/newran/str.h

raw symbols SCIP
1 // Character string manipulation library
2 // 19 June, 2004
3 // Copyright (c) R B Davies 1996
4
5 // The is a string library that is intended to be compatible with the
6 // "class string" library in the April 1995 draft of the C++ standard.
7
8
9 #ifndef STRING_LIB
10 #define STRING_LIB
11
12 #ifndef INCLUDE_LIB
13 #include "include.h"
14 #endif
15
16 #include "myexcept.h"
17
18 #ifdef use_namespace
19 namespace RBD_STRING { using namespace RBD_COMMON; }
20 namespace RBD_LIBRARIES { using namespace RBD_STRING; }
21 namespace RBD_STRING {
22 #endif
23
24 typedef unsigned int uint;
25
26
27 // helper classes defined later containing different forms of string
28
29 class StrBase; // base of remaining classes (virtual)
30 class CharSeq; // pointer to character sequence
31 class StrRep; // string as char seq; no copy on write
32 class StrRepMult; // string as char seq; copy on write OK
33 class StrRepCap; // string as char seq; with capacity
34 class StrRepNullTerm; // null terminated string
35 class StrNull; // null string
36 class CharSingle; // single character
37 class CharRepeated; // repeated character
38 class StrSumBase; // base for sum classes
39 class StrSum; // sum of two strings
40 class StrSumLC; // character+sum
41 class StrSumRC; // sum+character
42
43 // the string class seen by the user
44
45 class String : public Janitor
46 // the class Janitor is from my simulated exception package; you may
47 // remove the dependence on Janitor if you do not want to use my
48 // exception package
49 {
50 StrBase *px; // pointer to a StrBase
51
52 char* GetData() const; // unprotected pointer to s
53 StrBase* Clone() const; // "copy" a StrBase
54 StrRep* GetStrRep() const; // get its StrRep
55 StrRep* GetStrRepW(); // get its StrRep, writing allowed
56 StrRep* Protect() const; // get protected StrRep
57 // (change StrRepMult to StrRep)
58 bool HasCapacity() const; // does it have capacity defined
59 String(StrBase* sb); // string pointing to StrBase
60 static uint s_index; // used in searches
61 static StrNull* ASN; // pointer to null string - only one instance
62
63 public:
64 unsigned int refcount() const; // ref count (for debugging)
65 StrBase* WhereIsIt() const
66 { return px; } // for debugging
67 const char* StringType() const;// for debugging
68 void CleanUp(); // for my exception simulator
69
70 static uint npos; // set to (uint)(-1)
71
72 String();
73 String(const String& str); // not explicitly in standard
74 String(const String& str, uint pos, uint n = npos);
75 String(const char* s, uint n);
76 String(const char* s);
77 String(uint n, char c);
78 ~String();
79
80 String& operator=(const String& str);
81 String& operator=(const char* s);
82 String& operator=(const char c);
83
84 uint size() const;
85 uint length() const;
86 uint max_size() const;
87 void resize(uint n, char c = 0); // std does default separately
88 uint capacity() const;
89 void reserve(uint res_arg = 0);
90 void clear();
91 bool empty() const;
92
93 char operator[](uint pos) const;
94 char& operator[](uint pos);
95 char at(uint n) const;
96 char& at(uint n);
97
98 String& operator+=(const String& rhs);
99 String& operator+=(const char* s);
100 String& operator+=(char c);
101 void push_back(char c) { operator+=(c); }
102
103 String& append(const String& str); // not explicitly in standard
104 String& append(const String& str, uint pos, uint n = npos);
105 String& append(const char* s, uint n);
106 String& append(const char* s);
107 String& append(uint n, char c = 0);
108
109 String& assign(const String& str); // not explicitly in standard
110 String& assign(const String& str, uint pos, uint n = npos);
111 String& assign(const char* s, uint n);
112 String& assign(const char* s);
113 String& assign(uint n, char c = 0);
114
115 String& insert(uint pos1, const String& str);
116 // not explicitly in standard
117 String& insert(uint pos1, const String& str, uint pos2, uint n = npos);
118 String& insert(uint pos, const char* s, uint n = npos);
119 // std does default separately
120 String& insert(uint pos, uint n, char c = 0);
121
122 String& erase(uint pos = 0, uint n = npos);
123
124 String& replace(uint pos1, uint n1, const String& str);
125 String& replace(uint pos1, uint n1, const String& str, uint pos2, uint n2);
126 String& replace(uint pos, uint n1, const char* s, uint n2 = npos);
127 // std does default separately
128 String& replace(uint pos, uint n1, uint n2, char c = 0);
129
130 uint copy(char* s, uint n, uint pos = 0) const;
131 void swap(String&);
132
133 const char* c_str() const;
134 const char* data() const;
135
136 uint find(const String& str, uint pos = 0) const;
137 uint find(const char* s, uint pos, uint n) const;
138 uint find(const char* s, uint pos = 0) const;
139 uint find(const char c, uint pos = 0) const;
140
141 uint rfind(const String& str, uint pos = npos) const;
142 uint rfind(const char* s, uint pos, uint n) const;
143 uint rfind(const char* s, uint pos = npos) const;
144 uint rfind(const char c, uint pos = npos) const;
145
146 uint find_first_of(const String& str, uint pos = 0) const;
147 uint find_first_of(const char* s, uint pos, uint n) const;
148 uint find_first_of(const char* s, uint pos = 0) const;
149 uint find_first_of(const char c, uint pos = 0) const;
150
151 uint find_last_of(const String& str, uint pos = npos) const;
152 uint find_last_of(const char* s, uint pos, uint n) const;
153 uint find_last_of(const char* s, uint pos = npos) const;
154 uint find_last_of(const char c, uint pos = npos) const;
155
156 uint find_first_not_of(const String& str, uint pos = 0) const;
157 uint find_first_not_of(const char* s, uint pos, uint n) const;
158 uint find_first_not_of(const char* s, uint pos = 0) const;
159 uint find_first_not_of(const char c, uint pos = 0) const;
160
161 uint find_last_not_of(const String& str, uint pos = npos) const;
162 uint find_last_not_of(const char* s, uint pos, uint n) const;
163 uint find_last_not_of(const char* s, uint pos = npos) const;
164 uint find_last_not_of(const char c, uint pos = npos) const;
165
166 String substr(uint pos = 0, uint n = npos) const;
167
168 int compare(const String& str) const;
169 int compare(uint pos1, uint n1, const String& str) const;
170 int compare(uint pos1, uint n1, const String& str, uint pos2, uint n2) const;
171 int compare(const char* s) const;
172 int compare(uint pos, uint n1, const char* s, uint n2 = npos) const;
173
174 friend String operator+(const String& lhs, const String& rhs);
175 friend String operator+(const char* lhs, const String& rhs);
176 friend String operator+(char lhs, const String& rhs);
177 friend String operator+(const String& lhs, const char* rhs);
178 friend String operator+(const String& lhs, char rhs);
179
180 friend bool operator==(const String& lhs, const String& rhs);
181 friend bool operator==(const char* lhs, const String& rhs);
182 friend bool operator==(const String& lhs, const char* rhs);
183
184 friend bool operator!=(const String& lhs, const String& rhs);
185 friend bool operator!=(const char* lhs, const String& rhs);
186 friend bool operator!=(const String& lhs, const char* rhs);
187
188 friend bool operator<(const String& lhs, const String& rhs);
189 friend bool operator<(const char* lhs, const String& rhs);
190 friend bool operator<(const String& lhs, const char* rhs);
191
192 friend bool operator>(const String& lhs, const String& rhs);
193 friend bool operator>(const char* lhs, const String& rhs);
194 friend bool operator>(const String& lhs, const char* rhs);
195
196 friend bool operator<=(const String& lhs, const String& rhs);
197 friend bool operator<=(const char* lhs, const String& rhs);
198 friend bool operator<=(const String& lhs, const char* rhs);
199
200 friend bool operator>=(const String& lhs, const String& rhs);
201 friend bool operator>=(const char* lhs, const String& rhs);
202 friend bool operator>=(const String& lhs, const char* rhs);
203
204 friend void swap(String& A, String& B);
205
206 friend istream& operator>>(istream& is, String& str);
207 friend ostream& operator<<(ostream& os, const String& str);
208 friend istream& getline(istream& is, String& str, char delim = '\n');
209
210 friend class CharSeq;
211 friend class StrSum;
212 friend class StringPackageInitialise;
213
214 private:
215
216 // these are called by the official string functions
217 int compare(uint pos, uint n, const CharSeq& sb) const;
218 String& my_append(const StrBase& sb);
219 String& my_assign(const StrBase& sb);
220 String& my_insert(uint pos, const StrBase& sb);
221 String& my_replace(uint pos1, uint n1, const StrBase& str);
222 uint find(const CharSeq& str, uint pos) const;
223 uint rfind(const CharSeq& str, uint pos) const;
224 uint find_first_of(const CharSeq& str, uint pos) const;
225 uint find_last_of(const CharSeq& str, uint pos) const;
226 uint find_first_not_of(const CharSeq& str, uint pos) const;
227 uint find_last_not_of(const CharSeq& str, uint pos) const;
228
229 // this is for linking in with the exception package and can be removed
230 // if you do not want the simulated exceptions
231 FREE_CHECK(String)
232 };
233
234
235 // the helper classes
236
237
238 class StrBase
239 {
240 public:
241 virtual uint size() const = 0;
242 virtual uint capacity() const;
243 virtual void Load(char*& ps) const = 0; // get data from a StrBase
244 virtual StrBase* Clone(StrBase**) = 0; // "copy" a StrBase
245 virtual StrRep* GetStrRep(StrBase**); // convert to StrRep
246 virtual StrRep* GetStrRepW(StrBase**); // convert to StrRep - writing OK
247 virtual StrRep* Protect(StrBase**); // convert to prot StrRep
248 virtual void Drop();
249 virtual StrBase* UnProtect(); // StrRep to StrRepMult
250 virtual void WithCapacity(StrBase**); // convert to StrRepCap
251 virtual const char* NullTerminate(StrBase**);
252 // convert to StrRepNullTerm
253 virtual bool HasCapacity() const; // is capacity defined
254 virtual unsigned int refcount() const; // for debugging
255 virtual ~StrBase() {}
256 virtual const char* StringType() const = 0; // return storage mode of string
257 };
258
259
260 // point to a character sequence
261
262 class CharSeq : public StrBase
263 {
264 protected:
265 CharSeq(uint SZ) : sz(SZ) {}
266 public:
267 uint sz; // size of string
268 char* s; // actual character string
269 CharSeq() : sz(0), s(0) {}
270 CharSeq(char* S);
271 CharSeq(char* S, uint SZ);
272 CharSeq(const String& str);
273 CharSeq(const String& str, uint pos, uint n);
274 uint size() const;
275 uint capacity() const;
276 void Load(char*& ps) const;
277 StrBase* Clone(StrBase**);
278 const char* StringType() const { return "CharSeq"; }
279 };
280
281
282 // the class to manage the actual contiguous character string
283 // this version does not allow copy on write
284
285 class StrRep : public CharSeq
286 {
287 public:
288 StrRep() {}
289 StrRep(uint SZ);
290 StrRep(uint n, char c);
291 StrRep(const char* x);
292 StrRep(const char* x, uint len);
293 StrRep(const char* x, uint len, uint n, char c);
294 // len characters from x and n-len copies of c
295 ~StrRep();
296
297 StrRep* GetStrRep(StrBase**);
298 StrRep* GetStrRepW(StrBase**);
299 StrRep* Protect(StrBase**);
300 void WithCapacity(StrBase**);
301 StrBase* Clone(StrBase**);
302 StrBase* UnProtect();
303 const char* StringType() const { return "StrRep"; }
304 };
305
306
307 // the class to manage the actual contiguous character string
308 // this version does allow copy on write
309 // ie more than one string can be pointing at an object in this class
310
311 class StrRepMult : public StrRep
312 {
313 public:
314 unsigned int refcnt; // 0 means one string assigned
315 StrRepMult() : refcnt(0) {}
316 StrRepMult(uint SZ);
317 StrRepMult(uint n, char c);
318 StrRepMult(const char* x);
319 StrRepMult(const char* x, uint len);
320 StrRepMult(const char* x, uint len, uint n, char c);
321 // len characters from x and n-len copies of c
322 void Drop(); // delete if refcnt==0 otherwise --refcnt
323
324 StrRep* GetStrRepW(StrBase**);
325 StrRep* Protect(StrBase**);
326 void WithCapacity(StrBase**);
327 StrBase* Clone(StrBase**);
328 unsigned int refcount() const;
329 StrBase* UnProtect();
330 const char* StringType() const { return "StrRepMult"; }
331 };
332
333
334 // a StrRep which can have capacity() > size()
335
336 class StrRepCap : public StrRep
337 {
338 public:
339 uint cpx; // allocated space for string
340 StrRepCap() : StrRep(), cpx(0) {}
341 StrRepCap(uint len, uint cp); // string with length & capacity
342 StrRepCap(const char* s, uint len); // length and capacity = len
343 uint capacity() const;
344 StrBase* UnProtect();
345 void WithCapacity(StrBase**);
346 const char* NullTerminate(StrBase**);
347 bool HasCapacity() const; // is capacity defined
348 const char* StringType() const { return "StrRepCap"; }
349 };
350
351 // a StrRep stored as a null-terminated string
352
353 class StrRepNullTerm : public StrRep
354 {
355 public:
356 StrRepNullTerm(uint len);
357 void WithCapacity(StrBase**);
358 StrBase* UnProtect();
359 const char* NullTerminate(StrBase**);
360 const char* StringType() const { return "StrRepNullTerm"; }
361 };
362
363
364 // single character
365
366 class CharSingle : public StrBase
367 {
368 protected:
369 char c;
370 public:
371 CharSingle(char C) : c(C) {}
372 uint size() const;
373 void Load(char*& ps) const;
374 StrBase* Clone(StrBase**);
375 const char* StringType() const { return "CharSingle"; }
376 };
377
378
379 // repeated character
380
381 class CharRepeated : public CharSingle
382 {
383 protected:
384 uint n;
385 public:
386 CharRepeated(int N, char C);
387 uint size() const;
388 void Load(char*& ps) const;
389 StrBase* Clone(StrBase**);
390 const char* StringType() const { return "CharRepeated"; }
391 };
392
393
394 // the base for the sum of string classes
395
396 class StrSumBase : public StrBase
397 {
398 protected:
399 bool ref; // true if already accessed
400 StrSumBase();
401 public:
402 StrBase* Clone(StrBase**);
403 unsigned int refcount() const;
404 const char* StringType() const { return "StrSumBase"; }
405 };
406
407
408 // string + string
409
410 class StrSum : public StrSumBase
411 {
412 protected:
413 StrBase* lhs;
414 StrBase* rhs;
415 public:
416 StrSum(StrBase* L, StrBase* R);
417 uint size() const;
418 void Load(char*& ps) const;
419 void Drop();
420 const char* StringType() const { return "StrSum"; }
421 };
422
423
424 // character + string
425
426 class StrSumLC : public StrSumBase
427 {
428 protected:
429 char lhs;
430 StrBase* rhs;
431 public:
432 StrSumLC(char L, StrBase* R);
433 uint size() const;
434 void Load(char*& ps) const;
435 void Drop();
436 const char* StringType() const { return "StrSumLC"; }
437 };
438
439
440 // string + character
441
442 class StrSumRC : public StrSumBase
443 {
444 protected:
445 StrBase* lhs;
446 char rhs;
447 public:
448 StrSumRC(StrBase* L, char R);
449 uint size() const;
450 void Load(char*& ps) const;
451 void Drop();
452 const char* StringType() const { return "StrSumRC"; }
453 };
454
455
456 // null string
457
458 class StrNull : public StrRepNullTerm
459 {
460 public:
461 StrNull();
462 uint size() const;
463 void Load(char*& ps) const;
464 StrBase* Clone(StrBase**);
465 StrBase* UnProtect();
466 void Drop();
467 const char* StringType() const { return "StrNull"; }
468 };
469
470 // global inline
471
472 inline void swap(String& A, String& B) { A.swap(B); }
473
474
475 // initialise string package
476 // to make sure String::npos = -1 and the null string is initialised
477 // before anything happens
478
479 class StringPackageInitialise
480 {
481 public:
482 StringPackageInitialise();
483 };
484
485 static StringPackageInitialise DoStringPackageInitialise;
486
487
488 #ifdef use_namespace
489 }
490 #endif
491
492
493 #endif // STRING_LIB
494
495
496 // body file: str.cpp
497
498
499