1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 ******************************************************************************
5 * Copyright (C) 2015, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ******************************************************************************
9 * File unistrappender.h
10 ******************************************************************************
13 #ifndef __UNISTRAPPENDER_H__
14 #define __UNISTRAPPENDER_H__
16 #include "unicode/unistr.h"
17 #include "unicode/uobject.h"
18 #include "unicode/utf16.h"
19 #include "unicode/utypes.h"
25 * An optimization for the slowness of calling UnicodeString::append()
26 * one character at a time in a loop. It stores appends in a buffer while
27 * never actually calling append on the unicode string unless the buffer
28 * fills up or is flushed.
32 * UnicodeStringAppender appender(astring);
33 * for (int32_t i = 0; i < 100; ++i) {
34 * appender.append((UChar) i);
36 * // appender flushed automatically when it goes out of scope.
39 class UnicodeStringAppender : public UMemory {
43 * dest is the UnicodeString being appended to. It must always
44 * exist while this instance exists.
46 UnicodeStringAppender(UnicodeString &dest) : fDest(&dest), fIdx(0) { }
48 inline void append(UChar x) {
49 if (fIdx == UPRV_LENGTHOF(fBuffer)) {
50 fDest->append(fBuffer, 0, fIdx);
56 inline void append(UChar32 x) {
57 if (fIdx >= UPRV_LENGTHOF(fBuffer) - 1) {
58 fDest->append(fBuffer, 0, fIdx);
61 U16_APPEND_UNSAFE(fBuffer, fIdx, x);
65 * Ensures that all appended characters have been written out to dest.
69 fDest->append(fBuffer, 0, fIdx);
75 * flush the buffer when we go out of scope.
77 ~UnicodeStringAppender() {
84 UnicodeStringAppender(const UnicodeStringAppender &other);
85 UnicodeStringAppender &operator=(const UnicodeStringAppender &other);