| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 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 |
|
| 28 |
|
| 29 |
class StrBase; |
| 30 |
class CharSeq; |
| 31 |
class StrRep; |
| 32 |
class StrRepMult; |
| 33 |
class StrRepCap; |
| 34 |
class StrRepNullTerm; |
| 35 |
class StrNull; |
| 36 |
class CharSingle; |
| 37 |
class CharRepeated; |
| 38 |
class StrSumBase; |
| 39 |
class StrSum; |
| 40 |
class StrSumLC; |
| 41 |
class StrSumRC; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
class String : public Janitor |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
{ |
| 50 |
StrBase *px; |
| 51 |
|
| 52 |
char* GetData() const; |
| 53 |
StrBase* Clone() const; |
| 54 |
StrRep* GetStrRep() const; |
| 55 |
StrRep* GetStrRepW(); |
| 56 |
StrRep* Protect() const; |
| 57 |
|
| 58 |
bool HasCapacity() const; |
| 59 |
String(StrBase* sb); |
| 60 |
static uint s_index; |
| 61 |
static StrNull* ASN; |
| 62 |
|
| 63 |
public: |
| 64 |
unsigned int refcount() const; |
| 65 |
StrBase* WhereIsIt() const |
| 66 |
{ return px; } |
| 67 |
const char* StringType() const; |
| 68 |
void CleanUp(); |
| 69 |
|
| 70 |
static uint npos; |
| 71 |
|
| 72 |
String(); |
| 73 |
String(const String& str); |
| 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); |
| 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); |
| 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); |
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 230 |
|
| 231 |
FREE_CHECK(String) |
| 232 |
}; |
| 233 |
|
| 234 |
|
| 235 |
|
| 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; |
| 244 |
virtual StrBase* Clone(StrBase**) = 0; |
| 245 |
virtual StrRep* GetStrRep(StrBase**); |
| 246 |
virtual StrRep* GetStrRepW(StrBase**); |
| 247 |
virtual StrRep* Protect(StrBase**); |
| 248 |
virtual void Drop(); |
| 249 |
virtual StrBase* UnProtect(); |
| 250 |
virtual void WithCapacity(StrBase**); |
| 251 |
virtual const char* NullTerminate(StrBase**); |
| 252 |
|
| 253 |
virtual bool HasCapacity() const; |
| 254 |
virtual unsigned int refcount() const; |
| 255 |
virtual ~StrBase() {} |
| 256 |
virtual const char* StringType() const = 0; |
| 257 |
}; |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
class CharSeq : public StrBase |
| 263 |
{ |
| 264 |
protected: |
| 265 |
CharSeq(uint SZ) : sz(SZ) {} |
| 266 |
public: |
| 267 |
uint sz; |
| 268 |
char* s; |
| 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 |
|
| 283 |
|
| 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 |
|
| 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 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
class StrRepMult : public StrRep |
| 312 |
{ |
| 313 |
public: |
| 314 |
unsigned int refcnt; |
| 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 |
|
| 322 |
void Drop(); |
| 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 |
|
| 335 |
|
| 336 |
class StrRepCap : public StrRep |
| 337 |
{ |
| 338 |
public: |
| 339 |
uint cpx; |
| 340 |
StrRepCap() : StrRep(), cpx(0) {} |
| 341 |
StrRepCap(uint len, uint cp); |
| 342 |
StrRepCap(const char* s, uint len); |
| 343 |
uint capacity() const; |
| 344 |
StrBase* UnProtect(); |
| 345 |
void WithCapacity(StrBase**); |
| 346 |
const char* NullTerminate(StrBase**); |
| 347 |
bool HasCapacity() const; |
| 348 |
const char* StringType() const { return "StrRepCap"; } |
| 349 |
}; |
| 350 |
|
| 351 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 395 |
|
| 396 |
class StrSumBase : public StrBase |
| 397 |
{ |
| 398 |
protected: |
| 399 |
bool ref; |
| 400 |
StrSumBase(); |
| 401 |
public: |
| 402 |
StrBase* Clone(StrBase**); |
| 403 |
unsigned int refcount() const; |
| 404 |
const char* StringType() const { return "StrSumBase"; } |
| 405 |
}; |
| 406 |
|
| 407 |
|
| 408 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 471 |
|
| 472 |
inline void swap(String& A, String& B) { A.swap(B); } |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 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 |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|