12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- #include <cstring>
- #include <cstdio>
- #include "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(int i) {
- uint left = MAX_LENGTH - length;
- uint written = snprintf(data + length, left, "%d", i);
- if(written < left) {
- length += written;
- } else {
- length = MAX_LENGTH;
- }
- return *this;
- }
- String& String::append(float f) {
- uint left = MAX_LENGTH - length;
- uint written = snprintf(data + length, left, "%.2f", f);
- if(written < left) {
- length += written;
- } else {
- length = MAX_LENGTH;
- }
- return *this;
- }
|