home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
magazine
/
drdobbs
/
1991
/
10
/
string
/
striins3.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1991-07-30
|
609b
|
29 lines
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <string.hpp>
String &String::insert(int pos, char c)
{
if (pos > length()) { // pos == length() is concatenate
errno = ERANGE;
return *this;
}
int n = length()+1;
srep *p = new(n) srep(n);
if (!p) {
errno = ENOMEM;
return *this;
}
memmove(p->body,body(),pos);
*(p->body+pos) = c;
memmove(p->body+pos+1, body()+pos, length()-pos);
if (rp) {
if (--rp->refs < 1)
delete rp;
}
rp = p;
rp->refs++;
return *this;
}