00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "CeylanUnicode.h"
00028
00029
00030 #include "CeylanUtils.h"
00031 #include "CeylanOperators.h"
00032
00033
00034 using std::string ;
00035
00036 using namespace Ceylan ;
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 UnicodeString::UnicodeString() :
00048 _buffer( 0 )
00049 {
00050
00051 }
00052
00053
00054
00055 UnicodeString::~UnicodeString() throw()
00056 {
00057
00058 if ( _buffer != 0 )
00059 delete [] _buffer ;
00060
00061 }
00062
00063
00064
00065 Ceylan::StringSize UnicodeString::size() const
00066 {
00067
00068
00069
00070
00071
00072
00073
00074 if ( _buffer == 0 )
00075 return 0 ;
00076
00077 Ceylan::StringSize res = 0 ;
00078
00079 Unicode * p = _buffer ;
00080
00081 while ( *( p++ ) )
00082 res++ ;
00083
00084 return res ;
00085
00086 }
00087
00088
00089
00090 UnicodeString & UnicodeString::copy( bool swap ) const
00091 {
00092
00093 Ceylan::StringSize sourceSize = size() ;
00094
00095 UnicodeString & res = * new UnicodeString() ;
00096
00097 if ( sourceSize == 0 )
00098 return res ;
00099
00100 res.setCapacity( sourceSize ) ;
00101
00102 Unicode * from = _buffer ;
00103 Unicode * to = res._buffer ;
00104
00105 if ( swap )
00106 {
00107 while ( *from != 0 )
00108 {
00109 *to = Ceylan::swapBytes( *from ) ;
00110 from++ ;
00111 to++ ;
00112 }
00113 }
00114 else
00115 {
00116 while ( *from != 0 )
00117 {
00118 *to = *from ;
00119 from++ ;
00120 to++ ;
00121 }
00122 }
00123
00124 *to = '\0' ;
00125
00126 return res ;
00127
00128 }
00129
00130
00131
00132 void UnicodeString::setFromLatin1( const std::string & latin1String )
00133 {
00134
00135 setCapacity( latin1String.size() ) ;
00136
00137 Ceylan::StringSize i = 0 ;
00138 for ( string::const_iterator it = latin1String.begin();
00139 it != latin1String.end(); it++ )
00140 {
00141 _buffer[i] = static_cast<unsigned char>( *it ) ;
00142 i++ ;
00143 }
00144 _buffer[i] = 0 ;
00145
00146 }
00147
00148
00149
00150 void UnicodeString::setFromUTF8( const std::string & utf8String,
00151 Ceylan::StringSize characterCount )
00152 {
00153
00154
00155
00156
00157
00158
00159
00160 setCapacity( characterCount ) ;
00161
00162 Unicode uniChar ;
00163 Ceylan::StringSize index = 0 ;
00164
00165
00166 for ( string::const_iterator it = utf8String.begin();
00167 it != utf8String.end(); it++ )
00168 {
00169 uniChar = static_cast<unsigned char>( *it ) ;
00170
00171 if ( uniChar >= 0xF0 )
00172 {
00173 uniChar = static_cast<Unicode>( (*it) & 0x07 ) << 18 ;
00174 it++ ;
00175 uniChar |= static_cast<Unicode>( (*it) & 0x3F ) << 12 ;
00176 it++ ;
00177 uniChar |= static_cast<Unicode>( (*it) & 0x3F ) << 6 ;
00178 it++ ;
00179 uniChar |= static_cast<Unicode>( (*it) & 0x3F ) ;
00180
00181 }
00182 else
00183 {
00184 if ( uniChar >= 0xE0 )
00185 {
00186 uniChar = static_cast<Unicode>( (*it) & 0x0F ) << 12 ;
00187 it++ ;
00188 uniChar |= static_cast<Unicode>( (*it) & 0x3F ) << 6 ;
00189 it++ ;
00190 uniChar |= static_cast<Unicode>( (*it) & 0x3F ) ;
00191 }
00192 else
00193 {
00194 if ( uniChar >= 0xC0 )
00195 {
00196 uniChar = static_cast<Unicode>( (*it) & 0x1F ) << 6 ;
00197 uniChar |= static_cast<Unicode>( (*it) & 0x3F ) ;
00198 }
00199
00200 }
00201 }
00202
00203 _buffer[index] = uniChar ;
00204 index++ ;
00205 }
00206
00207 _buffer[index] = 0 ;
00208
00209 }
00210
00211
00212
00213 const string UnicodeString::toString( Ceylan::VerbosityLevels level ) const
00214 {
00215
00216 return "Unicode string made of " + Ceylan::toString(
00217 static_cast<Ceylan::Uint32>( size() ) )
00218 + " characters" ;
00219
00220 }
00221
00222
00223
00224 Unicode UnicodeString::ConvertFromLatin1( Ceylan::Latin1Char latin1Char )
00225 {
00226
00227 return static_cast<Unicode>( latin1Char ) ;
00228
00229 }
00230
00231
00232
00233 void UnicodeString::setCapacity( Ceylan::StringSize newSize )
00234 {
00235
00236 if ( newSize == size() )
00237 return ;
00238
00239 if ( _buffer != 0 )
00240 delete [] _buffer ;
00241
00242 _buffer = new Unicode[ newSize ] ;
00243
00244 }
00245