String.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #include "utils/String.h"
  2. #include <stdarg.h>
  3. #include <stdio.h>
  4. Core::String::String() : length(0), hash(0) {
  5. data[0] = '\0';
  6. }
  7. bool Core::String::operator==(const char* s) const {
  8. return *this == String(s);
  9. }
  10. bool Core::String::operator==(const String& other) const {
  11. if(length != other.length) {
  12. return false;
  13. }
  14. for(int i = 0; i < length; i++) {
  15. if(data[i] != other[i]) {
  16. return false;
  17. }
  18. }
  19. return true;
  20. }
  21. bool Core::String::operator!=(const char* s) const {
  22. return !((*this) == s);
  23. }
  24. bool Core::String::operator!=(const String& other) const {
  25. return !((*this) == other);
  26. }
  27. u32 Core::String::operator[](int index) const {
  28. return data[index];
  29. }
  30. int Core::String::getLength() const {
  31. return length;
  32. }
  33. Core::String& Core::String::append(char c) {
  34. return appendUnicode(static_cast<u32>(c < 0 ? '?' : c));
  35. }
  36. Core::String& Core::String::append(signed char c) {
  37. return appendUnicode(static_cast<u32>(c < 0 ? '?' : c));
  38. }
  39. Core::String& Core::String::append(unsigned char c) {
  40. return appendUnicode(c);
  41. }
  42. static u32 read(const char*& s) {
  43. if(*s == '\0') {
  44. return 0;
  45. }
  46. return static_cast<u32>(*(s++));
  47. }
  48. Core::String& Core::String::append(const char* s) {
  49. while(true) {
  50. u32 u = read(s);
  51. if(u == 0) {
  52. break;
  53. } else if((u & 0xE0) == 0xC0) {
  54. u = ((u & 0x1F) << 6) | (read(s) & 0x3F);
  55. } else if((u & 0xF0) == 0xE0) {
  56. u = ((u & 0xF) << 12) | ((read(s) & 0x3F) << 6);
  57. u |= read(s) & 0x3F;
  58. } else if((u & 0xF8) == 0xF0) {
  59. u = ((u & 0x07) << 18) | ((read(s) & 0x3F) << 12);
  60. u |= (read(s) & 0x3F) << 6;
  61. u |= read(s) & 0x3F;
  62. }
  63. appendUnicode(u);
  64. }
  65. return *this;
  66. }
  67. Core::String& Core::String::append(const signed char* s) {
  68. return append(reinterpret_cast<const char*>(s));
  69. }
  70. Core::String& Core::String::append(const unsigned char* s) {
  71. return append(reinterpret_cast<const char*>(s));
  72. }
  73. Core::String& Core::String::append(signed short s) {
  74. return appendFormat("%hd", s);
  75. }
  76. Core::String& Core::String::append(unsigned short s) {
  77. return appendFormat("%hu", s);
  78. }
  79. Core::String& Core::String::append(signed int i) {
  80. return appendFormat("%d", i);
  81. }
  82. Core::String& Core::String::append(unsigned int i) {
  83. return appendFormat("%u", i);
  84. }
  85. Core::String& Core::String::append(signed long l) {
  86. return appendFormat("%ld", l);
  87. }
  88. Core::String& Core::String::append(unsigned long l) {
  89. return appendFormat("%lu", l);
  90. }
  91. Core::String& Core::String::append(signed long long ll) {
  92. return appendFormat("%lld", ll);
  93. }
  94. Core::String& Core::String::append(unsigned long long ll) {
  95. return appendFormat("%llu", ll);
  96. }
  97. Core::String& Core::String::append(float f) {
  98. return appendFormat("%.2f", static_cast<double>(f));
  99. }
  100. Core::String& Core::String::append(double d) {
  101. return appendFormat("%.2f", d);
  102. }
  103. Core::String& Core::String::append(long double ld) {
  104. return appendFormat("%.2Lf", ld);
  105. }
  106. Core::String& Core::String::append(bool b) {
  107. return b ? append("true") : append("false");
  108. }
  109. Core::String& Core::String::appendUnicode(u32 c) {
  110. if(length < DATA_LENGTH) {
  111. data[length++] = c;
  112. addToHash(c);
  113. }
  114. return *this;
  115. }
  116. check_format(2, 3) Core::String& Core::String::appendFormat(const char* format,
  117. ...) {
  118. constexpr int BUFFER_SIZE = 64;
  119. char buffer[BUFFER_SIZE];
  120. va_list args;
  121. va_start(args, format);
  122. vsnprintf(buffer, BUFFER_SIZE, format, args);
  123. va_end(args);
  124. append(static_cast<const char*>(buffer));
  125. return *this;
  126. }
  127. void Core::String::toString(String& s) const {
  128. if(this == &s) {
  129. return;
  130. }
  131. for(int i = 0; i < length; i++) {
  132. s.appendUnicode(data[i]);
  133. }
  134. }
  135. Core::String& Core::String::clear() {
  136. length = 0;
  137. hash = 0;
  138. data[0] = '\0';
  139. return *this;
  140. }
  141. Hash Core::String::hashCode() const {
  142. return hash;
  143. }
  144. void Core::String::print() const {
  145. for(int i = 0; i < length; i++) {
  146. u32 c = data[i];
  147. if(c < (1 << 7)) {
  148. putchar(static_cast<int>(c & 0x7F));
  149. } else if(c < (1 << 11)) {
  150. putchar(static_cast<int>(((c >> 6) & 0x1F) | 0xC0));
  151. putchar(static_cast<int>(((c >> 0) & 0x3F) | 0x80));
  152. } else if(c < (1 << 16)) {
  153. putchar(static_cast<int>(((c >> 12) & 0x0F) | 0xE0));
  154. putchar(static_cast<int>(((c >> 6) & 0x3F) | 0x80));
  155. putchar(static_cast<int>(((c >> 0) & 0x3F) | 0x80));
  156. } else if(c < (1 << 21)) {
  157. putchar(static_cast<int>(((c >> 18) & 0x07) | 0xF0));
  158. putchar(static_cast<int>(((c >> 12) & 0x3F) | 0x80));
  159. putchar(static_cast<int>(((c >> 6) & 0x3F) | 0x80));
  160. putchar(static_cast<int>(((c >> 0) & 0x3F) | 0x80));
  161. }
  162. }
  163. }
  164. void Core::String::printLine() const {
  165. print();
  166. putchar('\n');
  167. }
  168. void Core::String::addToHash(u32 u) {
  169. hash = static_cast<Hash>(2120251889) * hash + static_cast<Hash>(u);
  170. }
  171. bool operator==(const char* cs, const Core::String& s) {
  172. return s == cs;
  173. }
  174. bool operator!=(const char* cs, const Core::String& s) {
  175. return s != cs;
  176. }