String.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #ifndef CORE_STRING_H
  2. #define CORE_STRING_H
  3. #include "utils/Check.h"
  4. #include "utils/Types.h"
  5. #include "utils/Utility.h"
  6. namespace Core {
  7. class String final {
  8. int length;
  9. Hash hash;
  10. static constexpr int DATA_LENGTH =
  11. (1024 - (sizeof(length) >= sizeof(hash) ? sizeof(length) * 2
  12. : sizeof(hash) * 2)) /
  13. sizeof(u32);
  14. u32 data[DATA_LENGTH];
  15. public:
  16. String();
  17. template<typename T>
  18. String(const T& t) : String() {
  19. append(t);
  20. }
  21. bool operator==(const char* s) const;
  22. bool operator==(const String& other) const;
  23. bool operator!=(const char* s) const;
  24. bool operator!=(const String& other) const;
  25. u32 operator[](int index) const;
  26. int getLength() const;
  27. String& append(char c);
  28. String& append(signed char c);
  29. String& append(unsigned char c);
  30. String& append(const char* s);
  31. String& append(const signed char* s);
  32. String& append(const unsigned char* s);
  33. String& append(signed short s);
  34. String& append(unsigned short s);
  35. String& append(signed int i);
  36. String& append(unsigned int i);
  37. String& append(signed long l);
  38. String& append(unsigned long l);
  39. String& append(signed long long ll);
  40. String& append(unsigned long long ll);
  41. String& append(float f);
  42. String& append(double d);
  43. String& append(long double ld);
  44. String& append(bool b);
  45. String& appendUnicode(u32 c);
  46. check_format(2, 3) String& appendFormat(const char* format, ...);
  47. template<typename T>
  48. String& append(const T& t) {
  49. t.toString(*this);
  50. return *this;
  51. }
  52. void toString(String& s) const;
  53. String& clear();
  54. Hash hashCode() const;
  55. void print() const;
  56. void printLine() const;
  57. template<typename... Args>
  58. static String format(const String& format, Args&&... args) {
  59. String s;
  60. formatBuffer(s, 0, format, Core::forward<Args>(args)...);
  61. return s;
  62. }
  63. private:
  64. void addToHash(u32 u);
  65. template<typename T, typename... Args>
  66. static void formatBuffer(String& s, int index, const String& format,
  67. const T& t, Args&&... args) {
  68. while(index < format.getLength()) {
  69. u32 u = format[index++];
  70. if(u == '#') {
  71. if(index < format.getLength() && format[index] != '#') {
  72. break;
  73. }
  74. index++;
  75. }
  76. s.appendUnicode(u);
  77. }
  78. s.append(t);
  79. formatBuffer(s, index, format, Core::forward<Args>(args)...);
  80. }
  81. static void formatBuffer(String& s, int index, const String& format) {
  82. while(index < format.getLength()) {
  83. s.appendUnicode(format[index++]);
  84. }
  85. }
  86. };
  87. }
  88. static_assert(sizeof(Core::String) == 1024,
  89. "String should have a size of 1024 byte");
  90. bool operator==(const char* cs, const Core::String& s);
  91. bool operator!=(const char* cs, const Core::String& s);
  92. #endif