String.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /******************************************************************************
  2. * Spine Runtimes Software License v2.5
  3. *
  4. * Copyright (c) 2013-2016, Esoteric Software
  5. * All rights reserved.
  6. *
  7. * You are granted a perpetual, non-exclusive, non-sublicensable, and
  8. * non-transferable license to use, install, execute, and perform the Spine
  9. * Runtimes software and derivative works solely for personal or internal
  10. * use. Without the written permission of Esoteric Software (see Section 2 of
  11. * the Spine Software License Agreement), you may not (a) modify, translate,
  12. * adapt, or develop new applications using the Spine Runtimes or otherwise
  13. * create derivative works or improvements of the Spine Runtimes or (b) remove,
  14. * delete, alter, or obscure any trademarks or any copyright, trademark, patent,
  15. * or other intellectual property or proprietary rights notices on or in the
  16. * Software, including any copy thereof. Redistributions in binary or source
  17. * form must include this license and terms.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  22. * EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS INTERRUPTION, OR LOSS OF
  25. * USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  26. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *****************************************************************************/
  30. #ifndef SPINE_STRING_H
  31. #define SPINE_STRING_H
  32. #include <spine/SpineObject.h>
  33. #include <spine/Extension.h>
  34. #include <string.h>
  35. #include <stdio.h>
  36. // Required for sprintf on MSVC
  37. #ifdef _MSC_VER
  38. #pragma warning(disable:4996)
  39. #endif
  40. namespace Spine {
  41. class String : public SpineObject {
  42. public:
  43. String() : _length(0), _buffer(NULL) {
  44. }
  45. String(const char *chars, bool own = false) {
  46. if (!chars) {
  47. _length = 0;
  48. _buffer = NULL;
  49. } else {
  50. _length = strlen(chars);
  51. if (!own) {
  52. _buffer = SpineExtension::calloc<char>(_length + 1, __FILE__, __LINE__);
  53. memcpy((void *) _buffer, chars, _length + 1);
  54. } else {
  55. _buffer = (char *) chars;
  56. }
  57. }
  58. }
  59. String(const String &other) {
  60. if (!other._buffer) {
  61. _length = 0;
  62. _buffer = NULL;
  63. } else {
  64. _length = other._length;
  65. _buffer = SpineExtension::calloc<char>(other._length + 1, __FILE__, __LINE__);
  66. memcpy((void *) _buffer, other._buffer, other._length + 1);
  67. }
  68. }
  69. size_t length() const {
  70. return _length;
  71. }
  72. bool isEmpty() const {
  73. return _length == 0;
  74. }
  75. const char *buffer() const {
  76. return _buffer;
  77. }
  78. void own(const String &other) {
  79. if (this == &other) return;
  80. if (_buffer) {
  81. SpineExtension::free(_buffer, __FILE__, __LINE__);
  82. }
  83. _length = other._length;
  84. _buffer = other._buffer;
  85. other._length = 0;
  86. other._buffer = NULL;
  87. }
  88. void own(const char *chars) {
  89. if (_buffer == chars) return;
  90. if (_buffer) {
  91. SpineExtension::free(_buffer, __FILE__, __LINE__);
  92. }
  93. if (!chars) {
  94. _length = 0;
  95. _buffer = NULL;
  96. } else {
  97. _length = strlen(chars);
  98. _buffer = (char *) chars;
  99. }
  100. }
  101. void unown() {
  102. _length = 0;
  103. _buffer = NULL;
  104. }
  105. String &operator=(const String &other) {
  106. if (this == &other) return *this;
  107. if (_buffer) {
  108. SpineExtension::free(_buffer, __FILE__, __LINE__);
  109. }
  110. if (!other._buffer) {
  111. _length = 0;
  112. _buffer = NULL;
  113. } else {
  114. _length = other._length;
  115. _buffer = SpineExtension::calloc<char>(other._length + 1, __FILE__, __LINE__);
  116. memcpy((void *) _buffer, other._buffer, other._length + 1);
  117. }
  118. return *this;
  119. }
  120. String &operator=(const char *chars) {
  121. if (_buffer == chars) return *this;
  122. if (_buffer) {
  123. SpineExtension::free(_buffer, __FILE__, __LINE__);
  124. }
  125. if (!chars) {
  126. _length = 0;
  127. _buffer = NULL;
  128. } else {
  129. _length = strlen(chars);
  130. _buffer = SpineExtension::calloc<char>(_length + 1, __FILE__, __LINE__);
  131. memcpy((void *) _buffer, chars, _length + 1);
  132. }
  133. return *this;
  134. }
  135. String &append(const char *chars) {
  136. size_t len = strlen(chars);
  137. size_t thisLen = _length;
  138. _length = _length + len;
  139. bool same = chars == _buffer;
  140. _buffer = SpineExtension::realloc(_buffer, _length + 1, __FILE__, __LINE__);
  141. memcpy((void *) (_buffer + thisLen), (void *) (same ? _buffer : chars), len + 1);
  142. return *this;
  143. }
  144. String &append(const String &other) {
  145. size_t len = other.length();
  146. size_t thisLen = _length;
  147. _length = _length + len;
  148. bool same = other._buffer == _buffer;
  149. _buffer = SpineExtension::realloc(_buffer, _length + 1, __FILE__, __LINE__);
  150. memcpy((void *) (_buffer + thisLen), (void *) (same ? _buffer : other._buffer), len + 1);
  151. return *this;
  152. }
  153. String &append(int other) {
  154. char str[100];
  155. sprintf(str, "%i", other);
  156. append(str);
  157. return *this;
  158. }
  159. String &append(float other) {
  160. char str[100];
  161. sprintf(str, "%f", other);
  162. append(str);
  163. return *this;
  164. }
  165. friend bool operator==(const String &a, const String &b) {
  166. if (a._buffer == b._buffer) return true;
  167. if (a._length != b._length) return false;
  168. if (a._buffer && b._buffer) {
  169. return strcmp(a._buffer, b._buffer) == 0;
  170. } else {
  171. return false;
  172. }
  173. }
  174. friend bool operator!=(const String &a, const String &b) {
  175. return !(a == b);
  176. }
  177. ~String() {
  178. if (_buffer) {
  179. SpineExtension::free(_buffer, __FILE__, __LINE__);
  180. }
  181. }
  182. private:
  183. mutable size_t _length;
  184. mutable char *_buffer;
  185. };
  186. }
  187. #endif //SPINE_STRING_H