-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.cpp
More file actions
51 lines (46 loc) · 1.29 KB
/
data.cpp
File metadata and controls
51 lines (46 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "data.h"
Data::Data()
{
}
void Data::erase(size_t _beg, size_t _len){
size_t newLen = this->len - _len;
char* temp = new char[newLen];
size_t i = 0, j = 0;
for (; i < this->len; ++i, ++j) {
if (i == _beg)
i = _beg + _len;
temp[j] = this->contents[i];
}
delete[] this->contents;
this->contents = new char[newLen];
for (i = 0; i < newLen; ++i)
this->contents[i] = temp[i];
delete[] temp;
}
void Data::insert(size_t _beg, std::string _str, size_t _len){
size_t newLen = this->len + _len;
char *temp = new char[newLen];
// copy contents up to _beg
size_t i = 0;
for (; i < _beg; ++i) {
temp[i] = this->contents[i];
}
size_t j = i; // where we stopped at contents
// append _str to temp
for (; i < (_beg + _len); ++i) {
temp[i] = _str[i - _beg];
}
// copy the rest of contents into temp
for (; j < len; ++i, ++j)
temp[i] = this->contents[j];
delete[] this->contents;
this->contents = new char[newLen];
for (i = 0; i < newLen; ++i)
this->contents[i] = temp[i];
delete[] temp;
}
void Data::replace(size_t _beg, std::string _str, size_t _len){
for (size_t i = _beg; i < (_beg + _len); ++i) {
this->contents[i] = _str[i - _beg];
}
}