string_builder.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*************************************************************************/
  2. /* string_builder.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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_builder.h"
  31. #include <string.h>
  32. StringBuilder &StringBuilder::append(const String &p_string) {
  33. strings.push_back(p_string);
  34. appended_strings.push_back(-1);
  35. string_length += p_string.length();
  36. return *this;
  37. }
  38. StringBuilder &StringBuilder::append(const char *p_cstring) {
  39. int32_t len = strlen(p_cstring);
  40. c_strings.push_back(p_cstring);
  41. appended_strings.push_back(len);
  42. string_length += len;
  43. return *this;
  44. }
  45. String StringBuilder::as_string() const {
  46. if (string_length == 0)
  47. return "";
  48. CharType *buffer = memnew_arr(CharType, string_length);
  49. int current_position = 0;
  50. int godot_string_elem = 0;
  51. int c_string_elem = 0;
  52. for (int i = 0; i < appended_strings.size(); i++) {
  53. if (appended_strings[i] == -1) {
  54. // Godot string
  55. const String &s = strings[godot_string_elem];
  56. memcpy(buffer + current_position, s.ptr(), s.length() * sizeof(CharType));
  57. current_position += s.length();
  58. godot_string_elem++;
  59. } else {
  60. const char *s = c_strings[c_string_elem];
  61. for (int32_t j = 0; j < appended_strings[i]; j++) {
  62. buffer[current_position + j] = s[j];
  63. }
  64. current_position += appended_strings[i];
  65. c_string_elem++;
  66. }
  67. }
  68. String final_string = String(buffer, string_length);
  69. memdelete_arr(buffer);
  70. return final_string;
  71. }