Created
October 30, 2022 18:16
-
-
Save kinoko87/16e64155329aee32316d1ff37cd12d15 to your computer and use it in GitHub Desktop.
Simple string class in c++
This file contains hidden or 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
#include<stdio.h> | |
#include<stdlib.h> | |
#include<string.h> | |
#include<ctype.h> | |
class SimpleString { | |
public: | |
SimpleString(const char* text) { | |
raw_str = (char**)malloc((size_t)(sizeof(const char*) * strlen(text))); | |
strcpy(*raw_str, text); | |
} | |
SimpleString operator = (const char* text) { | |
*this = SimpleString(text); | |
return *this; | |
} | |
SimpleString operator += (const char* text) { | |
concat(text); | |
return *this; | |
} | |
char* unwrap() { | |
return *raw_str; | |
} | |
private: | |
inline void resize(const char* new_str) { | |
size_t size = sizeof(char*) * strlen(new_str); | |
raw_str = (char**)realloc(raw_str, size); | |
strcpy(*raw_str, new_str); | |
} | |
inline void concat(const char* new_str) { | |
resize((const char*)strcat(*raw_str, new_str)); | |
} | |
char** raw_str; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment