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 #include <string.h>
00026 #include "ustring.hpp"
00027
00028
00029 const uint32_t UString::npos = 0xffffffff;
00030
00031
00032 UString::UString( const UString &rOther ): SkinObject( rOther.getIntf() )
00033 {
00034 m_length = rOther.m_length;
00035 m_pString = new uint32_t[m_length + 1];
00036 memcpy( m_pString, rOther.m_pString, 4 * m_length + 4);
00037 }
00038
00039
00040 UString::UString( intf_thread_t *pIntf, const char *pString ):
00041 SkinObject( pIntf )
00042 {
00043
00044 const char *pCur = pString;
00045 for( m_length = 0; pCur && *pCur; m_length++ )
00046 {
00047 if( (*pCur & 0xfc) == 0xfc )
00048 {
00049 pCur += 6;
00050 }
00051 else if ( (*pCur & 0xf8 ) == 0xf8 )
00052 {
00053 pCur += 5;
00054 }
00055 else if ( (*pCur & 0xf0 ) == 0xf0 )
00056 {
00057 pCur += 4;
00058 }
00059 else if ( (*pCur & 0xe0 ) == 0xe0 )
00060 {
00061 pCur += 3;
00062 }
00063 else if ( (*pCur & 0xc0 ) == 0xc0 )
00064 {
00065 pCur += 2;
00066 }
00067 else
00068 {
00069 pCur++;
00070 }
00071 }
00072 if( !pCur || *pCur )
00073 {
00074 msg_Err( pIntf, "Invalid UTF8 string: %s", pString );
00075 m_length = 0;
00076 m_pString = NULL;
00077 return;
00078 }
00079
00080 m_pString = new uint32_t[m_length + 1];
00081
00082
00083 pCur = pString;
00084 uint32_t aChar = 0;
00085 int remaining = 0;
00086 for( uint32_t i = 0; i <= m_length; i++ )
00087 {
00088 if( (*pCur & 0xfc) == 0xfc )
00089 {
00090 aChar = *pCur & 1;
00091 remaining = 5;
00092 }
00093 else if ( (*pCur & 0xf8 ) == 0xf8 )
00094 {
00095 aChar = *pCur & 3;
00096 remaining = 4;
00097 }
00098 else if ( (*pCur & 0xf0 ) == 0xf0 )
00099 {
00100 aChar = *pCur & 7;
00101 remaining = 3;
00102 }
00103 else if ( (*pCur & 0xe0 ) == 0xe0 )
00104 {
00105 aChar = *pCur & 15;
00106 remaining = 2;
00107 }
00108 else if ( (*pCur & 0xc0 ) == 0xc0 )
00109 {
00110 aChar = *pCur & 31;
00111 remaining = 1;
00112 }
00113 else
00114 {
00115 aChar = *pCur;
00116 remaining = 0;
00117 }
00118 while( remaining )
00119 {
00120 pCur++;
00121 remaining--;
00122 aChar = ( aChar << 6 ) | ( *pCur & 0x3f );
00123 }
00124 m_pString[i] = aChar;
00125 pCur++;
00126 }
00127 m_pString[m_length] = 0;
00128 }
00129
00130
00131 UString::~UString()
00132 {
00133 if( m_pString )
00134 {
00135 delete[] m_pString;
00136 }
00137 }
00138
00139
00140 bool UString::operator ==( const UString &rOther ) const
00141 {
00142 if( size() != rOther.size() )
00143 {
00144 return false;
00145 }
00146
00147 for( uint32_t i = 0; i < size(); i++ )
00148 {
00149 if( m_pString[i] != rOther.m_pString[i] )
00150 {
00151 return false;
00152 }
00153 }
00154
00155 return true;
00156 }
00157
00158
00159 bool UString::operator !=( const UString &rOther ) const
00160 {
00161 return !(*this == rOther);
00162 }
00163
00164
00165 bool UString::operator <( const UString &rOther ) const
00166 {
00167 const uint32_t *pOther = rOther.u_str();
00168 uint32_t i;
00169 for( i = 0; i < __MIN(m_length, rOther.length()); i++ )
00170 {
00171 if( m_pString[i] < pOther[i] )
00172 {
00173 return true;
00174 }
00175 else if( m_pString[i] > pOther[i] )
00176 {
00177 return false;
00178 }
00179 }
00180 return( m_pString[i] < pOther[i] );
00181 }
00182
00183
00184 bool UString::operator <=( const UString &rOther ) const
00185 {
00186 return !( rOther < *this );
00187 }
00188
00189
00190 bool UString::operator >( const UString &rOther ) const
00191 {
00192 return ( rOther < *this );
00193 }
00194
00195
00196 bool UString::operator >=( const UString &rOther ) const
00197 {
00198 return !( *this < rOther );
00199 }
00200
00201
00202 void UString::operator =( const UString &rOther )
00203 {
00204 m_length = rOther.m_length;
00205 delete[] m_pString;
00206 m_pString = new uint32_t[size() + 1];
00207 for( uint32_t i = 0; i <= size(); i++ )
00208 {
00209 m_pString[i] = rOther.m_pString[i];
00210 }
00211 }
00212
00213
00214 void UString::operator +=( const UString &rOther )
00215 {
00216 int tempLength = this->length() + rOther.length();
00217 uint32_t *pTempString = new uint32_t[tempLength + 1];
00218
00219 memcpy( pTempString, this->m_pString, 4 * this->size() );
00220
00221
00222
00223 for( uint32_t i = 0; i < rOther.size(); i++ )
00224 {
00225 pTempString[this->size() + i] = rOther.m_pString[i];
00226 }
00227 pTempString[tempLength] = 0;
00228
00229
00230 delete[] m_pString;
00231 m_pString = pTempString;
00232 m_length = tempLength;
00233 }
00234
00235
00236 const UString UString::operator +( const UString &rOther ) const
00237 {
00238 UString result( *this );
00239 result += rOther;
00240
00241 return result;
00242 }
00243
00244
00245 const UString UString::operator +( const char *pString ) const
00246 {
00247 UString temp( getIntf(), pString );
00248 return (*this + temp );
00249 }
00250
00251
00252 void UString::debug() const
00253 {
00254 char *s = new char[size() + 1];
00255 for( uint32_t i = 0; i < size(); i++ )
00256 {
00257 s[i] = (char)m_pString[i];
00258 }
00259 s[size()] = '\0';
00260 msg_Err( getIntf(), "%s", s );
00261 delete[] s;
00262 }
00263
00264
00265 uint32_t UString::find( const UString &str, uint32_t position ) const
00266 {
00267 uint32_t pos;
00268 for( pos = position; pos + str.size() <= size(); pos++ )
00269 {
00270 bool match = true;
00271 for( uint32_t i = 0; i < str.size(); i++ )
00272 {
00273 if( m_pString[pos + i] != str.m_pString[i] )
00274 {
00275 match = false;
00276 break;
00277 }
00278 }
00279
00280
00281 if( match )
00282 {
00283 return pos;
00284 }
00285 }
00286
00287
00288 return npos;
00289 }
00290
00291
00292 uint32_t UString::find( const char *pString, uint32_t position ) const
00293 {
00294 UString tmp( getIntf(), pString );
00295 return find( tmp, position );
00296 }
00297
00298
00299 void UString::replace( uint32_t position, uint32_t n1, const UString &str )
00300 {
00301 UString start( substr( 0, position ) );
00302 UString end( substr( position + n1 ) );
00303 *this = start + str + end;
00304 }
00305
00306
00307 void UString::replace( uint32_t position, uint32_t n1, const char *pString )
00308 {
00309 replace( position, n1, UString( getIntf(), pString ) );
00310 }
00311
00312
00313 UString UString::substr( uint32_t position, uint32_t n) const
00314 {
00315 UString tmp( getIntf(), "" );
00316 if( position > size() )
00317 {
00318 msg_Err( getIntf(), "Invalid position in UString::substr()" );
00319 return tmp;
00320 }
00321 tmp.m_length = (n < size() - position) ? n : size() - position;
00322 delete[] tmp.m_pString;
00323 tmp.m_pString = new uint32_t[tmp.size() + 1];
00324 for( uint32_t i = 0; i < tmp.size(); i++ )
00325 {
00326 tmp.m_pString[i] = m_pString[position + i];
00327 }
00328
00329 return tmp;
00330 }