]> git.sesse.net Git - vlc/blob - modules/gui/skins2/utils/ustring.cpp
0251db4c82e6e73cbf1de06518a7a7a82629647d
[vlc] / modules / gui / skins2 / utils / ustring.cpp
1 /*****************************************************************************
2  * ustring.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 the VideoLAN team
5  * $Id$
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 #include <string.h>
26 #include "ustring.hpp"
27
28
29 const uint32_t UString::npos = 0xffffffff;
30
31
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
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
131 UString::~UString()
132 {
133     if( m_pString )
134     {
135         delete[] m_pString;
136     }
137 }
138
139
140 bool UString::operator ==( const UString &rOther ) const
141 {
142     if( size() != rOther.size() )
143     {
144         return false;
145     }
146
147     for( uint32_t i = 0; i < size(); i++ )
148     {
149         if( m_pString[i] != rOther.m_pString[i] )
150         {
151             return false;
152         }
153     }
154
155     return true;
156 }
157
158
159 bool UString::operator !=( const UString &rOther ) const
160 {
161     return !(*this == rOther);
162 }
163
164
165 bool UString::operator <( const UString &rOther ) const
166 {
167     const uint32_t *pOther = rOther.u_str();
168     uint32_t i;
169     for( i = 0; i < __MIN(m_length, rOther.length()); i++ )
170     {
171         if( m_pString[i] < pOther[i] )
172         {
173             return true;
174         }
175         else if( m_pString[i] > pOther[i] )
176         {
177             return false;
178         }
179     }
180     return( m_pString[i] < pOther[i] );
181 }
182
183
184 bool UString::operator <=( const UString &rOther ) const
185 {
186     return !( rOther < *this );
187 }
188
189
190 bool UString::operator >( const UString &rOther ) const
191 {
192     return ( rOther < *this );
193 }
194
195
196 bool UString::operator >=( const UString &rOther ) const
197 {
198     return !( *this < rOther );
199 }
200
201
202 void UString::operator =( const UString &rOther )
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
213
214 void UString::operator +=( const UString &rOther )
215 {
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++ )
224     {
225         pTempString[this->size() + i] = rOther.m_pString[i];
226     }
227     pTempString[tempLength] = 0;
228
229     // Set the string internally
230     delete[] m_pString;
231     m_pString = pTempString;
232     m_length = tempLength;
233 }
234
235
236 const UString UString::operator +( const UString &rOther ) const
237 {
238     UString result( *this );
239     result += rOther;
240
241     return result;
242 }
243
244
245 const UString UString::operator +( const char *pString ) const
246 {
247     UString temp( getIntf(), pString );
248     return (*this + temp );
249 }
250
251
252 void UString::debug() const
253 {
254     char *s = new char[size() + 1];
255     for( uint32_t i = 0; i < size(); i++ )
256     {
257         s[i] = (char)m_pString[i];
258     }
259     s[size()] = '\0';
260     msg_Err( getIntf(), "%s", s );
261     delete[] s;
262 }
263
264
265 uint32_t UString::find( const UString &str, uint32_t position ) const
266 {
267     uint32_t pos;
268     for( pos = position; pos + str.size() <= size(); pos++ )
269     {
270         bool match = true;
271         for( uint32_t i = 0; i < str.size(); i++ )
272         {
273             if( m_pString[pos + i] != str.m_pString[i] )
274             {
275                 match = false;
276                 break;
277             }
278         }
279
280         // Found
281         if( match )
282         {
283             return pos;
284         }
285     }
286
287     // Not found
288     return npos;
289 }
290
291
292 uint32_t UString::find( const char *pString, uint32_t position ) const
293 {
294     UString tmp( getIntf(), pString );
295     return find( tmp, position );
296 }
297
298
299 void UString::replace( uint32_t position, uint32_t n1, const UString &str )
300 {
301     UString start( substr( 0, position ) );
302     UString end( substr( position + n1 ) );
303     *this = start + str + end;
304 }
305
306
307 void UString::replace( uint32_t position, uint32_t n1, const char *pString )
308 {
309     replace( position, n1, UString( getIntf(), pString ) );
310 }
311
312
313 UString UString::substr( uint32_t position, uint32_t n) const
314 {
315     UString tmp( getIntf(), "" );
316     if( position > size() )
317     {
318         msg_Err( getIntf(), "Invalid position in UString::substr()" );
319         return tmp;
320     }
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++ )
325     {
326         tmp.m_pString[i] = m_pString[position + i];
327     }
328
329     return tmp;
330 }