string_buffer.h 5.4 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) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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 "ustring.h"
  33. #include <string.h>
  34. template <int SHORT_BUFFER_SIZE = 64>
  35. class StringBuffer {
  36. CharType short_buffer[SHORT_BUFFER_SIZE];
  37. String buffer;
  38. int string_length = 0;
  39. _FORCE_INLINE_ CharType *current_buffer_ptr() {
  40. return static_cast<Vector<CharType> &>(buffer).empty() ? short_buffer : buffer.ptrw();
  41. }
  42. public:
  43. StringBuffer &append(CharType p_char);
  44. StringBuffer &append(const String &p_string);
  45. StringBuffer &append(const char *p_str);
  46. StringBuffer &append(const CharType *p_str, int p_clip_to_len = -1);
  47. _FORCE_INLINE_ void operator+=(CharType p_char) {
  48. append(p_char);
  49. }
  50. _FORCE_INLINE_ void operator+=(const String &p_string) {
  51. append(p_string);
  52. }
  53. _FORCE_INLINE_ void operator+=(const char *p_str) {
  54. append(p_str);
  55. }
  56. _FORCE_INLINE_ void operator+=(const CharType *p_str) {
  57. append(p_str);
  58. }
  59. StringBuffer &reserve(int p_size);
  60. int length() const;
  61. String as_string();
  62. double as_double();
  63. int64_t as_int();
  64. _FORCE_INLINE_ operator String() {
  65. return as_string();
  66. }
  67. };
  68. template <int SHORT_BUFFER_SIZE>
  69. StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::append(CharType p_char) {
  70. reserve(string_length + 2);
  71. current_buffer_ptr()[string_length++] = p_char;
  72. return *this;
  73. }
  74. template <int SHORT_BUFFER_SIZE>
  75. StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::append(const String &p_string) {
  76. return append(p_string.c_str());
  77. }
  78. template <int SHORT_BUFFER_SIZE>
  79. StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::append(const char *p_str) {
  80. int len = strlen(p_str);
  81. reserve(string_length + len + 1);
  82. CharType *buf = current_buffer_ptr();
  83. for (const char *c_ptr = p_str; *c_ptr; ++c_ptr) {
  84. buf[string_length++] = *c_ptr;
  85. }
  86. return *this;
  87. }
  88. template <int SHORT_BUFFER_SIZE>
  89. StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::append(const CharType *p_str, int p_clip_to_len) {
  90. int len = 0;
  91. while ((p_clip_to_len < 0 || len < p_clip_to_len) && p_str[len]) {
  92. ++len;
  93. }
  94. reserve(string_length + len + 1);
  95. memcpy(&(current_buffer_ptr()[string_length]), p_str, len * sizeof(CharType));
  96. string_length += len;
  97. return *this;
  98. }
  99. template <int SHORT_BUFFER_SIZE>
  100. StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::reserve(int p_size) {
  101. if (p_size < SHORT_BUFFER_SIZE || p_size < buffer.size())
  102. return *this;
  103. bool need_copy = string_length > 0 && buffer.empty();
  104. buffer.resize(next_power_of_2(p_size));
  105. if (need_copy) {
  106. memcpy(buffer.ptrw(), short_buffer, string_length * sizeof(CharType));
  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.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_double(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