String.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #ifndef STRING_H
  2. #define STRING_H
  3. #include "common/utils/Types.h"
  4. class String final {
  5. public:
  6. String();
  7. String(const char* str);
  8. String(const String& other);
  9. String& operator=(const String& other);
  10. bool operator==(const String& other) const;
  11. bool operator!=(const String& other) const;
  12. operator const char*() const;
  13. char operator[](uint index) const;
  14. uint getLength() const;
  15. template<typename T>
  16. String& append(const char* format, const T& t) {
  17. uint left = MAX_LENGTH - length;
  18. uint written = snprintf(data + length, left, format, t);
  19. if(written < left) {
  20. length += written;
  21. } else {
  22. length = MAX_LENGTH;
  23. }
  24. return *this;
  25. }
  26. String& append(const char* str);
  27. String& append(u8 u);
  28. String& append(u16 u);
  29. String& append(u32 u);
  30. String& append(u64 u);
  31. String& append(s8 s);
  32. String& append(s16 s);
  33. String& append(s32 s);
  34. String& append(s64 s);
  35. String& append(float f);
  36. String& append(bool b);
  37. String& clear();
  38. static constexpr uint MAX_LENGTH = 255;
  39. private:
  40. char data[MAX_LENGTH];
  41. u8 length;
  42. };
  43. #endif