mirror of
https://github.com/wichtounet/thor-os.git
synced 2025-09-19 01:25:05 -04:00
Transform string into template
This commit is contained in:
parent
7af86c284d
commit
9095ac83cf
@ -2,69 +2,204 @@
|
||||
#define STRING_H
|
||||
|
||||
#include "types.hpp"
|
||||
#include "utils.hpp"
|
||||
|
||||
struct string {
|
||||
template<typename CharT>
|
||||
struct basic_string {
|
||||
public:
|
||||
typedef char* iterator;
|
||||
typedef const char* const_iterator;
|
||||
typedef CharT* iterator;
|
||||
typedef const CharT* const_iterator;
|
||||
|
||||
private:
|
||||
char* _data;
|
||||
CharT* _data;
|
||||
size_t _size;
|
||||
size_t _capacity;
|
||||
|
||||
public:
|
||||
//Constructors
|
||||
|
||||
string();
|
||||
string(const char* s);
|
||||
explicit string(size_t capacity);
|
||||
basic_string(){
|
||||
_size = 0;
|
||||
_capacity = 1;
|
||||
_data = new CharT[_capacity];
|
||||
_data[0] = '\0';
|
||||
}
|
||||
|
||||
basic_string(const CharT* s){
|
||||
_size = str_len(s);
|
||||
_capacity = _size + 1;
|
||||
_data = new CharT[_capacity];
|
||||
memcopy(_data, s, _capacity);
|
||||
}
|
||||
|
||||
explicit basic_string(size_t __capacity){
|
||||
_size = 0;
|
||||
_capacity = __capacity;
|
||||
_data = new CharT[_capacity];
|
||||
}
|
||||
|
||||
//Copy constructors
|
||||
|
||||
string(const string& rhs);
|
||||
string& operator=(const string& rhs);
|
||||
basic_string(const basic_string& rhs){
|
||||
_capacity = rhs._capacity;
|
||||
_size = rhs._size;
|
||||
_data = new CharT[_capacity];
|
||||
memcopy(_data, rhs._data, _size + 1);
|
||||
}
|
||||
|
||||
basic_string& operator=(const basic_string& rhs){
|
||||
if(this != &rhs){
|
||||
if(_capacity < rhs._capacity || !_data){
|
||||
if(_data){
|
||||
delete[] _data;
|
||||
}
|
||||
|
||||
_capacity = rhs._capacity;
|
||||
_data = new CharT[_capacity];
|
||||
}
|
||||
|
||||
_size = rhs._size;
|
||||
memcopy(_data, rhs._data, _size + 1);
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
//Move constructors
|
||||
|
||||
string(string&& rhs);
|
||||
string& operator=(string&& rhs);
|
||||
basic_string(basic_string&& rhs){
|
||||
_size = rhs._size;
|
||||
_capacity = rhs._capacity;
|
||||
_data = rhs._data;
|
||||
|
||||
rhs._size = 0;
|
||||
rhs._capacity = 0;
|
||||
rhs._data = nullptr;
|
||||
}
|
||||
|
||||
basic_string& operator=(basic_string&& rhs){
|
||||
if(_data){
|
||||
delete[] _data;
|
||||
}
|
||||
|
||||
_size = rhs._size;
|
||||
_capacity = rhs._capacity;
|
||||
_data = rhs._data;
|
||||
|
||||
rhs._size = 0;
|
||||
rhs._capacity = 0;
|
||||
rhs._data = nullptr;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
//Destructors
|
||||
|
||||
~string();
|
||||
~basic_string(){
|
||||
if(_data){
|
||||
delete[] _data;
|
||||
}
|
||||
}
|
||||
|
||||
//Modifiers
|
||||
|
||||
void clear();
|
||||
void clear(){
|
||||
_size = 0;
|
||||
_data[0] = '\0';
|
||||
}
|
||||
|
||||
void pop_back();
|
||||
void pop_back(){
|
||||
_data[--_size] = '\0';
|
||||
}
|
||||
|
||||
string operator+(char c) const;
|
||||
string& operator+=(char c);
|
||||
basic_string operator+(CharT c) const {
|
||||
basic_string copy(*this);
|
||||
|
||||
copy += c;
|
||||
|
||||
return move(copy);
|
||||
}
|
||||
|
||||
basic_string& operator+=(CharT c){
|
||||
if(!_data || _capacity <= _size + 1){
|
||||
_capacity = _capacity ? _capacity * 2 : 1;
|
||||
auto new_data = new CharT[_capacity];
|
||||
|
||||
if(_data){
|
||||
memcopy(new_data, _data, _size);
|
||||
delete[] _data;
|
||||
}
|
||||
|
||||
_data = new_data;
|
||||
}
|
||||
|
||||
_data[_size] = c;
|
||||
_data[++_size] = '\0';
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
//Accessors
|
||||
|
||||
size_t size() const;
|
||||
size_t capacity() const;
|
||||
bool empty() const;
|
||||
size_t size() const {
|
||||
return _size;
|
||||
}
|
||||
|
||||
const char* c_str() const;
|
||||
size_t capacity() const {
|
||||
return _capacity;
|
||||
}
|
||||
|
||||
char& operator[](size_t i);
|
||||
const char& operator[](size_t i) const;
|
||||
bool empty() const {
|
||||
return !_size;
|
||||
}
|
||||
|
||||
const CharT* c_str() const {
|
||||
return _data;
|
||||
}
|
||||
|
||||
CharT& operator[](size_t i){
|
||||
return _data[i];
|
||||
}
|
||||
|
||||
const CharT& operator[](size_t i) const {
|
||||
return _data[i];
|
||||
}
|
||||
|
||||
//Operators
|
||||
|
||||
bool operator==(const char* s) const;
|
||||
bool operator==(const CharT* s) const {
|
||||
if(size() != str_len(s)){
|
||||
return false;
|
||||
}
|
||||
|
||||
for(size_t i = 0; i < size(); ++i){
|
||||
if(_data[i] != s[i]){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//Iterators
|
||||
|
||||
iterator begin();
|
||||
iterator end();
|
||||
iterator begin(){
|
||||
return iterator(&_data[0]);
|
||||
}
|
||||
|
||||
const_iterator begin() const;
|
||||
const_iterator end() const;
|
||||
iterator end(){
|
||||
return iterator(&_data[_size]);
|
||||
}
|
||||
|
||||
const_iterator begin() const {
|
||||
return const_iterator(&_data[0]);
|
||||
}
|
||||
|
||||
const_iterator end() const {
|
||||
return const_iterator(&_data[_size]);
|
||||
}
|
||||
};
|
||||
|
||||
typedef basic_string<char> string;
|
||||
|
||||
#endif
|
||||
|
@ -9,7 +9,10 @@
|
||||
#define UTILS_H
|
||||
|
||||
#include "types.hpp"
|
||||
#include "string.hpp"
|
||||
|
||||
template<typename CharT>
|
||||
struct basic_string;
|
||||
typedef basic_string<char> string;
|
||||
|
||||
uint64_t parse(const char* str);
|
||||
uint64_t parse(const char* str, const char* end);
|
||||
|
@ -160,9 +160,9 @@ void k_print(char key){
|
||||
}
|
||||
}
|
||||
|
||||
void k_print(const char* string){
|
||||
for(uint64_t i = 0; string[i] != 0; ++i){
|
||||
k_print(string[i]);
|
||||
void k_print(const char* str){
|
||||
for(uint64_t i = 0; str[i] != 0; ++i){
|
||||
k_print(str[i]);
|
||||
}
|
||||
}
|
||||
|
||||
@ -172,9 +172,9 @@ void k_print(const string& s){
|
||||
}
|
||||
}
|
||||
|
||||
void k_print(const char* string, uint64_t end){
|
||||
for(uint64_t i = 0; string[i] != 0 && i < end; ++i){
|
||||
k_print(string[i]);
|
||||
void k_print(const char* str, uint64_t end){
|
||||
for(uint64_t i = 0; str[i] != 0 && i < end; ++i){
|
||||
k_print(str[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,170 +0,0 @@
|
||||
#include "string.hpp"
|
||||
#include "utils.hpp"
|
||||
#include "console.hpp"
|
||||
|
||||
string::string(){
|
||||
_size = 0;
|
||||
_capacity = 1;
|
||||
_data = new char[_capacity];
|
||||
_data[0] = '\0';
|
||||
}
|
||||
|
||||
string::string(const char* s){
|
||||
_size = str_len(s);
|
||||
_capacity = _size + 1;
|
||||
_data = new char[_capacity];
|
||||
memcopy(_data, s, _capacity);
|
||||
}
|
||||
|
||||
string::string(size_t capacity){
|
||||
_size = 0;
|
||||
_capacity = capacity;
|
||||
_data = new char[_capacity];
|
||||
}
|
||||
|
||||
string::string(const string& rhs){
|
||||
_capacity = rhs._capacity;
|
||||
_size = rhs._size;
|
||||
_data = new char[_capacity];
|
||||
memcopy(_data, rhs._data, _size + 1);
|
||||
}
|
||||
|
||||
string& string::operator=(const string& rhs){
|
||||
if(this != &rhs){
|
||||
if(_capacity < rhs._capacity || !_data){
|
||||
if(_data){
|
||||
delete[] _data;
|
||||
}
|
||||
|
||||
_capacity = rhs._capacity;
|
||||
_data = new char[_capacity];
|
||||
}
|
||||
|
||||
_size = rhs._size;
|
||||
memcopy(_data, rhs._data, _size + 1);
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
string::string(string&& rhs){
|
||||
_size = rhs._size;
|
||||
_capacity = rhs._capacity;
|
||||
_data = rhs._data;
|
||||
|
||||
rhs._size = 0;
|
||||
rhs._capacity = 0;
|
||||
rhs._data = nullptr;
|
||||
}
|
||||
|
||||
string& string::operator=(string&& rhs){
|
||||
if(_data){
|
||||
delete[] _data;
|
||||
}
|
||||
|
||||
_size = rhs._size;
|
||||
_capacity = rhs._capacity;
|
||||
_data = rhs._data;
|
||||
|
||||
rhs._size = 0;
|
||||
rhs._capacity = 0;
|
||||
rhs._data = nullptr;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
string::~string(){
|
||||
if(_data){
|
||||
delete[] _data;
|
||||
}
|
||||
}
|
||||
|
||||
void string::clear(){
|
||||
_size = 0;
|
||||
_data[0] = '\0';
|
||||
}
|
||||
|
||||
void string::pop_back(){
|
||||
_data[--_size] = '\0';
|
||||
}
|
||||
|
||||
size_t string::size() const {
|
||||
return _size;
|
||||
}
|
||||
|
||||
size_t string::capacity() const {
|
||||
return _capacity;
|
||||
}
|
||||
|
||||
bool string::empty() const {
|
||||
return !_size;
|
||||
}
|
||||
|
||||
const char* string::c_str() const {
|
||||
return _data;
|
||||
}
|
||||
|
||||
char& string::operator[](size_t i){
|
||||
return _data[i];
|
||||
}
|
||||
|
||||
const char& string::operator[](size_t i) const {
|
||||
return _data[i];
|
||||
}
|
||||
|
||||
string string::operator+(char c) const {
|
||||
string copy(*this);
|
||||
|
||||
copy += c;
|
||||
|
||||
return move(copy);
|
||||
}
|
||||
|
||||
string& string::operator+=(char c){
|
||||
if(!_data || _capacity <= _size + 1){
|
||||
_capacity = _capacity ? _capacity * 2 : 1;
|
||||
auto new_data = new char[_capacity];
|
||||
|
||||
if(_data){
|
||||
memcopy(new_data, _data, _size);
|
||||
delete[] _data;
|
||||
}
|
||||
|
||||
_data = new_data;
|
||||
}
|
||||
|
||||
_data[_size] = c;
|
||||
_data[++_size] = '\0';
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool string::operator==(const char* s) const {
|
||||
if(size() != str_len(s)){
|
||||
return false;
|
||||
}
|
||||
|
||||
for(size_t i = 0; i < size(); ++i){
|
||||
if(_data[i] != s[i]){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
string::iterator string::begin(){
|
||||
return iterator(&_data[0]);
|
||||
}
|
||||
|
||||
string::iterator string::end(){
|
||||
return iterator(&_data[_size]);
|
||||
}
|
||||
|
||||
string::const_iterator string::begin() const {
|
||||
return const_iterator(&_data[0]);
|
||||
}
|
||||
|
||||
string::const_iterator string::end() const {
|
||||
return const_iterator(&_data[_size]);
|
||||
}
|
@ -6,6 +6,7 @@
|
||||
//=======================================================================
|
||||
|
||||
#include "utils.hpp"
|
||||
#include "string.hpp"
|
||||
|
||||
bool str_equals(const char* a, const char* b){
|
||||
while(*a && *a == *b){
|
||||
|
Loading…
x
Reference in New Issue
Block a user