Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/intl/icu/source/i18n/number_stringbuilder.h
Line
Count
Source (jump to first uncovered line)
1
// © 2017 and later: Unicode, Inc. and others.
2
// License & terms of use: http://www.unicode.org/copyright.html
3
4
#include "unicode/utypes.h"
5
6
#if !UCONFIG_NO_FORMATTING
7
#ifndef __NUMBER_STRINGBUILDER_H__
8
#define __NUMBER_STRINGBUILDER_H__
9
10
11
#include <cstdint>
12
#include "unicode/numfmt.h"
13
#include "unicode/ustring.h"
14
#include "cstring.h"
15
#include "uassert.h"
16
#include "number_types.h"
17
#include "fphdlimp.h"
18
19
U_NAMESPACE_BEGIN namespace number {
20
namespace impl {
21
22
class U_I18N_API NumberStringBuilder : public UMemory {
23
  private:
24
    static const int32_t DEFAULT_CAPACITY = 40;
25
26
    template<typename T>
27
    union ValueOrHeapArray {
28
        T value[DEFAULT_CAPACITY];
29
        struct {
30
            T *ptr;
31
            int32_t capacity;
32
        } heap;
33
    };
34
35
  public:
36
    NumberStringBuilder();
37
38
    ~NumberStringBuilder();
39
40
    NumberStringBuilder(const NumberStringBuilder &other);
41
42
    NumberStringBuilder &operator=(const NumberStringBuilder &other);
43
44
    int32_t length() const;
45
46
    int32_t codePointCount() const;
47
48
0
    inline char16_t charAt(int32_t index) const {
49
0
        U_ASSERT(index >= 0);
50
0
        U_ASSERT(index < fLength);
51
0
        return getCharPtr()[fZero + index];
52
0
    }
53
54
0
    inline Field fieldAt(int32_t index) const {
55
0
        U_ASSERT(index >= 0);
56
0
        U_ASSERT(index < fLength);
57
0
        return getFieldPtr()[fZero + index];
58
0
    }
59
60
    UChar32 getFirstCodePoint() const;
61
62
    UChar32 getLastCodePoint() const;
63
64
    UChar32 codePointAt(int32_t index) const;
65
66
    UChar32 codePointBefore(int32_t index) const;
67
68
    NumberStringBuilder &clear();
69
70
    int32_t appendCodePoint(UChar32 codePoint, Field field, UErrorCode &status);
71
72
    int32_t insertCodePoint(int32_t index, UChar32 codePoint, Field field, UErrorCode &status);
73
74
    int32_t append(const UnicodeString &unistr, Field field, UErrorCode &status);
75
76
    int32_t insert(int32_t index, const UnicodeString &unistr, Field field, UErrorCode &status);
77
78
    int32_t insert(int32_t index, const UnicodeString &unistr, int32_t start, int32_t end, Field field,
79
                   UErrorCode &status);
80
81
    int32_t splice(int32_t startThis, int32_t endThis,  const UnicodeString &unistr,
82
                   int32_t startOther, int32_t endOther, Field field, UErrorCode& status);
83
84
    int32_t append(const NumberStringBuilder &other, UErrorCode &status);
85
86
    int32_t insert(int32_t index, const NumberStringBuilder &other, UErrorCode &status);
87
88
    /**
89
     * Gets a "safe" UnicodeString that can be used even after the NumberStringBuilder is destructed.
90
     * */
91
    UnicodeString toUnicodeString() const;
92
93
    /**
94
     * Gets an "unsafe" UnicodeString that is valid only as long as the NumberStringBuilder is alive and
95
     * unchanged. Slightly faster than toUnicodeString().
96
     */
97
    const UnicodeString toTempUnicodeString() const;
98
99
    UnicodeString toDebugString() const;
100
101
    const char16_t *chars() const;
102
103
    bool contentEquals(const NumberStringBuilder &other) const;
104
105
    bool nextFieldPosition(FieldPosition& fp, UErrorCode& status) const;
106
107
    void getAllFieldPositions(FieldPositionIteratorHandler& fpih, UErrorCode& status) const;
108
109
  private:
110
    bool fUsingHeap = false;
111
    ValueOrHeapArray<char16_t> fChars;
112
    ValueOrHeapArray<Field> fFields;
113
    int32_t fZero = DEFAULT_CAPACITY / 2;
114
    int32_t fLength = 0;
115
116
0
    inline char16_t *getCharPtr() {
117
0
        return fUsingHeap ? fChars.heap.ptr : fChars.value;
118
0
    }
119
120
0
    inline const char16_t *getCharPtr() const {
121
0
        return fUsingHeap ? fChars.heap.ptr : fChars.value;
122
0
    }
123
124
0
    inline Field *getFieldPtr() {
125
0
        return fUsingHeap ? fFields.heap.ptr : fFields.value;
126
0
    }
127
128
0
    inline const Field *getFieldPtr() const {
129
0
        return fUsingHeap ? fFields.heap.ptr : fFields.value;
130
0
    }
131
132
0
    inline int32_t getCapacity() const {
133
0
        return fUsingHeap ? fChars.heap.capacity : DEFAULT_CAPACITY;
134
0
    }
135
136
    int32_t prepareForInsert(int32_t index, int32_t count, UErrorCode &status);
137
138
    int32_t prepareForInsertHelper(int32_t index, int32_t count, UErrorCode &status);
139
140
    int32_t remove(int32_t index, int32_t count);
141
};
142
143
} // namespace impl
144
} // namespace number
145
U_NAMESPACE_END
146
147
148
#endif //__NUMBER_STRINGBUILDER_H__
149
150
#endif /* #if !UCONFIG_NO_FORMATTING */