ICU-13269 add StringByteSink(dest, initialAppendCapacity) constructor

X-SVN-Rev: 40277
This commit is contained in:
Markus Scherer 2017-07-20 19:56:45 +00:00
parent ac2fb96a07
commit 09b77193dc
5 changed files with 38 additions and 13 deletions

View File

@ -126,8 +126,8 @@ public:
virtual void Flush();
private:
ByteSink(const ByteSink &); // copy constructor not implemented
ByteSink &operator=(const ByteSink &); // assignment operator not implemented
ByteSink(const ByteSink &) = delete;
ByteSink &operator=(const ByteSink &) = delete;
};
// -------------------------------------------------------------
@ -217,9 +217,10 @@ private:
int32_t size_;
int32_t appended_;
UBool overflowed_;
CheckedArrayByteSink(); ///< default constructor not implemented
CheckedArrayByteSink(const CheckedArrayByteSink &); ///< copy constructor not implemented
CheckedArrayByteSink &operator=(const CheckedArrayByteSink &); ///< assignment operator not implemented
CheckedArrayByteSink() = delete;
CheckedArrayByteSink(const CheckedArrayByteSink &) = delete;
CheckedArrayByteSink &operator=(const CheckedArrayByteSink &) = delete;
};
/**
@ -236,6 +237,21 @@ class StringByteSink : public ByteSink {
* @stable ICU 4.2
*/
StringByteSink(StringClass* dest) : dest_(dest) { }
#ifndef U_HIDE_DRAFT_API
/**
* Constructs a ByteSink that reserves append capacity and will append bytes to the dest string.
*
* @param dest pointer to string object to append to
* @param initialAppendCapacity capacity beyond dest->length() to be reserve()d
* @draft ICU 60
*/
StringByteSink(StringClass* dest, int32_t initialAppendCapacity) : dest_(dest) {
if (initialAppendCapacity > 0 &&
(uint32_t)initialAppendCapacity > (dest->capacity() - dest->length())) {
dest->reserve(dest->length() + initialAppendCapacity);
}
}
#endif // U_HIDE_DRAFT_API
/**
* Append "bytes[0,n-1]" to this.
* @param data the pointer to the bytes
@ -245,9 +261,10 @@ class StringByteSink : public ByteSink {
virtual void Append(const char* data, int32_t n) { dest_->append(data, n); }
private:
StringClass* dest_;
StringByteSink(); ///< default constructor not implemented
StringByteSink(const StringByteSink &); ///< copy constructor not implemented
StringByteSink &operator=(const StringByteSink &); ///< assignment operator not implemented
StringByteSink() = delete;
StringByteSink(const StringByteSink &) = delete;
StringByteSink &operator=(const StringByteSink &) = delete;
};
U_NAMESPACE_END

View File

@ -1720,7 +1720,7 @@ public:
*/
template<typename StringClass>
StringClass &toUTF8String(StringClass &result) const {
StringByteSink<StringClass> sbs(&result);
StringByteSink<StringClass> sbs(&result, length());
toUTF8(sbs);
return result;
}

View File

@ -450,10 +450,9 @@ UBool NormalizerConformanceTest::checkNorm(UNormalizationMode mode, int32_t opti
std::string exp8;
exp.toUTF8String(exp8);
std::string out8;
out8.reserve(exp8.length());
Edits edits;
Edits *editsPtr = (mode == UNORM_NFC || mode == UNORM_NFKC) ? &edits : nullptr;
StringByteSink<std::string> sink(&out8);
StringByteSink<std::string> sink(&out8, exp8.length());
norm2->normalizeUTF8(0, s8, sink, editsPtr, errorCode);
if (U_FAILURE(errorCode)) {
errln("Normalizer2.%s.normalizeUTF8(%s) failed: %s",

View File

@ -461,7 +461,7 @@ StringTest::TestCheckedArrayByteSink() {
void
StringTest::TestStringByteSink() {
// Not much to test because only the constructor and Append()
// Not much to test because only the constructors and Append()
// are implemented, and trivially so.
std::string result("abc"); // std::string
StringByteSink<std::string> sink(&result);
@ -469,6 +469,15 @@ StringTest::TestStringByteSink() {
if(result != "abcdef") {
errln("StringByteSink did not Append() as expected");
}
StringByteSink<std::string> sink2(&result, 20);
if(result.capacity() < (result.length() + 20)) {
errln("StringByteSink should have 20 append capacity, has only %d",
(int)(result.capacity() - result.length()));
}
sink.Append("ghi", 3);
if(result != "abcdefghi") {
errln("StringByteSink did not Append() as expected");
}
}
#if defined(_MSC_VER)

View File

@ -1542,7 +1542,7 @@ BasicNormalizerTest::TestNormalizeUTF8WithEdits() {
u8" AÄA\u0308A\u0308\u00ad\u0323Ä\u0323,\u00ad\u1100\u1161\u11A8\u3133 ";
std::string expected = u8" aääạ\u0308\u0308,가각갃 ";
std::string result;
StringByteSink<std::string> sink(&result);
StringByteSink<std::string> sink(&result, expected.length());
Edits edits;
nfkc_cf->normalizeUTF8(0, src, sink, &edits, errorCode);
assertSuccess("normalizeUTF8 with Edits", errorCode.get());