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

  1. #include <string.h>
  2. #include <ctype.h>
  3. #include <errno.h>
  4. #include <string.hpp>
  5.  
  6. String &String::insert(int pos, const char *s, int count)
  7. {
  8.     int sl = count? count: strlen(s);
  9.     if (!sl)
  10.         return *this;
  11.     if (pos > length()) { // pos == length() is concatenate
  12.         errno = ERANGE;
  13.         return *this;
  14.     }
  15.     int n = length()+sl;
  16.     srep *p = new(n) srep(n);
  17.     if (!p) {
  18.         errno = ENOMEM;
  19.         return *this;
  20.     }
  21.     memmove(p->body,body(),pos);
  22.     memmove(p->body+pos, s, sl);
  23.     memmove(p->body+pos+sl, body()+pos, length()-pos);
  24.     if (rp) {
  25.         if (--rp->refs < 1)
  26.         delete rp;
  27.     }
  28.     rp = p;
  29.     rp->refs++;
  30.     return *this;
  31. }
  32.