string_buffer.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /**************************************************************************/
  2. /* string_buffer.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef STRING_BUFFER_H
  31. #define STRING_BUFFER_H
  32. #include "core/string/ustring.h"
  33. template <int SHORT_BUFFER_SIZE = 64>
  34. class StringBuffer {
  35. char32_t short_buffer[SHORT_BUFFER_SIZE];
  36. String buffer;
  37. int string_length = 0;
  38. _FORCE_INLINE_ char32_t *current_buffer_ptr() {
  39. return static_cast<String &>(buffer).is_empty() ? short_buffer : buffer.ptrw();
  40. }
  41. public:
  42. StringBuffer &append(char32_t p_char);
  43. StringBuffer &append(const String &p_string);
  44. StringBuffer &append(const char *p_str);
  45. StringBuffer &append(const char32_t *p_str, int p_clip_to_len = -1);
  46. _FORCE_INLINE_ void operator+=(char32_t p_char) {
  47. append(p_char);
  48. }
  49. _FORCE_INLINE_ void operator+=(const String &p_string) {
  50. append(p_string);
  51. }
  52. _FORCE_INLINE_ void operator+=(const char *p_str) {
  53. append(p_str);
  54. }
  55. _FORCE_INLINE_ void operator+=(const char32_t *p_str) {
  56. append(p_str);
  57. }
  58. StringBuffer &reserve(int p_size);
  59. int length() const;
  60. String as_string();
  61. double as_double();
  62. int64_t as_int();
  63. _FORCE_INLINE_ operator String() {
  64. return as_string();
  65. }
  66. };
  67. template <int SHORT_BUFFER_SIZE>
  68. StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::append(char32_t p_char) {
  69. reserve(string_length + 2);
  70. current_buffer_ptr()[string_length++] = p_char;
  71. return *this;
  72. }
  73. template <int SHORT_BUFFER_SIZE>
  74. StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::append(const String &p_string) {
  75. return append(p_string.get_data());
  76. }
  77. template <int SHORT_BUFFER_SIZE>
  78. StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::append(const char *p_str) {
  79. int len = strlen(p_str);
  80. reserve(string_length + len + 1);
  81. char32_t *buf = current_buffer_ptr();
  82. for (const char *c_ptr = p_str; *c_ptr; ++c_ptr) {
  83. buf[string_length++] = *c_ptr;
  84. }
  85. return *this;
  86. }
  87. template <int SHORT_BUFFER_SIZE>
  88. StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::append(const char32_t *p_str, int p_clip_to_len) {
  89. int len = 0;
  90. while ((p_clip_to_len < 0 || len < p_clip_to_len) && p_str[len]) {
  91. ++len;
  92. }
  93. reserve(string_length + len + 1);
  94. memcpy(&(current_buffer_ptr()[string_length]), p_str, len * sizeof(char32_t));
  95. string_length += len;
  96. return *this;
  97. }
  98. template <int SHORT_BUFFER_SIZE>
  99. StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::reserve(int p_size) {
  100. if (p_size < SHORT_BUFFER_SIZE || p_size < buffer.size()) {
  101. return *this;
  102. }
  103. bool need_copy = string_length > 0 && buffer.is_empty();
  104. buffer.resize(next_power_of_2(p_size));
  105. if (need_copy) {
  106. memcpy(buffer.ptrw(), short_buffer, string_length * sizeof(char32_t));
  107. }
  108. return *this;
  109. }
  110. template <int SHORT_BUFFER_SIZE>
  111. int StringBuffer<SHORT_BUFFER_SIZE>::length() const {
  112. return string_length;
  113. }
  114. template <int SHORT_BUFFER_SIZE>
  115. String StringBuffer<SHORT_BUFFER_SIZE>::as_string() {
  116. current_buffer_ptr()[string_length] = '\0';
  117. if (buffer.is_empty()) {
  118. return String(short_buffer);
  119. } else {
  120. buffer.resize(string_length + 1);
  121. return buffer;
  122. }
  123. }
  124. template <int SHORT_BUFFER_SIZE>
  125. double StringBuffer<SHORT_BUFFER_SIZE>::as_double() {
  126. current_buffer_ptr()[string_length] = '\0';
  127. return String::to_float(current_buffer_ptr());
  128. }
  129. template <int SHORT_BUFFER_SIZE>
  130. int64_t StringBuffer<SHORT_BUFFER_SIZE>::as_int() {
  131. current_buffer_ptr()[string_length] = '\0';
  132. return String::to_int(current_buffer_ptr());
  133. }
  134. #endif // STRING_BUFFER_H