Author: sebor
Date: Sat Feb 25 13:16:12 2006
New Revision: 380995
URL: http://svn.apache.org/viewcvs?rev=380995&view=rev
Log:
2006-02-25 Martin Sebor <[EMAIL PROTECTED]>
STDCXX-142
* _defs.h (_RWSTD_MINIMUM_STRINGBUF_CAPACITY): New macro.
* sstream (xsputn): Added a declaration.
(_C_grow): Removed an unnecessary function argument, used the
new _RWSTD_MINIMUM_STRINGBUF_CAPACITY macro, and simplified.
* sstream.cc (str): Extended to allow to be called with own
buffer as an argument to grow it.
(xsputn): Defined for better efficiency than the base class
function.
(overflow): Called str() for code reuse.
(setbuf): Set the "high mark" in out mode.
Updated copyrights.
Modified:
incubator/stdcxx/trunk/include/rw/_defs.h
incubator/stdcxx/trunk/include/sstream
incubator/stdcxx/trunk/include/sstream.cc
Modified: incubator/stdcxx/trunk/include/rw/_defs.h
URL:
http://svn.apache.org/viewcvs/incubator/stdcxx/trunk/include/rw/_defs.h?rev=380995&r1=380994&r2=380995&view=diff
==============================================================================
--- incubator/stdcxx/trunk/include/rw/_defs.h (original)
+++ incubator/stdcxx/trunk/include/rw/_defs.h Sat Feb 25 13:16:12 2006
@@ -9,16 +9,22 @@
*
***************************************************************************
*
- * Copyright (c) 1994-2005 Quovadx, Inc., acting through its Rogue Wave
- * Software division. Licensed under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance with the
- * License. You may obtain a copy of the License at
- * http://www.apache.org/licenses/LICENSE-2.0. Unless required by
- * applicable law or agreed to in writing, software distributed under
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
- * CONDITIONS OF ANY KIND, either express or implied. See the License
- * for the specific language governing permissions and limitations under
- * the License.
+ * Copyright 2005-2006 The Apache Software Foundation or its licensors,
+ * as applicable.
+ *
+ * Copyright 1994-2006 Rogue Wave Software.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
*
**************************************************************************/
@@ -431,6 +437,10 @@
#if !defined(_RWSTD_STRING_CAPACITY_RATIO)
# define _RWSTD_STRING_CAPACITY_RATIO 1.618L
#endif
+
+#if !defined (_RWSTD_MINIMUM_STRINGBUF_CAPACITY)
+# define _RWSTD_MINIMUM_STRINGBUF_CAPACITY 512
+#endif // _RWSTD_MINIMUM_STRINGBUF_CAPACITY
// working around an HP aCC bug (PR #25354 and 22610)
#if !defined (__HP_aCC)
Modified: incubator/stdcxx/trunk/include/sstream
URL:
http://svn.apache.org/viewcvs/incubator/stdcxx/trunk/include/sstream?rev=380995&r1=380994&r2=380995&view=diff
==============================================================================
--- incubator/stdcxx/trunk/include/sstream (original)
+++ incubator/stdcxx/trunk/include/sstream Sat Feb 25 13:16:12 2006
@@ -7,16 +7,22 @@
*
***************************************************************************
*
- * Copyright (c) 1994-2005 Quovadx, Inc., acting through its Rogue Wave
- * Software division. Licensed under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance with the
- * License. You may obtain a copy of the License at
- * http://www.apache.org/licenses/LICENSE-2.0. Unless required by
- * applicable law or agreed to in writing, software distributed under
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
- * CONDITIONS OF ANY KIND, either express or implied. See the License
- * for the specific language governing permissions and limitations under
- * the License.
+ * Copyright 2006 The Apache Software Foundation or its licensors,
+ * as applicable.
+ *
+ * Copyright 1994-2006 Rogue Wave Software.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
*
**************************************************************************/
@@ -126,7 +132,9 @@
virtual ~basic_stringbuf ();
_C_string_type str () const {
- return _C_string_type (this->_C_buffer, _C_strlen ());
+ const _RWSTD_SIZE_T __slen = (this->egptr () < this->pptr () ?
+ this->pptr () : this->egptr ()) - this->pbase ();
+ return _C_string_type (this->_C_buffer, __slen);
}
#ifdef _RWSTD_NO_EXT_STRINGBUF_STR
@@ -146,6 +154,8 @@
protected:
+ virtual streamsize xsputn (const char_type*, streamsize);
+
virtual streamsize showmanyc ();
virtual int_type overflow (int_type = traits_type::eof ());
@@ -166,14 +176,7 @@
private:
- // returns the length of the underlying sequence as specified
- // by 27.7.1.2, p1
- streamsize _C_strlen () const {
- return ios_base::in == (this->_C_state & (ios_base::in |
ios_base::out))
- ? this->egptr () - this->eback () : this->pptr () - this->pbase ();
- }
-
- _RWSTD_STREAMSIZE _C_grow (_RWSTD_STREAMSIZE, _RWSTD_STREAMSIZE) const;
+ _RWSTD_STREAMSIZE _C_grow (_RWSTD_STREAMSIZE) const;
// called from overflow, underflow, et al to get egptr()
// caught up with pptr()
@@ -184,12 +187,15 @@
template <class _CharT, class _Traits, class _Allocator>
inline _RWSTD_STREAMSIZE
basic_stringbuf<_CharT, _Traits, _Allocator>::
-_C_grow (_RWSTD_STREAMSIZE __from, _RWSTD_STREAMSIZE __to) const
+_C_grow (_RWSTD_STREAMSIZE __to) const
{
- const _RWSTD_STREAMSIZE __new_cap =
- _RWSTD_NEW_CAPACITY (_C_string_type, (_C_string_type*)0, __from);
+ const _RWSTD_STREAMSIZE __cap =
+ _RWSTD_STATIC_CAST (_RWSTD_STREAMSIZE,
+ this->_C_bufsize ?
+ this->_C_bufsize * _RWSTD_NEW_CAPACITY_RATIO
+ : _RWSTD_MINIMUM_STRINGBUF_CAPACITY);
- return __new_cap < __to ? __to : __new_cap;
+ return __cap < __to ? __to : __cap;
}
Modified: incubator/stdcxx/trunk/include/sstream.cc
URL:
http://svn.apache.org/viewcvs/incubator/stdcxx/trunk/include/sstream.cc?rev=380995&r1=380994&r2=380995&view=diff
==============================================================================
--- incubator/stdcxx/trunk/include/sstream.cc (original)
+++ incubator/stdcxx/trunk/include/sstream.cc Sat Feb 25 13:16:12 2006
@@ -6,21 +6,27 @@
*
***************************************************************************
*
- * Copyright (c) 1994-2005 Quovadx, Inc., acting through its Rogue Wave
- * Software division. Licensed under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance with the
- * License. You may obtain a copy of the License at
- * http://www.apache.org/licenses/LICENSE-2.0. Unless required by
- * applicable law or agreed to in writing, software distributed under
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
- * CONDITIONS OF ANY KIND, either express or implied. See the License
- * for the specific language governing permissions and limitations under
- * the License.
- *
+ * Copyright 2006 The Apache Software Foundation or its licensors,
+ * as applicable.
+ *
+ * Copyright 1994-2006 Rogue Wave Software.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
**************************************************************************/
-_RWSTD_NAMESPACE (std) {
+_RWSTD_NAMESPACE (std) {
template<class _CharT, class _Traits, class _Allocator>
@@ -68,53 +74,125 @@
typedef _RWSTD_STREAMSIZE _Streamsize;
+ // compute the lenth if not specified
if (_RWSTD_SIZE_MAX == __slen)
__slen = traits_type::length (__s);
- if (0 == __slen) {
+ _ValueAlloc __alloc;
- if (this->_C_own_buf ())
- _ValueAlloc ().deallocate (this->_C_buffer, this->_C_bufsize);
+ // new buffer and size
+ char_type *__buf;
+ _RWSTD_SIZE_T __bufsize = __slen;
+
+ if (__s == this->_C_buffer) {
+ // special case: str(_C_buffer, _C_bufsize + N) called
+ // to increase the capacity of buffer
- this->setg (0, 0, 0);
- this->setp (0, 0);
+ _C_catchup (this->eback ());
- this->_C_buffer = 0;
- this->_C_bufsize = 0;
+ // set `slen' to the number of initialized characters
+ // in the buffer
+ __slen = this->egptr () - this->pbase ();
+ }
+
+ if (this->_C_bufsize < __bufsize) {
+ // requested capacity is greater than the current capacity
+ // allocate a new buffer of sufficient size
+ __bufsize = _C_grow (__bufsize);
+
+ if (__s != this->_C_buffer && this->_C_own_buf ()) {
+ // deallocate the existing buffer here only if the string
+ // is not the same as the buffer itself; otherwise, copy
+ // it to the newly allocated buffer first and deallocate
+ // it later
+ __alloc.deallocate (this->_C_buffer, this->_C_bufsize);
+ this->_C_buffer = 0;
+ }
+
+ __buf = __alloc.allocate (__bufsize);
+
+ // take the ownsership of the allocated buffer
+ this->_C_own_buf (true);
+ }
+ else if (0 < __bufsize) {
+ // requested capacity is the same or less than the current one
+ __buf = this->_C_buffer;
+ __bufsize = this->_C_bufsize;
}
else {
- if (this->_C_bufsize < _Streamsize (__slen)) {
+ // 0 size and capacity, deallocate and reset all pointers
+ __buf = 0;
+ __bufsize = 0;
- // buffer too small - need to reallocate
- if (this->_C_own_buf ())
- _ValueAlloc ().deallocate (this->_C_buffer, this->_C_bufsize);
+ _RWSTD_ASSERT (0 == __slen);
+ }
- this->_C_bufsize = _C_grow (this->_C_bufsize, __slen);
+ // compute the "high mark" (see lwg issue 432)
+ char_type* const __egptr = __buf + __slen;
- this->_C_buffer = _ValueAlloc ().allocate (this->_C_bufsize);
- this->_C_own_buf (true);
- }
+ if (__s != __buf) {
+ // copy the provided string to buffer
+ traits_type::copy (__buf, __s, __slen);
- traits_type::copy (this->_C_buffer, __s, __slen);
+ if (this->_C_buffer && this->_C_own_buf ())
+ __alloc.deallocate (this->_C_buffer, this->_C_bufsize);
- char_type* const __bufend = this->_C_buffer + __slen;
-
- if (this->_C_is_in ())
- this->setg (this->_C_buffer, this->_C_buffer, __bufend);
- else {
- // when not in in mode set all get pointers to the same
- // value and use egptr() as the "high mark" (see LWG
- // issue 432)
- this->setg (__bufend, __bufend, __bufend);
- }
-
- if (this->_C_is_out ()) {
- this->setp (this->_C_buffer, this->_C_buffer + this->_C_bufsize);
-
- if (this->_C_state & (ios_base::app | ios_base::ate))
- this->pbump (__slen); // seek to end
- }
+ this->_C_buffer = __buf;
+ this->_C_bufsize = __bufsize;
+ }
+
+ if (this->_C_is_in ())
+ this->setg (this->_C_buffer, this->_C_buffer, __egptr);
+ else {
+ // when not in in mode set all get pointers to the same
+ // value and use egptr() as the "high mark" (see lwg
+ // issue 432)
+ this->setg (__egptr, __egptr, __egptr);
+ }
+
+ if (this->_C_is_out ()) {
+ this->setp (this->_C_buffer, this->_C_buffer + this->_C_bufsize);
+
+ if (__s != __buf || this->_C_state & (ios_base::app | ios_base::ate))
+ this->pbump (__slen); // seek to end
}
+
+ _RWSTD_ASSERT (this->_C_is_valid ());
+}
+
+
+template <class _CharT, class _Traits, class _Allocator>
+streamsize
+basic_stringbuf<_CharT, _Traits, _Allocator>::
+xsputn (const char_type* __s, streamsize __n)
+{
+ _RWSTD_ASSERT (0 != __s || 0 == __n);
+ _RWSTD_ASSERT (this->_C_is_valid ());
+
+ if (__n <= 0 || !this->_C_is_out ())
+ return 0;
+
+ if (this->epptr () - this->pptr () < __n) {
+
+ // compute the total amount of space necessary
+ const _RWSTD_SIZE_T __bufsize =
+ __n + (this->pptr () - this->pbase ());
+
+ // grow the buffer if necessary to accommodate the whole
+ // string plus the contents of the buffer up to pptr()
+ str (this->_C_buffer, __bufsize);
+
+ _RWSTD_ASSERT (__n <= this->epptr () - this->pptr ());
+ }
+
+ // copy the whole string
+ traits_type::copy (this->pptr (), __s, __n);
+
+ this->pbump (__n);
+
+ _C_catchup (this->eback ());
+
+ return __n;
}
@@ -169,50 +247,29 @@
// indicate success even when not in out mode
if (this->_C_is_eof (__c))
return traits_type::not_eof (__c);
-
- if (!this->_C_is_out ())
+
+ if (!this->_C_is_out ())
return traits_type::eof ();
-
+
char_type* const __bufend = this->_C_buffer + this->_C_bufsize;
-
- if (this->epptr () < __bufend) {
+
+ if (this->pptr () == this->epptr ()) {
+
+ // compute the total amount of space necessary
+ const _RWSTD_SIZE_T __bufsize = this->_C_bufsize ?
+ this->_C_bufsize * _RWSTD_NEW_CAPACITY_RATIO
+ : _RWSTD_MINIMUM_STRINGBUF_CAPACITY;
+
+ // reallocate buffer
+ str (this->_C_buffer, this->_C_bufsize + 1);
+ }
+ else if (this->epptr () < __bufend) {
// bump up epptr() keeping pbase() and pptr() unchanged
const _RWSTD_STREAMSIZE __off = this->pptr () - this->pbase ();
this->setp (this->pbase (), __bufend);
this->pbump (__off);
}
- else if (this->pptr () == this->epptr ()) {
- // allocate new or reallocate existing buffer
-
- typedef _RWSTD_ALLOC_TYPE (allocator_type, char_type) _ValueAlloc;
-
- // calculate size of the new buffer to allocate
- const _RWSTD_STREAMSIZE __new_size =
- _C_grow (this->_C_bufsize + 1, this->_C_bufsize);
-
- char_type* const __new_buf = _ValueAlloc ().allocate (__new_size);
-
- // compute the length of the output sequence
- const _RWSTD_STREAMSIZE __slen = this->pptr () - this->pbase ();
-
- if (this->_C_buffer) {
- // copy the contents of the old buffer to the new one
- traits_type::copy (__new_buf, this->_C_buffer, __slen);
-
- // deallocate the old buffer if owned
- if (this->_C_own_buf ())
- _ValueAlloc ().deallocate (this->_C_buffer, this->_C_bufsize);
- }
-
- this->_C_own_buf (true);
- this->_C_bufsize = __new_size;
- this->_C_buffer = __new_buf;
-
- // set the put area
- this->setp (this->_C_buffer, this->_C_buffer + this->_C_bufsize);
- this->pbump (__slen);
- }
const int_type __retval = this->sputc (traits_type::to_char_type (__c));
@@ -236,7 +293,7 @@
int_type __retval;
const char_type __ch = traits_type::to_char_type (__c);
-
+
if (traits_type::eq (__ch, *(this->gptr () - 1)) || this->_C_is_eof (__c))
{
// "put back" original value
this->gbump (-1);
@@ -250,7 +307,7 @@
}
else
__retval = traits_type::eof ();
-
+
return __retval;
}
@@ -265,39 +322,50 @@
if (!__buf && !__n) // 27.7.1.3, p16
return this;
- if (__n < _C_strlen() || !this->_C_is_out())
+ const _RWSTD_STREAMSIZE __slen = (this->egptr () < this->pptr () ?
+ this->pptr () : this->egptr ()) - this->pbase ();
+
+ if (__n < __slen || !this->_C_is_out())
return 0; // failure
- bool __own_old_buf = this->_C_own_buf ();
+ // compute the gptr and pptr offsets so the pointers can be restored
+ const _RWSTD_STREAMSIZE __goff = this->gptr () - this->eback ();
+ const _RWSTD_STREAMSIZE __poff = this->pptr () - this->pbase ();
- typedef _RWSTD_STREAMSIZE _Streamsize;
+ const bool __own_old_buf = this->_C_own_buf ();
- const _Streamsize __slen = _C_strlen ();
-
typedef _RWSTD_ALLOC_TYPE (allocator_type, char_type) _ValueAlloc;
if (0 == __buf) {
+ // allocate a new buffer of the specified size
__buf = _ValueAlloc ().allocate (__n);
this->_C_own_buf (true);
}
else
this->_C_own_buf (false);
-
- traits_type::copy (__buf, this->_C_buffer, __slen);
+
+ // copy the contents of the existing buffer to the new one
+ traits_type::copy (__buf, this->_C_buffer, __slen);
if (__own_old_buf)
_ValueAlloc ().deallocate (this->_C_buffer, this->_C_bufsize);
-
+
this->_C_buffer = __buf;
this->_C_bufsize = __n;
- const _Streamsize __pptr_off = _Streamsize(this->pptr () - this->pbase ());
- this->setp (this->_C_buffer, this->_C_buffer + __slen);
- this->pbump (__pptr_off); // ... and restore it
-
- // get egptr() caught up with pptr()
- _C_catchup (this->_C_buffer);
-
+ // reset the output and input sequences within the new buffer
+ this->setp (this->_C_buffer, this->_C_buffer + this->_C_bufsize);
+ this->pbump (__poff); // ... and restore it
+
+ char_type* const __egptr = this->_C_buffer + __slen;
+
+ if (this->_C_is_in ())
+ this->setg (this->_C_buffer, this->_C_buffer + __goff, __egptr);
+ else {
+ // use egptr as the "high mark" (see lwg issue 432)
+ this->setg (__egptr, __egptr, __egptr);
+ }
+
return this;
}
@@ -313,7 +381,7 @@
_RWSTD_ASSERT ( ios_base::beg == __way
|| ios_base::cur == __way
|| ios_base::end == __way);
-
+
_RWSTD_STREAMSIZE __newoff = -1;
// get egptr() caught up with pptr()
@@ -337,12 +405,12 @@
__newoff += __off;
- if ( __newoff < 0 || (this->egptr () - this->eback ()) < __newoff)
+ if (__newoff < 0 || this->egptr () - this->eback () < __newoff)
return pos_type (off_type (-1));
this->setg (this->eback (), this->eback () + __newoff, this->egptr ());
}
-
+
if (__which & ios_base::out) {
if (!this->_C_is_out () || !this->pptr ())