added: size_t UTF8ToInt(const char * utf8, int & res, bool & correct)

git-svn-id: svn://ttmath.org/publicrep/ezc/trunk@359 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
Tomasz Sowa 2011-08-27 19:14:20 +00:00
parent 542587e5e8
commit b4f7d4b8de
2 changed files with 47 additions and 1 deletions

View File

@ -143,6 +143,51 @@ return len;
/*!
this function converts one UTF-8 character into one wide-character
input:
utf8 - an input UTF-8 string (null terminated)
output:
res - an output character
correct - true if it is a correct character
the function returns how many characters have been used from the input string
(returns zero only if the string has '\0' at the first character)
even if there are errors the functions returns a different from zero value
*/
size_t UTF8ToInt(const char * utf8, int & res, bool & correct)
{
size_t i, len;
res = 0;
correct = false;
if( *utf8 == 0 )
return 0;
if( !UTF8ToInt_FirstOctet(utf8[0], len, res) )
return 1;
for(i=1 ; i<len ; ++i)
{
if( utf8[i] == 0 )
return i;
if( !UTF8ToInt_AddNextOctet(utf8[i], res) )
return i;
}
if( UTF8_CheckRange(res) )
correct = true;
return len;
}
/*! /*!
this function converts one UTF-8 character into one wide-character this function converts one UTF-8 character into one wide-character
@ -522,7 +567,7 @@ static size_t WideToInt(const wchar_t * wide_string, size_t string_len, int & z,
if( z2>=0xDC00 && z2<=0xDFFF ) if( z2>=0xDC00 && z2<=0xDFFF )
{ {
z = 0x10000 + ((z & 0x3FF) << 10) | (z2 & 0x3FF); z = 0x10000 + (((z & 0x3FF) << 10) | (z2 & 0x3FF));
return 2; return 2;
} }
else else

View File

@ -69,6 +69,7 @@ bool UTF8_CheckRange(int c);
converting one character from UTF-8 to an int converting one character from UTF-8 to an int
*/ */
size_t UTF8ToInt(const char * utf8, size_t utf8_len, int & res, bool & correct); size_t UTF8ToInt(const char * utf8, size_t utf8_len, int & res, bool & correct);
size_t UTF8ToInt(const char * utf8, int & res, bool & correct);
size_t UTF8ToInt(const std::string & utf8, int & res, bool & correct); size_t UTF8ToInt(const std::string & utf8, int & res, bool & correct);
size_t UTF8ToInt(std::istream & utf8, int & res, bool & correct); size_t UTF8ToInt(std::istream & utf8, int & res, bool & correct);