2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #if !defined( INCLUDED_STRING_STRING_H )
23 #define INCLUDED_STRING_STRING_H
26 /// C-style null-terminated-character-array string library.
32 #include "memory/allocator.h"
33 #include "generic/arrayrange.h"
35 /// \brief Returns true if \p string length is zero.
37 inline bool string_empty( const char* string ){
38 return *string == '\0';
41 /// \brief Returns true if \p string length is not zero.
43 inline bool string_not_empty( const char* string ){
44 return !string_empty( string );
47 /// \brief Returns <0 if \p string is lexicographically less than \p other.
48 /// Returns >0 if \p string is lexicographically greater than \p other.
49 /// Returns 0 if \p string is lexicographically equal to \p other.
51 inline int string_compare( const char* string, const char* other ){
52 return std::strcmp( string, other );
55 /// \brief Returns true if \p string is lexicographically equal to \p other.
57 inline bool string_equal( const char* string, const char* other ){
58 return string_compare( string, other ) == 0;
61 /// \brief Returns true if [\p string, \p string + \p n) is lexicographically equal to [\p other, \p other + \p n).
63 inline bool string_equal_n( const char* string, const char* other, std::size_t n ){
64 return std::strncmp( string, other, n ) == 0;
67 /// \brief Returns true if \p string is lexicographically less than \p other.
69 inline bool string_less( const char* string, const char* other ){
70 return string_compare( string, other ) < 0;
73 /// \brief Returns true if \p string is lexicographically greater than \p other.
75 inline bool string_greater( const char* string, const char* other ){
76 return string_compare( string, other ) > 0;
79 /// \brief Returns <0 if \p string is lexicographically less than \p other after converting both to lower-case.
80 /// Returns >0 if \p string is lexicographically greater than \p other after converting both to lower-case.
81 /// Returns 0 if \p string is lexicographically equal to \p other after converting both to lower-case.
83 inline int string_compare_nocase( const char* string, const char* other ){
85 return _stricmp( string, other );
87 return strcasecmp( string, other );
91 /// \brief Returns <0 if [\p string, \p string + \p n) is lexicographically less than [\p other, \p other + \p n).
92 /// Returns >0 if [\p string, \p string + \p n) is lexicographically greater than [\p other, \p other + \p n).
93 /// Returns 0 if [\p string, \p string + \p n) is lexicographically equal to [\p other, \p other + \p n).
94 /// Treats all ascii characters as lower-case during comparisons.
96 inline int string_compare_nocase_n( const char* string, const char* other, std::size_t n ){
98 return _strnicmp( string, other, n );
100 return strncasecmp( string, other, n );
104 /// \brief Returns true if \p string is lexicographically equal to \p other.
105 /// Treats all ascii characters as lower-case during comparisons.
107 inline bool string_equal_nocase( const char* string, const char* other ){
108 return string_compare_nocase( string, other ) == 0;
111 /// \brief Returns true if [\p string, \p string + \p n) is lexicographically equal to [\p other, \p other + \p n).
112 /// Treats all ascii characters as lower-case during comparisons.
114 inline bool string_equal_nocase_n( const char* string, const char* other, std::size_t n ){
115 return string_compare_nocase_n( string, other, n ) == 0;
118 /// \brief Returns true if \p string is lexicographically less than \p other.
119 /// Treats all ascii characters as lower-case during comparisons.
121 inline bool string_less_nocase( const char* string, const char* other ){
122 return string_compare_nocase( string, other ) < 0;
125 /// \brief Returns true if \p string is lexicographically greater than \p other.
126 /// Treats all ascii characters as lower-case during comparisons.
128 inline bool string_greater_nocase( const char* string, const char* other ){
129 return string_compare_nocase( string, other ) > 0;
132 /// \brief Returns the number of non-null characters in \p string.
134 inline std::size_t string_length( const char* string ){
135 return std::strlen( string );
138 /// \brief Returns true if the beginning of \p string is equal to \p prefix.
140 inline bool string_equal_prefix( const char* string, const char* prefix ){
141 return string_equal_n( string, prefix, string_length( prefix ) );
144 /// \brief Returns true if the ending of \p string is equal to \p suffix.
146 inline bool string_equal_suffix( const char* string, const char* suffix){
147 const char *s = string + string_length( string ) - string_length( suffix );
148 return string_equal_n( s , suffix, string_length( suffix ) );
151 /// \brief Copies \p other into \p string and returns \p string.
152 /// Assumes that the space allocated for \p string is at least string_length(other) + 1.
154 inline char* string_copy( char* string, const char* other ){
155 return std::strcpy( string, other );
158 /// \brief Allocates a string buffer large enough to hold \p length characters, using \p allocator.
159 /// The returned buffer must be released with \c string_release using a matching \p allocator.
160 template<typename Allocator>
161 inline char* string_new( std::size_t length, Allocator& allocator ){
162 return allocator.allocate( length + 1 );
165 /// \brief Deallocates the \p buffer large enough to hold \p length characters, using \p allocator.
166 template<typename Allocator>
167 inline void string_release( char* buffer, std::size_t length, Allocator& allocator ){
168 allocator.deallocate( buffer, length + 1 );
171 /// \brief Returns a newly-allocated string which is a clone of \p other, using \p allocator.
172 /// The returned buffer must be released with \c string_release using a matching \p allocator.
173 template<typename Allocator>
174 inline char* string_clone( const char* other, Allocator& allocator ){
175 char* copied = string_new( string_length( other ), allocator );
176 std::strcpy( copied, other );
180 /// \brief Returns a newly-allocated string which is a clone of [\p first, \p last), using \p allocator.
181 /// The returned buffer must be released with \c string_release using a matching \p allocator.
182 template<typename Allocator>
183 inline char* string_clone_range( StringRange range, Allocator& allocator ){
184 std::size_t length = range.last - range.first;
185 char* copied = strncpy( string_new( length, allocator ), range.first, length );
186 copied[length] = '\0';
190 /// \brief Allocates a string buffer large enough to hold \p length characters.
191 /// The returned buffer must be released with \c string_release.
192 inline char* string_new( std::size_t length ){
193 DefaultAllocator<char> allocator;
194 return string_new( length, allocator );
197 /// \brief Allocates a new buffer large enough to hold two concatenated strings and fills it with strings.
198 inline char* string_new_concat( const char* a, const char* b ){
199 char* str = string_new( string_length( a ) + string_length( b ) );
205 /// \brief Deallocates the \p buffer large enough to hold \p length characters.
206 inline void string_release( char* string, std::size_t length ){
207 DefaultAllocator<char> allocator;
208 string_release( string, length, allocator );
211 /// \brief Returns a newly-allocated string which is a clone of \p other.
212 /// The returned buffer must be released with \c string_release.
213 inline char* string_clone( const char* other ){
214 DefaultAllocator<char> allocator;
215 return string_clone( other, allocator );
218 /// \brief Returns a newly-allocated string which is a clone of [\p first, \p last).
219 /// The returned buffer must be released with \c string_release.
220 inline char* string_clone_range( StringRange range ){
221 DefaultAllocator<char> allocator;
222 return string_clone_range( range, allocator );
225 typedef char* char_pointer;
226 /// \brief Swaps the values of \p string and \p other.
227 inline void string_swap( char_pointer& string, char_pointer& other ){
228 std::swap( string, other );
231 typedef const char* char_const_pointer;
232 /// \brief Swaps the values of \p string and \p other.
233 inline void string_swap( char_const_pointer& string, char_const_pointer& other ){
234 std::swap( string, other );
237 /// \brief Converts each character of \p string to lower-case and returns \p string.
239 inline char* string_to_lowercase( char* string ){
240 for ( char* p = string; *p != '\0'; ++p )
242 *p = (char)std::tolower( *p );
247 /// \brief Converts each character of \p string to upper-case and returns \p string.
249 inline char* string_to_uppercase( char* string ){
250 for ( char* p = string; *p != '\0'; ++p )
252 *p = (char)std::toupper( *p );
257 /// \brief A re-entrant string tokeniser similar to strchr.
258 class StringTokeniser
260 bool istoken( char c ) const {
261 if ( strchr( m_delimiters, c ) != 0 ) {
266 const char* advance(){
267 const char* token = m_pos;
269 while ( !string_empty( m_pos ) )
271 if ( !istoken( *m_pos ) ) {
275 else if ( !intoken ) {
282 std::size_t m_length;
285 const char* m_delimiters;
287 StringTokeniser( const char* string, const char* delimiters = " \n\r\t\v" ) :
288 m_length( string_length( string ) ),
289 m_string( string_copy( string_new( m_length ), string ) ),
291 m_delimiters( delimiters ){
292 while ( !string_empty( m_pos ) && !istoken( *m_pos ) )
298 string_release( m_string, m_length );
300 /// \brief Returns the next token or "" if there are no more tokens available.
301 const char* getToken(){
306 /// \brief A non-mutable c-style string.
308 /// \param Buffer The string storage implementation. Must be DefaultConstructible, CopyConstructible and Assignable. Must implement:
309 /// \li Buffer(const char* string) - constructor which copies a c-style \p string.
310 /// \li Buffer(const char* first, const char*) - constructor which copies a c-style string range [\p first, \p last).
311 /// \li void swap(Buffer& other) - swaps contents with \p other.
312 /// \li const char* c_str() - returns the stored non-mutable c-style string.
313 template<typename Buffer>
314 class String : public Buffer
321 String( const char* string )
324 String( StringRange range )
328 String& operator=( const String& other ){
329 String temp( other );
333 String& operator=( const char* string ){
334 String temp( string );
338 String& operator=( StringRange range ){
339 String temp( range );
344 void swap( String& other ){
345 Buffer::swap( other );
349 return string_empty( Buffer::c_str() );
353 template<typename Buffer>
354 inline bool operator<( const String<Buffer>& self, const String<Buffer>& other ){
355 return string_less( self.c_str(), other.c_str() );
358 template<typename Buffer>
359 inline bool operator>( const String<Buffer>& self, const String<Buffer>& other ){
360 return string_greater( self.c_str(), other.c_str() );
363 template<typename Buffer>
364 inline bool operator==( const String<Buffer>& self, const String<Buffer>& other ){
365 return string_equal( self.c_str(), other.c_str() );
368 template<typename Buffer>
369 inline bool operator!=( const String<Buffer>& self, const String<Buffer>& other ){
370 return !string_equal( self.c_str(), other.c_str() );
373 template<typename Buffer>
374 inline bool operator==( const String<Buffer>& self, const char* other ){
375 return string_equal( self.c_str(), other );
378 template<typename Buffer>
379 inline bool operator!=( const String<Buffer>& self, const char* other ){
380 return !string_equal( self.c_str(), other );
385 /// \brief Swaps the values of \p self and \p other.
386 /// Overloads std::swap.
387 template<typename Buffer>
388 inline void swap( String<Buffer>& self, String<Buffer>& other ){
394 /// \brief A non-mutable string buffer which manages memory allocation.
395 template<typename Allocator>
396 class CopiedBuffer : private Allocator
400 char* copy_range( StringRange range ){
401 return string_clone_range( range, static_cast<Allocator&>( *this ) );
403 char* copy( const char* other ){
404 return string_clone( other, static_cast<Allocator&>( *this ) );
406 void destroy( char* string ){
407 string_release( string, string_length( string ), static_cast<Allocator&>( *this ) );
416 : m_string( copy( "" ) ){
418 explicit CopiedBuffer( const Allocator& allocator )
419 : Allocator( allocator ), m_string( copy( "" ) ){
421 CopiedBuffer( const CopiedBuffer& other )
422 : Allocator( other ), m_string( copy( other.m_string ) ){
424 CopiedBuffer( const char* string, const Allocator& allocator = Allocator() )
425 : Allocator( allocator ), m_string( copy( string ) ){
427 CopiedBuffer( StringRange range, const Allocator& allocator = Allocator() )
428 : Allocator( allocator ), m_string( copy_range( range ) ){
430 const char* c_str() const {
433 void swap( CopiedBuffer& other ){
434 string_swap( m_string, other.m_string );
438 /// \brief A non-mutable string which uses copy-by-value for assignment.
439 typedef String< CopiedBuffer< DefaultAllocator<char> > > CopiedString;
442 /// \brief A non-mutable string buffer which uses reference-counting to avoid unnecessary allocations.
443 template<typename Allocator>
444 class SmartBuffer : private Allocator
448 char* copy_range( StringRange range ){
449 char* buffer = Allocator::allocate( sizeof( std::size_t ) + ( range.last - range.first ) + 1 );
450 strncpy( buffer + sizeof( std::size_t ), range.first, range.last - range.first );
451 buffer[sizeof( std::size_t ) + ( range.last - range.first )] = '\0';
452 *reinterpret_cast<std::size_t*>( buffer ) = 0;
455 char* copy( const char* string ){
456 char* buffer = Allocator::allocate( sizeof( std::size_t ) + string_length( string ) + 1 );
457 strcpy( buffer + sizeof( std::size_t ), string );
458 *reinterpret_cast<std::size_t*>( buffer ) = 0;
461 void destroy( char* buffer ){
462 Allocator::deallocate( buffer, sizeof( std::size_t ) + string_length( c_str() ) + 1 );
465 void incref( char* buffer ){
466 ++( *reinterpret_cast<std::size_t*>( buffer ) );
468 void decref( char* buffer ){
469 if ( --( *reinterpret_cast<std::size_t*>( buffer ) ) == 0 ) {
480 : m_buffer( copy( "" ) ){
483 explicit SmartBuffer( const Allocator& allocator )
484 : Allocator( allocator ), m_buffer( copy( "" ) ){
487 SmartBuffer( const SmartBuffer& other )
488 : Allocator( other ), m_buffer( other.m_buffer ){
491 SmartBuffer( const char* string, const Allocator& allocator = Allocator() )
492 : Allocator( allocator ), m_buffer( copy( string ) ){
495 SmartBuffer( StringRange range, const Allocator& allocator = Allocator() )
496 : Allocator( allocator ), m_buffer( copy_range( range ) ){
499 const char* c_str() const {
500 return m_buffer + sizeof( std::size_t );
502 void swap( SmartBuffer& other ){
503 string_swap( m_buffer, other.m_buffer );
507 /// \brief A non-mutable string which uses copy-by-reference for assignment of SmartString.
508 typedef String< SmartBuffer< DefaultAllocator<char> > > SmartString;
510 class StringEqualNoCase
513 bool operator()( const CopiedString& key, const CopiedString& other ) const {
514 return string_equal_nocase( key.c_str(), other.c_str() );
518 struct StringLessNoCase
520 bool operator()( const CopiedString& x, const CopiedString& y ) const {
521 return string_less_nocase( x.c_str(), y.c_str() );
525 struct RawStringEqual
527 bool operator()( const char* x, const char* y ) const {
528 return string_equal( x, y );
534 bool operator()( const char* x, const char* y ) const {
535 return string_less( x, y );
539 struct RawStringLessNoCase
541 bool operator()( const char* x, const char* y ) const {
542 return string_less_nocase( x, y );