1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 the VideoLAN team
7 * Authors: Cyril Deguet <asmax@via.ecp.fr>
8 * Olivier Teulière <ipkiss@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
26 #include "ustring.hpp"
29 const uint32_t UString::npos = 0xffffffff;
32 UString::UString( const UString &rOther ): SkinObject( rOther.getIntf() )
34 m_length = rOther.m_length;
35 m_pString = new uint32_t[m_length + 1];
36 memcpy( m_pString, rOther.m_pString, 4 * m_length + 4);
40 UString::UString( intf_thread_t *pIntf, const char *pString ):
43 // First we compute the length of the string
44 const char *pCur = pString;
45 for( m_length = 0; pCur && *pCur; m_length++ )
47 if( (*pCur & 0xfc) == 0xfc )
51 else if ( (*pCur & 0xf8 ) == 0xf8 )
55 else if ( (*pCur & 0xf0 ) == 0xf0 )
59 else if ( (*pCur & 0xe0 ) == 0xe0 )
63 else if ( (*pCur & 0xc0 ) == 0xc0 )
74 msg_Err( pIntf, "Invalid UTF8 string: %s", pString );
80 m_pString = new uint32_t[m_length + 1];
82 // Convert the UTF8 string into UNICODE
84 uint32_t aChar = 0; // current unicode character
85 int remaining = 0; // remaining bytes
86 for( uint32_t i = 0; i <= m_length; i++ )
88 if( (*pCur & 0xfc) == 0xfc )
93 else if ( (*pCur & 0xf8 ) == 0xf8 )
98 else if ( (*pCur & 0xf0 ) == 0xf0 )
103 else if ( (*pCur & 0xe0 ) == 0xe0 )
108 else if ( (*pCur & 0xc0 ) == 0xc0 )
122 aChar = ( aChar << 6 ) | ( *pCur & 0x3f );
124 m_pString[i] = aChar;
127 m_pString[m_length] = 0;
140 bool UString::operator ==( const UString &rOther ) const
142 if( size() != rOther.size() )
147 for( uint32_t i = 0; i < size(); i++ )
149 if( m_pString[i] != rOther.m_pString[i] )
159 bool UString::operator !=( const UString &rOther ) const
161 return !(*this == rOther);
165 bool UString::operator <( const UString &rOther ) const
167 const uint32_t *pOther = rOther.u_str();
169 for( i = 0; i < __MIN(m_length, rOther.length()); i++ )
171 if( m_pString[i] < pOther[i] )
175 else if( m_pString[i] > pOther[i] )
180 return( m_pString[i] < pOther[i] );
184 bool UString::operator <=( const UString &rOther ) const
186 return !( rOther < *this );
190 bool UString::operator >( const UString &rOther ) const
192 return ( rOther < *this );
196 bool UString::operator >=( const UString &rOther ) const
198 return !( *this < rOther );
202 void UString::operator =( const UString &rOther )
204 m_length = rOther.m_length;
206 m_pString = new uint32_t[size() + 1];
207 for( uint32_t i = 0; i <= size(); i++ )
209 m_pString[i] = rOther.m_pString[i];
214 void UString::operator +=( const UString &rOther )
216 int tempLength = this->length() + rOther.length();
217 uint32_t *pTempString = new uint32_t[tempLength + 1];
218 // Copy the first string
219 memcpy( pTempString, this->m_pString, 4 * this->size() );
220 // Append the second string
221 // memcpy( pTempString + 4 * size(), rOther.m_pString,
222 // 4 * rOther.size() );
223 for( uint32_t i = 0; i < rOther.size(); i++ )
225 pTempString[this->size() + i] = rOther.m_pString[i];
227 pTempString[tempLength] = 0;
229 // Set the string internally
231 m_pString = pTempString;
232 m_length = tempLength;
236 const UString UString::operator +( const UString &rOther ) const
238 UString result( *this );
245 const UString UString::operator +( const char *pString ) const
247 UString temp( getIntf(), pString );
248 return (*this + temp );
252 void UString::debug() const
254 char *s = new char[size() + 1];
255 for( uint32_t i = 0; i < size(); i++ )
257 s[i] = (char)m_pString[i];
260 msg_Err( getIntf(), "%s", s );
265 uint32_t UString::find( const UString &str, uint32_t position ) const
268 for( pos = position; pos + str.size() <= size(); pos++ )
271 for( uint32_t i = 0; i < str.size(); i++ )
273 if( m_pString[pos + i] != str.m_pString[i] )
292 uint32_t UString::find( const char *pString, uint32_t position ) const
294 UString tmp( getIntf(), pString );
295 return find( tmp, position );
299 void UString::replace( uint32_t position, uint32_t n1, const UString &str )
301 UString start( substr( 0, position ) );
302 UString end( substr( position + n1 ) );
303 *this = start + str + end;
307 void UString::replace( uint32_t position, uint32_t n1, const char *pString )
309 replace( position, n1, UString( getIntf(), pString ) );
313 UString UString::substr( uint32_t position, uint32_t n) const
315 UString tmp( getIntf(), "" );
316 if( position > size() )
318 msg_Err( getIntf(), "Invalid position in UString::substr()" );
321 tmp.m_length = (n < size() - position) ? n : size() - position;
322 delete[] tmp.m_pString;
323 tmp.m_pString = new uint32_t[tmp.size() + 1];
324 for( uint32_t i = 0; i < tmp.size(); i++ )
326 tmp.m_pString[i] = m_pString[position + i];