SpineString.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. * or otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. #ifndef SPINE_STRING_H
  30. #define SPINE_STRING_H
  31. #include <spine/SpineObject.h>
  32. #include <spine/extension.h>
  33. #include <string.h>
  34. #include <stdio.h>
  35. // Required for sprintf on MSVC
  36. #ifdef _MSC_VER
  37. #pragma warning(disable:4996)
  38. #endif
  39. namespace spine {
  40. class SP_API String : public SpineObject {
  41. public:
  42. String() : _length(0), _buffer(NULL) {
  43. }
  44. String(const char *chars, bool own = false) {
  45. if (!chars) {
  46. _length = 0;
  47. _buffer = NULL;
  48. } else {
  49. _length = strlen(chars);
  50. if (!own) {
  51. _buffer = SpineExtension::calloc<char>(_length + 1, __FILE__, __LINE__);
  52. memcpy((void *) _buffer, chars, _length + 1);
  53. } else {
  54. _buffer = (char *) chars;
  55. }
  56. }
  57. }
  58. String(const String &other) {
  59. if (!other._buffer) {
  60. _length = 0;
  61. _buffer = NULL;
  62. } else {
  63. _length = other._length;
  64. _buffer = SpineExtension::calloc<char>(other._length + 1, __FILE__, __LINE__);
  65. memcpy((void *) _buffer, other._buffer, other._length + 1);
  66. }
  67. }
  68. size_t length() const {
  69. return _length;
  70. }
  71. bool isEmpty() const {
  72. return _length == 0;
  73. }
  74. const char *buffer() const {
  75. return _buffer;
  76. }
  77. void own(const String &other) {
  78. if (this == &other) return;
  79. if (_buffer) {
  80. SpineExtension::free(_buffer, __FILE__, __LINE__);
  81. }
  82. _length = other._length;
  83. _buffer = other._buffer;
  84. other._length = 0;
  85. other._buffer = NULL;
  86. }
  87. void own(const char *chars) {
  88. if (_buffer == chars) return;
  89. if (_buffer) {
  90. SpineExtension::free(_buffer, __FILE__, __LINE__);
  91. }
  92. if (!chars) {
  93. _length = 0;
  94. _buffer = NULL;
  95. } else {
  96. _length = strlen(chars);
  97. _buffer = (char *) chars;
  98. }
  99. }
  100. void unown() {
  101. _length = 0;
  102. _buffer = NULL;
  103. }
  104. String &operator=(const String &other) {
  105. if (this == &other) return *this;
  106. if (_buffer) {
  107. SpineExtension::free(_buffer, __FILE__, __LINE__);
  108. }
  109. if (!other._buffer) {
  110. _length = 0;
  111. _buffer = NULL;
  112. } else {
  113. _length = other._length;
  114. _buffer = SpineExtension::calloc<char>(other._length + 1, __FILE__, __LINE__);
  115. memcpy((void *) _buffer, other._buffer, other._length + 1);
  116. }
  117. return *this;
  118. }
  119. String &operator=(const char *chars) {
  120. if (_buffer == chars) return *this;
  121. if (_buffer) {
  122. SpineExtension::free(_buffer, __FILE__, __LINE__);
  123. }
  124. if (!chars) {
  125. _length = 0;
  126. _buffer = NULL;
  127. } else {
  128. _length = strlen(chars);
  129. _buffer = SpineExtension::calloc<char>(_length + 1, __FILE__, __LINE__);
  130. memcpy((void *) _buffer, chars, _length + 1);
  131. }
  132. return *this;
  133. }
  134. String &append(const char *chars) {
  135. size_t len = strlen(chars);
  136. size_t thisLen = _length;
  137. _length = _length + len;
  138. bool same = chars == _buffer;
  139. _buffer = SpineExtension::realloc(_buffer, _length + 1, __FILE__, __LINE__);
  140. memcpy((void *) (_buffer + thisLen), (void *) (same ? _buffer : chars), len + 1);
  141. return *this;
  142. }
  143. String &append(const String &other) {
  144. size_t len = other.length();
  145. size_t thisLen = _length;
  146. _length = _length + len;
  147. bool same = other._buffer == _buffer;
  148. _buffer = SpineExtension::realloc(_buffer, _length + 1, __FILE__, __LINE__);
  149. memcpy((void *) (_buffer + thisLen), (void *) (same ? _buffer : other._buffer), len + 1);
  150. return *this;
  151. }
  152. String &append(int other) {
  153. char str[100];
  154. sprintf(str, "%i", other);
  155. append(str);
  156. return *this;
  157. }
  158. String &append(float other) {
  159. char str[100];
  160. sprintf(str, "%f", other);
  161. append(str);
  162. return *this;
  163. }
  164. friend bool operator==(const String &a, const String &b) {
  165. if (a._buffer == b._buffer) return true;
  166. if (a._length != b._length) return false;
  167. if (a._buffer && b._buffer) {
  168. return strcmp(a._buffer, b._buffer) == 0;
  169. } else {
  170. return false;
  171. }
  172. }
  173. friend bool operator!=(const String &a, const String &b) {
  174. return !(a == b);
  175. }
  176. ~String() {
  177. if (_buffer) {
  178. SpineExtension::free(_buffer, __FILE__, __LINE__);
  179. }
  180. }
  181. private:
  182. mutable size_t _length;
  183. mutable char *_buffer;
  184. };
  185. }
  186. #endif //SPINE_STRING_H