ccUTF8.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /****************************************************************************
  2. Copyright (c) 2014 cocos2d-x.org
  3. Copyright (c) 2014-2016 Chukong Technologies Inc.
  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 __cocos2dx__ccUTF8__
  23. #define __cocos2dx__ccUTF8__
  24. #include "base/ccMacros.h"
  25. #include <vector>
  26. #include <string>
  27. #include <sstream>
  28. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  29. #include "platform/android/jni/JniHelper.h"
  30. #endif
  31. NS_CC_BEGIN
  32. namespace StringUtils {
  33. template<typename T>
  34. std::string toString(T arg)
  35. {
  36. std::stringstream ss;
  37. ss << arg;
  38. return ss.str();
  39. }
  40. std::string CC_DLL format(const char* format, ...) CC_FORMAT_PRINTF(1, 2);
  41. /**
  42. * @brief Converts from UTF8 string to UTF16 string.
  43. *
  44. * This function resizes \p outUtf16 to required size and
  45. * fill its contents with result UTF16 string if conversion success.
  46. * If conversion fails it guarantees not to change \p outUtf16.
  47. *
  48. * @param inUtf8 The source UTF8 string to be converted from.
  49. * @param outUtf16 The output string to hold the result UTF16s.
  50. * @return True if succeed, otherwise false.
  51. * @note Please check the return value before using \p outUtf16
  52. * e.g.
  53. * @code
  54. * std::u16string utf16;
  55. * bool ret = StringUtils::UTF8ToUTF16("你好hello", utf16);
  56. * if (ret) {
  57. * do_some_thing_with_utf16(utf16);
  58. * }
  59. * @endcode
  60. */
  61. CC_DLL bool UTF8ToUTF16(const std::string& inUtf8, std::u16string& outUtf16);
  62. /**
  63. * @brief Same as \a UTF8ToUTF16 but converts form UTF8 to UTF32.
  64. *
  65. * @see UTF8ToUTF16
  66. */
  67. CC_DLL bool UTF8ToUTF32(const std::string& inUtf8, std::u32string& outUtf32);
  68. /**
  69. * @brief Same as \a UTF8ToUTF16 but converts form UTF16 to UTF8.
  70. *
  71. * @see UTF8ToUTF16
  72. */
  73. CC_DLL bool UTF16ToUTF8(const std::u16string& inUtf16, std::string& outUtf8);
  74. /**
  75. * @brief Same as \a UTF8ToUTF16 but converts form UTF16 to UTF32.
  76. *
  77. * @see UTF8ToUTF16
  78. */
  79. CC_DLL bool UTF16ToUTF32(const std::u16string& inUtf16, std::u32string& outUtf32);
  80. /**
  81. * @brief Same as \a UTF8ToUTF16 but converts form UTF32 to UTF8.
  82. *
  83. * @see UTF8ToUTF16
  84. */
  85. CC_DLL bool UTF32ToUTF8(const std::u32string& inUtf32, std::string& outUtf8);
  86. /**
  87. * @brief Same as \a UTF8ToUTF16 but converts form UTF32 to UTF16.
  88. *
  89. * @see UTF8ToUTF16
  90. */
  91. CC_DLL bool UTF32ToUTF16(const std::u32string& inUtf32, std::u16string& outUtf16);
  92. /**
  93. * @brief Skip some bad char code.
  94. */
  95. CC_DLL void UTF8LooseFix(const std::string &in, std::string &out);
  96. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  97. /**
  98. * @brief convert jstring to utf8 std::string, same function with env->getStringUTFChars.
  99. * because getStringUTFChars can not pass special emoticon
  100. * @param env The JNI Env
  101. * @param srcjStr The jstring which want to convert
  102. * @param ret True if the conversion succeeds and the ret pointer isn't null
  103. * @returns the result of utf8 string
  104. */
  105. CC_DLL std::string getStringUTFCharsJNI(JNIEnv* env, jstring srcjStr, bool* ret = nullptr);
  106. /**
  107. * @brief create a jstring with utf8 std::string, same function with env->newStringUTF
  108. * because newStringUTF can not convert special emoticon
  109. * @param env The JNI Env
  110. * @param srcjStr The std::string which want to convert
  111. * @param ret True if the conversion succeeds and the ret pointer isn't null
  112. * @returns the result of jstring,the jstring need to DeleteLocalRef(jstring);
  113. */
  114. CC_DLL jstring newStringUTFJNI(JNIEnv* env, const std::string& utf8Str, bool* ret = nullptr);
  115. #endif
  116. /**
  117. * @brief Trims the unicode spaces at the end of char16_t vector.
  118. */
  119. CC_DLL void trimUTF16Vector(std::vector<char16_t>& str);
  120. /**
  121. * @brief Whether the character is a whitespace character.
  122. * @param ch The unicode character.
  123. * @returns Whether the character is a white space character.
  124. *
  125. * @see http://en.wikipedia.org/wiki/Whitespace_character#Unicode
  126. *
  127. */
  128. CC_DLL bool isUnicodeSpace(char16_t ch);
  129. /**
  130. * @brief Whether the character is a Chinese/Japanese/Korean character.
  131. * @param ch The unicode character.
  132. * @returns Whether the character is a Chinese character.
  133. *
  134. * @see http://www.searchtb.com/2012/04/chinese_encode.html
  135. * @see http://tieba.baidu.com/p/748765987
  136. *
  137. */
  138. CC_DLL bool isCJKUnicode(char16_t ch);
  139. /**
  140. * @brief Returns the length of the string in characters.
  141. * @param utf8 An UTF-8 encoded string.
  142. * @returns The length of the string in characters.
  143. */
  144. CC_DLL long getCharacterCountInUTF8String(const std::string& utf8);
  145. /**
  146. * @brief Gets the index of the last character that is not equal to the character given.
  147. * @param str The string to be searched.
  148. * @param c The character to be searched for.
  149. * @returns The index of the last character that is not \p c.
  150. */
  151. CC_DLL unsigned int getIndexOfLastNotChar16(const std::vector<char16_t>& str, char16_t c);
  152. /**
  153. * @brief Gets char16_t vector from a given utf16 string.
  154. */
  155. CC_DLL std::vector<char16_t> getChar16VectorFromUTF16String(const std::u16string& utf16);
  156. /**
  157. * Utf8 sequence
  158. * Store all utf8 chars as std::string
  159. * Build from std::string
  160. */
  161. class CC_DLL StringUTF8
  162. {
  163. public:
  164. struct CharUTF8
  165. {
  166. std::string _char;
  167. bool isAnsi() { return _char.size() == 1; }
  168. };
  169. typedef std::vector<CharUTF8> CharUTF8Store;
  170. StringUTF8();
  171. StringUTF8(const std::string& newStr);
  172. ~StringUTF8();
  173. std::size_t length() const;
  174. void replace(const std::string& newStr);
  175. std::string getAsCharSequence() const;
  176. bool deleteChar(std::size_t pos);
  177. bool insert(std::size_t pos, const std::string& insertStr);
  178. bool insert(std::size_t pos, const StringUTF8& insertStr);
  179. CharUTF8Store& getString() { return _str; }
  180. private:
  181. CharUTF8Store _str;
  182. };
  183. } // namespace StringUtils {
  184. NS_CC_END
  185. #endif /** defined(__cocos2dx__ccUTF8__) */