string_buffer.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. #pragma once
  31. #include "core/string/ustring.h"
  32. template <int SHORT_BUFFER_SIZE = 64>
  33. class StringBuffer {
  34. char32_t short_buffer[SHORT_BUFFER_SIZE];
  35. String buffer;
  36. int string_length = 0;
  37. _FORCE_INLINE_ char32_t *current_buffer_ptr() {
  38. return static_cast<String &>(buffer).is_empty() ? short_buffer : buffer.ptrw();
  39. }
  40. public:
  41. StringBuffer &append(char32_t p_char);
  42. StringBuffer &append(const String &p_string);
  43. StringBuffer &append(const char *p_str);
  44. StringBuffer &append(const char32_t *p_str, int p_clip_to_len = -1);
  45. _FORCE_INLINE_ void operator+=(char32_t p_char) {
  46. append(p_char);
  47. }
  48. _FORCE_INLINE_ void operator+=(const String &p_string) {
  49. append(p_string);
  50. }
  51. _FORCE_INLINE_ void operator+=(const char *p_str) {
  52. append(p_str);
  53. }
  54. _FORCE_INLINE_ void operator+=(const char32_t *p_str) {
  55. append(p_str);
  56. }
  57. StringBuffer &reserve(int p_size);
  58. int length() const;
  59. String as_string();
  60. double as_double();
  61. int64_t as_int();
  62. _FORCE_INLINE_ operator String() {
  63. return as_string();
  64. }
  65. };
  66. template <int SHORT_BUFFER_SIZE>
  67. StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::append(char32_t p_char) {
  68. reserve(string_length + 2);
  69. current_buffer_ptr()[string_length++] = p_char;
  70. return *this;
  71. }
  72. template <int SHORT_BUFFER_SIZE>
  73. StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::append(const String &p_string) {
  74. return append(p_string.get_data());
  75. }
  76. template <int SHORT_BUFFER_SIZE>
  77. StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::append(const char *p_str) {
  78. int len = strlen(p_str);
  79. reserve(string_length + len + 1);
  80. char32_t *buf = current_buffer_ptr();
  81. for (const char *c_ptr = p_str; *c_ptr; ++c_ptr) {
  82. buf[string_length++] = *c_ptr;
  83. }
  84. return *this;
  85. }
  86. template <int SHORT_BUFFER_SIZE>
  87. StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::append(const char32_t *p_str, int p_clip_to_len) {
  88. int len = 0;
  89. while ((p_clip_to_len < 0 || len < p_clip_to_len) && p_str[len]) {
  90. ++len;
  91. }
  92. reserve(string_length + len + 1);
  93. memcpy(&(current_buffer_ptr()[string_length]), p_str, len * sizeof(char32_t));
  94. string_length += len;
  95. return *this;
  96. }
  97. template <int SHORT_BUFFER_SIZE>
  98. StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::reserve(int p_size) {
  99. if (p_size < SHORT_BUFFER_SIZE || p_size < buffer.size() || !p_size) {
  100. return *this;
  101. }
  102. bool need_copy = string_length > 0 && buffer.is_empty();
  103. buffer.resize(next_power_of_2(p_size));
  104. if (need_copy) {
  105. memcpy(buffer.ptrw(), short_buffer, string_length * sizeof(char32_t));
  106. }
  107. return *this;
  108. }
  109. template <int SHORT_BUFFER_SIZE>
  110. int StringBuffer<SHORT_BUFFER_SIZE>::length() const {
  111. return string_length;
  112. }
  113. template <int SHORT_BUFFER_SIZE>
  114. String StringBuffer<SHORT_BUFFER_SIZE>::as_string() {
  115. current_buffer_ptr()[string_length] = '\0';
  116. if (buffer.is_empty()) {
  117. return String(short_buffer);
  118. } else {
  119. buffer.resize(string_length + 1);
  120. return buffer;
  121. }
  122. }
  123. template <int SHORT_BUFFER_SIZE>
  124. double StringBuffer<SHORT_BUFFER_SIZE>::as_double() {
  125. current_buffer_ptr()[string_length] = '\0';
  126. return String::to_float(current_buffer_ptr());
  127. }
  128. template <int SHORT_BUFFER_SIZE>
  129. int64_t StringBuffer<SHORT_BUFFER_SIZE>::as_int() {
  130. current_buffer_ptr()[string_length] = '\0';
  131. return String::to_int(current_buffer_ptr());
  132. }