ToString.cppm 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. export module Core.ToString;
  2. import Core.Math;
  3. import Core.Meta;
  4. import Core.Types;
  5. export import Core.StringFormat;
  6. export namespace Core {
  7. class StringBase;
  8. template<typename T>
  9. concept ToString = requires(const T& t, StringBase& b) { t.toString(b); };
  10. class StringBase {
  11. public:
  12. size_t index = 0;
  13. size_t capacity = 0;
  14. char* buffer = nullptr;
  15. public:
  16. StringBase() {
  17. }
  18. StringBase(char* s, size_t n) : capacity(n), buffer(s) {
  19. }
  20. StringBase(StringBase&& other) = default;
  21. StringBase(const StringBase& other) = delete;
  22. StringBase& operator=(StringBase&& other) = default;
  23. StringBase& operator=(const StringBase& other) = delete;
  24. operator const char*() const {
  25. return buffer;
  26. }
  27. void clear() {
  28. index = 0;
  29. if(buffer != nullptr) {
  30. *buffer = '\0';
  31. }
  32. }
  33. size_t getCapacity() const {
  34. return index >= capacity ? 0 : capacity - index;
  35. }
  36. size_t getTotal() const {
  37. return index;
  38. }
  39. char* getCurrent() const {
  40. return buffer + min(index, capacity);
  41. }
  42. template<typename... Args>
  43. size_t addFormat(const char* format, Args&&... args) {
  44. size_t oldIndex = index;
  45. (
  46. [&] {
  47. StringFormat f;
  48. copyFormatUntil(format, f);
  49. switcher(args, f);
  50. }(),
  51. ...);
  52. while(*format != '\0') {
  53. StringFormat f;
  54. copyFormatUntil(format, f);
  55. }
  56. return index - oldIndex;
  57. }
  58. template<typename... Args>
  59. size_t format(const char* format, Args&&... args) {
  60. clear();
  61. return addFormat<Args...>(format, Core::forward<Args>(args)...);
  62. }
  63. template<typename T>
  64. size_t add(const T& t) {
  65. size_t oldIndex = index;
  66. switcher(t);
  67. return index - oldIndex;
  68. }
  69. void print() const;
  70. private:
  71. void copyFormatUntil(const char*& format, StringFormat& f);
  72. #define TO_STRING(type) void toString(type t, const StringFormat& format)
  73. TO_STRING(signed char);
  74. TO_STRING(char);
  75. TO_STRING(short);
  76. TO_STRING(int);
  77. TO_STRING(long);
  78. TO_STRING(long long);
  79. TO_STRING(unsigned char);
  80. TO_STRING(unsigned short);
  81. TO_STRING(unsigned int);
  82. TO_STRING(unsigned long);
  83. TO_STRING(unsigned long long);
  84. TO_STRING(float);
  85. TO_STRING(double);
  86. TO_STRING(const char*);
  87. TO_STRING(const unsigned char*);
  88. TO_STRING(unsigned char*);
  89. TO_STRING(bool);
  90. template<typename T>
  91. void switcher(const T& t, const StringFormat& format = {}) {
  92. size_t oldIndex = index;
  93. if constexpr(Core::Iterable<T>) {
  94. switcher("[");
  95. auto current = t.begin();
  96. auto end = t.end();
  97. if(current != end) {
  98. switcher(*current);
  99. ++current;
  100. }
  101. while(current != end) {
  102. switcher(", ");
  103. switcher(*current);
  104. ++current;
  105. }
  106. switcher("]");
  107. } else if constexpr(ToString<T>) {
  108. t.toString(*this);
  109. } else {
  110. toString(t, format);
  111. }
  112. applyPostFormat(oldIndex, index - oldIndex, format);
  113. }
  114. void applyPostFormat(
  115. size_t startIndex, size_t length, const StringFormat& format);
  116. void insertSpace(size_t startIndex);
  117. };
  118. template<size_t N>
  119. struct String : public StringBase {
  120. char data[N]{};
  121. String() : StringBase(data, N) {
  122. }
  123. };
  124. template<typename... Args>
  125. void print(const char* format, Args&&... args) {
  126. String<256> s;
  127. s.addFormat(format, Core::forward<Args>(args)...);
  128. s.print();
  129. }
  130. }