String.h 925 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #ifndef STRING_H
  2. #define STRING_H
  3. class String final {
  4. public:
  5. String();
  6. String(const char* str);
  7. bool operator==(const String& other) const;
  8. bool operator!=(const String& other) const;
  9. operator const char*() const;
  10. char operator[](int index) const;
  11. int getLength() const;
  12. String& clear();
  13. template<typename T>
  14. String& append(const char* format, const T& t) {
  15. int left = MAX_LENGTH - length;
  16. int written = snprintf(data + length, left, format, t);
  17. if(written < left) {
  18. length += written;
  19. } else {
  20. length = MAX_LENGTH;
  21. }
  22. return *this;
  23. }
  24. String& append(char c);
  25. String& append(const char* str);
  26. String& append(int i);
  27. String& append(float f);
  28. String& append(bool b);
  29. private:
  30. static constexpr int MAX_LENGTH = 256 - sizeof(int);
  31. int length;
  32. char data[MAX_LENGTH];
  33. };
  34. #endif