aboutsummaryrefslogtreecommitdiff
path: root/backend/String.cpp
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-10-30 19:59:38 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-10-30 19:59:38 +0100
commit6e1d62955c7a7f39bc9126d709a42a70e02a1d30 (patch)
treec2505409c68d554b1e776cdb0c8104af54d375bf /backend/String.cpp
parentc1d43cddee94dd370078f755d33147c9a8181852 (diff)
create backend string class
Diffstat (limited to 'backend/String.cpp')
-rw-r--r--backend/String.cpp65
1 files changed, 65 insertions, 0 deletions
diff --git a/backend/String.cpp b/backend/String.cpp
new file mode 100644
index 0000000..2b40c88
--- /dev/null
+++ b/backend/String.cpp
@@ -0,0 +1,65 @@
+#include <string.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "String.h"
+#include "util.h"
+
+String::String() {
+ this->set("");
+}
+
+String::String(const String & other) {
+ this->set(other.data(), other.size());
+}
+
+String::String(const char * c_str) {
+ if (c_str == nullptr) c_str = "";
+ this->set(c_str);
+}
+
+String String::fmt(const char * fmt, ...) {
+ String out;
+ va_list args, args_copy;
+ va_start(args, fmt);
+
+ va_copy(args_copy, args);
+ out._data_len = vsnprintf(NULL, 0, fmt, args_copy);
+ va_end(args_copy);
+
+ out._data = static_cast<char *>(malloc(out._data_len + 1));
+ vsnprintf(out._data, out._data_len + 1, fmt, args);
+ va_end(args);
+ return out;
+}
+
+String::~String() {
+ safe_free(this->_data);
+}
+
+void String::set(const char * data) {
+ this->set(data, strlen(data));
+}
+
+void String::set(const char * data, size_t data_len) {
+ safe_free(this->_data);
+ this->_data = static_cast<char *>(malloc(data_len + 1));
+ this->_data_len = data_len;
+ memcpy(this->_data, data, data_len);
+ this->_data[this->_data_len] = '\0';
+}
+
+const char * String::c_str() const {
+ return this->_data;
+}
+char * String::data() const {
+ return this->_data;
+}
+size_t String::size() const {
+ return this->_data_len;
+}
+bool String::empty() const {
+ return this->_data_len == 0;
+}
+