2013-02-27 15:16:49 +08:00
|
|
|
//
|
|
|
|
// ccUTF8.h
|
|
|
|
// cocos2dx
|
|
|
|
//
|
|
|
|
// Created by James Chen on 2/27/13.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef __cocos2dx__ccUTF8__
|
|
|
|
#define __cocos2dx__ccUTF8__
|
|
|
|
|
2013-10-14 14:01:00 +08:00
|
|
|
#include "CCPlatformMacros.h"
|
2013-02-27 15:16:49 +08:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
CC_DLL int cc_wcslen(const unsigned short* str);
|
|
|
|
|
|
|
|
CC_DLL void cc_utf8_trim_ws(std::vector<unsigned short>* str);
|
|
|
|
|
2013-08-01 17:47:37 +08:00
|
|
|
/**
|
|
|
|
* Whether the character is a whitespace character.
|
2013-02-27 15:16:49 +08:00
|
|
|
*
|
2013-08-01 17:47:37 +08:00
|
|
|
* @param ch the unicode character
|
|
|
|
* @returns whether the character is a white space character.
|
2013-02-27 15:16:49 +08:00
|
|
|
*
|
2013-08-01 17:47:37 +08:00
|
|
|
* @see http://en.wikipedia.org/wiki/Whitespace_character#Unicode
|
2013-02-27 15:16:49 +08:00
|
|
|
* */
|
|
|
|
CC_DLL bool isspace_unicode(unsigned short ch);
|
|
|
|
|
2013-08-01 17:47:37 +08:00
|
|
|
/**
|
2013-02-27 15:45:23 +08:00
|
|
|
* Returns the length of the string in characters.
|
|
|
|
*
|
2013-08-01 17:47:37 +08:00
|
|
|
* @param p pointer to the start of a UTF-8 encoded string.
|
|
|
|
* @param max the maximum number of bytes to examine. If \p max is less than
|
|
|
|
* 0, then the string is assumed to be null-terminated. If \p max
|
2013-12-18 17:47:20 +08:00
|
|
|
* is 0, \p p will not be examined and my be %nullptr.
|
2013-08-01 17:47:37 +08:00
|
|
|
*
|
|
|
|
* @returns the length of the string in characters
|
2013-02-27 15:45:23 +08:00
|
|
|
**/
|
|
|
|
CC_DLL long
|
|
|
|
cc_utf8_strlen (const char * p, int max);
|
|
|
|
|
2013-08-01 17:47:37 +08:00
|
|
|
/**
|
|
|
|
* Find the last character that is not equal to the character given.
|
2013-02-27 15:16:49 +08:00
|
|
|
*
|
2013-08-01 17:47:37 +08:00
|
|
|
* @param str the string to be searched.
|
|
|
|
* @param c the character to be searched for.
|
|
|
|
*
|
|
|
|
* @returns the index of the last character that is not \p c.
|
2013-02-27 15:16:49 +08:00
|
|
|
* */
|
|
|
|
CC_DLL unsigned int cc_utf8_find_last_not_char(std::vector<unsigned short> str, unsigned short c);
|
|
|
|
|
|
|
|
CC_DLL std::vector<unsigned short> cc_utf16_vec_from_utf16_str(const unsigned short* str);
|
|
|
|
|
2013-08-01 17:47:37 +08:00
|
|
|
/**
|
2013-02-27 15:16:49 +08:00
|
|
|
* Creates a utf8 string from a cstring.
|
|
|
|
*
|
2013-08-01 17:47:37 +08:00
|
|
|
* @param str_old pointer to the start of a C string.
|
|
|
|
*
|
|
|
|
* @returns the newly created utf8 string.
|
2013-02-27 15:16:49 +08:00
|
|
|
* */
|
2013-12-18 17:47:20 +08:00
|
|
|
CC_DLL unsigned short* cc_utf8_to_utf16(const char* str_old, int length = -1, int* rUtf16Size = nullptr);
|
2013-02-27 15:16:49 +08:00
|
|
|
|
|
|
|
/**
|
2013-08-01 17:47:37 +08:00
|
|
|
* Convert a string from UTF-16 to UTF-8. The result will be null terminated.
|
2013-02-27 15:16:49 +08:00
|
|
|
*
|
2013-08-01 17:47:37 +08:00
|
|
|
* @param str a UTF-16 encoded string
|
|
|
|
* @param len the maximum length of \p str to use. If \p len < 0, then the
|
|
|
|
* string is null terminated.
|
2013-12-18 17:47:20 +08:00
|
|
|
* @param items_read location to store number of words read, or %nullptr.
|
|
|
|
* If %nullptr, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
|
2013-08-01 17:47:37 +08:00
|
|
|
* returned in case \p str contains a trailing partial
|
|
|
|
* character. If an error occurs then the index of the
|
|
|
|
* invalid input is stored here.
|
2013-12-18 17:47:20 +08:00
|
|
|
* @param items_written location to store number of bytes written, or %nullptr.
|
2013-08-01 17:47:37 +08:00
|
|
|
* The value stored here does not include the trailing null.
|
2013-02-27 15:16:49 +08:00
|
|
|
*
|
2013-08-01 17:47:37 +08:00
|
|
|
* @returns a pointer to a newly allocated UTF-8 string. This value must be
|
2013-12-18 17:47:20 +08:00
|
|
|
* freed with free(). If an error occurs, %nullptr will be returned.
|
2013-02-27 15:16:49 +08:00
|
|
|
**/
|
|
|
|
CC_DLL char *
|
|
|
|
cc_utf16_to_utf8 (const unsigned short *str,
|
|
|
|
long len,
|
|
|
|
long *items_read,
|
|
|
|
long *items_written);
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
#endif /* defined(__cocos2dx__ccUTF8__) */
|