1
0

ccUTF8.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /****************************************************************************
  2. Copyright (c) 2014 cocos2d-x.org
  3. Copyright (c) 2014-2017 Chukong Technologies Inc.
  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 __cocos2dx__ccUTF8__
  22. #define __cocos2dx__ccUTF8__
  23. #include "platform/CCPlatformMacros.h"
  24. #include <vector>
  25. #include <string>
  26. #include <sstream>
  27. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  28. #include "platform/android/jni/JniHelper.h"
  29. #endif
  30. NS_CC_BEGIN
  31. namespace StringUtils {
  32. template<typename T>
  33. std::string toString(T arg)
  34. {
  35. std::stringstream ss;
  36. ss << arg;
  37. return ss.str();
  38. }
  39. std::string CC_DLL format(const char* format, ...) CC_FORMAT_PRINTF(1, 2);
  40. /**
  41. * @brief Converts from UTF8 string to UTF16 string.
  42. *
  43. * This function resizes \p outUtf16 to required size and
  44. * fill its contents with result UTF16 string if conversion success.
  45. * If conversion fails it guarantees not to change \p outUtf16.
  46. *
  47. * @param inUtf8 The source UTF8 string to be converted from.
  48. * @param outUtf16 The output string to hold the result UTF16s.
  49. * @return True if succeed, otherwise false.
  50. * @note Please check the return value before using \p outUtf16
  51. * e.g.
  52. * @code
  53. * std::u16string utf16;
  54. * bool ret = StringUtils::UTF8ToUTF16("你好hello", utf16);
  55. * if (ret) {
  56. * do_some_thing_with_utf16(utf16);
  57. * }
  58. * @endcode
  59. */
  60. CC_DLL bool UTF8ToUTF16(const std::string& inUtf8, std::u16string& outUtf16);
  61. /**
  62. * @brief Same as \a UTF8ToUTF16 but converts form UTF8 to UTF32.
  63. *
  64. * @see UTF8ToUTF16
  65. */
  66. CC_DLL bool UTF8ToUTF32(const std::string& inUtf8, std::u32string& outUtf32);
  67. /**
  68. * @brief Same as \a UTF8ToUTF16 but converts form UTF16 to UTF8.
  69. *
  70. * @see UTF8ToUTF16
  71. */
  72. CC_DLL bool UTF16ToUTF8(const std::u16string& inUtf16, std::string& outUtf8);
  73. /**
  74. * @brief Same as \a UTF8ToUTF16 but converts form UTF16 to UTF32.
  75. *
  76. * @see UTF8ToUTF16
  77. */
  78. CC_DLL bool UTF16ToUTF32(const std::u16string& inUtf16, std::u32string& outUtf32);
  79. /**
  80. * @brief Same as \a UTF8ToUTF16 but converts form UTF32 to UTF8.
  81. *
  82. * @see UTF8ToUTF16
  83. */
  84. CC_DLL bool UTF32ToUTF8(const std::u32string& inUtf32, std::string& outUtf8);
  85. /**
  86. * @brief Same as \a UTF8ToUTF16 but converts form UTF32 to UTF16.
  87. *
  88. * @see UTF8ToUTF16
  89. */
  90. CC_DLL bool UTF32ToUTF16(const std::u32string& inUtf32, std::u16string& outUtf16);
  91. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  92. /**
  93. * @brief convert jstring to utf8 std::string, same function with env->getStringUTFChars.
  94. * because getStringUTFChars can not pass special emoticon
  95. * @param env The JNI Env
  96. * @param srcjStr The jstring which want to convert
  97. * @param ret True if the conversion succeeds and the ret pointer isn't null
  98. * @returns the result of utf8 string
  99. */
  100. CC_DLL std::string getStringUTFCharsJNI(JNIEnv* env, jstring srcjStr, bool* ret = nullptr);
  101. /**
  102. * @brief create a jstring with utf8 std::string, same function with env->newStringUTF
  103. * because newStringUTF can not convert special emoticon
  104. * @param env The JNI Env
  105. * @param srcjStr The std::string which want to convert
  106. * @param ret True if the conversion succeeds and the ret pointer isn't null
  107. * @returns the result of jstring,the jstring need to DeleteLocalRef(jstring);
  108. */
  109. CC_DLL jstring newStringUTFJNI(JNIEnv* env, const std::string& utf8Str, bool* ret = nullptr);
  110. #endif
  111. /**
  112. * @brief Trims the unicode spaces at the end of char16_t vector.
  113. */
  114. CC_DLL void trimUTF16Vector(std::vector<char16_t>& str);
  115. /**
  116. * @brief Trims the unicode spaces at the end of char32_t vector.
  117. */
  118. CC_DLL void trimUTF32Vector(std::vector<char32_t>& str);
  119. /**
  120. * @brief Whether the character is a whitespace character.
  121. * @param ch The unicode character.
  122. * @returns Whether the character is a white space character.
  123. *
  124. * @see http://en.wikipedia.org/wiki/Whitespace_character#Unicode
  125. *
  126. */
  127. CC_DLL bool isUnicodeSpace(char32_t ch);
  128. /**
  129. * @brief Whether the character is a Chinese/Japanese/Korean character.
  130. * @param ch The unicode character.
  131. * @returns Whether the character is a Chinese character.
  132. *
  133. * @see http://www.searchtb.com/2012/04/chinese_encode.html
  134. * @see http://tieba.baidu.com/p/748765987
  135. *
  136. */
  137. CC_DLL bool isCJKUnicode(char32_t ch);
  138. /**
  139. * @brief Returns the length of the string in characters.
  140. * @param utf8 An UTF-8 encoded string.
  141. * @returns The length of the string in characters.
  142. */
  143. CC_DLL long getCharacterCountInUTF8String(const std::string& utf8);
  144. /**
  145. * @brief Gets the index of the last character that is not equal to the character given.
  146. * @param str The string to be searched.
  147. * @param c The character to be searched for.
  148. * @returns The index of the last character that is not \p c.
  149. */
  150. CC_DLL unsigned int getIndexOfLastNotChar16(const std::vector<char16_t>& str, char16_t c);
  151. /**
  152. * @brief Gets char16_t vector from a given utf16 string.
  153. */
  154. CC_DLL std::vector<char16_t> getChar16VectorFromUTF16String(const std::u16string& utf16);
  155. /**
  156. * Utf8 sequence
  157. * Store all utf8 chars as std::string
  158. * Build from std::string
  159. */
  160. class CC_DLL StringUTF8
  161. {
  162. public:
  163. struct CharUTF8
  164. {
  165. std::string _char;
  166. bool isAnsi() { return _char.size() == 1; }
  167. };
  168. typedef std::vector<CharUTF8> CharUTF8Store;
  169. StringUTF8();
  170. StringUTF8(const std::string& newStr);
  171. ~StringUTF8();
  172. std::size_t length() const;
  173. void replace(const std::string& newStr);
  174. std::string getAsCharSequence() const;
  175. bool deleteChar(std::size_t pos);
  176. bool insert(std::size_t pos, const std::string& insertStr);
  177. bool insert(std::size_t pos, const StringUTF8& insertStr);
  178. CharUTF8Store& getString() { return _str; }
  179. private:
  180. CharUTF8Store _str;
  181. };
  182. } // namespace StringUtils {
  183. /**
  184. * Returns the character count in UTF16 string.
  185. * @param str Pointer to the start of a UTF-16 encoded string. It must be an NULL terminal UTF8 string.
  186. * @deprecated Please use c++11 `std::u16string::length` instead, don't use `unsigned short*` directly.
  187. */
  188. CC_DEPRECATED_ATTRIBUTE CC_DLL int cc_wcslen(const unsigned short* str);
  189. /** Trims the space characters at the end of UTF8 string.
  190. * @deprecated Please use `StringUtils::trimUTF16Vector` instead.
  191. */
  192. CC_DEPRECATED_ATTRIBUTE CC_DLL void cc_utf8_trim_ws(std::vector<unsigned short>* str);
  193. /**
  194. * Whether the character is a whitespace character.
  195. *
  196. * @param ch The unicode character.
  197. * @returns Whether the character is a white space character.
  198. * @deprecated Please use `StringUtils::isUnicodeSpace` instead.
  199. *
  200. * @see http://en.wikipedia.org/wiki/Whitespace_character#Unicode
  201. */
  202. CC_DEPRECATED_ATTRIBUTE CC_DLL bool isspace_unicode(unsigned short ch);
  203. /**
  204. * Whether the character is a Chinese/Japanese/Korean character.
  205. *
  206. * @param ch The unicode character
  207. * @returns Whether the character is a Chinese character.
  208. * @deprecated Please use `StringUtils::isCJKUnicode` instead.
  209. *
  210. * @see http://www.searchtb.com/2012/04/chinese_encode.html
  211. * @see http://tieba.baidu.com/p/748765987
  212. */
  213. CC_DEPRECATED_ATTRIBUTE CC_DLL bool iscjk_unicode(unsigned short ch);
  214. /**
  215. * Returns the length of the string in characters.
  216. *
  217. * @param p Pointer to the start of a UTF-8 encoded string. It must be an NULL terminal UTF8 string.
  218. * @param max Not used from 3.1, just keep it for backward compatibility.
  219. * @deprecated Please use `StringUtils::getCharacterCountInUTF8String` instead.
  220. * @returns The length of the string in characters.
  221. **/
  222. CC_DEPRECATED_ATTRIBUTE CC_DLL long cc_utf8_strlen (const char * p, int max = -1);
  223. /**
  224. * Find the last character that is not equal to the character given.
  225. *
  226. * @param str The string to be searched.
  227. * @param c The character to be searched for.
  228. * @deprecated Please use `StringUtils::getIndexOfLastNotChar16` instead.
  229. * @returns The index of the last character that is not \p c.
  230. */
  231. CC_DEPRECATED_ATTRIBUTE CC_DLL unsigned int cc_utf8_find_last_not_char(const std::vector<unsigned short>& str, unsigned short c);
  232. /**
  233. * @brief Gets `unsigned short` vector from a given utf16 string.
  234. * @param str A given utf16 string.
  235. * @deprecated Please use `StringUtils::getChar16VectorFromUTF16String` instead.
  236. */
  237. CC_DEPRECATED_ATTRIBUTE CC_DLL std::vector<unsigned short> cc_utf16_vec_from_utf16_str(const unsigned short* str);
  238. /**
  239. * Creates an utf8 string from a c string. The result will be null terminated.
  240. *
  241. * @param str_old Pointer to the start of a C string. It must be an NULL terminal UTF8 string.
  242. * @param length Not used from 3.1, keep it just for backward compatibility.
  243. * @param rUtf16Size The character count in the return UTF16 string.
  244. * @deprecated Please use `StringUtils::UTF8ToUTF16` instead.
  245. * @returns The newly created utf16 string, it must be released with `delete[]`,
  246. * If an error occurs, %NULL will be returned.
  247. */
  248. CC_DEPRECATED_ATTRIBUTE CC_DLL unsigned short* cc_utf8_to_utf16(const char* str_old, int length = -1, int* rUtf16Size = nullptr);
  249. /**
  250. * Converts a string from UTF-16 to UTF-8. The result will be null terminated.
  251. *
  252. * @param utf16 An UTF-16 encoded string, It must be an NULL terminal UTF16 string.
  253. * @param len Not used from 3.1, keep it just for backward compatibility.
  254. * @param items_read Not used from 3.1, keep it just for backward compatibility.
  255. * @param items_written Not used from 3.1, keep it just for backward compatibility.
  256. * @deprecated Please use `StringUtils::UTF16ToUTF8` instead.
  257. * @returns A pointer to a newly allocated UTF-8 string. This value must be
  258. * released with `delete[]`. If an error occurs, %NULL will be returned.
  259. */
  260. CC_DEPRECATED_ATTRIBUTE CC_DLL char * cc_utf16_to_utf8 (const unsigned short *str,
  261. int len = -1,
  262. long *items_read = nullptr,
  263. long *items_written = nullptr);
  264. NS_CC_END
  265. #endif /** defined(__cocos2dx__ccUTF8__) */