Merge branch 'fixes' into main/rendor-staging
[ryzomcore.git] / ryzom / tools / leveldesign / georges_convert / string_ex.cpp
bloba3987dada0faef6347d11ba33b816e77ab793e69
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "stdgeorgesconvert.h"
18 #include "string_ex.h"
20 namespace NLOLDGEORGES
23 //////////////////////////////////////////////////////////////////////
24 // Construction/Destruction
25 //////////////////////////////////////////////////////////////////////
26 using namespace std;
27 CStringEx::~CStringEx()
32 void CStringEx::remove( const char _c )
34 if( empty() )
35 return;
36 iterator it = begin();
37 while( it != end() )
39 if( (*it) == _c )
40 it = erase( it );
41 else
42 ++it;
46 void CStringEx::remove()
48 remove(' ');
49 remove('\t');
50 remove('\n');
53 void CStringEx::make_lower( )
55 for( iterator it = begin(); it != end(); ++it )
56 if( (*it >= 'A')&&(*it <= 'Z') )
57 *it += 'a'-'A';
60 void CStringEx::make_upper( )
62 for( iterator it = begin(); it != end(); ++it )
63 if( (*it >= 'a')&&(*it <= 'z') )
64 *it += 'A'-'a';
67 void CStringEx::trim_left( )
69 if( empty() )
70 return;
71 iterator it;
72 for( it = begin(); (it != end())&&( (*it==' ')||(*it=='\t')||(*it=='\n') ); ++it );
73 erase( begin(), it );
76 void CStringEx::trim_left( const char _c )
78 if( empty() )
79 return;
80 iterator it;
81 for( it = begin(); (it != end())&&( *it == _c ); ++it );
82 erase( begin(), it );
85 void CStringEx::trim_right( )
87 if( empty() )
88 return;
89 iterator it = end();
90 --it;
91 while( it != begin() )
93 iterator i = it--;
94 if( (*i==' ')||(*i=='\t')||(*i=='\n') )
95 erase( i );
96 else
97 break;
99 if( (*it==' ')||(*it=='\t')||(*it=='\n') )
100 erase( it );
103 void CStringEx::trim_right( char c )
105 if( empty() )
106 return;
107 iterator it = end();
108 while( it != begin() )
110 iterator i = it--;
111 if( *i == c )
112 erase( i );
113 else
114 break;
116 if( *it == c )
117 erase( it );
120 void CStringEx::trim()
122 trim_left();
123 trim_right();
126 void CStringEx::purge()
128 make_lower();
129 remove(' ');
130 remove('\t');
131 remove('\n');
134 void CStringEx::trim( const char _c )
136 trim_left( _c );
137 trim_right( _c );
140 void CStringEx::mid( const int nFirst )
142 CStringEx s( *this );
143 erase();
144 append( s.get_mid( nFirst ));
147 void CStringEx::mid( const int nFirst, const int nCount )
149 CStringEx s( *this );
150 erase();
151 append( s.get_mid( nFirst, nCount ));
154 void CStringEx::left( const int nCount )
156 CStringEx s( *this );
157 erase();
158 append( s.get_left( nCount ));
161 void CStringEx::right( const int nCount )
163 CStringEx s( *this );
164 erase();
165 append( s.get_right( nCount ));
169 CStringEx CStringEx::get_remove( const char _c ) const
171 CStringEx s( *this );
172 s.remove( _c );
173 return( s );
176 CStringEx CStringEx::get_remove() const
178 CStringEx s( *this );
179 s.remove();
180 return( s );
183 CStringEx CStringEx::get_make_lower() const
185 CStringEx s( *this );
186 s.make_lower();
187 return( s );
190 CStringEx CStringEx::get_make_upper() const
192 CStringEx s( *this );
193 s.make_upper();
194 return( s );
197 CStringEx CStringEx::get_trim_left() const
199 CStringEx s( *this );
200 s.trim_left();
201 return( s );
204 CStringEx CStringEx::get_trim_left( const char _c ) const
206 CStringEx s( *this );
207 s.trim_left( _c );
208 return( s );
211 CStringEx CStringEx::get_trim_right() const
213 CStringEx s( *this );
214 s.trim_right();
215 return( s );
218 CStringEx CStringEx::get_trim_right( const char _c ) const
220 CStringEx s( *this );
221 s.trim_right( _c );
222 return( s );
225 CStringEx CStringEx::get_trim() const
227 CStringEx s( *this );
228 s.trim();
229 return( s );
232 CStringEx CStringEx::get_purge() const
234 CStringEx s( *this );
235 s.purge();
236 return( s );
239 CStringEx CStringEx::get_trim( const char _c ) const
241 CStringEx s( *this );
242 s.trim( _c );
243 return( s );
246 CStringEx CStringEx::get_mid( const int nFirst ) const
248 if( !size() )
250 CStringEx object;
251 return( object );
253 return( get_right( size()-nFirst ) );
256 CStringEx CStringEx::get_mid( const int nFirst, const int nCount ) const
258 if( !size() )
260 CStringEx object;
261 return( object );
263 return( substr( nFirst, nCount ) );
266 CStringEx CStringEx::get_left( const int nCount ) const
268 if( !size() )
270 CStringEx object;
271 return( object );
273 return( substr( 0, nCount ) );
276 CStringEx CStringEx::get_right( const int nCount ) const
278 if( !size() )
280 CStringEx object;
281 return( object );
283 return( substr( size()-nCount, nCount ) );
287 bool CStringEx::operator <= ( const CStringEx& s ) const
289 const_iterator it = begin();
290 const_iterator is = s.begin();
291 while( ( it != end() )&&( is != s.end() ) )
293 if( *it != *is )
294 return( *it < *is );
295 it++;
296 is++;
298 return( ( it == end() )&&( is == s.end() ) );
301 bool CStringEx::operator < ( const CStringEx& s ) const
303 const_iterator it = begin();
304 const_iterator is = s.begin();
305 while( ( it != end() )&&( is != s.end() ) )
307 if( *it != *is )
308 return( *it < *is );
309 it++;
310 is++;
312 return( is != s.end() );
315 std::string::size_type CStringEx::reverse_find( const char _c ) const
317 size_type i = length();
318 const_iterator it = end();
319 while( it != begin() )
321 --it;
322 --i;
323 if( *it == _c )
324 return i ;
326 return npos;
329 void CStringEx::format( const char* s, ... )
331 char *p = new char[256];
332 va_list ap;
333 va_start(ap, s);
334 int x = vsprintf( p, s, ap);
335 erase();
336 append(p);
337 delete[] p;
341 #include <stdio.h>
342 #include <stdlib.h>
343 #include <stdarg.h>
344 char *newfmt(const char *fmt, ...)
346 char *p;
347 va_list ap;
348 if ((p = malloc(128)) == NULL)
349 return (NULL);
350 va_start(ap, fmt);
351 (void) vsnprintf(p, 128, fmt, ap);
352 va_end(ap);
353 return (p);