CCString.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2017 Chukong Technologies
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #ifndef __CCSTRING_H__
  23. #define __CCSTRING_H__
  24. /// @cond DO_NOT_SHOW
  25. #if (CC_TARGET_PLATFORM == CC_PLATFORM_BLACKBERRY)
  26. #include <string.h>
  27. #endif
  28. #include <stdarg.h>
  29. #include <string>
  30. #include <functional>
  31. #include "deprecated/CCArray.h"
  32. #include "base/CCRef.h"
  33. // We need to include `StringUtils::format()` and `StringUtils::toString()`
  34. // for keeping the backward compatibility
  35. #include "base/ccUTF8.h"
  36. NS_CC_BEGIN
  37. /**
  38. * @addtogroup data_structures
  39. * @{
  40. */
  41. class CC_DLL __String : public Ref, public Clonable
  42. {
  43. public:
  44. /**
  45. * @js NA
  46. * @lua NA
  47. */
  48. __String();
  49. /**
  50. * @js NA
  51. * @lua NA
  52. */
  53. __String(const char* str);
  54. /**
  55. * @js NA
  56. * @lua NA
  57. */
  58. __String(const std::string& str);
  59. /**
  60. * @js NA
  61. * @lua NA
  62. */
  63. __String(const __String& str);
  64. /**
  65. * @js NA
  66. * @lua NA
  67. */
  68. virtual ~__String();
  69. /* override assignment operator
  70. * @js NA
  71. * @lua NA
  72. */
  73. __String& operator= (const __String& other);
  74. /** init a string with format, it's similar with the c function 'sprintf'
  75. * @js NA
  76. * @lua NA
  77. */
  78. bool initWithFormat(const char* format, ...) CC_FORMAT_PRINTF(2, 3);
  79. /** convert to int value
  80. * @js NA
  81. */
  82. int intValue() const;
  83. /** convert to unsigned int value
  84. * @js NA
  85. */
  86. unsigned int uintValue() const;
  87. /** convert to float value
  88. * @js NA
  89. */
  90. float floatValue() const;
  91. /** convert to double value
  92. * @js NA
  93. */
  94. double doubleValue() const;
  95. /** convert to bool value
  96. * @js NA
  97. */
  98. bool boolValue() const;
  99. /** get the C string
  100. * @js NA
  101. */
  102. const char* getCString() const;
  103. /** get the length of string
  104. * @js NA
  105. */
  106. int length() const;
  107. /** compare to a c string
  108. * @js NA
  109. */
  110. int compare(const char *) const;
  111. /** append additional characters at the end of its current value
  112. * @js NA
  113. * @lua NA
  114. */
  115. void append(const std::string& str);
  116. /** append(w/ format) additional characters at the end of its current value
  117. * @js NA
  118. * @lua NA
  119. */
  120. void appendWithFormat(const char* format, ...);
  121. /** split a string
  122. * @js NA
  123. * @lua NA
  124. */
  125. __Array* componentsSeparatedByString(const char *delimiter);
  126. /* override functions
  127. * @js NA
  128. */
  129. virtual bool isEqual(const Ref* pObject);
  130. /** create a string with std string, you can also pass a c string pointer because the default constructor of std::string can access a c string pointer.
  131. * @return A String pointer which is an autorelease object pointer,
  132. * it means that you needn't do a release operation unless you retain it.
  133. * @js NA
  134. */
  135. static __String* create(const std::string& str);
  136. /** create a string with format, it's similar with the c function 'sprintf', the default buffer size is (1024*100) bytes,
  137. * if you want to change it, you should modify the kMax__StringLen macro in __String.cpp file.
  138. * @return A String pointer which is an autorelease object pointer,
  139. * it means that you needn't do a release operation unless you retain it.
  140. * @js NA
  141. */
  142. static __String* createWithFormat(const char* format, ...) CC_FORMAT_PRINTF(1, 2);
  143. /** create a string with binary data
  144. * @return A String pointer which is an autorelease object pointer,
  145. * it means that you needn't do a release operation unless you retain it.
  146. * @js NA
  147. */
  148. static __String* createWithData(const unsigned char* pData, size_t nLen);
  149. /** create a string with a file,
  150. * @return A String pointer which is an autorelease object pointer,
  151. * it means that you needn't do a release operation unless you retain it.
  152. * @js NA
  153. */
  154. static __String* createWithContentsOfFile(const std::string& filename);
  155. /**
  156. * @js NA
  157. * @lua NA
  158. */
  159. virtual void acceptVisitor(DataVisitor &visitor);
  160. /**
  161. * @js NA
  162. * @lua NA
  163. */
  164. virtual __String* clone() const override;
  165. private:
  166. /** only for internal use */
  167. bool initWithFormatAndValist(const char* format, va_list ap);
  168. public:
  169. std::string _string;
  170. };
  171. struct StringCompare : public std::binary_function<__String *, __String *, bool> {
  172. public:
  173. bool operator() (__String * a, __String * b) const {
  174. return strcmp(a->getCString(), b->getCString()) < 0;
  175. }
  176. };
  177. #define StringMake(str) String::create(str)
  178. #define ccs StringMake
  179. // end of data_structure group
  180. /// @}
  181. NS_CC_END
  182. /// @endcond
  183. #endif //__CCSTRING_H__