StringBuffer.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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(const char* str) {
  57. for(int i = 0; length < N - 1 && str[i] != '\0'; length++, i++) {
  58. data[length] = str[i];
  59. addToHash(str[i]);
  60. }
  61. data[length] = '\0';
  62. return *this;
  63. }
  64. StringBuffer& append(char* str) {
  65. return append(static_cast<const char*>(str));
  66. }
  67. template<typename T>
  68. StringBuffer& append(const char* format, const T& t) {
  69. int left = N - length;
  70. int written = snprintf(data + length, left, format, t);
  71. int oldLength = length;
  72. if(written < left) {
  73. length += written;
  74. } else {
  75. length = N - 1;
  76. }
  77. addToHash(oldLength, length);
  78. return *this;
  79. }
  80. StringBuffer& append(int i) {
  81. return append("%d", i);
  82. }
  83. StringBuffer& append(float i) {
  84. return append("%.2f", i);
  85. }
  86. StringBuffer& append(bool b) {
  87. return b ? append("true") : append("false");
  88. }
  89. template<typename T>
  90. StringBuffer& append(const T& t) {
  91. t.toString(*this);
  92. return *this;
  93. }
  94. StringBuffer& clear() {
  95. length = 0;
  96. hash = 0;
  97. data[0] = '\0';
  98. return *this;
  99. }
  100. Hash hashCode() const {
  101. return hash;
  102. }
  103. void print() {
  104. std::cout << data;
  105. }
  106. void printLine() {
  107. std::cout << data << '\n';
  108. }
  109. };
  110. template<int N>
  111. bool operator==(const char* str, const StringBuffer<N> buffer) {
  112. return buffer == str;
  113. }
  114. template<int N>
  115. bool operator!=(const char* str, const StringBuffer<N> buffer) {
  116. return buffer != str;
  117. }
  118. #endif