StringBuffer.h 4.2 KB

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