Last active
March 25, 2023 08:49
-
-
Save DuCanhGH/8bcddb455f24c2af381653c27fc4458f to your computer and use it in GitHub Desktop.
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
// basically I went insane | |
// for emergency reasons (thanks orac) | |
// implemented as if using C because what sort of C++ doesn't support std::string | |
#ifndef __STRING_HPP | |
#define __STRING_HPP | |
#include <cstdlib> | |
#include <cstring> | |
#include <limits> | |
#include <istream> | |
typedef unsigned short u16; | |
typedef short i16; | |
typedef unsigned int u32; | |
typedef int i32; | |
typedef float f32; | |
#define U32_MAX std::numeric_limits<u32>::max() | |
class String | |
{ | |
protected: | |
char *_str; | |
u32 _size; | |
void _reset() | |
{ | |
if (_str != nullptr) | |
{ | |
free(_str); | |
} | |
_size = 0; | |
_str = (char *)calloc(0, sizeof(char)); | |
} | |
void _resize(const u32 &new_size) | |
{ | |
_size = new_size; | |
_str = (char *)realloc(_str, _size); | |
} | |
void _add_to_string(const char &new_char) | |
{ | |
_resize(_size + 1); | |
_str[_size - 1] = new_char; | |
} | |
void _append_str(const char *str, const u32 &len) | |
{ | |
u32 start = _size; | |
_resize(_size + len); | |
for (u32 i = 0; i < len; i++) | |
{ | |
_str[start + i] = str[i]; | |
} | |
} | |
public: | |
String() | |
{ | |
_size = 0; | |
_str = (char *)calloc(0, sizeof(char)); | |
} | |
String(const char *provided_str) | |
{ | |
_size = (u32)strlen(provided_str); | |
_str = (char *)calloc(_size, sizeof(char)); | |
strncpy(_str, provided_str, _size); | |
} | |
String(const String &str) | |
{ | |
_size = str.length(); | |
_str = (char *)calloc(_size, sizeof(char)); | |
strncpy(_str, str._str, _size); | |
} | |
~String() | |
{ | |
free(_str); | |
} | |
const u32 &length() const | |
{ | |
return _size; | |
} | |
const char &operator[](const u32 &ind) const | |
{ | |
return _str[ind]; | |
} | |
char &operator[](const u32 &ind) | |
{ | |
return _str[ind]; | |
} | |
friend std::ostream &operator<<(std::ostream &stream, const String &str) | |
{ | |
for (u32 i = 0; i < str._size; i++) | |
{ | |
stream << str[i]; | |
} | |
return stream; | |
} | |
friend std::istream &operator>>(std::istream &stream, String &str) | |
{ | |
std::istream::sentry cerb(stream, false); | |
u32 extracted = 0; | |
if (cerb) | |
{ | |
str._reset(); | |
const std::streamsize w = stream.width(); | |
u32 len = 0; | |
const u32 n = w > 0 ? (u32)(w) : U32_MAX; | |
char buf[128]; | |
i32 c = stream.rdbuf()->sgetc(); | |
while (extracted < n && c != std::istream::traits_type::eof() && c != '\n' && !isspace(c)) | |
{ | |
if (len == sizeof(buf) / sizeof(char)) | |
{ | |
str._append_str(buf, len); | |
len = 0; | |
} | |
buf[len++] = (char)(c); | |
++extracted; | |
c = stream.rdbuf()->snextc(); | |
} | |
str._append_str(buf, len); | |
} | |
return stream; | |
} | |
bool operator==(const String &str) | |
{ | |
return strcmp(_str, str._str); | |
} | |
String &operator+=(const String &str) | |
{ | |
_append_str(str._str, str._size); | |
return *this; | |
} | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
code như thế thì buồn lắm