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 Copies \p other into \p string and returns \p string.
145 /// Assumes that the space allocated for \p string is at least string_length(other) + 1.
147 inline char* string_copy( char* string, const char* other ){
148 return std::strcpy( string, other );
151 /// \brief Allocates a string buffer large enough to hold \p length characters, using \p allocator.
152 /// The returned buffer must be released with \c string_release using a matching \p allocator.
153 template<typename Allocator>
154 inline char* string_new( std::size_t length, Allocator& allocator ){
155 return allocator.allocate( length + 1 );
158 /// \brief Deallocates the \p buffer large enough to hold \p length characters, using \p allocator.
159 template<typename Allocator>
160 inline void string_release( char* buffer, std::size_t length, Allocator& allocator ){
161 allocator.deallocate( buffer, length + 1 );
164 /// \brief Returns a newly-allocated string which is a clone of \p other, using \p allocator.
165 /// The returned buffer must be released with \c string_release using a matching \p allocator.
166 template<typename Allocator>
167 inline char* string_clone( const char* other, Allocator& allocator ){
168 char* copied = string_new( string_length( other ), allocator );
169 std::strcpy( copied, other );
173 /// \brief Returns a newly-allocated string which is a clone of [\p first, \p last), using \p allocator.
174 /// The returned buffer must be released with \c string_release using a matching \p allocator.
175 template<typename Allocator>
176 inline char* string_clone_range( StringRange range, Allocator& allocator ){
177 std::size_t length = range.last - range.first;
178 char* copied = strncpy( string_new( length, allocator ), range.first, length );
179 copied[length] = '\0';
183 /// \brief Allocates a string buffer large enough to hold \p length characters.
184 /// The returned buffer must be released with \c string_release.
185 inline char* string_new( std::size_t length ){
186 DefaultAllocator<char> allocator;
187 return string_new( length, allocator );
190 /// \brief Deallocates the \p buffer large enough to hold \p length characters.
191 inline void string_release( char* string, std::size_t length ){
192 DefaultAllocator<char> allocator;
193 string_release( string, length, allocator );
196 /// \brief Returns a newly-allocated string which is a clone of \p other.
197 /// The returned buffer must be released with \c string_release.
198 inline char* string_clone( const char* other ){
199 DefaultAllocator<char> allocator;
200 return string_clone( other, allocator );
203 /// \brief Returns a newly-allocated string which is a clone of [\p first, \p last).
204 /// The returned buffer must be released with \c string_release.
205 inline char* string_clone_range( StringRange range ){
206 DefaultAllocator<char> allocator;
207 return string_clone_range( range, allocator );
210 typedef char* char_pointer;
211 /// \brief Swaps the values of \p string and \p other.
212 inline void string_swap( char_pointer& string, char_pointer& other ){
213 std::swap( string, other );
216 typedef const char* char_const_pointer;
217 /// \brief Swaps the values of \p string and \p other.
218 inline void string_swap( char_const_pointer& string, char_const_pointer& other ){
219 std::swap( string, other );
222 /// \brief Converts each character of \p string to lower-case and returns \p string.
224 inline char* string_to_lowercase( char* string ){
225 for ( char* p = string; *p != '\0'; ++p )
227 *p = (char)std::tolower( *p );
232 /// \brief Converts each character of \p string to upper-case and returns \p string.
234 inline char* string_to_uppercase( char* string ){
235 for ( char* p = string; *p != '\0'; ++p )
237 *p = (char)std::toupper( *p );
242 /// \brief A re-entrant string tokeniser similar to strchr.
243 class StringTokeniser
245 bool istoken( char c ) const {
246 if ( strchr( m_delimiters, c ) != 0 ) {
251 const char* advance(){
252 const char* token = m_pos;
254 while ( !string_empty( m_pos ) )
256 if ( !istoken( *m_pos ) ) {
260 else if ( !intoken ) {
267 std::size_t m_length;
270 const char* m_delimiters;
272 StringTokeniser( const char* string, const char* delimiters = " \n\r\t\v" ) :
273 m_length( string_length( string ) ),
274 m_string( string_copy( string_new( m_length ), string ) ),
276 m_delimiters( delimiters ){
277 while ( !string_empty( m_pos ) && !istoken( *m_pos ) )
283 string_release( m_string, m_length );
285 /// \brief Returns the next token or "" if there are no more tokens available.
286 const char* getToken(){
291 /// \brief A non-mutable c-style string.
293 /// \param Buffer The string storage implementation. Must be DefaultConstructible, CopyConstructible and Assignable. Must implement:
294 /// \li Buffer(const char* string) - constructor which copies a c-style \p string.
295 /// \li Buffer(const char* first, const char*) - constructor which copies a c-style string range [\p first, \p last).
296 /// \li void swap(Buffer& other) - swaps contents with \p other.
297 /// \li const char* c_str() - returns the stored non-mutable c-style string.
298 template<typename Buffer>
299 class String : public Buffer
306 String( const char* string )
309 String( StringRange range )
313 String& operator=( const String& other ){
314 String temp( other );
318 String& operator=( const char* string ){
319 String temp( string );
323 String& operator=( StringRange range ){
324 String temp( range );
329 void swap( String& other ){
330 Buffer::swap( other );
334 return string_empty( Buffer::c_str() );
338 template<typename Buffer>
339 inline bool operator<( const String<Buffer>& self, const String<Buffer>& other ){
340 return string_less( self.c_str(), other.c_str() );
343 template<typename Buffer>
344 inline bool operator>( const String<Buffer>& self, const String<Buffer>& other ){
345 return string_greater( self.c_str(), other.c_str() );
348 template<typename Buffer>
349 inline bool operator==( const String<Buffer>& self, const String<Buffer>& other ){
350 return string_equal( self.c_str(), other.c_str() );
353 template<typename Buffer>
354 inline bool operator!=( const String<Buffer>& self, const String<Buffer>& other ){
355 return !string_equal( self.c_str(), other.c_str() );
358 template<typename Buffer>
359 inline bool operator==( const String<Buffer>& self, const char* other ){
360 return string_equal( self.c_str(), other );
363 template<typename Buffer>
364 inline bool operator!=( const String<Buffer>& self, const char* other ){
365 return !string_equal( self.c_str(), other );
370 /// \brief Swaps the values of \p self and \p other.
371 /// Overloads std::swap.
372 template<typename Buffer>
373 inline void swap( String<Buffer>& self, String<Buffer>& other ){
379 /// \brief A non-mutable string buffer which manages memory allocation.
380 template<typename Allocator>
381 class CopiedBuffer : private Allocator
385 char* copy_range( StringRange range ){
386 return string_clone_range( range, static_cast<Allocator&>( *this ) );
388 char* copy( const char* other ){
389 return string_clone( other, static_cast<Allocator&>( *this ) );
391 void destroy( char* string ){
392 string_release( string, string_length( string ), static_cast<Allocator&>( *this ) );
401 : m_string( copy( "" ) ){
403 explicit CopiedBuffer( const Allocator& allocator )
404 : Allocator( allocator ), m_string( copy( "" ) ){
406 CopiedBuffer( const CopiedBuffer& other )
407 : Allocator( other ), m_string( copy( other.m_string ) ){
409 CopiedBuffer( const char* string, const Allocator& allocator = Allocator() )
410 : Allocator( allocator ), m_string( copy( string ) ){
412 CopiedBuffer( StringRange range, const Allocator& allocator = Allocator() )
413 : Allocator( allocator ), m_string( copy_range( range ) ){
415 const char* c_str() const {
418 void swap( CopiedBuffer& other ){
419 string_swap( m_string, other.m_string );
423 /// \brief A non-mutable string which uses copy-by-value for assignment.
424 typedef String< CopiedBuffer< DefaultAllocator<char> > > CopiedString;
427 /// \brief A non-mutable string buffer which uses reference-counting to avoid unnecessary allocations.
428 template<typename Allocator>
429 class SmartBuffer : private Allocator
433 char* copy_range( StringRange range ){
434 char* buffer = Allocator::allocate( sizeof( std::size_t ) + ( range.last - range.first ) + 1 );
435 strncpy( buffer + sizeof( std::size_t ), range.first, range.last - range.first );
436 buffer[sizeof( std::size_t ) + ( range.last - range.first )] = '\0';
437 *reinterpret_cast<std::size_t*>( buffer ) = 0;
440 char* copy( const char* string ){
441 char* buffer = Allocator::allocate( sizeof( std::size_t ) + string_length( string ) + 1 );
442 strcpy( buffer + sizeof( std::size_t ), string );
443 *reinterpret_cast<std::size_t*>( buffer ) = 0;
446 void destroy( char* buffer ){
447 Allocator::deallocate( buffer, sizeof( std::size_t ) + string_length( c_str() ) + 1 );
450 void incref( char* buffer ){
451 ++( *reinterpret_cast<std::size_t*>( buffer ) );
453 void decref( char* buffer ){
454 if ( --( *reinterpret_cast<std::size_t*>( buffer ) ) == 0 ) {
465 : m_buffer( copy( "" ) ){
468 explicit SmartBuffer( const Allocator& allocator )
469 : Allocator( allocator ), m_buffer( copy( "" ) ){
472 SmartBuffer( const SmartBuffer& other )
473 : Allocator( other ), m_buffer( other.m_buffer ){
476 SmartBuffer( const char* string, const Allocator& allocator = Allocator() )
477 : Allocator( allocator ), m_buffer( copy( string ) ){
480 SmartBuffer( StringRange range, const Allocator& allocator = Allocator() )
481 : Allocator( allocator ), m_buffer( copy_range( range ) ){
484 const char* c_str() const {
485 return m_buffer + sizeof( std::size_t );
487 void swap( SmartBuffer& other ){
488 string_swap( m_buffer, other.m_buffer );
492 /// \brief A non-mutable string which uses copy-by-reference for assignment of SmartString.
493 typedef String< SmartBuffer< DefaultAllocator<char> > > SmartString;
495 class StringEqualNoCase
498 bool operator()( const CopiedString& key, const CopiedString& other ) const {
499 return string_equal_nocase( key.c_str(), other.c_str() );
503 struct StringLessNoCase
505 bool operator()( const CopiedString& x, const CopiedString& y ) const {
506 return string_less_nocase( x.c_str(), y.c_str() );
510 struct RawStringEqual
512 bool operator()( const char* x, const char* y ) const {
513 return string_equal( x, y );
519 bool operator()( const char* x, const char* y ) const {
520 return string_less( x, y );
524 struct RawStringLessNoCase
526 bool operator()( const char* x, const char* y ) const {
527 return string_less_nocase( x, y );