string_buffer.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*************************************************************************/
  2. /* string_buffer.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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/ustring.h"
  33. template <int SHORT_BUFFER_SIZE = 64>
  34. class StringBuffer {
  35. CharType short_buffer[SHORT_BUFFER_SIZE];
  36. String buffer;
  37. int string_length;
  38. _FORCE_INLINE_ CharType *current_buffer_ptr() {
  39. return static_cast<String &>(buffer).empty() ? short_buffer : buffer.ptrw();
  40. }
  41. public:
  42. StringBuffer &append(CharType p_char);
  43. StringBuffer &append(const String &p_string);
  44. StringBuffer &append(const char *p_str);
  45. StringBuffer &append(const CharType *p_str, int p_clip_to_len = -1);
  46. _FORCE_INLINE_ void operator+=(CharType 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 CharType *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. StringBuffer() {
  67. string_length = 0;
  68. }
  69. };
  70. template <int SHORT_BUFFER_SIZE>
  71. StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::append(CharType p_char) {
  72. reserve(string_length + 2);
  73. current_buffer_ptr()[string_length++] = p_char;
  74. return *this;
  75. }
  76. template <int SHORT_BUFFER_SIZE>
  77. StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::append(const String &p_string) {
  78. return append(p_string.c_str());
  79. }
  80. template <int SHORT_BUFFER_SIZE>
  81. StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::append(const char *p_str) {
  82. int len = strlen(p_str);
  83. reserve(string_length + len + 1);
  84. CharType *buf = current_buffer_ptr();
  85. for (const char *c_ptr = p_str; *c_ptr; ++c_ptr) {
  86. buf[string_length++] = *c_ptr;
  87. }
  88. return *this;
  89. }
  90. template <int SHORT_BUFFER_SIZE>
  91. StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::append(const CharType *p_str, int p_clip_to_len) {
  92. int len = 0;
  93. while ((p_clip_to_len < 0 || len < p_clip_to_len) && p_str[len]) {
  94. ++len;
  95. }
  96. reserve(string_length + len + 1);
  97. memcpy(&(current_buffer_ptr()[string_length]), p_str, len * sizeof(CharType));
  98. string_length += len;
  99. return *this;
  100. }
  101. template <int SHORT_BUFFER_SIZE>
  102. StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::reserve(int p_size) {
  103. if (p_size < SHORT_BUFFER_SIZE || p_size < buffer.size())
  104. return *this;
  105. bool need_copy = string_length > 0 && buffer.empty();
  106. buffer.resize(next_power_of_2(p_size));
  107. if (need_copy) {
  108. memcpy(buffer.ptrw(), short_buffer, string_length * sizeof(CharType));
  109. }
  110. return *this;
  111. }
  112. template <int SHORT_BUFFER_SIZE>
  113. int StringBuffer<SHORT_BUFFER_SIZE>::length() const {
  114. return string_length;
  115. }
  116. template <int SHORT_BUFFER_SIZE>
  117. String StringBuffer<SHORT_BUFFER_SIZE>::as_string() {
  118. current_buffer_ptr()[string_length] = '\0';
  119. if (buffer.empty()) {
  120. return String(short_buffer);
  121. } else {
  122. buffer.resize(string_length + 1);
  123. return buffer;
  124. }
  125. }
  126. template <int SHORT_BUFFER_SIZE>
  127. double StringBuffer<SHORT_BUFFER_SIZE>::as_double() {
  128. current_buffer_ptr()[string_length] = '\0';
  129. return String::to_double(current_buffer_ptr());
  130. }
  131. template <int SHORT_BUFFER_SIZE>
  132. int64_t StringBuffer<SHORT_BUFFER_SIZE>::as_int() {
  133. current_buffer_ptr()[string_length] = '\0';
  134. return String::to_int(current_buffer_ptr());
  135. }
  136. #endif