]> granicus.if.org Git - icu/commitdiff
ICU-13269 add StringByteSink(dest, initialAppendCapacity) constructor
authorMarkus Scherer <markus.icu@gmail.com>
Thu, 20 Jul 2017 19:56:45 +0000 (19:56 +0000)
committerMarkus Scherer <markus.icu@gmail.com>
Thu, 20 Jul 2017 19:56:45 +0000 (19:56 +0000)
X-SVN-Rev: 40277

icu4c/source/common/unicode/bytestream.h
icu4c/source/common/unicode/unistr.h
icu4c/source/test/intltest/normconf.cpp
icu4c/source/test/intltest/strtest.cpp
icu4c/source/test/intltest/tstnorm.cpp

index a31992e99e8d938e569fa64359875ba8b27756a0..6f61c384f795866b165ce95e9b5a26381f043a21 100644 (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
index ede23973c92d67633c31225dfe6b6cfa37b979f7..b99a686126c4e14faa489f844de2f783bb69c1f1 100644 (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;
   }
index 50d4a6fec05044c2df65be03502ca75188ec72ea..b8af4fd3c16405d87689273623344267d54b0605 100644 (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",
index ccc618a55444ad6fd72b046153b46b42053c864b..afb1a47272f533d40f2d318a74d8f2343964763a 100644 (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)
index 25ebdc19b089c55961e3ce952eb3dbc9add39242..240a3a81064339aeac08929c5c21912c3ef5f96b 100644 (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());