2010-09-18 13:46:25 +02:00
|
|
|
#include <wctype.h>
|
2010-09-17 17:15:21 +02:00
|
|
|
#include "wstring.hpp"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
wString::wString( const wchar_t *s ) :
|
|
|
|
std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >( s )
|
2010-09-17 17:15:21 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
wString::wString( const basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> > &ws ) :
|
|
|
|
basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> >( ws )
|
2010-09-17 17:15:21 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
wString::wString( const string &s )
|
2010-09-17 17:15:21 +02:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
std::string::size_type size;
|
2010-09-17 17:15:21 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
size = s.size();
|
|
|
|
resize( size );
|
|
|
|
for ( std::string::size_type i = 0; i < size; ++i )
|
|
|
|
( *this )[i] = ( unsigned char )s[i];
|
2010-09-17 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
wString &wString::operator=( const string & s )
|
2010-09-17 17:15:21 +02:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
std::string::size_type size;
|
2010-09-17 17:15:21 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
size = s.size();
|
|
|
|
this->resize( size );
|
|
|
|
for ( std::string::size_type i = 0; i < size; ++i )
|
|
|
|
( *this )[i] = ( unsigned char )s[i];
|
|
|
|
return *this;
|
2010-09-17 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
void wString::fromUTF8( const char *s )
|
2010-09-17 17:15:21 +02:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
size_t len = utf8Len( s );
|
|
|
|
|
|
|
|
clear();
|
|
|
|
if ( len == 0 )
|
|
|
|
return;
|
|
|
|
reserve( len );
|
|
|
|
for ( int i = 0; s[i] != 0; )
|
|
|
|
{
|
|
|
|
if ( ( s[i] & 0xF8 ) == 0xF0 )
|
|
|
|
{
|
|
|
|
push_back( ( ( wchar_t )( s[i] & 0x07 ) << 18 ) | ( ( wchar_t )( s[i + 1] & 0x3F ) << 12 ) | ( ( wchar_t )( s[i + 2] & 0x3F ) << 6 ) | ( wchar_t )( s[i + 3] & 0x3F ) );
|
|
|
|
i += 4;
|
|
|
|
}
|
|
|
|
else if ( ( s[i] & 0xF0 ) == 0xE0 )
|
|
|
|
{
|
|
|
|
push_back( ( ( wchar_t )( s[i] & 0x0F ) << 12 ) | ( ( wchar_t )( s[i + 1] & 0x3F ) << 6 ) | ( wchar_t )( s[i + 2] & 0x3F ) );
|
|
|
|
i += 3;
|
|
|
|
}
|
|
|
|
else if ( ( s[i] & 0xE0 ) == 0xC0 )
|
|
|
|
{
|
|
|
|
push_back( ( ( wchar_t )( s[i] & 0x1F ) << 6 ) | ( wchar_t )( s[i + 1] & 0x3F ) );
|
|
|
|
i += 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
push_back( ( wchar_t )s[i] );
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
2010-09-17 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
string wString::toUTF8( void ) const
|
2010-09-17 17:15:21 +02:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
string s;
|
|
|
|
size_t len = 0;
|
|
|
|
wchar_t wc;
|
|
|
|
|
|
|
|
for ( size_t i = 0; i < size(); ++i )
|
|
|
|
{
|
|
|
|
wc = operator[]( i );
|
|
|
|
if ( wc < 0x80 )
|
|
|
|
++len;
|
|
|
|
else if ( wc < 0x800 )
|
|
|
|
len += 2;
|
|
|
|
else if ( wc < 0x10000 )
|
|
|
|
len += 3;
|
|
|
|
else
|
|
|
|
len += 4;
|
|
|
|
}
|
|
|
|
s.reserve( len );
|
|
|
|
for ( size_t i = 0; i < size(); ++i )
|
|
|
|
{
|
|
|
|
wc = operator[]( i );
|
|
|
|
if ( wc < 0x80 )
|
|
|
|
s.push_back( ( char )wc );
|
|
|
|
else if ( wc < 0x800 )
|
|
|
|
{
|
|
|
|
s.push_back( ( char )( ( wc >> 6 ) | 0xC0 ) );
|
|
|
|
s.push_back( ( char )( ( wc & 0x3F ) | 0x80 ) );
|
|
|
|
}
|
|
|
|
else if ( wc < 0x10000 )
|
|
|
|
{
|
|
|
|
s.push_back( ( char )( ( wc >> 12 ) | 0xE0 ) );
|
|
|
|
s.push_back( ( char )( ( ( wc >> 6 ) & 0x3F ) | 0x80 ) );
|
|
|
|
s.push_back( ( char )( ( wc & 0x3F ) | 0x80 ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
s.push_back( ( char )( ( ( wc >> 18 ) & 0x07 ) | 0xF0 ) );
|
|
|
|
s.push_back( ( char )( ( ( wc >> 12 ) & 0x3F ) | 0x80 ) );
|
|
|
|
s.push_back( ( char )( ( ( wc >> 6 ) & 0x3F ) | 0x80 ) );
|
|
|
|
s.push_back( ( char )( ( wc & 0x3F ) | 0x80 ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s;
|
2010-09-17 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
size_t utf8Len( const char *s )
|
2010-09-17 17:15:21 +02:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
size_t len = 0;
|
|
|
|
|
|
|
|
for ( int i = 0; s[i] != 0; )
|
|
|
|
{
|
|
|
|
if ( ( s[i] & 0xF8 ) == 0xF0 )
|
|
|
|
{
|
|
|
|
if ( ( ( s[i + 1] & 0xC0 ) != 0x80 ) || ( ( s[i + 2] & 0xC0 ) != 0x80 ) || ( ( s[i + 3] & 0xC0 ) != 0x80 ) )
|
|
|
|
return 0;
|
|
|
|
++len;
|
|
|
|
i += 4;
|
|
|
|
}
|
|
|
|
else if ( ( s[i] & 0xF0 ) == 0xE0 )
|
|
|
|
{
|
|
|
|
if ( ( ( s[i + 1] & 0xC0 ) != 0x80 ) || ( ( s[i + 2] & 0xC0 ) != 0x80 ) )
|
|
|
|
return 0;
|
|
|
|
++len;
|
|
|
|
i += 3;
|
|
|
|
}
|
|
|
|
else if ( ( s[i] & 0xE0 ) == 0xC0 )
|
|
|
|
{
|
|
|
|
if ( ( ( s[i + 1] & 0xC0 ) != 0x80 ) )
|
|
|
|
return 0;
|
|
|
|
++len;
|
|
|
|
i += 2;
|
|
|
|
}
|
|
|
|
else if ( ( s[i] & 0x80 ) == 0x00 )
|
|
|
|
{
|
|
|
|
++len;
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return len;
|
2010-09-17 17:15:21 +02:00
|
|
|
}
|
2010-09-18 13:46:25 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
int wcsnicmp( const wchar_t *s1, const wchar_t *s2, int len )
|
2010-09-18 13:46:25 +02:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( len <= 0 )
|
|
|
|
return ( 0 );
|
2010-09-18 13:46:25 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
do
|
2010-09-18 13:46:25 +02:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
int r = towupper( *s1 ) - towupper( *s2++ );
|
|
|
|
if ( r ) return r;
|
|
|
|
if ( *s1++ == 0 )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
while ( --len != 0 );
|
|
|
|
|
|
|
|
return ( 0 );
|
2010-09-18 13:46:25 +02:00
|
|
|
}
|