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 *****************************************************************************/
27 #include "ustring.hpp"
30 const uint32_t UString::npos = 0xffffffff;
33 UString::UString( const UString &rOther ): SkinObject( rOther.getIntf() )
35 m_length = rOther.m_length;
36 m_pString = new uint32_t[m_length + 1];
37 memcpy( m_pString, rOther.m_pString, 4 * m_length + 4);
41 UString::UString( intf_thread_t *pIntf, const char *pString ):
44 // First we compute the length of the string
45 const char *pCur = pString;
46 for( m_length = 0; pCur && *pCur; m_length++ )
48 if( (*pCur & 0xfc) == 0xfc )
52 else if ( (*pCur & 0xf8 ) == 0xf8 )
56 else if ( (*pCur & 0xf0 ) == 0xf0 )
60 else if ( (*pCur & 0xe0 ) == 0xe0 )
64 else if ( (*pCur & 0xc0 ) == 0xc0 )
75 msg_Err( pIntf, "invalid UTF8 string: %s", pString );
81 m_pString = new uint32_t[m_length + 1];
83 // Convert the UTF8 string into UNICODE
85 uint32_t aChar = 0; // current unicode character
86 int remaining = 0; // remaining bytes
87 for( uint32_t i = 0; i <= m_length; i++ )
89 if( (*pCur & 0xfc) == 0xfc )
94 else if ( (*pCur & 0xf8 ) == 0xf8 )
99 else if ( (*pCur & 0xf0 ) == 0xf0 )
104 else if ( (*pCur & 0xe0 ) == 0xe0 )
109 else if ( (*pCur & 0xc0 ) == 0xc0 )
123 aChar = ( aChar << 6 ) | ( *pCur & 0x3f );
125 m_pString[i] = aChar;
128 m_pString[m_length] = 0;
141 bool UString::operator ==( const UString &rOther ) const
143 if( size() != rOther.size() )
148 for( uint32_t i = 0; i < size(); i++ )
150 if( m_pString[i] != rOther.m_pString[i] )
160 bool UString::operator !=( const UString &rOther ) const
162 return !(*this == rOther);
166 bool UString::operator <( const UString &rOther ) const
168 const uint32_t *pOther = rOther.u_str();
170 for( i = 0; i < __MIN(m_length, rOther.length()); i++ )
172 if( m_pString[i] < pOther[i] )
176 else if( m_pString[i] > pOther[i] )
181 return( m_pString[i] < pOther[i] );
185 bool UString::operator <=( const UString &rOther ) const
187 return !( rOther < *this );
191 bool UString::operator >( const UString &rOther ) const
193 return ( rOther < *this );
197 bool UString::operator >=( const UString &rOther ) const
199 return !( *this < rOther );
203 void UString::operator =( const UString &rOther )
205 m_length = rOther.m_length;
207 m_pString = new uint32_t[size() + 1];
208 for( uint32_t i = 0; i <= size(); i++ )
210 m_pString[i] = rOther.m_pString[i];
215 void UString::operator +=( const UString &rOther )
217 int tempLength = this->length() + rOther.length();
218 uint32_t *pTempString = new uint32_t[tempLength + 1];
219 // Copy the first string
220 memcpy( pTempString, this->m_pString, 4 * this->size() );
221 // Append the second string
222 // memcpy( pTempString + 4 * size(), rOther.m_pString,
223 // 4 * rOther.size() );
224 for( uint32_t i = 0; i < rOther.size(); i++ )
226 pTempString[this->size() + i] = rOther.m_pString[i];
228 pTempString[tempLength] = 0;
230 // Set the string internally
232 m_pString = pTempString;
233 m_length = tempLength;
237 const UString UString::operator +( const UString &rOther ) const
239 UString result( *this );
246 const UString UString::operator +( const char *pString ) const
248 UString temp( getIntf(), pString );
249 return (*this + temp );
253 void UString::debug() const
255 char *s = new char[size() + 1];
256 for( uint32_t i = 0; i < size(); i++ )
258 s[i] = (char)m_pString[i];
261 msg_Err( getIntf(), "%s", s );
266 uint32_t UString::find( const UString &str, uint32_t position ) const
269 for( pos = position; pos + str.size() <= size(); pos++ )
272 for( uint32_t i = 0; i < str.size(); i++ )
274 if( m_pString[pos + i] != str.m_pString[i] )
293 uint32_t UString::find( const char *pString, uint32_t position ) const
295 UString tmp( getIntf(), pString );
296 return find( tmp, position );
300 void UString::replace( uint32_t position, uint32_t n1, const UString &str )
302 UString start( substr( 0, position ) );
303 UString end( substr( position + n1 ) );
304 *this = start + str + end;
308 void UString::replace( uint32_t position, uint32_t n1, const char *pString )
310 replace( position, n1, UString( getIntf(), pString ) );
314 UString UString::substr( uint32_t position, uint32_t n) const
316 UString tmp( getIntf(), "" );
317 if( position > size() )
319 msg_Err( getIntf(), "invalid position in UString::substr()" );
322 tmp.m_length = (n < size() - position) ? n : size() - position;
323 delete[] tmp.m_pString;
324 tmp.m_pString = new uint32_t[tmp.size() + 1];
325 for( uint32_t i = 0; i < tmp.size(); i++ )
327 tmp.m_pString[i] = m_pString[position + i];
334 UString UString::fromInt( intf_thread_t *pIntf, int number)
338 return UString( pIntf, ss.str().c_str() );