String.h 948 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #ifndef STRING_H
  2. #define STRING_H
  3. #include "utils/Types.h"
  4. class String final {
  5. public:
  6. String();
  7. String(const char* str);
  8. bool operator==(const String& other) const;
  9. bool operator!=(const String& other) const;
  10. operator const char*() const;
  11. char operator[](uint index) const;
  12. uint getLength() const;
  13. template<typename T>
  14. String& append(const char* format, const T& t) {
  15. uint left = MAX_LENGTH - length;
  16. uint 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(uint i);
  27. String& append(int i);
  28. String& append(float f);
  29. String& append(bool b);
  30. private:
  31. static constexpr uint MAX_LENGTH = 255;
  32. char data[MAX_LENGTH];
  33. u8 length;
  34. };
  35. #endif