Skip to content

Instantly share code, notes, and snippets.

@kinoko87
Created October 30, 2022 18:16
Show Gist options
  • Save kinoko87/16e64155329aee32316d1ff37cd12d15 to your computer and use it in GitHub Desktop.
Save kinoko87/16e64155329aee32316d1ff37cd12d15 to your computer and use it in GitHub Desktop.
Simple string class in c++
#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