2023-06-21 14:42:23 -07:00
|
|
|
#include "string/string.h"
|
|
|
|
|
|
|
|
namespace glcr {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
uint64_t cstrlen(const char* cstr) {
|
|
|
|
uint64_t len = 0;
|
|
|
|
while (*cstr != '\0') {
|
|
|
|
cstr++;
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2023-10-13 15:03:35 -07:00
|
|
|
String::String() : cstr_(nullptr), length_(0) {}
|
|
|
|
|
2023-07-06 10:40:36 -07:00
|
|
|
String::String(const char* str) : String(str, cstrlen(str)) {}
|
|
|
|
|
|
|
|
String::String(const char* cstr, uint64_t str_len) : length_(str_len) {
|
2023-06-21 14:42:23 -07:00
|
|
|
cstr_ = new char[length_ + 1];
|
2023-07-06 10:40:36 -07:00
|
|
|
for (uint64_t i = 0; i < length_; i++) {
|
|
|
|
cstr_[i] = cstr[i];
|
2023-06-21 14:42:23 -07:00
|
|
|
}
|
2023-07-06 10:40:36 -07:00
|
|
|
cstr_[length_] = '\0';
|
2023-06-21 14:42:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool String::operator==(const String& other) {
|
|
|
|
if (other.length_ != length_) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (uint64_t i = 0; i < length_; i++) {
|
|
|
|
if (cstr_[i] != other.cstr_[i]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-10-24 12:34:52 -07:00
|
|
|
char String::operator[](uint64_t offset) const {
|
|
|
|
// FIXME: bounds check.
|
|
|
|
return cstr_[offset];
|
|
|
|
}
|
|
|
|
|
2023-06-21 14:42:23 -07:00
|
|
|
} // namespace glcr
|