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
25 #include "globaldefs.h"
28 /// C-style null-terminated-character-array string library.
34 #include "memory/allocator.h"
35 #include "generic/arrayrange.h"
37 /// \brief Returns true if \p string length is zero.
39 inline bool string_empty( const char* string ){
40 return *string == '\0';
43 /// \brief Returns true if \p string length is not zero.
45 inline bool string_not_empty( const char* string ){
46 return !string_empty( string );
49 /// \brief Returns <0 if \p string is lexicographically less than \p other.
50 /// Returns >0 if \p string is lexicographically greater than \p other.
51 /// Returns 0 if \p string is lexicographically equal to \p other.
53 inline int string_compare( const char* string, const char* other ){
54 return std::strcmp( string, other );
57 /// \brief Returns true if \p string is lexicographically equal to \p other.
59 inline bool string_equal( const char* string, const char* other ){
60 return string_compare( string, other ) == 0;
63 /// \brief Returns true if [\p string, \p string + \p n) is lexicographically equal to [\p other, \p other + \p n).
65 inline bool string_equal_n( const char* string, const char* other, std::size_t n ){
66 return std::strncmp( string, other, n ) == 0;
69 /// \brief Returns true if \p string is lexicographically less than \p other.
71 inline bool string_less( const char* string, const char* other ){
72 return string_compare( string, other ) < 0;
75 /// \brief Returns true if \p string is lexicographically greater than \p other.
77 inline bool string_greater( const char* string, const char* other ){
78 return string_compare( string, other ) > 0;
81 /// \brief Returns <0 if \p string is lexicographically less than \p other after converting both to lower-case.
82 /// Returns >0 if \p string is lexicographically greater than \p other after converting both to lower-case.
83 /// Returns 0 if \p string is lexicographically equal to \p other after converting both to lower-case.
85 inline int string_compare_nocase( const char* string, const char* other ){
87 return _stricmp( string, other );
89 return strcasecmp( string, other );
93 /// \brief Returns <0 if [\p string, \p string + \p n) is lexicographically less than [\p other, \p other + \p n).
94 /// Returns >0 if [\p string, \p string + \p n) is lexicographically greater than [\p other, \p other + \p n).
95 /// Returns 0 if [\p string, \p string + \p n) is lexicographically equal to [\p other, \p other + \p n).
96 /// Treats all ascii characters as lower-case during comparisons.
98 inline int string_compare_nocase_n( const char* string, const char* other, std::size_t n ){
100 return _strnicmp( string, other, n );
102 return strncasecmp( string, other, n );
106 /// \brief Returns true if \p string is lexicographically equal to \p other.
107 /// Treats all ascii characters as lower-case during comparisons.
109 inline bool string_equal_nocase( const char* string, const char* other ){
110 return string_compare_nocase( string, other ) == 0;
113 /// \brief Returns true if [\p string, \p string + \p n) is lexicographically equal to [\p other, \p other + \p n).
114 /// Treats all ascii characters as lower-case during comparisons.
116 inline bool string_equal_nocase_n( const char* string, const char* other, std::size_t n ){
117 return string_compare_nocase_n( string, other, n ) == 0;
120 /// \brief Returns true if \p string is lexicographically less than \p other.
121 /// Treats all ascii characters as lower-case during comparisons.
123 inline bool string_less_nocase( const char* string, const char* other ){
124 return string_compare_nocase( string, other ) < 0;
127 /// \brief Returns true if \p string is lexicographically greater than \p other.
128 /// Treats all ascii characters as lower-case during comparisons.
130 inline bool string_greater_nocase( const char* string, const char* other ){
131 return string_compare_nocase( string, other ) > 0;
134 /// \brief Returns the number of non-null characters in \p string.
136 inline std::size_t string_length( const char* string ){
137 return std::strlen( string );
140 /// \brief Returns true if the beginning of \p string is equal to \p prefix.
142 inline bool string_equal_prefix( const char* string, const char* prefix ){
143 return string_equal_n( string, prefix, string_length( prefix ) );
146 /// \brief Returns true if the ending of \p string is equal to \p suffix.
148 inline bool string_equal_suffix( const char* string, const char* suffix){
149 const char *s = string + string_length( string ) - string_length( suffix );
150 return string_equal_n( s , suffix, string_length( suffix ) );
153 inline bool string_equal_suffix_nocase( const char* string, const char* suffix){
154 const char *s = string + string_length( string ) - string_length( suffix );
155 return string_equal_nocase_n( s , suffix, string_length( suffix ) );
158 /// \brief Copies \p other into \p string and returns \p string.
159 /// Assumes that the space allocated for \p string is at least string_length(other) + 1.
161 inline char* string_copy( char* string, const char* other ){
162 return std::strcpy( string, other );
165 /// \brief Allocates a string buffer large enough to hold \p length characters, using \p allocator.
166 /// The returned buffer must be released with \c string_release using a matching \p allocator.
167 template<typename Allocator>
168 inline char* string_new( std::size_t length, Allocator& allocator ){
169 return allocator.allocate( length + 1 );
172 /// \brief Deallocates the \p buffer large enough to hold \p length characters, using \p allocator.
173 template<typename Allocator>
174 inline void string_release( char* buffer, std::size_t length, Allocator& allocator ){
175 allocator.deallocate( buffer, length + 1 );
178 /// \brief Returns a newly-allocated string which is a clone of \p other, using \p allocator.
179 /// The returned buffer must be released with \c string_release using a matching \p allocator.
180 template<typename Allocator>
181 inline char* string_clone( const char* other, Allocator& allocator ){
182 char* copied = string_new( string_length( other ), allocator );
183 std::strcpy( copied, other );
187 /// \brief Returns a newly-allocated string which is a clone of [\p first, \p last), using \p allocator.
188 /// The returned buffer must be released with \c string_release using a matching \p allocator.
189 template<typename Allocator>
190 inline char* string_clone_range( StringRange range, Allocator& allocator ){
191 std::size_t length = range.last - range.first;
192 char* copied = strncpy( string_new( length, allocator ), range.first, length );
193 copied[length] = '\0';
197 /// \brief Allocates a string buffer large enough to hold \p length characters.
198 /// The returned buffer must be released with \c string_release.
199 inline char* string_new( std::size_t length ){
200 DefaultAllocator<char> allocator;
201 return string_new( length, allocator );
204 /// \brief Allocates a new buffer large enough to hold two concatenated strings and fills it with strings.
205 inline char* string_new_concat( const char* a, const char* b ){
206 char* str = string_new( string_length( a ) + string_length( b ) );
212 /// \brief Deallocates the \p buffer large enough to hold \p length characters.
213 inline void string_release( char* string, std::size_t length ){
214 DefaultAllocator<char> allocator;
215 string_release( string, length, allocator );
218 /// \brief Returns a newly-allocated string which is a clone of \p other.
219 /// The returned buffer must be released with \c string_release.
220 inline char* string_clone( const char* other ){
221 DefaultAllocator<char> allocator;
222 return string_clone( other, allocator );
225 /// \brief Returns a newly-allocated string which is a clone of [\p first, \p last).
226 /// The returned buffer must be released with \c string_release.
227 inline char* string_clone_range( StringRange range ){
228 DefaultAllocator<char> allocator;
229 return string_clone_range( range, allocator );
232 typedef char* char_pointer;
233 /// \brief Swaps the values of \p string and \p other.
234 inline void string_swap( char_pointer& string, char_pointer& other ){
235 std::swap( string, other );
238 typedef const char* char_const_pointer;
239 /// \brief Swaps the values of \p string and \p other.
240 inline void string_swap( char_const_pointer& string, char_const_pointer& other ){
241 std::swap( string, other );
244 /// \brief Converts each character of \p string to lower-case and returns \p string.
246 inline char* string_to_lowercase( char* string ){
247 for ( char* p = string; *p != '\0'; ++p )
249 *p = (char)std::tolower( *p );
254 /// \brief Converts each character of \p string to upper-case and returns \p string.
256 inline char* string_to_uppercase( char* string ){
257 for ( char* p = string; *p != '\0'; ++p )
259 *p = (char)std::toupper( *p );
264 /// \brief A re-entrant string tokeniser similar to strchr.
265 class StringTokeniser
268 bool istoken( char c ) const {
269 if ( strchr( m_delimiters, c ) != 0 ) {
274 const char* advance(){
275 const char* token = m_pos;
277 while ( !string_empty( m_pos ) )
279 if ( !istoken( *m_pos ) ) {
283 else if ( !intoken ) {
290 std::size_t m_length;
293 const char* m_delimiters;
296 StringTokeniser( const char* string, const char* delimiters = " \n\r\t\v" ) :
297 m_length( string_length( string ) ),
298 m_string( string_copy( string_new( m_length ), string ) ),
300 m_delimiters( delimiters ){
301 while ( !string_empty( m_pos ) && !istoken( *m_pos ) )
307 string_release( m_string, m_length );
309 /// \brief Returns the next token or "" if there are no more tokens available.
310 const char* getToken(){
315 /// \brief A non-mutable c-style string.
317 /// \param Buffer The string storage implementation. Must be DefaultConstructible, CopyConstructible and Assignable. Must implement:
318 /// \li Buffer(const char* string) - constructor which copies a c-style \p string.
319 /// \li Buffer(const char* first, const char*) - constructor which copies a c-style string range [\p first, \p last).
320 /// \li void swap(Buffer& other) - swaps contents with \p other.
321 /// \li const char* c_str() - returns the stored non-mutable c-style string.
322 template<typename Buffer>
323 class String : public Buffer
329 String( const char* string )
332 String( StringRange range )
336 String(const String&) = default;
338 String& operator=( const String& other ){
339 String temp( other );
343 String& operator=( const char* string ){
344 String temp( string );
348 String& operator=( StringRange range ){
349 String temp( range );
354 void swap( String& other ){
355 Buffer::swap( other );
359 return string_empty( Buffer::c_str() );
363 template<typename Buffer>
364 inline bool operator<( const String<Buffer>& self, const String<Buffer>& other ){
365 return string_less( 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_greater( self.c_str(), other.c_str() );
373 template<typename Buffer>
374 inline bool operator==( const String<Buffer>& self, const String<Buffer>& other ){
375 return string_equal( self.c_str(), other.c_str() );
378 template<typename Buffer>
379 inline bool operator!=( const String<Buffer>& self, const String<Buffer>& other ){
380 return !string_equal( self.c_str(), other.c_str() );
383 template<typename Buffer>
384 inline bool operator==( const String<Buffer>& self, const char* other ){
385 return string_equal( self.c_str(), other );
388 template<typename Buffer>
389 inline bool operator!=( const String<Buffer>& self, const char* other ){
390 return !string_equal( self.c_str(), other );
395 /// \brief Swaps the values of \p self and \p other.
396 /// Overloads std::swap.
397 template<typename Buffer>
398 inline void swap( String<Buffer>& self, String<Buffer>& other ){
404 /// \brief A non-mutable string buffer which manages memory allocation.
405 template<typename Allocator>
406 class CopiedBuffer : private Allocator
410 char* copy_range( StringRange range ){
411 return string_clone_range( range, static_cast<Allocator&>( *this ) );
413 char* copy( const char* other ){
414 return string_clone( other, static_cast<Allocator&>( *this ) );
416 void destroy( char* string ){
417 string_release( string, string_length( string ), static_cast<Allocator&>( *this ) );
427 : m_string( copy( "" ) ){
429 explicit CopiedBuffer( const Allocator& allocator )
430 : Allocator( allocator ), m_string( copy( "" ) ){
432 CopiedBuffer( const CopiedBuffer& other )
433 : Allocator( other ), m_string( copy( other.m_string ) ){
435 CopiedBuffer( const char* string, const Allocator& allocator = Allocator() )
436 : Allocator( allocator ), m_string( copy( string ) ){
438 CopiedBuffer( StringRange range, const Allocator& allocator = Allocator() )
439 : Allocator( allocator ), m_string( copy_range( range ) ){
441 const char* c_str() const {
444 void swap( CopiedBuffer& other ){
445 string_swap( m_string, other.m_string );
449 /// \brief A non-mutable string which uses copy-by-value for assignment.
450 typedef String< CopiedBuffer< DefaultAllocator<char> > > CopiedString;
453 /// \brief A non-mutable string buffer which uses reference-counting to avoid unnecessary allocations.
454 template<typename Allocator>
455 class SmartBuffer : private Allocator
460 char* copy_range( StringRange range ){
461 char* buffer = Allocator::allocate( sizeof( std::size_t ) + ( range.last - range.first ) + 1 );
462 strncpy( buffer + sizeof( std::size_t ), range.first, range.last - range.first );
463 buffer[sizeof( std::size_t ) + ( range.last - range.first )] = '\0';
464 *reinterpret_cast<std::size_t*>( buffer ) = 0;
467 char* copy( const char* string ){
468 char* buffer = Allocator::allocate( sizeof( std::size_t ) + string_length( string ) + 1 );
469 strcpy( buffer + sizeof( std::size_t ), string );
470 *reinterpret_cast<std::size_t*>( buffer ) = 0;
473 void destroy( char* buffer ){
474 Allocator::deallocate( buffer, sizeof( std::size_t ) + string_length( c_str() ) + 1 );
477 void incref( char* buffer ){
478 ++( *reinterpret_cast<std::size_t*>( buffer ) );
480 void decref( char* buffer ){
481 if ( --( *reinterpret_cast<std::size_t*>( buffer ) ) == 0 ) {
493 : m_buffer( copy( "" ) ){
496 explicit SmartBuffer( const Allocator& allocator )
497 : Allocator( allocator ), m_buffer( copy( "" ) ){
500 SmartBuffer( const SmartBuffer& other )
501 : Allocator( other ), m_buffer( other.m_buffer ){
504 SmartBuffer( const char* string, const Allocator& allocator = Allocator() )
505 : Allocator( allocator ), m_buffer( copy( string ) ){
508 SmartBuffer( StringRange range, const Allocator& allocator = Allocator() )
509 : Allocator( allocator ), m_buffer( copy_range( range ) ){
512 const char* c_str() const {
513 return m_buffer + sizeof( std::size_t );
515 void swap( SmartBuffer& other ){
516 string_swap( m_buffer, other.m_buffer );
520 /// \brief A non-mutable string which uses copy-by-reference for assignment of SmartString.
521 typedef String< SmartBuffer< DefaultAllocator<char> > > SmartString;
523 class StringEqualNoCase
526 bool operator()( const CopiedString& key, const CopiedString& other ) const {
527 return string_equal_nocase( key.c_str(), other.c_str() );
531 struct StringLessNoCase
533 bool operator()( const CopiedString& x, const CopiedString& y ) const {
534 return string_less_nocase( x.c_str(), y.c_str() );
538 struct RawStringEqual
540 bool operator()( const char* x, const char* y ) const {
541 return string_equal( x, y );
547 bool operator()( const char* x, const char* y ) const {
548 return string_less( x, y );
552 struct RawStringLessNoCase
554 bool operator()( const char* x, const char* y ) const {
555 return string_less_nocase( x, y );