|
| 1 | +#pragma once |
| 2 | + |
| 3 | +// This is internal headerfile to be included by string.h only |
| 4 | + |
| 5 | +#include <string.h> |
| 6 | + |
| 7 | +namespace std { |
| 8 | + |
| 9 | +template <typename CharT> |
| 10 | +basic_string<CharT>::basic_string() : _data(1, '\0') {} |
| 11 | + |
| 12 | +template <typename CharT> |
| 13 | +basic_string<CharT>::basic_string(std::size_t n, CharT c) : _data(n+1, c) { |
| 14 | + this->_data[n] = '\0'; |
| 15 | +} |
| 16 | + |
| 17 | +template <typename CharT> |
| 18 | +basic_string<CharT>::basic_string(const CharT* str) : _data(std::strlen(str)+1, '\0') { |
| 19 | + for (std::size_t i = 0; i < this->_data.size(); i++) { |
| 20 | + this->_data[i] = str[i]; |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +template <typename CharT> |
| 25 | +basic_string<CharT>::basic_string(const CharT* str, std::size_t n) { |
| 26 | + int str_n = std::strlen(str); |
| 27 | + this->_data(n+1, '\0'); |
| 28 | + for (std::size_t i = 0; i < n && i < str_n; i++) { |
| 29 | + this->_data[i] = str[i]; |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +template <typename CharT> |
| 34 | +CharT& basic_string<CharT>::back() { |
| 35 | + return this->_data.at(this->_data.size()-2); |
| 36 | +} |
| 37 | + |
| 38 | +template <typename CharT> |
| 39 | +CharT& basic_string<CharT>::front() { |
| 40 | + return this->_data.at(0); |
| 41 | +} |
| 42 | + |
| 43 | +template <typename CharT> |
| 44 | +CharT& basic_string<CharT>::at(std::size_t pos) { |
| 45 | + return this->_data.at(pos); |
| 46 | +} |
| 47 | + |
| 48 | +template <typename CharT> |
| 49 | +CharT& basic_string<CharT>::operator[](std::size_t pos) { |
| 50 | + return this->_data[pos]; |
| 51 | +} |
| 52 | + |
| 53 | +template <typename CharT> |
| 54 | +const CharT *basic_string<CharT>::c_str() const { |
| 55 | + return this->_data.raw_data(); |
| 56 | +} |
| 57 | + |
| 58 | +} // namespace std end |
0 commit comments