home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / 1991 / 10 / string / stripleq.cpp < prev    next >
C/C++ Source or Header  |  1991-07-30  |  1KB  |  65 lines

  1. #include <string.h>
  2. #include <ctype.h>
  3. #include <errno.h>
  4. #include <string.hpp>
  5.  
  6. String &String::operator+=(const String &s)
  7. {
  8.     if (!s.rp) return *this;
  9.     int n = length()+s.length();
  10.     srep *p = new(n) srep(n);
  11.     if (!p) {
  12.         errno = ENOMEM;
  13.         return *this;
  14.     }
  15.     memmove(p->body,body(),length());
  16.     memmove(p->body+length(),s.body(),s.length());
  17.     if (rp) {
  18.         if (--rp->refs < 1)
  19.         delete rp;
  20.     }
  21.     rp = p;
  22.     rp->refs++;
  23.     return *this;
  24. }
  25.  
  26. String &String::operator+=(const char *s)
  27. {
  28.     int n = strlen(s);
  29.     if (!n) return *this;
  30.     int m = n+length();
  31.     srep *p = new(m) srep(m);
  32.     if (!p) {
  33.         errno = ENOMEM;
  34.         return *this;
  35.     }
  36.     memmove(p->body,body(),length());
  37.     memmove(p->body+length(),s,n);
  38.     if (rp) {
  39.         if (--rp->refs < 1)
  40.         delete rp;
  41.     }
  42.     rp = p;
  43.     rp->refs++;
  44.     return *this;
  45. }
  46.  
  47. String &String::operator+=(char c)
  48. {
  49.     int n = length()+1;
  50.     srep *p = new(n) srep(n);
  51.     if (!p) {
  52.         errno = ENOMEM;
  53.         return *this;
  54.     }
  55.     memmove(p->body,body(),length());
  56.     p->body[n-1] = c;
  57.     if (rp) {
  58.         if (--rp->refs < 1)
  59.         delete rp;
  60.     }
  61.     rp = p;
  62.     rp->refs++;
  63.     return *this;
  64. }
  65.