#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';
}

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(char c) {
    if(length < MAX_LENGTH - 1) {
        data[length++] = c;
        data[length] = '\0';
    }
    return *this;
}

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(uint i) {
    return append("%u", i);
}

String& String::append(int i) {
    return append("%d", i);
}

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;
}