12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- #include <cstdio>
- #include <cstring>
- #include "utils/String.h"
- String::String() : String("") {
- }
- String::String(const char* str) : usedCapacity(1) {
- *this += str;
- }
- String& String::operator+=(const char* str) {
- usedCapacity--;
- size_t start = usedCapacity;
- while(usedCapacity + 1 < capacity && str[usedCapacity - start] != '\0') {
- data[usedCapacity] = str[usedCapacity - start];
- usedCapacity++;
- }
- data[usedCapacity++] = '\0';
- return *this;
- }
- String& String::operator+=(char c) {
- if(usedCapacity + 1 < capacity) {
- data[usedCapacity - 1] = c;
- data[usedCapacity] = '\0';
- usedCapacity++;
- }
- return *this;
- }
- String& String::operator+=(char32_t c) {
- if(c <= 0x7F) {
- *this += (char) c;
- } else if(c <= 0x7FF) {
- *this += (char) (0xC0 | ((c >> 6) & 0x1F));
- *this += (char) (0x80 | ((c >> 0) & 0x3F));
- } else if(c <= 0xFFFF) {
- *this += (char) (0xE0 | ((c >> 12) & 0xF));
- *this += (char) (0x80 | ((c >> 6) & 0x3F));
- *this += (char) (0x80 | ((c >> 0) & 0x3F));
- } else {
- *this += (char) (0xF0 | ((c >> 18) & 0x7));
- *this += (char) (0x80 | ((c >> 12) & 0x3F));
- *this += (char) (0x80 | ((c >> 6) & 0x3F));
- *this += (char) (0x80 | ((c >> 0) & 0x3F));
- }
- return *this;
- }
- String& String::operator+=(unsigned int i) {
- usedCapacity += snprintf(data + usedCapacity - 1, capacity - usedCapacity, "%u", i);
- return *this;
- }
- String& String::operator+=(double d) {
- usedCapacity += snprintf(data + usedCapacity - 1, capacity - usedCapacity, (d == (long) d) ? "%lg.0" : "%lg", d);
- return *this;
- }
- String String::operator+(const char* str) const {
- String s(this->data);
- s += str;
- return s;
- }
- String::operator const char*() const {
- return data;
- }
- size_t String::getLength() const {
- return usedCapacity - 1;
- }
- bool String::isFull() const {
- return usedCapacity >= capacity;
- }
- bool String::operator==(const String& other) const {
- return usedCapacity == other.usedCapacity && *this == other.data;
- }
- bool String::operator!=(const String& other) const {
- return !(*this == other);
- }
- bool String::operator==(const char* other) const {
- return strcmp(data, other) == 0;
- }
- bool String::operator!=(const char* other) const {
- return !(*this == other);
- }
|