summaryrefslogtreecommitdiffhomepage
path: root/dev/CompilerKit
diff options
context:
space:
mode:
Diffstat (limited to 'dev/CompilerKit')
-rw-r--r--dev/CompilerKit/src/BasicString.cc181
1 files changed, 76 insertions, 105 deletions
diff --git a/dev/CompilerKit/src/BasicString.cc b/dev/CompilerKit/src/BasicString.cc
index b9aaa3f..6ca1e46 100644
--- a/dev/CompilerKit/src/BasicString.cc
+++ b/dev/CompilerKit/src/BasicString.cc
@@ -21,6 +21,7 @@
#include <CompilerKit/BasicString.h>
namespace CompilerKit {
+
Char* BasicString::Data() {
return m_Data;
}
@@ -34,43 +35,24 @@ SizeType BasicString::Length() const {
}
bool BasicString::operator==(const BasicString& rhs) const {
- if (rhs.Length() != Length()) return false;
-
- for (SizeType index = 0; index < Length(); ++index) {
- if (rhs.m_Data[index] != m_Data[index]) return false;
- }
-
- return true;
+ const SizeType len = Length();
+ if (rhs.Length() != len) return false;
+ return memcmp(m_Data, rhs.m_Data, len) == 0;
}
bool BasicString::operator==(const Char* rhs) const {
- if (string_length(rhs) != Length()) return false;
-
- for (SizeType index = 0; index < string_length(rhs); ++index) {
- if (rhs[index] != m_Data[index]) return false;
- }
-
- return true;
+ const SizeType rhs_len = string_length(rhs);
+ const SizeType len = Length();
+ if (rhs_len != len) return false;
+ return memcmp(m_Data, rhs, len) == 0;
}
bool BasicString::operator!=(const BasicString& rhs) const {
- if (rhs.Length() != Length()) return false;
-
- for (SizeType index = 0; index < rhs.Length(); ++index) {
- if (rhs.m_Data[index] == m_Data[index]) return false;
- }
-
- return true;
+ return !(*this == rhs);
}
bool BasicString::operator!=(const Char* rhs) const {
- if (string_length(rhs) != Length()) return false;
-
- for (SizeType index = 0; index < string_length(rhs); ++index) {
- if (rhs[index] == m_Data[index]) return false;
- }
-
- return true;
+ return !(*this == rhs);
}
BasicString StringBuilder::Construct(const Char* data) {
@@ -82,126 +64,115 @@ BasicString StringBuilder::Construct(const Char* data) {
return view;
}
-const char* StringBuilder::FromInt(const char* fmt, int i) {
- if (!fmt) return ("-1");
-
- auto ret_len = 8 + string_length(fmt);
- char* ret = new char[ret_len];
+BasicString StringBuilder::FromInt(const char* fmt, int i) {
+ if (!fmt) return BasicString(0);
- if (!ret) return ("-1");
+ Char result[sizeof(int64_t)] = {0};
+ if (!to_str(result, sizeof(int64_t), i)) return BasicString(0);
- memset(ret, 0, ret_len);
+ const SizeType fmt_len = string_length(fmt);
+ const SizeType res_len = string_length(result);
- Char result[sizeof(int64_t)];
-
- if (!to_str(result, sizeof(int64_t), i)) {
- delete[] ret;
- return ("-1");
- }
-
- const auto fmt_len = string_length(fmt);
- const auto res_len = string_length(result);
+ BasicString output(fmt_len + res_len);
+ bool inserted = false;
for (SizeType idx = 0; idx < fmt_len; ++idx) {
- if (fmt[idx] == '%') {
- SizeType result_cnt = idx;
-
- for (auto y_idx = 0; y_idx < res_len; ++y_idx) {
- ret[y_idx] = result[result_cnt];
- ++result_cnt;
- }
-
- break;
+ if (!inserted && fmt[idx] == '%') {
+ output += result;
+ inserted = true;
+ continue;
}
-
- ret[idx] = fmt[idx];
+ output += Char{fmt[idx]};
}
- return ret; /* Copy that ret into a buffer, Alloca allocates to the stack */
+ return output;
}
-const char* StringBuilder::FromBool(const char* fmt, bool i) {
- if (!fmt) return ("?");
-
- const char* boolean_expr = i ? "true" : "false";
- char* ret = new char[i ? 4 : 5 + string_length(fmt)];
+BasicString StringBuilder::FromBool(const char* fmt, bool val) {
+ if (!fmt) return BasicString(0);
- if (!ret) return ("?");
+ const Char* boolean_expr = val ? "true" : "false";
+ const SizeType fmt_len = string_length(fmt);
+ const SizeType res_len = string_length(boolean_expr);
- const auto fmt_len = string_length(fmt);
- const auto res_len = string_length(boolean_expr);
+ BasicString output(fmt_len + res_len);
+ bool inserted = false;
for (SizeType idx = 0; idx < fmt_len; ++idx) {
- if (fmt[idx] == '%') {
- SizeType result_cnt = idx;
-
- for (auto y_idx = idx; y_idx < res_len; ++y_idx) {
- ret[result_cnt] = boolean_expr[y_idx];
- ++result_cnt;
- }
-
- break;
+ if (!inserted && fmt[idx] == '%') {
+ output += boolean_expr;
+ inserted = true;
+ continue;
}
-
- ret[idx] = fmt[idx];
+ output += Char{fmt[idx]};
}
- return ret;
+ return output;
}
bool StringBuilder::Equals(const char* lhs, const char* rhs) {
- if (string_length(rhs) != string_length(lhs)) return false;
-
- for (SizeType index = 0; index < string_length(rhs); ++index) {
- if (rhs[index] != lhs[index]) return false;
- }
+ const SizeType lhs_len = string_length(lhs);
+ const SizeType rhs_len = string_length(rhs);
- return true;
+ if (lhs_len != rhs_len) return false;
+ return memcmp(lhs, rhs, lhs_len) == 0;
}
-const char* StringBuilder::Format(const char* fmt, const char* fmtRight) {
- if (!fmt || !fmtRight) return ("?");
-
- char* ret = new char[string_length(fmtRight) + string_length(fmtRight)];
- if (!ret) return ("?");
+BasicString StringBuilder::Format(const char* fmt, const char* fmtRight) {
+ if (!fmt || !fmtRight) return BasicString(0);
- for (SizeType idx = 0; idx < string_length(fmt); ++idx) {
- if (fmt[idx] == '%') {
- SizeType result_cnt = idx;
+ const SizeType fmt_len = string_length(fmt);
+ const SizeType rhs_len = string_length(fmtRight);
- for (SizeType y_idx = 0; y_idx < string_length(fmtRight); ++y_idx) {
- ret[result_cnt] = fmtRight[y_idx];
- ++result_cnt;
- }
+ BasicString output(fmt_len + rhs_len);
+ bool inserted = false;
- break;
+ for (SizeType idx = 0; idx < fmt_len; ++idx) {
+ if (!inserted && fmt[idx] == '%') {
+ output += fmtRight;
+ inserted = true;
+ continue;
}
-
- ret[idx] = fmt[idx];
+ output += Char{fmt[idx]};
}
- return ret;
+ return output;
}
BasicString& BasicString::operator+=(const Char* rhs) {
- if (strlen(rhs) > this->m_Sz) {
+ const SizeType rhs_len = strlen(rhs);
+ if (this->m_Cur + rhs_len >= this->m_Sz) {
throw std::runtime_error("out_of_bounds: BasicString");
}
- memcpy(this->m_Data + this->m_Cur, rhs, strlen(rhs));
- this->m_Cur += strlen(rhs);
+ memcpy(this->m_Data + this->m_Cur, rhs, rhs_len);
+ this->m_Cur += rhs_len;
+ this->m_Data[this->m_Cur] = '\0';
return *this;
}
BasicString& BasicString::operator+=(const BasicString& rhs) {
- if (rhs.m_Cur > this->m_Sz) {
+ if (this->m_Cur + rhs.m_Cur >= this->m_Sz) {
throw std::runtime_error("out_of_bounds: BasicString");
}
- memcpy(this->m_Data + this->m_Cur, rhs.CData(), strlen(rhs.CData()));
- this->m_Cur += strlen(rhs.CData());
+ memcpy(this->m_Data + this->m_Cur, rhs.CData(), rhs.m_Cur);
+ this->m_Cur += rhs.m_Cur;
+ this->m_Data[this->m_Cur] = '\0';
return *this;
}
-} // namespace CompilerKit
+
+BasicString& BasicString::operator+=(Char ch) {
+ if (this->m_Cur + 1 >= this->m_Sz) {
+ //
+ }
+
+ this->m_Data[this->m_Cur++] = ch;
+ this->m_Data[this->m_Cur] = '\0';
+
+ return *this;
+}
+
+} // namespace CompilerKit