1 /*****************************************************************************
2  * ustring.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 the VideoLAN team
5  * $Id: 56ed4659c0bc9e8bda3ada5fc0d675790fca7548 $
6  *
7  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
8  *          Olivier Teulière <ipkiss@via.ecp.fr>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24 
25 #include <sstream>
26 #include "ustring.hpp"
27 
28 
29 const uint32_t UString::npos = 0xffffffff;
30 
31 
UString(const UString & rOther)32 UString::UString( const UString &rOther ): SkinObject( rOther.getIntf() )
33 {
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);
37 }
38 
39 
UString(intf_thread_t * pIntf,const char * pString)40 UString::UString( intf_thread_t *pIntf, const char *pString ):
41     SkinObject( pIntf )
42 {
43     // First we compute the length of the string
44     const char *pCur = pString;
45     for( m_length = 0; pCur && *pCur; m_length++ )
46     {
47         if( (*pCur & 0xfc) == 0xfc )
48         {
49             pCur += 6;
50         }
51         else if ( (*pCur & 0xf8 ) == 0xf8 )
52         {
53             pCur += 5;
54         }
55         else if ( (*pCur & 0xf0 ) == 0xf0 )
56         {
57             pCur += 4;
58         }
59         else if ( (*pCur & 0xe0 ) == 0xe0 )
60         {
61             pCur += 3;
62         }
63         else if ( (*pCur & 0xc0 ) == 0xc0 )
64         {
65             pCur += 2;
66         }
67         else
68         {
69             pCur++;
70         }
71     }
72     if( !pCur || *pCur )
73     {
74         msg_Err( pIntf, "invalid UTF8 string: %s", pString );
75         m_length = 0;
76         m_pString = NULL;
77         return;
78     }
79 
80     m_pString = new uint32_t[m_length + 1];
81 
82     // Convert the UTF8 string into UNICODE
83     pCur = pString;
84     uint32_t aChar = 0;  // current unicode character
85     int remaining = 0;   // remaining bytes
86     for( uint32_t i = 0; i <= m_length; i++ )
87     {
88         if( (*pCur & 0xfc) == 0xfc )
89         {
90             aChar = *pCur & 1;
91             remaining = 5;
92         }
93         else if ( (*pCur & 0xf8 ) == 0xf8 )
94         {
95             aChar = *pCur & 3;
96             remaining = 4;
97         }
98         else if ( (*pCur & 0xf0 ) == 0xf0 )
99         {
100             aChar = *pCur & 7;
101             remaining = 3;
102         }
103         else if ( (*pCur & 0xe0 ) == 0xe0 )
104         {
105             aChar = *pCur & 15;
106             remaining = 2;
107         }
108         else if ( (*pCur & 0xc0 ) == 0xc0 )
109         {
110             aChar = *pCur & 31;
111             remaining = 1;
112         }
113         else
114         {
115             aChar = *pCur;
116             remaining = 0;
117         }
118         while( remaining )
119         {
120             pCur++;
121             remaining--;
122             aChar = ( aChar << 6 ) | ( *pCur & 0x3f );
123         }
124         m_pString[i] = aChar;
125         pCur++;
126     }
127     m_pString[m_length] = 0;
128 }
129 
130 
~UString()131 UString::~UString()
132 {
133     delete[] m_pString;
134 }
135 
136 
operator ==(const UString & rOther) const137 bool UString::operator ==( const UString &rOther ) const
138 {
139     if( size() != rOther.size() )
140     {
141         return false;
142     }
143 
144     for( uint32_t i = 0; i < size(); i++ )
145     {
146         if( m_pString[i] != rOther.m_pString[i] )
147         {
148             return false;
149         }
150     }
151 
152     return true;
153 }
154 
155 
operator !=(const UString & rOther) const156 bool UString::operator !=( const UString &rOther ) const
157 {
158     return !(*this == rOther);
159 }
160 
161 
operator <(const UString & rOther) const162 bool UString::operator <( const UString &rOther ) const
163 {
164     const uint32_t *pOther = rOther.u_str();
165     uint32_t i;
166     for( i = 0; i < __MIN(m_length, rOther.length()); i++ )
167     {
168         if( m_pString[i] < pOther[i] )
169         {
170             return true;
171         }
172         else if( m_pString[i] > pOther[i] )
173         {
174             return false;
175         }
176     }
177     return( m_pString[i] < pOther[i] );
178 }
179 
180 
operator <=(const UString & rOther) const181 bool UString::operator <=( const UString &rOther ) const
182 {
183     return !( rOther < *this );
184 }
185 
186 
operator >(const UString & rOther) const187 bool UString::operator >( const UString &rOther ) const
188 {
189     return ( rOther < *this );
190 }
191 
192 
operator >=(const UString & rOther) const193 bool UString::operator >=( const UString &rOther ) const
194 {
195     return !( *this < rOther );
196 }
197 
198 
operator =(const UString & rOther)199 UString& UString::operator =( const UString &rOther )
200 {
201     if( this == &rOther )
202         return *this;
203 
204     m_length = rOther.m_length;
205     delete[] m_pString;
206     m_pString = new uint32_t[size() + 1];
207     for( uint32_t i = 0; i <= size(); i++ )
208     {
209         m_pString[i] = rOther.m_pString[i];
210     }
211 
212     return *this;
213 }
214 
215 
operator +=(const UString & rOther)216 UString& UString::operator +=( const UString &rOther )
217 {
218     if( this == &rOther )
219         return *this;
220 
221     int tempLength = this->length() + rOther.length();
222     uint32_t *pTempString = new uint32_t[tempLength + 1];
223     // Copy the first string
224     memcpy( pTempString, this->m_pString, sizeof(uint32_t) * this->size() );
225     // Append the second string
226 //     memcpy( pTempString + 4 * size(), rOther.m_pString,
227 //             4 * rOther.size() );
228     for( uint32_t i = 0; i < rOther.size(); i++ )
229     {
230         pTempString[this->size() + i] = rOther.m_pString[i];
231     }
232     pTempString[tempLength] = 0;
233 
234     // Set the string internally
235     delete[] m_pString;
236     m_pString = pTempString;
237     m_length = tempLength;
238 
239     return *this;
240 }
241 
242 
operator +(const UString & rOther) const243 const UString UString::operator +( const UString &rOther ) const
244 {
245     UString result( *this );
246     result += rOther;
247 
248     return result;
249 }
250 
251 
operator +(const char * pString) const252 const UString UString::operator +( const char *pString ) const
253 {
254     UString temp( getIntf(), pString );
255     return (*this + temp );
256 }
257 
258 
find(const UString & str,uint32_t position) const259 uint32_t UString::find( const UString &str, uint32_t position ) const
260 {
261     uint32_t pos;
262     for( pos = position; pos + str.size() <= size(); pos++ )
263     {
264         bool match = true;
265         for( uint32_t i = 0; i < str.size(); i++ )
266         {
267             if( m_pString[pos + i] != str.m_pString[i] )
268             {
269                 match = false;
270                 break;
271             }
272         }
273 
274         // Found
275         if( match )
276         {
277             return pos;
278         }
279     }
280 
281     // Not found
282     return npos;
283 }
284 
285 
find(const char * pString,uint32_t position) const286 uint32_t UString::find( const char *pString, uint32_t position ) const
287 {
288     UString tmp( getIntf(), pString );
289     return find( tmp, position );
290 }
291 
292 
replace(uint32_t position,uint32_t n1,const UString & str)293 void UString::replace( uint32_t position, uint32_t n1, const UString &str )
294 {
295     UString start( substr( 0, position ) );
296     UString end( substr( position + n1 ) );
297     *this = start + str + end;
298 }
299 
300 
replace(uint32_t position,uint32_t n1,const char * pString)301 void UString::replace( uint32_t position, uint32_t n1, const char *pString )
302 {
303     replace( position, n1, UString( getIntf(), pString ) );
304 }
305 
306 
substr(uint32_t position,uint32_t n) const307 UString UString::substr( uint32_t position, uint32_t n) const
308 {
309     UString tmp( getIntf(), "" );
310     if( position > size() )
311     {
312         msg_Err( getIntf(), "invalid position in UString::substr()" );
313         return tmp;
314     }
315     tmp.m_length = (n < size() - position) ? n : size() - position;
316     delete[] tmp.m_pString;
317     tmp.m_pString = new uint32_t[tmp.size() + 1];
318     for( uint32_t i = 0; i < tmp.size(); i++ )
319     {
320         tmp.m_pString[i] = m_pString[position + i];
321     }
322 
323     return tmp;
324 }
325 
326 
fromInt(intf_thread_t * pIntf,int number)327 UString UString::fromInt( intf_thread_t *pIntf, int number)
328 {
329     std::stringstream ss;
330     ss << number;
331     return UString( pIntf, ss.str().c_str() );
332 }
333 
334