ICU-7684 add CheckedArrayByteSink::Reset()

X-SVN-Rev: 28067
This commit is contained in:
Markus Scherer 2010-05-15 23:18:12 +00:00
parent 0c2aec0b46
commit 06c7d56b8e
3 changed files with 26 additions and 4 deletions

View File

@ -26,7 +26,12 @@ void ByteSink::Flush() {}
CheckedArrayByteSink::CheckedArrayByteSink(char* outbuf, int32_t capacity)
: outbuf_(outbuf), capacity_(capacity < 0 ? 0 : capacity),
size_(0), appended_(0), overflowed_(false) {
size_(0), appended_(0), overflowed_(FALSE) {
}
CheckedArrayByteSink& CheckedArrayByteSink::Reset() {
size_ = appended_ = 0;
overflowed_ = FALSE;
}
void CheckedArrayByteSink::Append(const char* bytes, int32_t n) {
@ -37,7 +42,7 @@ void CheckedArrayByteSink::Append(const char* bytes, int32_t n) {
int32_t available = capacity_ - size_;
if (n > available) {
n = available;
overflowed_ = true;
overflowed_ = TRUE;
}
if (n > 0 && bytes != (outbuf_ + size_)) {
uprv_memcpy(outbuf_ + size_, bytes, n);

View File

@ -148,6 +148,14 @@ public:
* @stable ICU 4.2
*/
CheckedArrayByteSink(char* outbuf, int32_t capacity);
/**
* Returns the sink to its original state, without modifying the buffer.
* Useful for reusing both the buffer and the sink for multiple streams.
* Resets the state to NumberOfBytesWritten()=NumberOfBytesAppended()=0
* and Overflowed()=FALSE.
* @draft ICU 4.6
*/
CheckedArrayByteSink& Reset();
/**
* Append "bytes[0,n-1]" to this.
* @param bytes the pointer to the bytes
@ -199,7 +207,7 @@ private:
const int32_t capacity_;
int32_t size_;
int32_t appended_;
bool overflowed_;
UBool overflowed_;
CheckedArrayByteSink(); ///< default constructor not implemented
CheckedArrayByteSink(const CheckedArrayByteSink &); ///< copy constructor not implemented
CheckedArrayByteSink &operator=(const CheckedArrayByteSink &); ///< assignment operator not implemented

View File

@ -443,7 +443,8 @@ StringTest::TestCheckedArrayByteSink() {
CheckedArrayByteSink sink(buffer, (int32_t)sizeof(buffer));
sink.Append("abc", 3);
if(!(sink.NumberOfBytesAppended() == 3 && sink.NumberOfBytesWritten() == 3 &&
0 == memcmp("abc", buffer, 3) && buffer[3] == '!')
0 == memcmp("abc", buffer, 3) && buffer[3] == '!') &&
!sink.Overflowed()
) {
errln("CheckedArrayByteSink did not Append() as expected");
return;
@ -488,6 +489,14 @@ StringTest::TestCheckedArrayByteSink() {
errln("CheckedArrayByteSink did not Append(scratch buffer) as expected");
return;
}
sink.Reset().Append("123", 3);
if(!(sink.NumberOfBytesAppended() == 3 && sink.NumberOfBytesWritten() == 3 &&
0 == memcmp("123defghijklmnopqrstuvwxyz", buffer, (int32_t)sizeof(buffer)) &&
!sink.Overflowed())
) {
errln("CheckedArrayByteSink did not Reset().Append() as expected");
return;
}
}
void