12345678910111213141516171819202122232425262728293031323334353637383940 |
- #ifndef STRING_H
- #define STRING_H
- class String final {
- public:
- String();
- String(const char* str);
- bool operator==(const String& other) const;
- bool operator!=(const String& other) const;
- operator const char*() const;
- char operator[](int index) const;
- int getLength() const;
- String& clear();
- template<typename T>
- String& append(const char* format, const T& t) {
- int left = MAX_LENGTH - length;
- int written = snprintf(data + length, left, format, t);
- if(written < left) {
- length += written;
- } else {
- length = MAX_LENGTH;
- }
- return *this;
- }
- String& append(char c);
- String& append(const char* str);
- String& append(int i);
- String& append(float f);
- String& append(bool b);
- private:
- static constexpr int MAX_LENGTH = 256 - sizeof(int);
- int length;
- char data[MAX_LENGTH];
- };
- #endif
|