Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r83809 - trunk/tools/build/v2/engine
From: steven_at_[hidden]
Date: 2013-04-08 11:15:50


Author: steven_watanabe
Date: 2013-04-08 11:15:50 EDT (Mon, 08 Apr 2013)
New Revision: 83809
URL: http://svn.boost.org/trac/boost/changeset/83809

Log:
Simplify string_append.
Text files modified:
   trunk/tools/build/v2/engine/strings.c | 50 ++++++++++++++++++++-------------------
   1 files changed, 26 insertions(+), 24 deletions(-)

Modified: trunk/tools/build/v2/engine/strings.c
==============================================================================
--- trunk/tools/build/v2/engine/strings.c (original)
+++ trunk/tools/build/v2/engine/strings.c 2013-04-08 11:15:50 EDT (Mon, 08 Apr 2013)
@@ -99,8 +99,7 @@
 }
 
 
-static void extend_full( string * self, char const * start, char const * finish
- )
+static void extend_full( string * self, char const * start, char const * finish )
 {
     size_t new_size = self->capacity + ( finish - start );
     size_t new_capacity = self->capacity;
@@ -113,43 +112,46 @@
     self->size = new_size;
 }
 
+static void maybe_reserve( string * self, size_t new_size )
+{
+ size_t capacity = self->capacity;
+ if ( capacity <= new_size )
+ {
+ size_t new_capacity = capacity;
+ while ( new_capacity <= new_size )
+ new_capacity <<= 1;
+ string_reserve_internal( self, new_capacity );
+ }
+}
+
 
 void string_append( string * self, char const * rhs )
 {
- char * p = self->value + self->size;
- char * end = self->value + self->capacity;
+ size_t rhs_size = strlen( rhs );
+ size_t new_size = self->size + rhs_size;
     assert_invariants( self );
 
- while ( *rhs && p != end )
- *p++ = *rhs++;
+ maybe_reserve( self, new_size );
+
+ memcpy( self->value + self->size, rhs, rhs_size + 1 );
+ self->size = new_size;
 
- if ( p != end )
- {
- *p = 0;
- self->size = p - self->value;
- }
- else
- extend_full( self, rhs, rhs + strlen(rhs) );
     assert_invariants( self );
 }
 
 
 void string_append_range( string * self, char const * start, char const * finish )
 {
- char * p = self->value + self->size;
- char * end = self->value + self->capacity;
+ size_t rhs_size = finish - start;
+ size_t new_size = self->size + rhs_size;
     assert_invariants( self );
 
- while ( p != end && start != finish )
- *p++ = *start++;
+ maybe_reserve( self, new_size );
+
+ memcpy( self->value + self->size, start, rhs_size );
+ self->size = new_size;
+ self->value[ new_size ] = 0;
 
- if ( p != end )
- {
- *p = 0;
- self->size = p - self->value;
- }
- else
- extend_full( self, start, finish );
     assert_invariants( self );
 }
 


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk