home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / 1991 / 10 / string / stritrun.cpp < prev   
C/C++ Source or Header  |  1991-07-30  |  506b  |  29 lines

  1. #include <string.h>
  2. #include <ctype.h>
  3. #include <errno.h>
  4. #include <string.hpp>
  5.  
  6. String &String::truncate(int n)
  7. {
  8.     if (n >= length())
  9.         return *this;
  10.     srep *p;
  11.     if (!n)
  12.         p = 0;
  13.     else {
  14.         p = new(n) srep(n);
  15.         if (!p) {
  16.             errno = ENOMEM;
  17.             return *this;
  18.         }
  19.         memmove(p->body,body(),n);
  20.     }
  21.     if (rp) {
  22.         if (--rp->refs < 1)
  23.         delete rp;
  24.     }
  25.     rp = p;
  26.     rp->refs++;
  27.     return *this;
  28.