1
0

CCString.h 5.8 KB

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