StringBuffer.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. for(int i = 0; length < N - 1 && str[i] != '\0'; length++, i++) {
  61. data[length] = str[i];
  62. addToHash(str[i]);
  63. }
  64. data[length] = '\0';
  65. return *this;
  66. }
  67. StringBuffer& append(char* str) {
  68. return append(static_cast<const char*>(str));
  69. }
  70. template<typename T>
  71. StringBuffer& append(const char* format, const T& t) {
  72. int left = N - length;
  73. int written = snprintf(data + length, left, format, t);
  74. int oldLength = length;
  75. if(written < left) {
  76. length += written;
  77. } else {
  78. length = N - 1;
  79. }
  80. addToHash(oldLength, length);
  81. return *this;
  82. }
  83. StringBuffer& append(int i) {
  84. return append("%d", i);
  85. }
  86. StringBuffer& append(unsigned int i) {
  87. return append("%u", i);
  88. }
  89. StringBuffer& append(float i) {
  90. return append("%.2f", i);
  91. }
  92. StringBuffer& append(bool b) {
  93. return b ? append("true") : append("false");
  94. }
  95. template<typename T>
  96. StringBuffer& append(const T& t) {
  97. t.toString(*this);
  98. return *this;
  99. }
  100. StringBuffer& appendUnicode(unsigned int c) {
  101. if(c < (1 << 7)) {
  102. append(static_cast<char>(c & 0x7F));
  103. } else if(c < (1 << 11)) {
  104. append(static_cast<char>(((c >> 6) & 0x1F) | 0xC0));
  105. append(static_cast<char>(((c >> 0) & 0x3F) | 0x80));
  106. } else if(c < (1 << 16)) {
  107. append(static_cast<char>(((c >> 12) & 0x0F) | 0xE0));
  108. append(static_cast<char>(((c >> 6) & 0x3F) | 0x80));
  109. append(static_cast<char>(((c >> 0) & 0x3F) | 0x80));
  110. } else if(c < (1 << 21)) {
  111. append(static_cast<char>(((c >> 18) & 0x07) | 0xF0));
  112. append(static_cast<char>(((c >> 12) & 0x3F) | 0x80));
  113. append(static_cast<char>(((c >> 6) & 0x3F) | 0x80));
  114. append(static_cast<char>(((c >> 0) & 0x3F) | 0x80));
  115. }
  116. return *this;
  117. }
  118. StringBuffer& clear() {
  119. length = 0;
  120. hash = 0;
  121. data[0] = '\0';
  122. return *this;
  123. }
  124. Hash hashCode() const {
  125. return hash;
  126. }
  127. void print() const {
  128. std::cout << data;
  129. }
  130. void printLine() const {
  131. std::cout << data << '\n';
  132. }
  133. };
  134. template<int N>
  135. bool operator==(const char* str, const StringBuffer<N> buffer) {
  136. return buffer == str;
  137. }
  138. template<int N>
  139. bool operator!=(const char* str, const StringBuffer<N> buffer) {
  140. return buffer != str;
  141. }
  142. #endif