StringBuffer.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #ifndef STRINGBUFFER_H
  2. #define STRINGBUFFER_H
  3. #include <cstdarg>
  4. #include <cstring>
  5. #include <iostream>
  6. #include "utils/Check.h"
  7. #include "utils/Types.h"
  8. // ignore buffer overflow warnings with snprintf
  9. #pragma GCC diagnostic push
  10. #pragma GCC diagnostic ignored "-Wformat-truncation"
  11. template<int N>
  12. class StringBuffer final {
  13. int length;
  14. Hash hash;
  15. static_assert(N > 0, "StringBuffer must have a positive size");
  16. char data[static_cast<unsigned int>(N)];
  17. void addToHash(char c) {
  18. hash = static_cast<Hash>(2120251889) * hash + static_cast<Hash>(c);
  19. }
  20. void addToHash(int from, int to) {
  21. for(int i = from; i < to; i++) {
  22. addToHash(data[i]);
  23. }
  24. }
  25. public:
  26. StringBuffer() : length(0), hash(0) {
  27. data[0] = '\0';
  28. }
  29. template<typename T>
  30. StringBuffer(const T& t) : StringBuffer() {
  31. append(t);
  32. }
  33. bool operator==(const char* str) const {
  34. return strcmp(data, str) == 0;
  35. }
  36. bool operator==(const StringBuffer& other) const {
  37. return length == other.length && other == data;
  38. }
  39. bool operator!=(const char* str) const {
  40. return !((*this) == str);
  41. }
  42. bool operator!=(const StringBuffer& other) const {
  43. return !((*this) == other);
  44. }
  45. operator const char*() const {
  46. return data;
  47. }
  48. char operator[](int index) const {
  49. return data[index];
  50. }
  51. int getLength() const {
  52. return length;
  53. }
  54. StringBuffer& append(char c) {
  55. if(length < N - 1) {
  56. data[length++] = c;
  57. addToHash(c);
  58. data[length] = '\0';
  59. }
  60. return *this;
  61. }
  62. StringBuffer& append(signed char c) {
  63. return append(static_cast<char>(c));
  64. }
  65. StringBuffer& append(unsigned char c) {
  66. return append(static_cast<char>(c));
  67. }
  68. StringBuffer& append(const char* str) {
  69. return appendString<const char*>(str);
  70. }
  71. StringBuffer& append(const signed char* str) {
  72. return appendString<const char*>(str);
  73. }
  74. StringBuffer& append(const unsigned char* str) {
  75. return appendString<const unsigned char*>(str);
  76. }
  77. check_format(2, 3) StringBuffer& appendFormat(const char* format, ...) {
  78. int left = N - length;
  79. va_list args;
  80. va_start(args, format);
  81. int written = vsnprintf(data + length, static_cast<unsigned int>(left),
  82. format, args);
  83. va_end(args);
  84. int oldLength = length;
  85. if(written < left) {
  86. length += written;
  87. } else {
  88. length = N - 1;
  89. }
  90. addToHash(oldLength, length);
  91. return *this;
  92. }
  93. StringBuffer& append(signed short s) {
  94. return appendFormat("%hd", s);
  95. }
  96. StringBuffer& append(unsigned short s) {
  97. return appendFormat("%hu", s);
  98. }
  99. StringBuffer& append(signed int i) {
  100. return appendFormat("%d", i);
  101. }
  102. StringBuffer& append(unsigned int i) {
  103. return appendFormat("%u", i);
  104. }
  105. StringBuffer& append(float f) {
  106. return appendFormat("%.2f", static_cast<double>(f));
  107. }
  108. StringBuffer& append(bool b) {
  109. return b ? append("true") : append("false");
  110. }
  111. template<typename T>
  112. StringBuffer& append(const T& t) {
  113. t.toString(*this);
  114. return *this;
  115. }
  116. template<int L>
  117. void toString(StringBuffer<L>& s) const {
  118. if(reinterpret_cast<const void*>(this) ==
  119. reinterpret_cast<const void*>(&s)) {
  120. return;
  121. }
  122. s.append(data);
  123. }
  124. StringBuffer& appendUnicode(unsigned int c) {
  125. if(c < (1 << 7)) {
  126. append(static_cast<char>(c & 0x7F));
  127. } else if(c < (1 << 11)) {
  128. append(static_cast<char>(((c >> 6) & 0x1F) | 0xC0));
  129. append(static_cast<char>(((c >> 0) & 0x3F) | 0x80));
  130. } else if(c < (1 << 16)) {
  131. append(static_cast<char>(((c >> 12) & 0x0F) | 0xE0));
  132. append(static_cast<char>(((c >> 6) & 0x3F) | 0x80));
  133. append(static_cast<char>(((c >> 0) & 0x3F) | 0x80));
  134. } else if(c < (1 << 21)) {
  135. append(static_cast<char>(((c >> 18) & 0x07) | 0xF0));
  136. append(static_cast<char>(((c >> 12) & 0x3F) | 0x80));
  137. append(static_cast<char>(((c >> 6) & 0x3F) | 0x80));
  138. append(static_cast<char>(((c >> 0) & 0x3F) | 0x80));
  139. }
  140. return *this;
  141. }
  142. StringBuffer& clear() {
  143. length = 0;
  144. hash = 0;
  145. data[0] = '\0';
  146. return *this;
  147. }
  148. Hash hashCode() const {
  149. return hash;
  150. }
  151. void print() const {
  152. std::cout << data;
  153. }
  154. void printLine() const {
  155. std::cout << data << '\n';
  156. }
  157. private:
  158. template<typename T>
  159. StringBuffer& appendString(T str) {
  160. for(int i = 0; length < N - 1 && str[i] != '\0'; length++, i++) {
  161. data[length] = static_cast<char>(str[i]);
  162. addToHash(static_cast<char>(str[i]));
  163. }
  164. data[length] = '\0';
  165. return *this;
  166. }
  167. };
  168. template<int N>
  169. bool operator==(const char* str, const StringBuffer<N> buffer) {
  170. return buffer == str;
  171. }
  172. template<int N>
  173. bool operator!=(const char* str, const StringBuffer<N> buffer) {
  174. return buffer != str;
  175. }
  176. #pragma GCC diagnostic pop
  177. #endif