123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- #include <cstring>
- #include <cstdio>
- #include "common/utils/String.h"
- String::String() : length(0) {
- data[0] = '\0';
- }
- String::String(const char* str) : length(0) {
- for(; length < MAX_LENGTH - 1 && str[length] != '\0'; length++) {
- data[length] = str[length];
- }
- data[length] = '\0';
- }
- String::String(const String& other) : length(other.length) {
- memcpy(data, other.data, length + 1);
- }
- String& String::operator=(const String& other) {
- if(this != &other) {
- length = other.length;
- memcpy(data, other.data, length + 1);
- }
- return *this;
- }
- bool String::operator==(const String& other) const {
- return length == other.length && strcmp(data, other.data) == 0;
- }
- bool String::operator!=(const String& other) const {
- return !(*this == other);
- }
- String::operator const char*() const {
- return data;
- }
- char String::operator[](uint index) const {
- return data[index];
- }
- uint String::getLength() const {
- return length;
- }
- String& String::append(const char* str) {
- for(uint i = 0; length < MAX_LENGTH - 1 && str[i] != '\0'; length++, i++) {
- data[length] = str[i];
- }
- data[length] = '\0';
- return *this;
- }
- String& String::append(u8 u) {
- return append(static_cast<s8>(u));
- }
- String& String::append(u16 u) {
- return append("%hu", u);
- }
- String& String::append(u32 u) {
- return append("%u", u);
- }
- String& String::append(u64 u) {
- return append("%lu", u);
- }
- String& String::append(s8 c) {
- if(length < MAX_LENGTH - 1) {
- data[length++] = c;
- data[length] = '\0';
- }
- return *this;
- }
- String& String::append(s16 s) {
- return append("%hi", s);
- }
- String& String::append(s32 s) {
- return append("%i", s);
- }
- String& String::append(s64 s) {
- return append("%li", s);
- }
- String& String::append(float f) {
- return append("%.2f", f);
- }
- String& String::append(bool b) {
- return append(b ? "true" : "false");
- }
- String& String::clear() {
- data[0] = '\0';
- length = 0;
- return *this;
- }
|