Skip to content

Commit 69b1c65

Browse files
committed
[CPP] Add std::string::operator+=(...)
1 parent 5c4ce21 commit 69b1c65

File tree

5 files changed

+89
-15
lines changed

5 files changed

+89
-15
lines changed

src/usr/include/string.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,29 @@ class basic_string {
3434
std::vector<CharT> _data;
3535

3636
public:
37+
typedef CharT* iterator;
38+
typedef const CharT* const_iterator;
39+
3740
basic_string();
3841
basic_string(std::size_t n, CharT c);
3942
basic_string(const CharT* str);
4043
basic_string(const CharT* str, std::size_t n);
4144

45+
iterator begin();
46+
iterator end();
47+
const_iterator begin() const;
48+
const_iterator end() const;
49+
50+
void pop_back();
4251
CharT& back();
4352
CharT& front();
4453
CharT& at(std::size_t pos);
4554
CharT& operator[](std::size_t pos);
4655
const CharT *c_str() const;
56+
57+
basic_string<CharT>& operator+=(const CharT c);
58+
basic_string<CharT>& operator+=(const CharT* o);
59+
basic_string<CharT>& operator+=(const basic_string<CharT> &o);
4760
};
4861

4962
using string = basic_string<char>;

src/usr/include/string.tcc

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,33 @@ basic_string<CharT>::basic_string(const CharT* str) : _data(std::strlen(str)+1,
2222
}
2323

2424
template <typename CharT>
25-
basic_string<CharT>::basic_string(const CharT* str, std::size_t n) {
25+
basic_string<CharT>::basic_string(const CharT* str, std::size_t n) : _data(n+1, '\0') {
2626
int str_n = std::strlen(str);
27-
this->_data(n+1, '\0');
2827
for (std::size_t i = 0; i < n && i < str_n; i++) {
2928
this->_data[i] = str[i];
3029
}
3130
}
3231

32+
template <typename CharT>
33+
typename basic_string<CharT>::iterator basic_string<CharT>::begin() { return this->_data.begin(); }
34+
35+
template <typename CharT>
36+
typename basic_string<CharT>::iterator basic_string<CharT>::end() { return this->_data.end() - 1; }
37+
38+
template <typename CharT>
39+
typename basic_string<CharT>::const_iterator basic_string<CharT>::begin() const { return this->_data.begin(); }
40+
41+
template <typename CharT>
42+
typename basic_string<CharT>::const_iterator basic_string<CharT>::end() const { return this->_data.end() - 1; }
43+
44+
template <typename CharT>
45+
void basic_string<CharT>::pop_back() {
46+
if(this->_data.size()>1) {
47+
this->_data.pop_back();
48+
this->_data.last() = '\0';
49+
}
50+
}
51+
3352
template <typename CharT>
3453
CharT& basic_string<CharT>::back() {
3554
return this->_data.at(this->_data.size()-2);
@@ -52,7 +71,34 @@ CharT& basic_string<CharT>::operator[](std::size_t pos) {
5271

5372
template <typename CharT>
5473
const CharT *basic_string<CharT>::c_str() const {
55-
return this->_data.raw_data();
74+
return this->_data.begin();
75+
}
76+
77+
template <typename CharT>
78+
basic_string<CharT>& basic_string<CharT>::operator+=(const CharT c) {
79+
this->_data.back() = c; // replace null character
80+
this->_data.push_back('\0'); // add null character
81+
return *this;
82+
}
83+
84+
template <typename CharT>
85+
basic_string<CharT>& basic_string<CharT>::operator+=(const CharT* o) {
86+
this->_data.pop_back(); // remove null character
87+
while ((*o)) {
88+
this->_data.push_back(*(o++));
89+
}
90+
this->_data.push_back('\0'); // add null character
91+
return *this;
92+
}
93+
94+
template <typename CharT>
95+
basic_string<CharT>& basic_string<CharT>::operator+=(const basic_string<CharT> &o) {
96+
this->_data.pop_back(); // remove null character
97+
for(const CharT &c: o) {
98+
this->_data.push_back(c);
99+
}
100+
this->_data.push_back('\0'); // add null character
101+
return *this;
56102
}
57103

58104
} // namespace std end

src/usr/include/vector.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,17 @@ class vector {
1111
void resize_capacity(std::size_t capacity);
1212

1313
public:
14+
typedef T* iterator;
15+
typedef const T* const_iterator;
16+
1417
vector<T>();
1518
vector<T>(std::size_t size, const T &_default);
1619

20+
iterator begin();
21+
iterator end();
22+
const_iterator begin() const;
23+
const_iterator end() const;
24+
1725
T *raw_data() const;
1826
bool empty();
1927
std::size_t size();

src/usr/include/vector.tcc

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,30 @@ vector<T>::vector(std::size_t size, const T &_default): _size(size) {
3030
}
3131
}
3232

33+
template <typename T>
34+
typename vector<T>::iterator vector<T>::begin() { return this->_data; }
35+
36+
template <typename T>
37+
typename vector<T>::iterator vector<T>::end() { return this->_data + this->_size; }
38+
39+
template <typename T>
40+
typename vector<T>::const_iterator vector<T>::begin() const { return this->_data; }
41+
42+
template <typename T>
43+
typename vector<T>::const_iterator vector<T>::end() const { return this->_data + this->_size; }
44+
3345
template <typename T>
3446
void vector<T>::resize_capacity(std::size_t capacity) {
3547
// internal; assumes capacity to be power of 2
3648
// and will also make a copy of array and move _data pointer.
3749
// assumes size<=current_capacity and size<=new_capacity
38-
this->_capacity = capacity;
3950
const std::size_t data_size = capacity*sizeof(T);
4051
T *_new_data = (T*) std::malloc(data_size);
4152
if (!_new_data) return; // malloc failed
4253
std::memcpy(_new_data, this->_data, data_size);
4354
std::free(this->_data); // should be no-op if _data == NULL
4455
this->_data = _new_data;
45-
}
46-
47-
template <typename T>
48-
T *vector<T>::raw_data() const {
49-
return this->_data;
56+
this->_capacity = capacity;
5057
}
5158

5259
template <typename T>

src/usr/local/src/simplecpp.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,15 @@ int main(int argc, char *argv[]) {
6060
printShapeDetails(&r1);
6161
printShapeDetails(&s1);
6262

63-
// name
63+
// new, delete and std::string
6464
char *name = new char[1024];
6565
std::cout << "Enter your name: ";
6666
std::cin >> name;
67-
std::cout << "Your name is: " << name << std::endl;
67+
std::string cool_name;
68+
cool_name += '[';
69+
cool_name += name;
70+
cool_name += ']';
71+
std::cout << "Your name is: " << cool_name << std::endl;
6872
delete[] name;
6973

7074
// vector
@@ -74,10 +78,6 @@ int main(int argc, char *argv[]) {
7478
std::cout << "list[" << (int)i << "] = " << list[i] << std::endl;
7579
}
7680

77-
// std::string
78-
std::string cpp_string("i am c++ string");
79-
std::cout << "Out: " << cpp_string << std::endl;
80-
8181
std::cout << "Exiting..." << std::endl;
8282
return 0;
8383
}

0 commit comments

Comments
 (0)