string_buffer.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*************************************************************************/
  2. /* string_buffer.cpp */
  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. #include "string_buffer.h"
  31. #include <string.h>
  32. StringBuffer &StringBuffer::append(CharType p_char) {
  33. reserve(string_length + 2);
  34. current_buffer_ptr()[string_length++] = p_char;
  35. return *this;
  36. }
  37. StringBuffer &StringBuffer::append(const String &p_string) {
  38. return append(p_string.c_str());
  39. }
  40. StringBuffer &StringBuffer::append(const char *p_str) {
  41. int len = strlen(p_str);
  42. reserve(string_length + len + 1);
  43. CharType *buf = current_buffer_ptr();
  44. for (const char *c_ptr = p_str; *c_ptr; ++c_ptr) {
  45. buf[string_length++] = *c_ptr;
  46. }
  47. return *this;
  48. }
  49. StringBuffer &StringBuffer::append(const CharType *p_str, int p_clip_to_len) {
  50. int len = 0;
  51. while ((p_clip_to_len < 0 || len < p_clip_to_len) && p_str[len]) {
  52. ++len;
  53. }
  54. reserve(string_length + len + 1);
  55. memcpy(&(current_buffer_ptr()[string_length]), p_str, len * sizeof(CharType));
  56. string_length += len;
  57. return *this;
  58. }
  59. StringBuffer &StringBuffer::reserve(int p_size) {
  60. if (p_size < SHORT_BUFFER_SIZE || p_size < buffer.size())
  61. return *this;
  62. bool need_copy = string_length > 0 && buffer.empty();
  63. buffer.resize(next_power_of_2(p_size));
  64. if (need_copy) {
  65. memcpy(buffer.ptrw(), short_buffer, string_length * sizeof(CharType));
  66. }
  67. return *this;
  68. }
  69. int StringBuffer::length() const {
  70. return string_length;
  71. }
  72. String StringBuffer::as_string() {
  73. current_buffer_ptr()[string_length] = '\0';
  74. if (buffer.empty()) {
  75. return String(short_buffer);
  76. } else {
  77. buffer.resize(string_length + 1);
  78. return buffer;
  79. }
  80. }
  81. double StringBuffer::as_double() {
  82. current_buffer_ptr()[string_length] = '\0';
  83. return String::to_double(current_buffer_ptr());
  84. }
  85. int64_t StringBuffer::as_int() {
  86. current_buffer_ptr()[string_length] = '\0';
  87. return String::to_int(current_buffer_ptr());
  88. }