]> granicus.if.org Git - icu/commitdiff
ICU-13417 Add the internal helper class CharStringByteSink.
authorFredrik Roubert <fredrik@roubert.name>
Thu, 13 Sep 2018 03:40:14 +0000 (20:40 -0700)
committerShane Carr <shane@unicode.org>
Thu, 27 Sep 2018 21:27:39 +0000 (14:27 -0700)
This is an implementation of the public ICU ByteSink interface that
writes to the ICU internal class CharString.

icu4c/source/common/bytesinkutil.cpp
icu4c/source/common/bytesinkutil.h

index 6af7ddfd5976382bec00ea23c64fb94107121dfa..c64a845f87538e65ab4f5c67035f1c0f559b8c00 100644 (file)
@@ -11,6 +11,7 @@
 #include "unicode/utf8.h"
 #include "unicode/utf16.h"
 #include "bytesinkutil.h"
+#include "charstr.h"
 #include "cmemory.h"
 #include "uassert.h"
 
@@ -120,4 +121,41 @@ ByteSinkUtil::appendUnchanged(const uint8_t *s, const uint8_t *limit,
     return TRUE;
 }
 
+CharStringByteSink::CharStringByteSink(CharString* dest) : dest_(*dest) {
+}
+
+CharStringByteSink::~CharStringByteSink() = default;
+
+void
+CharStringByteSink::Append(const char* bytes, int32_t n) {
+    UErrorCode status = U_ZERO_ERROR;
+    dest_.append(bytes, n, status);
+    // Any errors are silently ignored.
+}
+
+char*
+CharStringByteSink::GetAppendBuffer(int32_t min_capacity,
+                                    int32_t desired_capacity_hint,
+                                    char* scratch,
+                                    int32_t scratch_capacity,
+                                    int32_t* result_capacity) {
+    if (min_capacity < 1 || scratch_capacity < min_capacity) {
+        *result_capacity = 0;
+        return nullptr;
+    }
+
+    UErrorCode status = U_ZERO_ERROR;
+    char* result = dest_.getAppendBuffer(
+            min_capacity,
+            desired_capacity_hint,
+            *result_capacity,
+            status);
+    if (U_SUCCESS(status)) {
+        return result;
+    }
+
+    *result_capacity = scratch_capacity;
+    return scratch;
+}
+
 U_NAMESPACE_END
index 8287ffea4ca713dfdc489c63cac84bbc18332f89..69e4cbcd26393268582899ab09f292fdebc8005b 100644 (file)
@@ -13,6 +13,7 @@
 U_NAMESPACE_BEGIN
 
 class ByteSink;
+class CharString;
 class Edits;
 
 class U_COMMON_API ByteSinkUtil {
@@ -58,4 +59,25 @@ private:
                                         ByteSink &sink, uint32_t options, Edits *edits);
 };
 
+class CharStringByteSink : public ByteSink {
+public:
+    CharStringByteSink(CharString* dest);
+    ~CharStringByteSink() override;
+
+    CharStringByteSink() = delete;
+    CharStringByteSink(const CharStringByteSink&) = delete;
+    CharStringByteSink& operator=(const CharStringByteSink&) = delete;
+
+    void Append(const char* bytes, int32_t n) override;
+
+    char* GetAppendBuffer(int32_t min_capacity,
+                          int32_t desired_capacity_hint,
+                          char* scratch,
+                          int32_t scratch_capacity,
+                          int32_t* result_capacity) override;
+
+private:
+    CharString& dest_;
+};
+
 U_NAMESPACE_END