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
);
88 void writeTerminator(UErrorCode
& status
);
91 * Gets a "safe" UnicodeString that can be used even after the NumberStringBuilder is destructed.
93 UnicodeString
toUnicodeString() const;
96 * Gets an "unsafe" UnicodeString that is valid only as long as the NumberStringBuilder is alive and
97 * unchanged. Slightly faster than toUnicodeString().
99 const UnicodeString
toTempUnicodeString() const;
101 UnicodeString
toDebugString() const;
103 const char16_t *chars() const;
105 bool contentEquals(const NumberStringBuilder
&other
) const;
107 bool nextFieldPosition(FieldPosition
& fp
, UErrorCode
& status
) const;
109 void getAllFieldPositions(FieldPositionIteratorHandler
& fpih
, UErrorCode
& status
) const;
111 bool nextPosition(ConstrainedFieldPosition
& cfpos
, Field numericField
, UErrorCode
& status
) const;
113 bool containsField(Field field
) const;
116 bool fUsingHeap
= false;
117 ValueOrHeapArray
<char16_t> fChars
;
118 ValueOrHeapArray
<Field
> fFields
;
119 int32_t fZero
= DEFAULT_CAPACITY
/ 2;
122 inline char16_t *getCharPtr() {
123 return fUsingHeap
? fChars
.heap
.ptr
: fChars
.value
;
126 inline const char16_t *getCharPtr() const {
127 return fUsingHeap
? fChars
.heap
.ptr
: fChars
.value
;
130 inline Field
*getFieldPtr() {
131 return fUsingHeap
? fFields
.heap
.ptr
: fFields
.value
;
134 inline const Field
*getFieldPtr() const {
135 return fUsingHeap
? fFields
.heap
.ptr
: fFields
.value
;
138 inline int32_t getCapacity() const {
139 return fUsingHeap
? fChars
.heap
.capacity
: DEFAULT_CAPACITY
;
142 int32_t prepareForInsert(int32_t index
, int32_t count
, UErrorCode
&status
);
144 int32_t prepareForInsertHelper(int32_t index
, int32_t count
, UErrorCode
&status
);
146 int32_t remove(int32_t index
, int32_t count
);
148 static bool isIntOrGroup(Field field
);
150 static bool isNumericField(Field field
);
152 int32_t trimBack(int32_t limit
) const;
154 int32_t trimFront(int32_t start
) const;
158 } // namespace number
162 #endif //__NUMBER_STRINGBUILDER_H__
164 #endif /* #if !UCONFIG_NO_FORMATTING */