changed in utf8: functions taking WTextStream now take a template TextStreamBase
git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@1157 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
parent
da4ed9c1f8
commit
6c4a76baad
167
utf8/utf8.cpp
167
utf8/utf8.cpp
|
@ -277,60 +277,6 @@ static void IntToWide(int c, std::wstring & res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void IntToWide(int c, WTextStream & res)
|
|
||||||
{
|
|
||||||
if( sizeof(wchar_t)==2 && c>0xffff )
|
|
||||||
{
|
|
||||||
// UTF16 surrogate pairs
|
|
||||||
c -= 0x10000;
|
|
||||||
res << static_cast<wchar_t>(((c >> 10) & 0x3FF) + 0xD800);
|
|
||||||
res << static_cast<wchar_t>((c & 0x3FF) + 0xDC00);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
res << static_cast<wchar_t>(c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static bool UTF8ToWideGeneric(const char * utf8, size_t utf8_len, int mode, auto convert_function)
|
|
||||||
{
|
|
||||||
int z;
|
|
||||||
size_t len;
|
|
||||||
bool correct, was_error = false;
|
|
||||||
|
|
||||||
while( utf8_len > 0 )
|
|
||||||
{
|
|
||||||
if( (unsigned char)*utf8 <= 0x7f )
|
|
||||||
{
|
|
||||||
// small optimization
|
|
||||||
len = 1;
|
|
||||||
correct = true;
|
|
||||||
z = static_cast<unsigned char>(*utf8);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
len = UTF8ToInt(utf8, utf8_len, z, correct); // the len will be different from zero
|
|
||||||
}
|
|
||||||
|
|
||||||
if( !correct )
|
|
||||||
{
|
|
||||||
if( mode == 1 )
|
|
||||||
convert_function(0xFFFD); // U+FFFD "replacement character"
|
|
||||||
|
|
||||||
was_error = true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
convert_function(z);
|
|
||||||
}
|
|
||||||
|
|
||||||
utf8 += len;
|
|
||||||
utf8_len -= len;
|
|
||||||
}
|
|
||||||
|
|
||||||
return !was_error;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
@ -353,7 +299,7 @@ bool UTF8ToWide(const char * utf8, size_t utf8_len, std::wstring & res, bool cle
|
||||||
if( clear )
|
if( clear )
|
||||||
res.clear();
|
res.clear();
|
||||||
|
|
||||||
bool status = UTF8ToWideGeneric(utf8, utf8_len, mode, [&res](int c) {
|
bool status = private_namespace::UTF8ToWideGeneric(utf8, utf8_len, mode, [&res](int c) {
|
||||||
IntToWide(c, res);
|
IntToWide(c, res);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -452,66 +398,6 @@ return !was_error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// need to be tested
|
|
||||||
bool UTF8ToWide(const char * utf8, size_t utf8_len, WTextStream & res, bool clear, int mode)
|
|
||||||
{
|
|
||||||
if( clear )
|
|
||||||
res.clear();
|
|
||||||
|
|
||||||
bool status = UTF8ToWideGeneric(utf8, utf8_len, mode, [&res](int c) {
|
|
||||||
IntToWide(c, res);
|
|
||||||
});
|
|
||||||
|
|
||||||
return status;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// need to be tested
|
|
||||||
bool UTF8ToWide(const char * utf8, WTextStream & res, bool clear, int mode)
|
|
||||||
{
|
|
||||||
size_t utf8_len = 0;
|
|
||||||
|
|
||||||
while( utf8[utf8_len] != 0 )
|
|
||||||
utf8_len += 1;
|
|
||||||
|
|
||||||
return UTF8ToWide(utf8, utf8_len, res, clear, mode);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// need to be tested
|
|
||||||
bool UTF8ToWide(const std::string & utf8, WTextStream & res, bool clear, int mode)
|
|
||||||
{
|
|
||||||
return UTF8ToWide(utf8.c_str(), utf8.size(), res, clear, mode);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// need to be tested
|
|
||||||
bool UTF8ToWide(std::istream & utf8, WTextStream & res, bool clear, int mode)
|
|
||||||
{
|
|
||||||
int z;
|
|
||||||
bool correct, was_error = false;
|
|
||||||
|
|
||||||
if( clear )
|
|
||||||
res.clear();
|
|
||||||
|
|
||||||
while( UTF8ToInt(utf8, z, correct) > 0 )
|
|
||||||
{
|
|
||||||
if( !correct )
|
|
||||||
{
|
|
||||||
if( mode == 1 )
|
|
||||||
res << 0xFFFD; // U+FFFD "replacement character"
|
|
||||||
|
|
||||||
was_error = true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
IntToWide(z, res);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return !was_error;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
@ -1262,57 +1148,6 @@ return res;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// not tested
|
|
||||||
// FIX ME it is not using surrogate pairs from input stream
|
|
||||||
// and mode parameter
|
|
||||||
static void WideToUTF8Generic(PT::WTextStream & buffer, int mode, auto write_function)
|
|
||||||
{
|
|
||||||
char utf8_buffer[256];
|
|
||||||
std::size_t buffer_len = sizeof(utf8_buffer) / sizeof(char);
|
|
||||||
std::size_t utf8_sequence_max_length = 10;
|
|
||||||
std::size_t index = 0;
|
|
||||||
|
|
||||||
PT::WTextStream::const_iterator i = buffer.begin();
|
|
||||||
|
|
||||||
while( i != buffer.end() )
|
|
||||||
{
|
|
||||||
if( index + utf8_sequence_max_length > buffer_len )
|
|
||||||
{
|
|
||||||
write_function(utf8_buffer, index);
|
|
||||||
index = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
index += PT::IntToUTF8(*i, utf8_buffer + index, buffer_len - index);
|
|
||||||
++i;
|
|
||||||
}
|
|
||||||
|
|
||||||
if( index > 0 )
|
|
||||||
{
|
|
||||||
write_function(utf8_buffer, index);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// not tested
|
|
||||||
void WideToUTF8(PT::WTextStream & buffer, std::string & utf8, bool clear, int mode)
|
|
||||||
{
|
|
||||||
if( clear )
|
|
||||||
utf8.clear();
|
|
||||||
|
|
||||||
WideToUTF8Generic(buffer, mode, [&utf8](const char * utf8_buffer, std::size_t buffer_len){
|
|
||||||
utf8.append(utf8_buffer, buffer_len);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// not tested
|
|
||||||
void WideToUTF8(PT::WTextStream & buffer, std::ostream & utf8, int mode)
|
|
||||||
{
|
|
||||||
WideToUTF8Generic(buffer, mode, [&utf8](const char * utf8_buffer, std::size_t buffer_len){
|
|
||||||
utf8.write(utf8_buffer, buffer_len);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
204
utf8/utf8.h
204
utf8/utf8.h
|
@ -90,11 +90,13 @@ bool UTF8ToWide(std::istream & utf8, std::wstring & res, bool cle
|
||||||
converting UTF-8 string to a WTextStream stream
|
converting UTF-8 string to a WTextStream stream
|
||||||
(need to be tested)
|
(need to be tested)
|
||||||
*/
|
*/
|
||||||
|
/*
|
||||||
|
implemented as templates below
|
||||||
bool UTF8ToWide(const char * utf8, size_t utf8_len, WTextStream & res, bool clear = true, int mode = 1);
|
bool UTF8ToWide(const char * utf8, size_t utf8_len, WTextStream & res, bool clear = true, int mode = 1);
|
||||||
bool UTF8ToWide(const char * utf8, WTextStream & res, bool clear = true, int mode = 1);
|
bool UTF8ToWide(const char * utf8, WTextStream & res, bool clear = true, int mode = 1);
|
||||||
bool UTF8ToWide(const std::string & utf8, WTextStream & res, bool clear = true, int mode = 1);
|
bool UTF8ToWide(const std::string & utf8, WTextStream & res, bool clear = true, int mode = 1);
|
||||||
bool UTF8ToWide(std::istream & utf8, WTextStream & res, bool clear = true, int mode = 1);
|
bool UTF8ToWide(std::istream & utf8, WTextStream & res, bool clear = true, int mode = 1);
|
||||||
|
*/
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
converting one int character to UTF-8
|
converting one int character to UTF-8
|
||||||
|
@ -110,12 +112,16 @@ size_t IntToUTF8(int z, std::ostream & utf8);
|
||||||
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, std::string & utf8, bool clear = true, int mode = 1);
|
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, std::string & utf8, bool clear = true, int mode = 1);
|
||||||
bool WideToUTF8(const wchar_t * wide_string, std::string & utf8, bool clear = true, int mode = 1);
|
bool WideToUTF8(const wchar_t * wide_string, std::string & utf8, bool clear = true, int mode = 1);
|
||||||
bool WideToUTF8(const std::wstring & wide_string, std::string & utf8, bool clear = true, int mode = 1);
|
bool WideToUTF8(const std::wstring & wide_string, std::string & utf8, bool clear = true, int mode = 1);
|
||||||
void WideToUTF8(PT::WTextStream & buffer, std::string & utf8, bool clear = true, int mode = 1);// not tested
|
|
||||||
|
// implemented as a template below
|
||||||
|
//void WideToUTF8(PT::WTextStream & buffer, std::string & utf8, bool clear = true, int mode = 1);// not tested
|
||||||
|
|
||||||
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, std::ostream & utf8, int mode = 1);
|
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, std::ostream & utf8, int mode = 1);
|
||||||
bool WideToUTF8(const wchar_t * wide_string, std::ostream & utf8, int mode = 1);
|
bool WideToUTF8(const wchar_t * wide_string, std::ostream & utf8, int mode = 1);
|
||||||
bool WideToUTF8(const std::wstring & wide_string, std::ostream & utf8, int mode = 1);
|
bool WideToUTF8(const std::wstring & wide_string, std::ostream & utf8, int mode = 1);
|
||||||
void WideToUTF8(PT::WTextStream & buffer, std::ostream & utf8, int mode = 1);// not tested
|
|
||||||
|
// implemented as a template below
|
||||||
|
//void WideToUTF8(PT::WTextStream & buffer, std::ostream & utf8, int mode = 1);// not tested
|
||||||
|
|
||||||
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
|
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
|
||||||
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
|
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
|
||||||
|
@ -129,6 +135,198 @@ bool WideToUTF8(const std::wstring & wide_string, char * utf8, siz
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
namespace private_namespace
|
||||||
|
{
|
||||||
|
template<typename function_type>
|
||||||
|
bool UTF8ToWideGeneric(const char * utf8, size_t utf8_len, int mode, function_type convert_function)
|
||||||
|
{
|
||||||
|
int z;
|
||||||
|
size_t len;
|
||||||
|
bool correct, was_error = false;
|
||||||
|
|
||||||
|
while( utf8_len > 0 )
|
||||||
|
{
|
||||||
|
if( (unsigned char)*utf8 <= 0x7f )
|
||||||
|
{
|
||||||
|
// small optimization
|
||||||
|
len = 1;
|
||||||
|
correct = true;
|
||||||
|
z = static_cast<unsigned char>(*utf8);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
len = UTF8ToInt(utf8, utf8_len, z, correct); // the len will be different from zero
|
||||||
|
}
|
||||||
|
|
||||||
|
if( !correct )
|
||||||
|
{
|
||||||
|
if( mode == 1 )
|
||||||
|
convert_function(0xFFFD); // U+FFFD "replacement character"
|
||||||
|
|
||||||
|
was_error = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
convert_function(z);
|
||||||
|
}
|
||||||
|
|
||||||
|
utf8 += len;
|
||||||
|
utf8_len -= len;
|
||||||
|
}
|
||||||
|
|
||||||
|
return !was_error;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
template<typename char_type, size_t stack_size, size_t heap_block_size>
|
||||||
|
void IntToWide(int c, TextStreamBase<char_type, stack_size, heap_block_size> & res)
|
||||||
|
{
|
||||||
|
if( sizeof(wchar_t)==2 && c>0xffff )
|
||||||
|
{
|
||||||
|
// UTF16 surrogate pairs
|
||||||
|
c -= 0x10000;
|
||||||
|
res << static_cast<wchar_t>(((c >> 10) & 0x3FF) + 0xD800);
|
||||||
|
res << static_cast<wchar_t>((c & 0x3FF) + 0xDC00);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
res << static_cast<wchar_t>(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// not tested
|
||||||
|
// FIX ME it is not using surrogate pairs from input stream
|
||||||
|
// and mode parameter
|
||||||
|
template<typename char_type, size_t stack_size, size_t heap_block_size, typename function_type>
|
||||||
|
void WideToUTF8Generic(TextStreamBase<char_type, stack_size, heap_block_size> & buffer, int mode, function_type write_function)
|
||||||
|
{
|
||||||
|
char utf8_buffer[256];
|
||||||
|
std::size_t buffer_len = sizeof(utf8_buffer) / sizeof(char);
|
||||||
|
std::size_t utf8_sequence_max_length = 10;
|
||||||
|
std::size_t index = 0;
|
||||||
|
|
||||||
|
typename TextStreamBase<char_type, stack_size, heap_block_size>::const_iterator i = buffer.begin();
|
||||||
|
|
||||||
|
while( i != buffer.end() )
|
||||||
|
{
|
||||||
|
if( index + utf8_sequence_max_length > buffer_len )
|
||||||
|
{
|
||||||
|
write_function(utf8_buffer, index);
|
||||||
|
index = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
index += PT::IntToUTF8(*i, utf8_buffer + index, buffer_len - index);
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
|
||||||
|
if( index > 0 )
|
||||||
|
{
|
||||||
|
write_function(utf8_buffer, index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// need to be tested
|
||||||
|
template<typename char_type, size_t stack_size, size_t heap_block_size>
|
||||||
|
bool UTF8ToWide(const char * utf8, size_t utf8_len, TextStreamBase<char_type, stack_size, heap_block_size> & res, bool clear = true, int mode = 1)
|
||||||
|
{
|
||||||
|
if( clear )
|
||||||
|
res.clear();
|
||||||
|
|
||||||
|
bool status = private_namespace::UTF8ToWideGeneric(utf8, utf8_len, mode, [&res](int c) {
|
||||||
|
private_namespace::IntToWide(c, res);
|
||||||
|
});
|
||||||
|
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// need to be tested
|
||||||
|
template<typename char_type, size_t stack_size, size_t heap_block_size>
|
||||||
|
bool UTF8ToWide(const char * utf8, TextStreamBase<char_type, stack_size, heap_block_size> & res, bool clear = true, int mode = 1)
|
||||||
|
{
|
||||||
|
size_t utf8_len = 0;
|
||||||
|
|
||||||
|
while( utf8[utf8_len] != 0 )
|
||||||
|
utf8_len += 1;
|
||||||
|
|
||||||
|
return UTF8ToWide(utf8, utf8_len, res, clear, mode);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// need to be tested
|
||||||
|
template<typename char_type, size_t stack_size, size_t heap_block_size>
|
||||||
|
bool UTF8ToWide(const std::string & utf8, TextStreamBase<char_type, stack_size, heap_block_size> & res, bool clear = true, int mode = 1)
|
||||||
|
{
|
||||||
|
return UTF8ToWide(utf8.c_str(), utf8.size(), res, clear, mode);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// need to be tested
|
||||||
|
template<typename char_type, size_t stack_size, size_t heap_block_size>
|
||||||
|
bool UTF8ToWide(std::istream & utf8, TextStreamBase<char_type, stack_size, heap_block_size> & res, bool clear = true, int mode = 1)
|
||||||
|
{
|
||||||
|
int z;
|
||||||
|
bool correct, was_error = false;
|
||||||
|
|
||||||
|
if( clear )
|
||||||
|
res.clear();
|
||||||
|
|
||||||
|
while( UTF8ToInt(utf8, z, correct) > 0 )
|
||||||
|
{
|
||||||
|
if( !correct )
|
||||||
|
{
|
||||||
|
if( mode == 1 )
|
||||||
|
res << 0xFFFD; // U+FFFD "replacement character"
|
||||||
|
|
||||||
|
was_error = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
private_namespace::IntToWide(z, res);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return !was_error;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// not tested
|
||||||
|
template<typename char_type, size_t stack_size, size_t heap_block_size>
|
||||||
|
void WideToUTF8(TextStreamBase<char_type, stack_size, heap_block_size> & buffer, std::string & utf8, bool clear = true, int mode = 1)
|
||||||
|
{
|
||||||
|
if( clear )
|
||||||
|
utf8.clear();
|
||||||
|
|
||||||
|
private_namespace::WideToUTF8Generic(buffer, mode, [&utf8](const char * utf8_buffer, std::size_t buffer_len){
|
||||||
|
utf8.append(utf8_buffer, buffer_len);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// not tested
|
||||||
|
template<typename char_type, size_t stack_size, size_t heap_block_size>
|
||||||
|
void WideToUTF8(TextStreamBase<char_type, stack_size, heap_block_size> & buffer, std::ostream & utf8, int mode = 1)
|
||||||
|
{
|
||||||
|
private_namespace::WideToUTF8Generic(buffer, mode, [&utf8](const char * utf8_buffer, std::size_t buffer_len){
|
||||||
|
utf8.write(utf8_buffer, buffer_len);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue