diff options
Diffstat (limited to 'ace/String_Base.cpp')
-rw-r--r-- | ace/String_Base.cpp | 128 |
1 files changed, 64 insertions, 64 deletions
diff --git a/ace/String_Base.cpp b/ace/String_Base.cpp index 77fe1c48529..687339b0a9c 100644 --- a/ace/String_Base.cpp +++ b/ace/String_Base.cpp @@ -20,13 +20,11 @@ ACE_RCSID (ace, ACE_ALLOC_HOOK_DEFINE(ACE_String_Base) template <class CHAR> -CHAR ACE_String_Base<CHAR>::NULL_String_ = '\0'; + CHAR ACE_String_Base<CHAR>::NULL_String_ = 0; // this method might benefit from a little restructuring. template <class CHAR> void -ACE_String_Base<CHAR>::set (const CHAR *s, - size_t len, - int release) +ACE_String_Base<CHAR>::set (const CHAR *s, size_t len, int release) { // Case 1. Going from memory to more memory size_t new_buf_len = len + 1; @@ -36,7 +34,7 @@ ACE_String_Base<CHAR>::set (const CHAR *s, ACE_ALLOCATOR (temp, (CHAR *) this->allocator_->malloc (new_buf_len * sizeof (CHAR))); - if (this->release_) + if (this->buf_len_ != 0 && this->release_ != 0) this->allocator_->free (this->rep_); this->rep_ = temp; @@ -44,41 +42,37 @@ ACE_String_Base<CHAR>::set (const CHAR *s, this->release_ = 1; this->len_ = len; ACE_OS::memcpy (this->rep_, s, len * sizeof (CHAR)); - // NUL terminate. - this->rep_[len] = '\0'; + this->rep_[len] = 0; } - - // Case 2. No memory allocation is necessary. - else + else // Case 2. No memory allocation is necessary. { // Free memory if necessary and figure out future ownership - if (!release || s == 0 || len == 0) + if (release == 0 || s == 0 || len == 0) { - if (this->release_) + if (this->buf_len_ != 0 && this->release_ != 0) { this->allocator_->free (this->rep_); this->release_ = 0; } } - // else - stay with whatever value for release_ we have. - // Populate data. if (s == 0 || len == 0) { this->buf_len_ = 0; this->len_ = 0; this->rep_ = &ACE_String_Base<CHAR>::NULL_String_; + this->release_ = 0; } - else if (!release) + else if (release == 0) // Note: No guarantee that rep_ is null terminated. { this->buf_len_ = len; this->len_ = len; this->rep_ = const_cast <CHAR *> (s); + this->release_ = 0; } else { ACE_OS::memcpy (this->rep_, s, len * sizeof (CHAR)); - // NUL terminate. this->rep_[len] = 0; this->len_ = len; } @@ -87,8 +81,7 @@ ACE_String_Base<CHAR>::set (const CHAR *s, // Return substring. template <class CHAR> ACE_String_Base<CHAR> -ACE_String_Base<CHAR>::substring (size_t offset, - ssize_t length) const +ACE_String_Base<CHAR>::substring (size_t offset, ssize_t length) const { ACE_String_Base<CHAR> nill; size_t count = length; @@ -107,59 +100,48 @@ ACE_String_Base<CHAR>::substring (size_t offset, else if (length == -1 || count > (this->len_ - offset)) count = this->len_ - offset; - return ACE_String_Base<CHAR> (&this->rep_[offset], - count, - this->allocator_); + return ACE_String_Base<CHAR> (&this->rep_[offset], count, this->allocator_); } -// Concat operator (does copy memory). - template <class CHAR> ACE_String_Base<CHAR> & -ACE_String_Base<CHAR>::operator+= (const ACE_String_Base<CHAR> &s) +ACE_String_Base<CHAR>::append (const CHAR* s, size_t slen) { - ACE_TRACE ("ACE_String_Base<CHAR>::operator+="); - - if (s.len_ > 0) + ACE_TRACE ("ACE_String_Base<CHAR>::append(const CHAR*, size_t)"); + if (slen > 0) + { + // case 1. No memory allocation needed. + if (this->buf_len_ >= this->len_ + slen + 1) { - const size_t new_buf_len = this->len_ + s.len_ + 1; - - // case 1. No memory allocation needed. - if (this->buf_len_ >= new_buf_len) - // Copy in data from new string. - ACE_OS::memcpy (this->rep_ + this->len_, - s.rep_, - s.len_ * sizeof (CHAR)); - // case 2. Memory reallocation is needed - else - { - CHAR *t = 0; + // Copy in data from new string. + ACE_OS::memcpy (this->rep_ + this->len_, s, slen * sizeof (CHAR)); + } + else // case 2. Memory reallocation is needed + { + const size_t new_buf_len = + ace_max(this->len_ + slen + 1, this->buf_len_ + this->buf_len_ / 2); - ACE_ALLOCATOR_RETURN (t, - (CHAR *) this->allocator_->malloc (new_buf_len * - sizeof (CHAR)), - *this); + CHAR *t = 0; - // Copy memory from old string into new string. - ACE_OS::memcpy (t, - this->rep_, - this->len_ * sizeof (CHAR)); + ACE_ALLOCATOR_RETURN (t, + (CHAR *) this->allocator_->malloc (new_buf_len * sizeof (CHAR)), *this); - ACE_OS::memcpy (t + this->len_, - s.rep_, - s.len_ * sizeof (CHAR)); + // Copy memory from old string into new string. + ACE_OS::memcpy (t, this->rep_, this->len_ * sizeof (CHAR)); - if (this->release_) - this->allocator_->free (this->rep_); + ACE_OS::memcpy (t + this->len_, s, slen * sizeof (CHAR)); - this->release_ = 1; - this->rep_ = t; - this->buf_len_ = new_buf_len; - } + if (this->buf_len_ != 0 && this->release_ != 0) + this->allocator_->free (this->rep_); - this->len_ += s.len_; - this->rep_[this->len_] = '\0'; + this->release_ = 1; + this->rep_ = t; + this->buf_len_ = new_buf_len; } + this->len_ += slen; + this->rep_[this->len_] = 0; + } + return *this; } @@ -180,19 +162,37 @@ ACE_String_Base<CHAR>::resize (size_t len, CHAR c) // Only reallocate if we don't have enough space... if (this->buf_len_ <= len) { - if (this->buf_len_ != 0 && this->release_) + if (this->buf_len_ != 0 && this->release_ != 0) this->allocator_->free (this->rep_); - this->rep_ = (CHAR *) - this->allocator_->malloc ((len + 1) * sizeof (CHAR)); + this->rep_ = static_cast<CHAR*>( + this->allocator_->malloc ((len + 1) * sizeof (CHAR))); this->buf_len_ = len + 1; this->release_ = 1; } + this->len_ = 0; + ACE_OS::memset (this->rep_, c, this->buf_len_ * sizeof (CHAR)); +} +template <class CHAR> void +ACE_String_Base<CHAR>::clear (int release) +{ + // This can't use set(), because that would free memory if release=0 + if (release != 0) + { + if (this->buf_len_ != 0 && this->release_ != 0) + this->allocator_->free (this->rep_); + + this->rep_ = &ACE_String_Base<CHAR>::NULL_String_; this->len_ = 0; - ACE_OS::memset (this->rep_, - c, - this->buf_len_ * sizeof (CHAR)); + this->buf_len_ = 0; + this->release_ = 0; } + else + { + this->fast_clear (); + } +} + #endif /* ACE_STRING_BASE_CPP */ |