]>
git.saurik.com Git - apple/icu.git/blob - icuSources/common/unistrappender.h
2 ******************************************************************************
3 * Copyright (C) 2015, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ******************************************************************************
7 * File unistrappender.h
8 ******************************************************************************
11 #ifndef __UNISTRAPPENDER_H__
12 #define __UNISTRAPPENDER_H__
14 #include "unicode/unistr.h"
15 #include "unicode/uobject.h"
16 #include "unicode/utf16.h"
17 #include "unicode/utypes.h"
23 * An optimization for the slowness of calling UnicodeString::append()
24 * one character at a time in a loop. It stores appends in a buffer while
25 * never actually calling append on the unicode string unless the buffer
26 * fills up or is flushed.
30 * UnicodeStringAppender appender(astring);
31 * for (int32_t i = 0; i < 100; ++i) {
32 * appender.append((UChar) i);
34 * // appender flushed automatically when it goes out of scope.
37 class UnicodeStringAppender
: public UMemory
{
41 * dest is the UnicodeString being appended to. It must always
42 * exist while this instance exists.
44 UnicodeStringAppender(UnicodeString
&dest
) : fDest(&dest
), fIdx(0) { }
46 inline void append(UChar x
) {
47 if (fIdx
== UPRV_LENGTHOF(fBuffer
)) {
48 fDest
->append(fBuffer
, 0, fIdx
);
54 inline void append(UChar32 x
) {
55 if (fIdx
>= UPRV_LENGTHOF(fBuffer
) - 1) {
56 fDest
->append(fBuffer
, 0, fIdx
);
59 U16_APPEND_UNSAFE(fBuffer
, fIdx
, x
);
63 * Ensures that all appended characters have been written out to dest.
67 fDest
->append(fBuffer
, 0, fIdx
);
73 * flush the buffer when we go out of scope.
75 ~UnicodeStringAppender() {
82 UnicodeStringAppender(const UnicodeStringAppender
&other
);
83 UnicodeStringAppender
&operator=(const UnicodeStringAppender
&other
);