| 1 | // character string manipulation library |
| 2 | // Copyright (c) R B Davies 1996 |
| 3 | |
| 4 | // The is a string library that is intended to be compatible with the |
| 5 | // "class string" library in the April 1995 draft of the C++ standard. |
| 6 | |
| 7 | // Parts are based on Tony Hansen s "The C++ Answer Book" |
| 8 | |
| 9 | |
| 10 | #define WANT_STREAM |
| 11 | #define WANT_MATH |
| 12 | #define WANT_STRING |
| 13 | |
| 14 | #include "include.h" |
| 15 | |
| 16 | #include "str.h" |
| 17 | |
| 18 | |
| 19 | #ifdef use_namespace |
| 20 | namespace RBD_STRING { using namespace RBD_COMMON; } |
| 21 | namespace RBD_LIBRARIES { using namespace RBD_STRING; } |
| 22 | namespace RBD_STRING { |
| 23 | #endif |
| 24 | |
| 25 | |
| 26 | //******************************* globals ******************************** |
| 27 | |
| 28 | uint String::npos = (uint)(-1); |
| 29 | // also initialised to -1 by StringPackageInitialise |
| 30 | uint String::s_index; |
| 31 | StrNull* String::ASN; |
| 32 | |
| 33 | // string bodies |
| 34 | |
| 35 | const char* String::StringType() const { return px->StringType(); } |
| 36 | |
| 37 | StrBase* String::Clone() const { return px->Clone(&(StrBase*&)px); } |
| 38 | |
| 39 | unsigned int String::refcount() const { return px->refcount(); } |
| 40 | |
| 41 | void String::CleanUp() { if (px) px->Drop(); px = ASN; } |
| 42 | |
| 43 | StrRep* String::GetStrRep() const |
| 44 | { return px->GetStrRep(&(StrBase*&)px); } |
| 45 | |
| 46 | StrRep* String::GetStrRepW() { return px->GetStrRepW(&px); } |
| 47 | |
| 48 | char* String::GetData() const { return GetStrRep()->s; } |
| 49 | |
| 50 | StrRep* String::Protect() const { return px->Protect(&(StrBase*&)px); } |
| 51 | |
| 52 | bool String::HasCapacity() const { return px->HasCapacity(); } |
| 53 | |
| 54 | String::String(StrBase* sb) : px(sb) {} |
| 55 | |
| 56 | String::String() : px(ASN) {} |
| 57 | |
| 58 | String::String(const String& str) { px = ASN; px = str.Clone(); } |
| 59 | |
| 60 | String::String(const String& str, uint pos, uint n) |
| 61 | { |
| 62 | px = ASN; // in case of later failure |
| 63 | StrRep* str_p = str.GetStrRep(); |
| 64 | uint sz = str.size(); |
| 65 | if (pos > sz) Throw(Out_of_range("string index error\n")); |
| 66 | sz -= pos; // length of new string |
| 67 | if (n < sz) sz = n; |
| 68 | if (sz == 0) px = ASN; |
| 69 | else px = new StrRepMult(str_p->s+pos, sz); |
| 70 | } |
| 71 | |
| 72 | String::String(const char* s, uint n) |
| 73 | { |
| 74 | px = ASN; // in case of later failure |
| 75 | if (!s) Throw(Invalid_argument("string constructor: char* s = 0\n")); |
| 76 | uint sz = 0; const char* S = s; |
| 77 | while (*S++) { if (sz >= n) break; sz++; } |
| 78 | if (sz == 0) px = ASN; |
| 79 | else px = new StrRepMult(s, sz); |
| 80 | } |
| 81 | |
| 82 | String::String(const char* s) |
| 83 | { |
| 84 | px = ASN; // in case of later failure |
| 85 | if (!s) Throw(Invalid_argument("string constructor: char* s = 0\n")); |
| 86 | uint sz = strlen(s); |
| 87 | if (sz == 0) px = ASN; |
| 88 | else px = new StrRepMult(s, sz); |
| 89 | } |
| 90 | |
| 91 | String::String(uint n, char c) |
| 92 | { |
| 93 | px = ASN; // in case of later failure |
| 94 | if (n == npos) Throw(Length_error("string length = npos\n")); |
| 95 | if (n == 0) px = ASN; |
| 96 | else if (n == 1) px = new CharSingle(c); |
| 97 | else px = new CharRepeated(n, c); |
| 98 | } |
| 99 | |
| 100 | String::~String() { if (px) px->Drop(); } |
| 101 | |
| 102 | // do the operator= separately from assign because we do not ever |
| 103 | // want to just copy into existing memory space. ie we do not |
| 104 | // maintain capacity when we use operator= . |
| 105 | |
| 106 | String& String::operator=(const String& str) |
| 107 | { StrBase* p = px; px = str.Clone(); p->Drop(); return *this; } |
| 108 | |
| 109 | String& String::operator=(const char* s) |
| 110 | { |
| 111 | StrBase* p = px; |
| 112 | if (strlen(s) == 0) px = ASN; |
| 113 | else px = new StrRepMult(s); |
| 114 | p->Drop(); return *this; |
| 115 | } |
| 116 | |
| 117 | |
| 118 | String& String::operator=(const char c) |
| 119 | { |
| 120 | px->Drop(); px = new CharSingle(c); |
| 121 | return *this; |
| 122 | } |
| 123 | |
| 124 | uint String::size() const { return px->size(); } |
| 125 | |
| 126 | uint String::length() const { return px->size(); } |
| 127 | |
| 128 | uint String::max_size() const { return npos-1; } |
| 129 | |
| 130 | void String::resize(uint n, char c) |
| 131 | { |
| 132 | if (n == npos) Throw(Length_error("string length = npos\n")); |
| 133 | if (n == 0) { px->Drop(); px = ASN; return; } |
| 134 | StrRep* p = GetStrRep(); |
| 135 | if (n <= p->sz) px = new StrRepMult(p->s, n); |
| 136 | else px = new StrRepMult(p->s, p->sz, n, c); |
| 137 | p->Drop(); |
| 138 | } |
| 139 | |
| 140 | uint String::capacity() const { return px->capacity(); } |
| 141 | |
| 142 | void String::reserve(uint res_arg) |
| 143 | { |
| 144 | if (res_arg == 0) // remove capacity |
| 145 | { |
| 146 | StrRep* p = new StrRepMult(size()); |
| 147 | char* target = p->s; px->Load(target); px->Drop(); px = p; |
| 148 | } |
| 149 | else if ( res_arg > size() || capacity() != size() ) |
| 150 | // need to change storage |
| 151 | { |
| 152 | if (res_arg < size()) res_arg = size(); |
| 153 | StrRep* p = new StrRepCap(size(), res_arg); |
| 154 | char* target = p->s; px->Load(target); px->Drop(); px = p; |
| 155 | } |
| 156 | else px->WithCapacity(&(StrBase*&)px); // convert to string with capacity |
| 157 | |
| 158 | } |
| 159 | |
| 160 | void String::clear() { if (px) px->Drop(); px = ASN; } |
| 161 | |
| 162 | bool String::empty() const { return px->size() == 0; } |
| 163 | |
| 164 | char String::operator[](uint pos) const |
| 165 | { |
| 166 | StrRep* p = GetStrRep(); |
| 167 | if (pos >= p->size()) |
| 168 | { |
| 169 | if (pos == p->size()) return 0; |
| 170 | else Throw(Out_of_range("string index error\n")); |
| 171 | } |
| 172 | return (p->s)[pos]; |
| 173 | } |
| 174 | |
| 175 | char& String::operator[](uint pos) |
| 176 | { |
| 177 | StrRep* p = GetStrRepW(); |
| 178 | if (pos >= p->size()) Throw(Out_of_range("string index error\n")); |
| 179 | return p->s[pos]; |
| 180 | } |
| 181 | |
| 182 | char String::at(uint n) const |
| 183 | { |
| 184 | StrRep* p = GetStrRep(); |
| 185 | if (n >= p->size()) |
| 186 | { |
| 187 | if (n == p->size()) return 0; |
| 188 | else Throw(Out_of_range("string index error\n")); |
| 189 | } |
| 190 | return p->s[n]; |
| 191 | } |
| 192 | |
| 193 | char& String::at(uint n) |
| 194 | { |
| 195 | StrRep* p = GetStrRepW(); |
| 196 | if (n >= p->size()) Throw(Out_of_range("string index error\n")); |
| 197 | return p->s[n]; |
| 198 | } |
| 199 | |
| 200 | |
| 201 | //******************** the line editing functions ************************ |
| 202 | |
| 203 | String& String::operator+=(const String& rhs) { return append(rhs); } |
| 204 | |
| 205 | String& String::operator+=(const char* s) { return append(s); } |
| 206 | |
| 207 | String& String::operator+=(char c) |
| 208 | { |
| 209 | if (HasCapacity()) return my_append(CharSingle(c)); |
| 210 | else |
| 211 | { |
| 212 | if (size() >= npos - 1) Throw(Length_error("string length >= npos\n")); |
| 213 | px = new StrSumRC(px ,c); |
| 214 | return *this; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | |
| 219 | String& String::my_append(const StrBase& sb) |
| 220 | { |
| 221 | StrRep* p = GetStrRep(); |
| 222 | uint sz = size(); uint rlen =sb.size(); |
| 223 | if (sz >= npos - rlen) Throw(Length_error("string length >= npos\n")); |
| 224 | rlen += sz; |
| 225 | if (rlen > capacity() || refcount() != 0) |
| 226 | { |
| 227 | // must make new string |
| 228 | StrRep* oldp = p; p = new StrRepMult(rlen); |
| 229 | memcpy(p->s, oldp->s, sz); oldp->Drop(); px = p; |
| 230 | } |
| 231 | char* target = p->s+sz; sb.Load(target); p->sz = rlen; return *this; |
| 232 | } |
| 233 | |
| 234 | String& String::append(const String& str) |
| 235 | { |
| 236 | if (HasCapacity()) return my_append(CharSeq(str)); |
| 237 | else |
| 238 | { |
| 239 | if (size() >= npos - str.size()) |
| 240 | Throw(Length_error("string length >= npos\n")); |
| 241 | px = new StrSum(px, str.Clone()); |
| 242 | return *this; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | String& String::append(const String& str, uint pos, uint n) |
| 247 | { |
| 248 | if (HasCapacity()) return my_append(CharSeq(str, pos, n)); |
| 249 | else |
| 250 | { |
| 251 | CharSeq CS(str, pos, n); |
| 252 | if (size() >= npos - CS.sz) |
| 253 | Throw(Length_error("string length >= npos\n")); |
| 254 | StrBase* rhs = new StrRep(CS.s, CS.sz); |
| 255 | px = new StrSum(px, rhs); |
| 256 | return *this; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | String& String::append(const char* s, uint n) |
| 261 | { |
| 262 | if (HasCapacity()) return my_append(CharSeq((char*)s, n)); |
| 263 | else |
| 264 | { |
| 265 | StrBase* rhs = new StrRep(s, n); |
| 266 | if (size() >= npos - rhs->size()) |
| 267 | { |
| 268 | delete rhs; |
| 269 | Throw(Length_error("string length >= npos\n")); |
| 270 | } |
| 271 | px = new StrSum(px, rhs); |
| 272 | return *this; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | String& String::append(const char* s) |
| 277 | { |
| 278 | if (HasCapacity()) return my_append(CharSeq((char*)s)); |
| 279 | else |
| 280 | { |
| 281 | StrBase* rhs = new StrRep(s); |
| 282 | if (size() >= npos - rhs->size()) |
| 283 | { |
| 284 | delete rhs; |
| 285 | Throw(Length_error("string length >= npos\n")); |
| 286 | } |
| 287 | px = new StrSum(px, rhs); |
| 288 | return *this; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | String& String::append(uint n, char c) |
| 293 | { |
| 294 | if (size() >= npos - n) |
| 295 | Throw(Length_error("string length >= npos\n")); |
| 296 | |
| 297 | if (HasCapacity()) return my_append(CharRepeated(n, c)); |
| 298 | else if (n == 1) |
| 299 | { |
| 300 | px = new StrSumRC(px ,c); |
| 301 | } |
| 302 | else |
| 303 | { |
| 304 | StrBase* rhs = new CharRepeated(n, c); |
| 305 | px = new StrSum(px, rhs); |
| 306 | } |
| 307 | return *this; |
| 308 | } |
| 309 | |
| 310 | // assign with argument being all or part of the target string |
| 311 | // should not be a problem under HasCapacity since Load uses |
| 312 | // memmove. |
| 313 | |
| 314 | String& String::assign(const String& str) |
| 315 | { |
| 316 | if (HasCapacity() && str.size() <= capacity()) |
| 317 | { |
| 318 | StrRep* p = GetStrRep(); char* target = p->s; |
| 319 | str.px->Load(target); p->sz = str.size(); |
| 320 | } |
| 321 | else if (px != str.px) { px->Drop(); px = str.Clone(); } |
| 322 | return *this; |
| 323 | } |
| 324 | |
| 325 | String& String::my_assign(const StrBase& sb) |
| 326 | { |
| 327 | uint sz = sb.size(); StrRep* p; |
| 328 | if (HasCapacity() && sz <= capacity()) |
| 329 | { |
| 330 | p = GetStrRep(); char* target = p->s; |
| 331 | sb.Load(target); p->sz = sz; return *this; |
| 332 | } |
| 333 | else if (sz == 0) p = ASN; |
| 334 | else |
| 335 | { |
| 336 | p = new StrRepMult(sb.size()); |
| 337 | char* target = p->s; sb.Load(target); |
| 338 | } |
| 339 | px->Drop(); px = p; return *this; |
| 340 | } |
| 341 | |
| 342 | String& String::assign(const String& str, uint pos, uint n) |
| 343 | { return my_assign(CharSeq(str, pos, n)); } |
| 344 | |
| 345 | String& String::assign(const char* s, uint n) |
| 346 | { return my_assign(CharSeq((char*)s, n)); } |
| 347 | |
| 348 | String& String::assign(const char* s) |
| 349 | { return my_assign(CharSeq((char*)s)); } |
| 350 | |
| 351 | String& String::assign(uint n, char c) |
| 352 | { return my_assign(CharRepeated(n, c)); } |
| 353 | |
| 354 | String& String::my_insert(uint pos, const StrBase& sb) |
| 355 | { |
| 356 | uint sz = size(); uint rlen =sb.size(); |
| 357 | if (pos > sz) Throw(Out_of_range("string index error\n")); |
| 358 | if (sz >= npos - rlen) Throw(Length_error("string length >= npos\n")); |
| 359 | StrRep* oldp = GetStrRep(); |
| 360 | if (HasCapacity() && sz + rlen <= capacity()) |
| 361 | { |
| 362 | // must allow for possibility that sb is part of *this |
| 363 | char* target = oldp->s + pos; |
| 364 | StrRep tail(target, sz-pos); |
| 365 | sb.Load(target); tail.Load(target); oldp->sz = sz + rlen; |
| 366 | } |
| 367 | else |
| 368 | { |
| 369 | StrRep* p = new StrRepMult(sz + rlen); px = p; |
| 370 | char* target = p->s; memcpy(target, oldp->s, pos); |
| 371 | target += pos; sb.Load(target); |
| 372 | memcpy(target, oldp->s + pos, sz-pos); |
| 373 | oldp->Drop(); |
| 374 | } |
| 375 | return *this; |
| 376 | } |
| 377 | |
| 378 | String& String::insert(uint pos1, const String& str) |
| 379 | { return my_insert(pos1, *(str.px)); } |
| 380 | |
| 381 | String& String::insert(uint pos1, const String& str, uint pos2, uint n) |
| 382 | { return my_insert(pos1, CharSeq(str, pos2, n)); } |
| 383 | |
| 384 | String& String::insert(uint pos, const char* s, uint n) |
| 385 | { return my_insert(pos, CharSeq((char*)s, n)); } |
| 386 | |
| 387 | String& String::insert(uint pos, uint n, char c) |
| 388 | { return my_insert(pos, CharRepeated(n, c)); } |
| 389 | |
| 390 | String& String::erase(uint pos, uint n) |
| 391 | { |
| 392 | uint sz = size(); |
| 393 | if (pos > sz) Throw(Out_of_range("string index error\n")); |
| 394 | uint xlen = sz-pos; if (xlen > n) xlen = n; sz -= xlen; |
| 395 | if (sz == 0) |
| 396 | { |
| 397 | if (HasCapacity()) GetStrRep()->sz = 0; |
| 398 | else { px->Drop(); px = ASN; return *this; } |
| 399 | } |
| 400 | StrRep* oldp = GetStrRep(); |
| 401 | if (HasCapacity()) |
| 402 | { |
| 403 | char* target = oldp->s + pos; |
| 404 | memmove(target, target + xlen, sz-pos); |
| 405 | oldp->sz = sz; |
| 406 | } |
| 407 | else |
| 408 | { |
| 409 | StrRep* p = new StrRepMult(sz); |
| 410 | px = p; |
| 411 | char* target = p->s; memcpy(target, oldp->s, pos); |
| 412 | target += pos; memcpy(target, oldp->s+pos+xlen, sz-pos); |
| 413 | oldp->Drop(); |
| 414 | } |
| 415 | return *this; |
| 416 | } |
| 417 | |
| 418 | String& String::my_replace(uint pos, uint n, const StrBase& sb) |
| 419 | { |
| 420 | uint sz = size(); uint rlen =sb.size(); |
| 421 | if (pos > sz) Throw(Out_of_range("string index error\n")); |
| 422 | uint xlen = sz-pos; if (xlen > n) xlen = n; sz -= xlen; |
| 423 | if (sz >= npos - rlen) Throw(Length_error("string length >= npos\n")); |
| 424 | StrRep* oldp = GetStrRep(); |
| 425 | if (rlen == xlen && refcount() == 0) // in-place replace OK |
| 426 | { char* target = oldp->s+pos; sb.Load(target); } |
| 427 | else |
| 428 | if (HasCapacity() && sz + rlen <= capacity()) |
| 429 | { |
| 430 | // must allow for possibility that sb is part of *this |
| 431 | char* target = oldp->s + pos; |
| 432 | StrRep tail(target + xlen, sz-pos); |
| 433 | sb.Load(target); tail.Load(target); oldp->sz = sz + rlen; |
| 434 | } |
| 435 | else if (sz + rlen == 0) { px->Drop(); px = ASN; return *this; } |
| 436 | else |
| 437 | { |
| 438 | StrRep* p = new StrRepMult(sz+rlen); px = p; |
| 439 | char* target = p->s; memcpy(target, oldp->s, pos); |
| 440 | target += pos; sb.Load(target); |
| 441 | memcpy(target, oldp->s+pos+xlen, sz-pos); |
| 442 | oldp->Drop(); |
| 443 | } |
| 444 | return *this; |
| 445 | } |
| 446 | |
| 447 | String& String::replace(uint pos1, uint n1, const String& str) |
| 448 | { return my_replace(pos1, n1, *(str.px)); } |
| 449 | |
| 450 | String& String::replace(uint pos1, uint n1, const String& str, uint pos2, |
| 451 | uint n2) |
| 452 | { return my_replace(pos1, n1, CharSeq(str, pos2, n2)); } |
| 453 | |
| 454 | String& String::replace(uint pos, uint n1, const char* s, uint n2) |
| 455 | { return my_replace(pos, n1, CharSeq((char*)s, n2)); } |
| 456 | |
| 457 | String& String::replace(uint pos, uint n1, uint n2, char c) |
| 458 | { return my_replace(pos, n1, CharRepeated(n2, c)); } |
| 459 | |
| 460 | |
| 461 | //************************* some odds and sods *************************** |
| 462 | |
| 463 | uint String::copy(char* s, uint n, uint pos) const |
| 464 | { |
| 465 | StrRep* p = GetStrRep(); uint rlen = size(); |
| 466 | if (pos > rlen) Throw(Out_of_range("string index error\n")); |
| 467 | rlen -= pos; if (rlen > n) rlen = n; |
| 468 | memcpy(s, p->s+pos, rlen); return rlen; |
| 469 | } |
| 470 | |
| 471 | void String::swap(String& str) |
| 472 | { |
| 473 | StrBase* oldp = px->UnProtect(); px = str.px->UnProtect(); str.px = oldp; |
| 474 | } |
| 475 | |
| 476 | const char* String::c_str() const |
| 477 | { return px->NullTerminate(&(StrBase*&)px); } |
| 478 | |
| 479 | const char* String::data() const |
| 480 | { StrRep* p = Protect(); ((String&)*this).px = p; return p->s; } |
| 481 | |
| 482 | |
| 483 | //***************************** the find functions *********************** |
| 484 | |
| 485 | uint String::find(const CharSeq& str, uint pos) const |
| 486 | { |
| 487 | uint str_sz = str.size(); uint sz = size(); |
| 488 | if (sz < str_sz || pos > sz - str_sz) return npos; |
| 489 | // need both checks to avoid overflows or negatives |
| 490 | if (str_sz == 0) return pos; |
| 491 | const char* start = GetData(); const char* newstart = start+pos; |
| 492 | const char* str_loc = str.s; |
| 493 | if (str_sz == 1) |
| 494 | { |
| 495 | newstart = (const char*)memchr(newstart, *str_loc, sz-pos); |
| 496 | if (!newstart) return npos; else return (uint)(newstart-start); |
| 497 | } |
| 498 | uint sindx = s_index; // otherwise may get problems if multithreading |
| 499 | for (;;) |
| 500 | { |
| 501 | if (sindx >= str_sz) sindx = 0; |
| 502 | uint m = sz+1-str_sz-(uint)(newstart-start); |
| 503 | if (m==0) { s_index = sindx; return npos; } |
| 504 | newstart = (const char*)memchr(newstart+sindx, str_loc[sindx], m); |
| 505 | if (!newstart) { s_index = sindx; return npos; } |
| 506 | newstart -= sindx; |
| 507 | if (memcmp(newstart, str_loc, str_sz) == 0) |
| 508 | { s_index = sindx; return (uint)(newstart-start); } |
| 509 | sindx++; newstart++; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | uint String::find(const String& str, uint pos) const |
| 514 | { return find(*(str.GetStrRep()), pos); } |
| 515 | |
| 516 | uint String::find(const char* s, uint pos, uint n) const |
| 517 | { return find(CharSeq((char*)s,n),pos); } |
| 518 | |
| 519 | uint String::find(const char* s, uint pos) const |
| 520 | { return find(CharSeq((char*)s),pos); } |
| 521 | |
| 522 | uint String::find(const char c, uint pos) const |
| 523 | { |
| 524 | uint sz = size(); |
| 525 | if (pos >= sz) return npos; // also ensures sz != 0 |
| 526 | const char* start = GetData(); const char* newstart = start+pos; |
| 527 | newstart = (const char*)memchr(newstart, c, sz-pos); |
| 528 | if (!newstart) return npos; else return (uint)(newstart-start); |
| 529 | } |
| 530 | |
| 531 | uint String::rfind(const CharSeq& str, uint pos) const |
| 532 | { |
| 533 | // can return sz if searching for a zero length string |
| 534 | // is this what the committee wants? |
| 535 | uint str_sz = str.size(); uint sz = size(); |
| 536 | if (sz < str_sz) return npos; |
| 537 | sz -= str_sz; if (pos > sz) pos = sz; |
| 538 | if (str_sz == 0) return pos; |
| 539 | const char* start = GetData(); const char* newstart = start+pos; |
| 540 | const char* str_loc = str.s; |
| 541 | uint sindx = s_index; |
| 542 | for (;;) |
| 543 | { |
| 544 | if (sindx >= str_sz) sindx = 0; |
| 545 | uint i = (uint)(newstart-start); |
| 546 | char c = str_loc[sindx]; newstart += sindx; |
| 547 | while (*newstart != c) |
| 548 | { |
| 549 | if (i-- == 0) { s_index = sindx; return npos; } |
| 550 | newstart--; |
| 551 | } |
| 552 | newstart -= sindx; |
| 553 | if (!memcmp(newstart, str_loc, str_sz)) |
| 554 | { s_index = sindx; return (uint)(newstart-start); } |
| 555 | if (--newstart < start) { s_index = sindx; return npos; } |
| 556 | sindx++; |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | uint String::rfind(const String& str, uint pos) const |
| 561 | { return rfind(*(str.GetStrRep()), pos); } |
| 562 | |
| 563 | uint String::rfind(const char* s, uint pos, uint n) const |
| 564 | { return rfind(CharSeq((char*)s,n),pos); } |
| 565 | |
| 566 | uint String::rfind(const char* s, uint pos) const |
| 567 | { return rfind(CharSeq((char*)s),pos); } |
| 568 | |
| 569 | uint String::rfind(const char c, uint pos) const |
| 570 | { |
| 571 | uint i = size(); if (i > pos) i = pos + 1; |
| 572 | char* s = GetData() + i; |
| 573 | while (i-- > 0) { if (*(--s) == c) return i; } |
| 574 | return npos; |
| 575 | } |
| 576 | |
| 577 | uint String::find_first_of(const CharSeq& str, uint pos) const |
| 578 | { |
| 579 | // this should be done without memchr at least when str is long |
| 580 | uint sz = size(); uint str_sz = str.size(); |
| 581 | if (str_sz == 0 || pos >= sz) return npos; // also ensures sz != 0 |
| 582 | const char* start = GetData(); const char* newstart = start+pos; |
| 583 | const char* str_loc = str.s; sz -= pos; |
| 584 | while (sz--) |
| 585 | { |
| 586 | if (memchr(str_loc, *newstart, str_sz)) return (uint)(newstart-start); |
| 587 | newstart++; |
| 588 | } |
| 589 | return npos; |
| 590 | } |
| 591 | |
| 592 | uint String::find_first_of(const String& str, uint pos) const |
| 593 | { return find_first_of(*(str.GetStrRep()), pos); } |
| 594 | |
| 595 | uint String::find_first_of(const char* s, uint pos, uint n) const |
| 596 | { return find_first_of(CharSeq((char*)s,n),pos); } |
| 597 | |
| 598 | uint String::find_first_of(const char* s, uint pos) const |
| 599 | { return find_first_of(CharSeq((char*)s),pos); } |
| 600 | |
| 601 | uint String::find_first_of(const char c, uint pos) const |
| 602 | { return find(c, pos); } |
| 603 | |
| 604 | uint String::find_last_of(const CharSeq& str, uint pos) const |
| 605 | { |
| 606 | uint sz = size(); if (sz > pos) sz = pos + 1; |
| 607 | uint str_sz = str.size(); if (str_sz == 0) return npos; |
| 608 | const char* start = GetData(); const char* newstart = start+sz; |
| 609 | const char* str_loc = str.s; |
| 610 | while (sz--) |
| 611 | { |
| 612 | if (memchr(str_loc, *(--newstart), str_sz)) |
| 613 | return (uint)(newstart-start); |
| 614 | } |
| 615 | return npos; |
| 616 | } |
| 617 | |
| 618 | uint String::find_last_of(const String& str, uint pos) const |
| 619 | { return find_last_of(*(str.GetStrRep()), pos); } |
| 620 | |
| 621 | uint String::find_last_of(const char* s, uint pos, uint n) const |
| 622 | { return find_last_of(CharSeq((char*)s,n),pos); } |
| 623 | |
| 624 | uint String::find_last_of(const char* s, uint pos) const |
| 625 | { return find_last_of(CharSeq((char*)s),pos); } |
| 626 | |
| 627 | uint String::find_last_of(const char c, uint pos) const |
| 628 | { return rfind(c, pos); } |
| 629 | |
| 630 | uint String::find_first_not_of(const CharSeq& str, uint pos) const |
| 631 | { |
| 632 | uint sz = size(); uint str_sz = str.size(); |
| 633 | if (pos >= sz) return npos; // also ensures sz != 0 |
| 634 | if (str_sz == 0 ) return pos; |
| 635 | const char* start = GetData(); const char* newstart = start+pos; |
| 636 | const char* str_loc = str.s; sz -= pos; |
| 637 | while (sz--) |
| 638 | { |
| 639 | if (!memchr(str_loc, *newstart, str_sz)) return (uint)(newstart-start); |
| 640 | newstart++; |
| 641 | } |
| 642 | return npos; |
| 643 | } |
| 644 | |
| 645 | uint String::find_first_not_of(const String& str, uint pos) const |
| 646 | { return find_first_not_of(*(str.GetStrRep()), pos); } |
| 647 | |
| 648 | uint String::find_first_not_of(const char* s, uint pos, uint n) const |
| 649 | { return find_first_not_of(CharSeq((char*)s,n),pos); } |
| 650 | |
| 651 | uint String::find_first_not_of(const char* s, uint pos) const |
| 652 | { return find_first_not_of(CharSeq((char*)s),pos); } |
| 653 | |
| 654 | uint String::find_first_not_of(const char c, uint pos) const |
| 655 | { |
| 656 | uint sz = size(); |
| 657 | if (pos >= sz) return npos; // also ensures sz != 0 |
| 658 | char* s = GetData() + pos; uint i = sz - pos; |
| 659 | while (i > 0) { if (*(s++) != c) return sz - i; i--; } |
| 660 | return npos; |
| 661 | } |
| 662 | |
| 663 | uint String::find_last_not_of(const CharSeq& str, uint pos) const |
| 664 | { |
| 665 | uint sz = size(); if (sz > pos) sz = pos + 1; |
| 666 | if (sz == 0) return npos; |
| 667 | uint str_sz = str.size(); if (str_sz == 0) return sz - 1; |
| 668 | const char* start = GetData(); const char* newstart = start+sz; |
| 669 | const char* str_loc = str.s; |
| 670 | while (sz--) |
| 671 | { |
| 672 | if (!memchr(str_loc, *(--newstart), str_sz)) |
| 673 | return (uint)(newstart-start); |
| 674 | } |
| 675 | return npos; |
| 676 | } |
| 677 | |
| 678 | uint String::find_last_not_of(const String& str, uint pos) const |
| 679 | { return find_last_not_of(*(str.GetStrRep()), pos); } |
| 680 | |
| 681 | uint String::find_last_not_of(const char* s, uint pos, uint n) const |
| 682 | { return find_last_not_of(CharSeq((char*)s,n),pos); } |
| 683 | |
| 684 | uint String::find_last_not_of(const char* s, uint pos) const |
| 685 | { return find_last_not_of(CharSeq((char*)s),pos); } |
| 686 | |
| 687 | uint String::find_last_not_of(const char c, uint pos) const |
| 688 | { |
| 689 | uint i = size(); if (i > pos) i = pos + 1; |
| 690 | char* s = GetData() + i; |
| 691 | while (i-- > 0) { if (*(--s) != c) return i; } |
| 692 | return npos; |
| 693 | } |
| 694 | |
| 695 | String String::substr(uint pos, uint n) const |
| 696 | { return String(*this, pos, n); } |
| 697 | |
| 698 | |
| 699 | //***************************** the booleans ***************************** |
| 700 | |
| 701 | int String::compare(uint pos, uint n, const CharSeq& sb) const |
| 702 | { |
| 703 | StrRep* p = GetStrRep(); uint sz = size(); uint str_sz = sb.size(); |
| 704 | if (pos > sz) Throw(Out_of_range("string index error\n")); |
| 705 | sz -= pos; |
| 706 | if (sz > n) sz = n; else n = sz; |
| 707 | if (n > str_sz) n = str_sz; |
| 708 | int m = memcmp(p->s+pos, sb.s, n); if (m) return m; |
| 709 | if (sz < str_sz) return -1; |
| 710 | if (sz > str_sz) return 1; |
| 711 | return 0; |
| 712 | } |
| 713 | |
| 714 | int String::compare(const String& str) const |
| 715 | { return compare(0, npos, CharSeq(str)); } |
| 716 | |
| 717 | int String::compare(uint pos1, uint n1, const String& str) const |
| 718 | { return compare(pos1, n1, CharSeq(str)); } |
| 719 | |
| 720 | int String::compare(uint pos1, uint n1, |
| 721 | const String& str, uint pos2, uint n2) const |
| 722 | { return compare(pos1, n1, CharSeq(str, pos2, n2)); } |
| 723 | |
| 724 | int String::compare(uint pos, uint n1, const char* s, uint n2) const |
| 725 | { return compare(pos, n1, CharSeq((char*)s, n2)); } |
| 726 | |
| 727 | int String::compare(const char* s) const |
| 728 | { return compare(0, npos, CharSeq((char*)s)); } |
| 729 | |
| 730 | |
| 731 | bool operator==(const String& lhs, const String& rhs) |
| 732 | { return lhs.compare(rhs) == 0; } |
| 733 | |
| 734 | bool operator==(const char* lhs, const String& rhs) |
| 735 | { return rhs.compare(lhs) == 0; } |
| 736 | |
| 737 | bool operator==(const String& lhs, const char* rhs) |
| 738 | { return lhs.compare(rhs) == 0; } |
| 739 | |
| 740 | bool operator!=(const String& lhs, const String& rhs) |
| 741 | { return lhs.compare(rhs) != 0; } |
| 742 | |
| 743 | bool operator!=(const char* lhs, const String& rhs) |
| 744 | { return rhs.compare(lhs) != 0; } |
| 745 | |
| 746 | bool operator!=(const String& lhs, const char* rhs) |
| 747 | { return lhs.compare(rhs) != 0; } |
| 748 | |
| 749 | bool operator<(const String& lhs, const String& rhs) |
| 750 | { return lhs.compare(rhs) < 0; } |
| 751 | |
| 752 | bool operator<(const char* lhs, const String& rhs) |
| 753 | { return rhs.compare(lhs) > 0; } |
| 754 | |
| 755 | bool operator<(const String& lhs, const char* rhs) |
| 756 | { return lhs.compare(rhs) < 0; } |
| 757 | |
| 758 | bool operator>(const String& lhs, const String& rhs) |
| 759 | { return lhs.compare(rhs) > 0; } |
| 760 | |
| 761 | bool operator>(const char* lhs, const String& rhs) |
| 762 | { return rhs.compare(lhs) < 0; } |
| 763 | |
| 764 | bool operator>(const String& lhs, const char* rhs) |
| 765 | { return lhs.compare(rhs) > 0; } |
| 766 | |
| 767 | bool operator<=(const String& lhs, const String& rhs) |
| 768 | { return lhs.compare(rhs) <= 0; } |
| 769 | |
| 770 | bool operator<=(const char* lhs, const String& rhs) |
| 771 | { return rhs.compare(lhs) >= 0; } |
| 772 | |
| 773 | bool operator<=(const String& lhs, const char* rhs) |
| 774 | { return lhs.compare(rhs) <= 0; } |
| 775 | |
| 776 | bool operator>=(const String& lhs, const String& rhs) |
| 777 | { return lhs.compare(rhs) >= 0; } |
| 778 | |
| 779 | bool operator>=(const char* lhs, const String& rhs) |
| 780 | { return rhs.compare(lhs) <= 0; } |
| 781 | |
| 782 | bool operator>=(const String& lhs, const char* rhs) |
| 783 | { return lhs.compare(rhs) >= 0; } |
| 784 | |
| 785 | |
| 786 | //****************** the io routines (need more work) ******************** |
| 787 | |
| 788 | istream& operator>>(istream& is, String& str) |
| 789 | { |
| 790 | char buf[1024]; |
| 791 | is.width(1024); |
| 792 | is >> buf; str = buf; |
| 793 | is.width(0); |
| 794 | return is; |
| 795 | } |
| 796 | |
| 797 | ostream& operator<<(ostream& os, const String& str) |
| 798 | { return os << str.c_str(); } |
| 799 | |
| 800 | |
| 801 | // from Alain Decamps |
| 802 | istream& getline(istream& is, String& str, char delim) |
| 803 | { |
| 804 | int token; |
| 805 | str.reserve(2048); |
| 806 | str.erase(); // with current implementation no reallocation here |
| 807 | while (((token = is.get()) != EOF) && (token != delim)) |
| 808 | { str += char(token); } |
| 809 | if (token == EOF) |
| 810 | { is.clear(ios::eofbit|is.rdstate()); } |
| 811 | // str.reserve(0); // waste of time if used in a "while not EOF" |
| 812 | return is; |
| 813 | } |
| 814 | |
| 815 | |
| 816 | //******************** bodies of the helper classes ********************** |
| 817 | |
| 818 | // the virtual functions - do not inline |
| 819 | |
| 820 | uint StrSum::size() const { return lhs->size() + rhs->size(); } |
| 821 | uint StrSumLC::size() const { return 1 + rhs->size(); } |
| 822 | uint StrSumRC::size() const { return lhs->size() + 1; } |
| 823 | uint CharSeq::size() const { return sz; } |
| 824 | uint CharSingle::size() const { return 1; } |
| 825 | uint CharRepeated::size() const { return n; } |
| 826 | uint StrNull::size() const { return 0; } |
| 827 | |
| 828 | |
| 829 | uint StrBase::capacity() const { return size(); } |
| 830 | uint CharSeq::capacity() const { return sz; } |
| 831 | uint StrRepCap::capacity() const { return cpx; } |
| 832 | |
| 833 | |
| 834 | void StrSum::Load(char*& ps) const { lhs->Load(ps); rhs->Load(ps); } |
| 835 | void StrSumLC::Load(char*& ps) const { *ps++ = lhs; rhs->Load(ps); } |
| 836 | void StrSumRC::Load(char*& ps) const { lhs->Load(ps); *ps++ = rhs; } |
| 837 | void CharSeq::Load(char*& ps) const { memmove(ps, s, sz); ps += sz; } |
| 838 | void CharSingle::Load(char*& ps) const { *ps++ = c; } |
| 839 | void CharRepeated::Load(char*& ps) const { memset(ps, c, n); ps += n; } |
| 840 | void StrNull::Load(char*&) const {} |
| 841 | |
| 842 | |
| 843 | StrRep* StrBase::GetStrRep(StrBase** px) |
| 844 | { |
| 845 | StrRep* p = new StrRepMult(size()); char* target = p->s; |
| 846 | Load(target); (*px)->Drop(); *px = p; return p; |
| 847 | } |
| 848 | |
| 849 | StrRep* StrRep::GetStrRep(StrBase**) { return this; } |
| 850 | |
| 851 | StrRep* StrBase::GetStrRepW(StrBase** px) |
| 852 | { |
| 853 | StrRep* p = new StrRepMult(size()); char* target = p->s; |
| 854 | Load(target); (*px)->Drop(); *px = p; return p; |
| 855 | } |
| 856 | |
| 857 | StrRep* StrRep::GetStrRepW(StrBase**) { return this; } |
| 858 | |
| 859 | StrRep* StrRepMult::GetStrRepW(StrBase** px) |
| 860 | { |
| 861 | if (refcnt == 0) return this; |
| 862 | StrRep* p = new StrRepMult(s, sz); |
| 863 | (*px)->Drop(); *px = p; return p; |
| 864 | } |
| 865 | |
| 866 | StrRep* StrBase::Protect(StrBase** px) |
| 867 | { |
| 868 | StrRep* p = new StrRep(size()); char* target = p->s; |
| 869 | Load(target); (*px)->Drop(); *px = p; return p; |
| 870 | } |
| 871 | |
| 872 | StrRep* StrRep::Protect(StrBase**) { return this; } |
| 873 | |
| 874 | StrRep* StrRepMult::Protect(StrBase** px) |
| 875 | { |
| 876 | StrRep* p; |
| 877 | if (refcnt == 0) |
| 878 | { |
| 879 | p = new StrRep(); |
| 880 | p->sz = sz; sz = 0; char* s1 = p->s; p->s = s; s = s1; |
| 881 | } |
| 882 | else |
| 883 | { |
| 884 | p = new StrRep(s, sz); |
| 885 | } |
| 886 | (*px)->Drop(); *px = p; return p; |
| 887 | } |
| 888 | |
| 889 | StrBase* StrBase::UnProtect() { return this; } |
| 890 | |
| 891 | StrBase* StrRep::UnProtect() |
| 892 | { |
| 893 | StrRepMult* p = new StrRepMult(); |
| 894 | p->sz = sz; sz = 0; char* s1 = p->s; p->s = s; s = s1; |
| 895 | p->refcnt = 0; delete this; return p; |
| 896 | } |
| 897 | |
| 898 | StrBase* StrRepMult::UnProtect() { return this; } |
| 899 | |
| 900 | StrBase* StrRepCap::UnProtect() |
| 901 | { |
| 902 | StrRepMult* p = new StrRepMult(s, sz); |
| 903 | delete this; return p; |
| 904 | } |
| 905 | |
| 906 | StrBase* StrRepNullTerm::UnProtect() |
| 907 | { |
| 908 | StrRepMult* p = new StrRepMult(s, sz); |
| 909 | delete this; return p; |
| 910 | } |
| 911 | |
| 912 | StrBase* StrNull::UnProtect() { return this; } |
| 913 | |
| 914 | void StrBase::WithCapacity(StrBase** px) |
| 915 | { |
| 916 | uint sz1 = size(); |
| 917 | StrRepCap* p = new StrRepCap(sz1,sz1); char* target = p->s; |
| 918 | Load(target); (*px)->Drop(); *px = p; |
| 919 | } |
| 920 | |
| 921 | void StrRepCap::WithCapacity(StrBase**) {} |
| 922 | |
| 923 | void StrRep::WithCapacity(StrBase** px) |
| 924 | { |
| 925 | StrRepCap* p = new StrRepCap(); |
| 926 | p->cpx = p->sz = sz; sz = 0; char* s1 = p->s; p->s = s; s = s1; |
| 927 | (*px)->Drop(); *px = p; |
| 928 | } |
| 929 | |
| 930 | void StrRepMult::WithCapacity(StrBase** px) |
| 931 | { |
| 932 | StrRepCap* p; |
| 933 | if (refcnt == 0) |
| 934 | { |
| 935 | p = new StrRepCap(); |
| 936 | p->cpx = p->sz = sz; sz = 0; char* s1 = p->s; p->s = s; s = s1; |
| 937 | } |
| 938 | else |
| 939 | { |
| 940 | p = new StrRepCap(s, sz); |
| 941 | } |
| 942 | (*px)->Drop(); *px = p; |
| 943 | } |
| 944 | |
| 945 | void StrRepNullTerm::WithCapacity(StrBase** px) |
| 946 | { |
| 947 | StrRepCap* p = new StrRepCap(s, sz); |
| 948 | (*px)->Drop(); *px = p; |
| 949 | } |
| 950 | |
| 951 | |
| 952 | const char* StrBase::NullTerminate(StrBase** px) |
| 953 | { |
| 954 | StrRep* p = new StrRepNullTerm(size()); |
| 955 | char* target = p->s; char* t = target; Load(target); *target = 0; |
| 956 | (*px)->Drop(); *px = p; return t; |
| 957 | } |
| 958 | |
| 959 | const char* StrRepNullTerm::NullTerminate(StrBase**) { return s; } |
| 960 | |
| 961 | const char* StrRepCap::NullTerminate(StrBase** px) |
| 962 | { |
| 963 | if (cpx > sz) { s[sz] = 0; return s; } |
| 964 | else return StrBase::NullTerminate(px); |
| 965 | } |
| 966 | |
| 967 | |
| 968 | void StrSum::Drop() |
| 969 | { if (ref) ref = false; else { lhs->Drop(); rhs->Drop(); delete this; } } |
| 970 | |
| 971 | void StrSumLC::Drop() |
| 972 | { if (ref) ref = false; else { rhs->Drop(); delete this; } } |
| 973 | |
| 974 | void StrSumRC::Drop() |
| 975 | { if (ref) ref = false; else { lhs->Drop(); delete this; } } |
| 976 | |
| 977 | void StrBase::Drop() { delete this; } |
| 978 | void StrRepMult::Drop() { if (refcnt-- == 0) delete this; } |
| 979 | void StrNull::Drop() {} |
| 980 | |
| 981 | |
| 982 | StrBase* StrSumBase::Clone(StrBase** px) |
| 983 | { |
| 984 | if (ref) |
| 985 | { |
| 986 | StrRepMult* p = new StrRepMult(size()); |
| 987 | p->refcnt++; |
| 988 | char* target = p->s; Load(target); (*px)->Drop(); *px = p; |
| 989 | return p; |
| 990 | } |
| 991 | else { ref = true; return this; } |
| 992 | } |
| 993 | |
| 994 | StrBase* CharSeq::Clone(StrBase**) |
| 995 | { |
| 996 | StrBase* sb = new StrRep(s,sz); |
| 997 | return sb; |
| 998 | } |
| 999 | |
| 1000 | StrBase* StrRep::Clone(StrBase**) |
| 1001 | { |
| 1002 | StrRepMult* p = new StrRepMult(s, size()); |
| 1003 | return p; |
| 1004 | } |
| 1005 | |
| 1006 | StrBase* StrRepMult::Clone(StrBase**) { refcnt++; return this; } |
| 1007 | |
| 1008 | StrBase* CharSingle::Clone(StrBase**) |
| 1009 | { |
| 1010 | StrBase* sb = new CharSingle(c); |
| 1011 | return sb; |
| 1012 | } |
| 1013 | |
| 1014 | StrBase* CharRepeated::Clone(StrBase**) |
| 1015 | { |
| 1016 | StrBase* sb = new CharRepeated(n,c); |
| 1017 | return sb; |
| 1018 | } |
| 1019 | |
| 1020 | StrBase* StrNull::Clone(StrBase**) { return this; } |
| 1021 | |
| 1022 | bool StrBase::HasCapacity() const { return false; } |
| 1023 | bool StrRepCap::HasCapacity() const { return true; } |
| 1024 | |
| 1025 | |
| 1026 | unsigned int StrBase::refcount() const { return 0; } |
| 1027 | unsigned int StrRepMult::refcount() const { return refcnt; } |
| 1028 | unsigned int StrSumBase::refcount() const { return (ref) ? 1 : 0; } |
| 1029 | |
| 1030 | |
| 1031 | |
| 1032 | //***************************** constructors ***************************** |
| 1033 | |
| 1034 | StrRep::StrRep(uint SZ) : CharSeq(SZ) |
| 1035 | { |
| 1036 | s = new char[sz]; |
| 1037 | } |
| 1038 | |
| 1039 | StrRep::StrRep(uint n, char c) : CharSeq(n) |
| 1040 | { |
| 1041 | s = new char[sz]; |
| 1042 | memset(s, c, sz); |
| 1043 | } |
| 1044 | |
| 1045 | StrRep::StrRep(const char* x) |
| 1046 | { |
| 1047 | sz = strlen(x); |
| 1048 | s = new char[sz]; |
| 1049 | memcpy(s, x, sz); |
| 1050 | } |
| 1051 | |
| 1052 | StrRep::StrRep(const char* x, uint len) : CharSeq(len) |
| 1053 | { |
| 1054 | s = new char[sz]; |
| 1055 | memcpy(s, x, sz); |
| 1056 | } |
| 1057 | |
| 1058 | StrRep::StrRep(const char* x, uint len, uint n, char c) |
| 1059 | : CharSeq(n) |
| 1060 | { |
| 1061 | s = new char[sz]; |
| 1062 | memcpy(s, x, len); memset(s+len, c, n-len); |
| 1063 | } |
| 1064 | |
| 1065 | StrRep::~StrRep() { if (s) delete [] s; } |
| 1066 | |
| 1067 | |
| 1068 | StrRepMult::StrRepMult(uint SZ) : StrRep(SZ), refcnt(0) {} |
| 1069 | StrRepMult::StrRepMult(uint n, char c) : StrRep(n, c), refcnt(0) {} |
| 1070 | StrRepMult::StrRepMult(const char* x) : StrRep(x), refcnt(0) {} |
| 1071 | |
| 1072 | StrRepMult::StrRepMult(const char* x, uint len) |
| 1073 | : StrRep(x, len), refcnt(0) {} |
| 1074 | |
| 1075 | StrRepMult::StrRepMult(const char* x, uint len, uint n, char c) |
| 1076 | : StrRep(x, len, n, c), refcnt(0) {} |
| 1077 | |
| 1078 | |
| 1079 | StrRepCap::StrRepCap(uint len, uint cp) : StrRep(cp), cpx(cp) { sz = len; } |
| 1080 | |
| 1081 | StrRepCap::StrRepCap(const char* x, uint len) : StrRep(x, len), cpx(len) {} |
| 1082 | |
| 1083 | |
| 1084 | StrRepNullTerm::StrRepNullTerm(uint len) : StrRep(len + 1) { sz = len; } |
| 1085 | |
| 1086 | StrNull::StrNull() : StrRepNullTerm(0) { *s = 0; } |
| 1087 | |
| 1088 | CharSeq::CharSeq(char* S) : sz(strlen(S)), s(S) {} |
| 1089 | |
| 1090 | CharSeq::CharSeq(char* S, uint n) : s(S) |
| 1091 | { uint i = 0; while (*S++) { if (i >= n) break; i++; } sz = i; } |
| 1092 | |
| 1093 | CharSeq::CharSeq(const String& str) : sz(str.size()), s(str.GetData()) {} |
| 1094 | |
| 1095 | CharSeq::CharSeq(const String& str, uint pos, uint n) |
| 1096 | { |
| 1097 | sz =str.size(); if (pos > sz) Throw(Out_of_range("string index error\n")); |
| 1098 | sz -= pos; if (sz > n) sz = n; |
| 1099 | s = str.GetData()+pos; |
| 1100 | } |
| 1101 | |
| 1102 | CharRepeated::CharRepeated(int N, char C) : CharSingle(C), n(N) |
| 1103 | { if (n==String::npos) Throw(Length_error("string length = npos\n")); } |
| 1104 | |
| 1105 | StrSum::StrSum(StrBase* L, StrBase* R) : lhs(L), rhs(R) {} |
| 1106 | StrSumLC::StrSumLC(char L, StrBase* R) : lhs(L), rhs(R) {} |
| 1107 | StrSumRC::StrSumRC(StrBase* L, char R) : lhs(L), rhs(R) {} |
| 1108 | StrSumBase::StrSumBase() : ref(false) {} |
| 1109 | |
| 1110 | |
| 1111 | //******************************** operator+ ***************************** |
| 1112 | |
| 1113 | String operator+(const String& lhs, const String& rhs) |
| 1114 | { |
| 1115 | StrSum* sos = new StrSum(lhs.Clone(), rhs.Clone()); |
| 1116 | return String(sos); |
| 1117 | } |
| 1118 | |
| 1119 | String operator+(const char* lhs, const String& rhs) |
| 1120 | { |
| 1121 | StrRep* lhscs = new StrRep((char*)lhs); |
| 1122 | StrSum* sos = new StrSum(lhscs, rhs.Clone()); |
| 1123 | return String(sos); |
| 1124 | } |
| 1125 | |
| 1126 | String operator+(char lhs, const String& rhs) |
| 1127 | { |
| 1128 | StrSumLC* sos = new StrSumLC(lhs, rhs.Clone()); |
| 1129 | return String(sos); |
| 1130 | } |
| 1131 | |
| 1132 | String operator+(const String& lhs, const char* rhs) |
| 1133 | { |
| 1134 | StrRep* rhscs = new StrRep((char*)rhs); |
| 1135 | StrSum* sos = new StrSum(lhs.Clone(), rhscs); |
| 1136 | return String(sos); |
| 1137 | } |
| 1138 | |
| 1139 | String operator+(const String& lhs, char rhs) |
| 1140 | { |
| 1141 | StrSumRC* sos = new StrSumRC(lhs.Clone(), rhs); |
| 1142 | return String(sos); |
| 1143 | } |
| 1144 | |
| 1145 | //******************** initialise string package ********************** |
| 1146 | |
| 1147 | // this is called whenever str.h is loaded; this ensures that npos is set to |
| 1148 | // -1 and ASN is initialised before any globals involving strings get |
| 1149 | // initialised. |
| 1150 | |
| 1151 | StringPackageInitialise::StringPackageInitialise() |
| 1152 | { |
| 1153 | if (!String::ASN) |
| 1154 | { String::ASN = new StrNull; String::npos = (uint)(-1); } |
| 1155 | } |
| 1156 | |
| 1157 | //********************************************************************* |
| 1158 | |
| 1159 | |
| 1160 | #ifdef use_namespace |
| 1161 | } |
| 1162 | #endif |