Created
October 16, 2017 20:56
-
-
Save unreadable/9f49dc9ce6c4bb6994d4b177131d4e27 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef VEC_CPP | |
#define VEC_CPP | |
#include "util.h" | |
#include "vec.cpp" | |
String::String() : s(nullptr) {}; | |
String::String(const char *str) { | |
int l = len(str); | |
this->s = new char[l + 1]; | |
// copy into s char by char | |
for(uint i = 0 ; i < l+1; i++) { | |
this->s[i] = (i == l) ? _null_char : str[i]; | |
} | |
} | |
String::String(const String &strc) { | |
uint l = str->size(); | |
if(l) { | |
this->s = new char[l + 1]; | |
for(uint i = 0 ; i < l+1; i++) { | |
this->s[i] = (i == l) ? _null_char : str[i]; | |
} | |
} else { | |
this->s = nullptr; | |
} | |
} | |
String::~String() { | |
if(this->s) { | |
delete[] this->s; | |
} | |
} | |
String &String::operator=(const String &strc) { | |
if (strc == *this) { return *this; } | |
uint l = strc->size(); | |
char *arr = new char[l + 1]; | |
for(uint i = 0 ; i < l+1; i++) { | |
this->s[i] = (i == l) ? _null_char : strc[i]; | |
} | |
delete[] this->s; | |
this->s = arr; | |
return *this; | |
} | |
String &String::operator=(const char *strc) { | |
uint l = len(strc); | |
char *arr = new char[l + 1]; | |
for(uint i = 0 ; i < l+1; i++) { | |
arr[i] = (i == l) ? _null_char : strc[i]; | |
} | |
delete[] this->s; | |
this->s = arr; | |
return *this; | |
} | |
String &String::operator+=(const char *strc) { | |
uint l = len(strc); | |
uint full_l = this.size() + l; | |
char *arr = new char[full_l + 1]; | |
for (uint i = 0; i < this->size(); i++) { | |
arr[i] = this->s[i]; | |
} | |
for (uint j = m_length; j < totalLength; j++) { | |
arr[j] = strc[j - this->size()]; | |
} | |
arr[full_l] = _null_char; | |
delete[] this->s; | |
this->s = arr; | |
return *this; | |
} | |
String &String::operator+=(const String &strc) { | |
uint full_l = this->size() + strc.size(); | |
char *arr = new char[full_l + 1]; | |
for (int i = 0; i < this->size(); i++) { | |
arr[i] = this->s[i]; | |
} | |
for (int j = this->size(); j < full_l; j++) { | |
arr[j] = strc->s[j - this->size()]; | |
} | |
arr[full_l] = _null_char; | |
delete[] this->s; | |
this->s = arr; | |
return *this; | |
} | |
String::size() { return len(this->s) }; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment