Created
July 14, 2021 21:50
-
-
Save DreamVB/7b65c6e6a1fa2269e75376100d6db670 to your computer and use it in GitHub Desktop.
Custom String Class
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
//XString class a custom string class for C++ | |
//Please follow me if you like this code | |
#include <iostream> | |
using namespace std; | |
class XString{ | |
private: | |
char *src = nullptr; | |
unsigned int s_size; | |
unsigned int length(const char *s) const{ | |
unsigned int x = 0; | |
while (s[x]){ | |
x++; | |
} | |
return x; | |
} | |
unsigned int str_comp(const char *s1, const char *s2) const{ | |
unsigned int flag = 0; | |
unsigned int x = 0; | |
while (flag == 0){ | |
if (s1[x] > s2[x]){ | |
flag = 1; | |
} | |
else if (s1[x] < s2[x]){ | |
flag = -1; | |
} | |
if (s1[x] == '\0'){ | |
break; | |
} | |
x++; | |
} | |
return flag; | |
} | |
int find(const char *s, bool find_last) const{ | |
//Locate a substring in buffer and return the index found at | |
//I am sure there are quicker ways of doing this but for now this works ok. | |
unsigned int s_pos = 0; | |
unsigned int l_pos = 0; | |
unsigned int x = 0; | |
unsigned int y = 0; | |
unsigned int len = length(this->src); | |
char *sub = nullptr; | |
unsigned int pi = 0; | |
unsigned int ti = 0; | |
int idx = -1; | |
l_pos = length(s); | |
sub = new char[l_pos + 1]; | |
if (l_pos > len){ | |
return -1; | |
} | |
while (x < len){ | |
if (this->src[x] == s[0]){ | |
s_pos = x; | |
pi = (x + l_pos); | |
ti = 0; | |
for (y = x; y < pi; y++){ | |
sub[ti] = this->src[y]; | |
ti++; | |
} | |
sub[ti] = '\0'; | |
if (str_comp(sub, s) == 0){ | |
if (s_pos == 0){ | |
idx = 0; | |
} | |
else{ | |
idx = (s_pos - 1); | |
} | |
if (!find_last){ | |
break; | |
} | |
} | |
} | |
x++; | |
} | |
//Clear up | |
sub = new char[0]; | |
return idx; | |
} | |
public: | |
typedef char *iterator; | |
unsigned int size() const{ | |
return length(src); | |
} | |
~XString(){ | |
src = new char[0]; | |
src = nullptr; | |
} | |
XString(){ | |
this->s_size = 1; | |
src = new char[this->s_size]; | |
src[0] = '\0'; | |
} | |
XString(char c){ | |
this->s_size = 2; | |
src = new char[this->s_size]; | |
src[0] = c; | |
src[1] = '\0'; | |
} | |
XString(const char *s){ | |
int n = length(s); | |
int x = 0; | |
if (!s){ | |
this->s_size = 0; | |
src = new char[0]; | |
} | |
else{ | |
src = new char[n + 1]; | |
while (x < n){ | |
src[x] = s[x]; | |
x++; | |
} | |
src[x] = '\0'; | |
} | |
} | |
XString(XString & xs){ | |
int n = length(xs.src); | |
int x = 0; | |
src = new char[n]; | |
while (x < n){ | |
src[x] = xs.src[x]; | |
x++; | |
} | |
src[x] = '\0'; | |
} | |
XString operator=(XString& xs){ | |
unsigned int n = length(xs.src); | |
unsigned int x = 0; | |
src = new char[n]; | |
while (x < n){ | |
src[x] = xs.src[x]; | |
x++; | |
} | |
src[x] = '\0'; | |
return *this; | |
} | |
XString operator=(char *s){ | |
unsigned int n = length(s); | |
unsigned int x = 0; | |
src = new char[n]; | |
while (x < n){ | |
src[x] = s[x]; | |
x++; | |
} | |
src[x] = '\0'; | |
return *this; | |
} | |
XString operator=(char c){ | |
unsigned int n = 1; | |
src = new char[n]; | |
src[0] = c; | |
src[1] = '\0'; | |
return *this; | |
} | |
XString& operator+=(const XString& xs){ | |
char *temp = nullptr; | |
unsigned int n = length(src) + xs.size(); | |
unsigned int x = 0; | |
unsigned int y = 0; | |
temp = new char[n]; | |
while (src[x]){ | |
temp[x] = src[x]; | |
x++; | |
} | |
while (y < length(xs.src)){ | |
temp[x] = xs.src[y]; | |
x++; | |
y++; | |
} | |
src = temp; | |
src[x] = '\0'; | |
return *this; | |
} | |
XString operator+(const XString& xs){ | |
char *temp = nullptr; | |
unsigned int n = length(src) + xs.size(); | |
unsigned int x = 0; | |
unsigned int y = 0; | |
temp = new char[n]; | |
while (src[x]){ | |
temp[x] = src[x]; | |
x++; | |
} | |
while (y < length(xs.src)){ | |
temp[x] = xs.src[y]; | |
x++; | |
y++; | |
} | |
src = temp; | |
src[x] = '\0'; | |
return *this; | |
} | |
operator const char*()const{ | |
return src; | |
} | |
char *c_str()const { | |
return src; | |
} | |
char at(unsigned int index){ | |
if (index >= 0 && index < size()) | |
return src[index]; | |
throw std::out_of_range("Index Out Of Range"); | |
} | |
char operator[](unsigned int index) const{ | |
if (index >= 0 && index < this->size()) | |
return src[index]; | |
throw std::out_of_range("Index Out Of Range"); | |
} | |
char& operator[](unsigned int index){ | |
if (index >= 0 && index < this->size()) | |
return src[index]; | |
throw std::out_of_range("Index Out Of Range"); | |
} | |
bool operator==(const XString& xs){ | |
if (!xs.src) | |
return false; | |
return str_comp(this->src, xs.src) == 0; | |
} | |
bool operator==(const char* s) const{ | |
if (!s) | |
return false; | |
return str_comp(this->src, s) == 0; | |
} | |
bool operator>(const char *s) const{ | |
if (!s) | |
return false; | |
return str_comp(this->src, s) == 1; | |
} | |
bool operator<(const char *s) const{ | |
int x; | |
if (!s) | |
{ | |
return false; | |
} | |
x = str_comp(this->src, s) == (unsigned)-1; | |
return x; | |
} | |
unsigned int Compare(const XString s1, const XString s2){ | |
return str_comp(s1.src, s2.src); | |
} | |
void append(const char *s){ | |
char *temp = nullptr; | |
unsigned int n = length(src) + length(s); | |
unsigned int x = 0; | |
unsigned int y = 0; | |
temp = new char[n]; | |
while (src[x]){ | |
temp[x] = src[x]; | |
x++; | |
} | |
while (y < length(s)){ | |
temp[x] = s[y]; | |
x++; | |
y++; | |
} | |
src = temp; | |
src[x] = '\0'; | |
} | |
XString toLowerCase() const{ | |
XString xs = *this; | |
unsigned int index = 0; | |
while (xs.src[index]){ | |
xs.src[index] = tolower(xs.src[index]); | |
index++; | |
} | |
return xs; | |
} | |
XString toUpperCase() const{ | |
XString xs = *this; | |
unsigned int index = 0; | |
while (xs.src[index]){ | |
xs.src[index] = toupper(xs.src[index]); | |
index++; | |
} | |
return xs; | |
} | |
void append(const XString xs){ | |
char *temp = nullptr; | |
unsigned int n = length(src) + xs.size(); | |
unsigned int x = 0; | |
unsigned int y = 0; | |
temp = new char[n]; | |
while (src[x]){ | |
temp[x] = src[x]; | |
x++; | |
} | |
while (y < length(xs.src)){ | |
temp[x] = xs.src[y]; | |
x++; | |
y++; | |
} | |
src = temp; | |
src[x] = '\0'; | |
} | |
char front() const{ | |
return src[0]; | |
} | |
char back() const{ | |
return src[length(src) - 1]; | |
} | |
iterator begin(){ | |
return &src[0]; | |
} | |
iterator end(){ | |
return &src[length(src)]; | |
} | |
char *substr(unsigned int start, unsigned int len){ | |
unsigned int n = len; | |
unsigned int y = 0; | |
char *temp = new char[n]; | |
for (unsigned x = start; x < (start + len); x++){ | |
temp[y] = this->src[x]; | |
y++; | |
} | |
temp[y] = '\0'; | |
return temp; | |
} | |
void push_back(char c){ | |
char *temp = nullptr; | |
unsigned int n = length(src) + 1; | |
unsigned int x = 0; | |
temp = new char[n]; | |
while (src[x]){ | |
temp[x] = src[x]; | |
x++; | |
} | |
temp[x] = c; | |
temp[x + 1] = '\0'; | |
src = temp; | |
} | |
void pop_back(void){ | |
unsigned int x = length(this->src) - 1; | |
this->src[x] = '\0'; | |
} | |
bool starts_with(const char *s) const{ | |
unsigned int x = 0; | |
unsigned int n = length(s); | |
char *tmp = nullptr; | |
tmp = new char[n + 1]; | |
while (x < n){ | |
tmp[x] = this->src[x]; | |
x++; | |
} | |
tmp[x] = '\0'; | |
return str_comp(tmp, s) == 0; | |
tmp = new char[0]; | |
} | |
bool equals(XString xs){ | |
return str_comp(this->src, xs.src) == 0; | |
} | |
bool ends_with(const char *s) const{ | |
unsigned int x = 0; | |
unsigned int n = length(s); | |
char *tmp = nullptr; | |
tmp = new char[n + 1]; | |
unsigned x_start = this->size() - n; | |
while (x_start < length(src)){ | |
tmp[x] = src[x_start]; | |
x++; | |
x_start++; | |
} | |
tmp[x] = '\0'; | |
return str_comp(tmp, s) == 0; | |
tmp = new char[0]; | |
} | |
void copy_to(char *out, unsigned int size){ | |
unsigned int x = 0; | |
while (x < size){ | |
out[x] = src[x]; | |
x++; | |
} | |
out[x] = '\0'; | |
} | |
int index_of_any(const char c) const{ | |
int x = 0; | |
int idx = -1; | |
while (this->src[x]){ | |
if (this->src[x] == c){ | |
idx = x; | |
break; | |
} | |
x++; | |
} | |
return idx; | |
} | |
int IndexOfAny(const char c[]) const{ | |
int n = sizeof(n) / sizeof(char) - 1; | |
int x = 0; | |
int idx = -1; | |
while (x < n){ | |
idx = index_of_any(c[x]); | |
if (idx != -1){ | |
break; | |
} | |
x++; | |
} | |
return idx; | |
} | |
void join(char seperator, const char *items[], int i){ | |
XString temp; | |
int x = 0; | |
while (x < i){ | |
temp.append(items[x]); | |
temp.append(seperator); | |
x++; | |
} | |
temp.pop_back(); | |
src = temp.src; | |
} | |
char* to_char_array(){ | |
char *s = new char[this->size()]; | |
s = this->src; | |
return s; | |
} | |
int find_last_of(const char *s) const{ | |
return this->find(s, true); | |
} | |
int find_first_of(const char *s) const{ | |
return this->find(s, false); | |
} | |
void clear(void){ | |
this->s_size = 0; | |
src = new char[0]; | |
src[0] = '\0'; | |
} | |
}; | |
int main(){ | |
XString hello("Hello World"); | |
XString a; | |
XString b; | |
char hi[3] = { 'H', 'I', '\0' }; | |
XString Test; | |
std::cout << "Contents of hello is : " << hello << std::endl; | |
std::cout << "Size of hello is : " << hello.size() << std::endl; | |
//Send hello to Text | |
Test = hello; | |
Test.append(" I like programming*"); | |
std::cout << "Added hello to Test and appened data : " << Test << std::endl; | |
//Remove last char from Test | |
Test.pop_back(); | |
std::cout << "Removed one char from test at the back using pop_back : " << Test << std::endl; | |
//Copy hi char to Test | |
Test = hi; | |
std::cout << Test.c_str() << std::endl; | |
a.push_back('A'); | |
b.push_back('Z'); | |
std::cout << "a == b = " << (a == b) << std::endl; | |
std::cout << "a < b = " << (a < b) << std::endl; | |
hello += " Welcome to XString Welcome"; | |
std::cout << "Using += operator : " << hello << std::endl; | |
std::cout << "hello begins with 'hello' ? " << hello.starts_with("hello") << std::endl; | |
std::cout << "hello begins with 'Hello' ? " << hello.starts_with("Hello") << std::endl; | |
std::cout << "hello ends with 'Welcome' ? " << hello.ends_with("Welcome") << std::endl; | |
//Find first welcome | |
std::cout << "Welcome was first found at pos : " << hello.find_first_of("Welcome") << std::endl; | |
std::cout << "Welcome was last found at pos : " << hello.find_last_of("Welcome") << std::endl; | |
std::cout << "More examples comming soon" << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment