diff options
author | Benji Dial <benji@benjidial.net> | 2024-07-27 16:57:39 -0400 |
---|---|---|
committer | Benji Dial <benji@benjidial.net> | 2024-07-27 16:57:39 -0400 |
commit | fbfc078e9f44c1c1e95c9c484f1d5650bcf631b7 (patch) | |
tree | cab539c8cbbac81d895b6f8be695f3f53bf8f4d5 /euler/include/std/string.hpp | |
parent | 9af5588c30c4126a2800aae1afcb0de2c373dc6c (diff) | |
download | hilbert-os-fbfc078e9f44c1c1e95c9c484f1d5650bcf631b7.tar.gz |
lots and lots of userspace stuff
Diffstat (limited to 'euler/include/std/string.hpp')
-rw-r--r-- | euler/include/std/string.hpp | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/euler/include/std/string.hpp b/euler/include/std/string.hpp new file mode 100644 index 0000000..7ccdbc2 --- /dev/null +++ b/euler/include/std/string.hpp @@ -0,0 +1,80 @@ +#pragma once + +#include <cstddef> +#include <vector> + +namespace std { + + class string { + + std::vector<char> characters; + + public: + constexpr string() : characters({'\0'}) {} + + constexpr string(const string &other) + : characters(other.characters) {} + + constexpr string(string &&other) noexcept + : characters(std::move(other.characters)) { + other.characters.push_back('\0'); + } + + constexpr string(const char *s) { + size_t count = 0; + while (s[count] != '\0') + ++count; + characters.resize(count + 1); + for (size_t i = 0; i <= count; ++i) + characters[i] = s[i]; + } + + constexpr ~string() {} + + constexpr string &operator =(const string &str) { + characters = str.characters; + return *this; + } + + constexpr string &operator =(string &&str) noexcept { + characters = std::move(str.characters); + str.characters.push_back('\0'); + return *this; + } + + constexpr size_t size() const noexcept { + return characters.size() - 1; + } + + constexpr void resize(size_t count) { + if (count < characters.size() - 1) { + characters.resize(count + 1); + characters[count] = '\0'; + } + else if (count > characters.size() - 1) + characters.resize(count + 1); + } + + constexpr void clear() noexcept { + resize(0); + } + + constexpr const char *data() const noexcept { + return characters.data(); + } + + constexpr char *data() noexcept { + return characters.data(); + } + + constexpr char &operator[](size_t pos) { + return characters[pos]; + } + + constexpr const char &operator[](size_t pos) const { + return characters[pos]; + } + + }; + +} |