String.h 904 B

123456789101112131415161718192021222324252627282930313233343536373839
  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. template<typename T>
  13. String& append(const char* format, const T& t) {
  14. int left = MAX_LENGTH - length;
  15. int written = snprintf(data + length, left, format, t);
  16. if(written < left) {
  17. length += written;
  18. } else {
  19. length = MAX_LENGTH;
  20. }
  21. return *this;
  22. }
  23. String& append(char c);
  24. String& append(const char* str);
  25. String& append(int i);
  26. String& append(float f);
  27. String& append(bool b);
  28. private:
  29. static constexpr int MAX_LENGTH = 256 - sizeof(int);
  30. char data[MAX_LENGTH];
  31. int length;
  32. };
  33. #endif