1 // © 2017 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 #include "unicode/utypes.h"
6 #if !UCONFIG_NO_FORMATTING
7 #ifndef __NUMBER_STRINGBUILDER_H__
8 #define __NUMBER_STRINGBUILDER_H__
12 #include "unicode/numfmt.h"
13 #include "unicode/ustring.h"
16 #include "number_types.h"
19 U_NAMESPACE_BEGIN
namespace number
{
22 class U_I18N_API NumberStringBuilder
: public UMemory
{
24 static const int32_t DEFAULT_CAPACITY
= 40;
27 union ValueOrHeapArray
{
28 T value
[DEFAULT_CAPACITY
];
36 NumberStringBuilder();
38 ~NumberStringBuilder();
40 NumberStringBuilder(const NumberStringBuilder
&other
);
42 NumberStringBuilder
&operator=(const NumberStringBuilder
&other
);
44 int32_t length() const;
46 int32_t codePointCount() const;
48 inline char16_t charAt(int32_t index
) const {
50 U_ASSERT(index
< fLength
);
51 return getCharPtr()[fZero
+ index
];
54 inline Field
fieldAt(int32_t index
) const {
56 U_ASSERT(index
< fLength
);
57 return getFieldPtr()[fZero
+ index
];
60 UChar32
getFirstCodePoint() const;
62 UChar32
getLastCodePoint() const;
64 UChar32
codePointAt(int32_t index
) const;
66 UChar32
codePointBefore(int32_t index
) const;
68 NumberStringBuilder
&clear();
70 int32_t appendCodePoint(UChar32 codePoint
, Field field
, UErrorCode
&status
);
72 int32_t insertCodePoint(int32_t index
, UChar32 codePoint
, Field field
, UErrorCode
&status
);
74 int32_t append(const UnicodeString
&unistr
, Field field
, UErrorCode
&status
);
76 int32_t insert(int32_t index
, const UnicodeString
&unistr
, Field field
, UErrorCode
&status
);
78 int32_t insert(int32_t index
, const UnicodeString
&unistr
, int32_t start
, int32_t end
, Field field
,
81 int32_t splice(int32_t startThis
, int32_t endThis
, const UnicodeString
&unistr
,
82 int32_t startOther
, int32_t endOther
, Field field
, UErrorCode
& status
);
84 int32_t append(const NumberStringBuilder
&other
, UErrorCode
&status
);
86 int32_t insert(int32_t index
, const NumberStringBuilder
&other
, UErrorCode
&status
);
89 * Gets a "safe" UnicodeString that can be used even after the NumberStringBuilder is destructed.
91 UnicodeString
toUnicodeString() const;
94 * Gets an "unsafe" UnicodeString that is valid only as long as the NumberStringBuilder is alive and
95 * unchanged. Slightly faster than toUnicodeString().
97 const UnicodeString
toTempUnicodeString() const;
99 UnicodeString
toDebugString() const;
101 const char16_t *chars() const;
103 bool contentEquals(const NumberStringBuilder
&other
) const;
105 bool nextFieldPosition(FieldPosition
& fp
, UErrorCode
& status
) const;
107 void getAllFieldPositions(FieldPositionIteratorHandler
& fpih
, UErrorCode
& status
) const;
110 bool fUsingHeap
= false;
111 ValueOrHeapArray
<char16_t> fChars
;
112 ValueOrHeapArray
<Field
> fFields
;
113 int32_t fZero
= DEFAULT_CAPACITY
/ 2;
116 inline char16_t *getCharPtr() {
117 return fUsingHeap
? fChars
.heap
.ptr
: fChars
.value
;
120 inline const char16_t *getCharPtr() const {
121 return fUsingHeap
? fChars
.heap
.ptr
: fChars
.value
;
124 inline Field
*getFieldPtr() {
125 return fUsingHeap
? fFields
.heap
.ptr
: fFields
.value
;
128 inline const Field
*getFieldPtr() const {
129 return fUsingHeap
? fFields
.heap
.ptr
: fFields
.value
;
132 inline int32_t getCapacity() const {
133 return fUsingHeap
? fChars
.heap
.capacity
: DEFAULT_CAPACITY
;
136 int32_t prepareForInsert(int32_t index
, int32_t count
, UErrorCode
&status
);
138 int32_t prepareForInsertHelper(int32_t index
, int32_t count
, UErrorCode
&status
);
140 int32_t remove(int32_t index
, int32_t count
);
144 } // namespace number
148 #endif //__NUMBER_STRINGBUILDER_H__
150 #endif /* #if !UCONFIG_NO_FORMATTING */