xref: /aosp_15_r20/external/icu/libicu/cts_headers/unistrappender.h (revision 0e209d3975ff4a8c132096b14b0e9364a753506e)
1*0e209d39SAndroid Build Coastguard Worker // © 2016 and later: Unicode, Inc. and others.
2*0e209d39SAndroid Build Coastguard Worker // License & terms of use: http://www.unicode.org/copyright.html
3*0e209d39SAndroid Build Coastguard Worker /*
4*0e209d39SAndroid Build Coastguard Worker ******************************************************************************
5*0e209d39SAndroid Build Coastguard Worker * Copyright (C) 2015, International Business Machines Corporation and
6*0e209d39SAndroid Build Coastguard Worker * others. All Rights Reserved.
7*0e209d39SAndroid Build Coastguard Worker ******************************************************************************
8*0e209d39SAndroid Build Coastguard Worker *
9*0e209d39SAndroid Build Coastguard Worker * File unistrappender.h
10*0e209d39SAndroid Build Coastguard Worker ******************************************************************************
11*0e209d39SAndroid Build Coastguard Worker */
12*0e209d39SAndroid Build Coastguard Worker 
13*0e209d39SAndroid Build Coastguard Worker #ifndef __UNISTRAPPENDER_H__
14*0e209d39SAndroid Build Coastguard Worker #define __UNISTRAPPENDER_H__
15*0e209d39SAndroid Build Coastguard Worker 
16*0e209d39SAndroid Build Coastguard Worker #include "unicode/unistr.h"
17*0e209d39SAndroid Build Coastguard Worker #include "unicode/uobject.h"
18*0e209d39SAndroid Build Coastguard Worker #include "unicode/utf16.h"
19*0e209d39SAndroid Build Coastguard Worker #include "unicode/utypes.h"
20*0e209d39SAndroid Build Coastguard Worker #include "cmemory.h"
21*0e209d39SAndroid Build Coastguard Worker 
22*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_BEGIN
23*0e209d39SAndroid Build Coastguard Worker 
24*0e209d39SAndroid Build Coastguard Worker /**
25*0e209d39SAndroid Build Coastguard Worker  * An optimization for the slowness of calling UnicodeString::append()
26*0e209d39SAndroid Build Coastguard Worker  * one character at a time in a loop. It stores appends in a buffer while
27*0e209d39SAndroid Build Coastguard Worker  * never actually calling append on the unicode string unless the buffer
28*0e209d39SAndroid Build Coastguard Worker  * fills up or is flushed.
29*0e209d39SAndroid Build Coastguard Worker  *
30*0e209d39SAndroid Build Coastguard Worker  * proper usage:
31*0e209d39SAndroid Build Coastguard Worker  * {
32*0e209d39SAndroid Build Coastguard Worker  *     UnicodeStringAppender appender(astring);
33*0e209d39SAndroid Build Coastguard Worker  *     for (int32_t i = 0; i < 100; ++i) {
34*0e209d39SAndroid Build Coastguard Worker  *        appender.append((char16_t) i);
35*0e209d39SAndroid Build Coastguard Worker  *     }
36*0e209d39SAndroid Build Coastguard Worker  *     // appender flushed automatically when it goes out of scope.
37*0e209d39SAndroid Build Coastguard Worker  * }
38*0e209d39SAndroid Build Coastguard Worker  */
39*0e209d39SAndroid Build Coastguard Worker class UnicodeStringAppender : public UMemory {
40*0e209d39SAndroid Build Coastguard Worker public:
41*0e209d39SAndroid Build Coastguard Worker 
42*0e209d39SAndroid Build Coastguard Worker     /**
43*0e209d39SAndroid Build Coastguard Worker      * dest is the UnicodeString being appended to. It must always
44*0e209d39SAndroid Build Coastguard Worker      * exist while this instance exists.
45*0e209d39SAndroid Build Coastguard Worker      */
UnicodeStringAppender(UnicodeString & dest)46*0e209d39SAndroid Build Coastguard Worker     UnicodeStringAppender(UnicodeString &dest) : fDest(&dest), fIdx(0) { }
47*0e209d39SAndroid Build Coastguard Worker 
append(char16_t x)48*0e209d39SAndroid Build Coastguard Worker     inline void append(char16_t x) {
49*0e209d39SAndroid Build Coastguard Worker         if (fIdx == UPRV_LENGTHOF(fBuffer)) {
50*0e209d39SAndroid Build Coastguard Worker             fDest->append(fBuffer, 0, fIdx);
51*0e209d39SAndroid Build Coastguard Worker             fIdx = 0;
52*0e209d39SAndroid Build Coastguard Worker         }
53*0e209d39SAndroid Build Coastguard Worker         fBuffer[fIdx++] = x;
54*0e209d39SAndroid Build Coastguard Worker     }
55*0e209d39SAndroid Build Coastguard Worker 
append(UChar32 x)56*0e209d39SAndroid Build Coastguard Worker     inline void append(UChar32 x) {
57*0e209d39SAndroid Build Coastguard Worker         if (fIdx >= UPRV_LENGTHOF(fBuffer) - 1) {
58*0e209d39SAndroid Build Coastguard Worker             fDest->append(fBuffer, 0, fIdx);
59*0e209d39SAndroid Build Coastguard Worker             fIdx = 0;
60*0e209d39SAndroid Build Coastguard Worker         }
61*0e209d39SAndroid Build Coastguard Worker         U16_APPEND_UNSAFE(fBuffer, fIdx, x);
62*0e209d39SAndroid Build Coastguard Worker     }
63*0e209d39SAndroid Build Coastguard Worker 
64*0e209d39SAndroid Build Coastguard Worker     /**
65*0e209d39SAndroid Build Coastguard Worker      * Ensures that all appended characters have been written out to dest.
66*0e209d39SAndroid Build Coastguard Worker      */
flush()67*0e209d39SAndroid Build Coastguard Worker     inline void flush() {
68*0e209d39SAndroid Build Coastguard Worker         if (fIdx) {
69*0e209d39SAndroid Build Coastguard Worker             fDest->append(fBuffer, 0, fIdx);
70*0e209d39SAndroid Build Coastguard Worker         }
71*0e209d39SAndroid Build Coastguard Worker         fIdx = 0;
72*0e209d39SAndroid Build Coastguard Worker     }
73*0e209d39SAndroid Build Coastguard Worker 
74*0e209d39SAndroid Build Coastguard Worker     /**
75*0e209d39SAndroid Build Coastguard Worker      * flush the buffer when we go out of scope.
76*0e209d39SAndroid Build Coastguard Worker      */
~UnicodeStringAppender()77*0e209d39SAndroid Build Coastguard Worker     ~UnicodeStringAppender() {
78*0e209d39SAndroid Build Coastguard Worker         flush();
79*0e209d39SAndroid Build Coastguard Worker     }
80*0e209d39SAndroid Build Coastguard Worker private:
81*0e209d39SAndroid Build Coastguard Worker     UnicodeString *fDest;
82*0e209d39SAndroid Build Coastguard Worker     int32_t fIdx;
83*0e209d39SAndroid Build Coastguard Worker     char16_t fBuffer[32];
84*0e209d39SAndroid Build Coastguard Worker     UnicodeStringAppender(const UnicodeStringAppender &other);
85*0e209d39SAndroid Build Coastguard Worker     UnicodeStringAppender &operator=(const UnicodeStringAppender &other);
86*0e209d39SAndroid Build Coastguard Worker };
87*0e209d39SAndroid Build Coastguard Worker 
88*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_END
89*0e209d39SAndroid Build Coastguard Worker 
90*0e209d39SAndroid Build Coastguard Worker #endif
91