aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/icu/i18n/formattedval_impl.h
blob: e19392c5b9d662a910766eabd8756673a94a1f38 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// © 2018 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html

#ifndef __FORMVAL_IMPL_H__
#define __FORMVAL_IMPL_H__

#include "unicode/utypes.h"
#if !UCONFIG_NO_FORMATTING

// This file contains compliant implementations of FormattedValue which can be
// leveraged by ICU formatters.
//
// Each implementation is defined in its own cpp file in order to split
// dependencies more modularly.

#include "unicode/formattedvalue.h"
#include "capi_helper.h"
#include "fphdlimp.h"
#include "util.h"
#include "uvectr32.h"
#include "formatted_string_builder.h"


/**
 * Represents the type of constraint for ConstrainedFieldPosition.
 *
 * Constraints are used to control the behavior of iteration in FormattedValue.
 *
 * @internal
 */
typedef enum UCFPosConstraintType {
    /**
     * Represents the lack of a constraint.
     *
     * This is the value of fConstraint if no "constrain" methods were called.
     *
     * @internal
     */
    UCFPOS_CONSTRAINT_NONE = 0,

    /**
     * Represents that the field category is constrained.
     *
     * This is the value of fConstraint if constraintCategory was called.
     *
     * FormattedValue implementations should not change the field category
     * while this constraint is active.
     *
     * @internal
     */
    UCFPOS_CONSTRAINT_CATEGORY,

    /**
     * Represents that the field and field category are constrained.
     *
     * This is the value of fConstraint if constraintField was called.
     *
     * FormattedValue implementations should not change the field or field category
     * while this constraint is active.
     *
     * @internal
     */
    UCFPOS_CONSTRAINT_FIELD
} UCFPosConstraintType;


U_NAMESPACE_BEGIN


/**
 * Implementation of FormattedValue using FieldPositionHandler to accept fields.
 *
 * TODO(ICU-20897): This class is unused. If it is not needed when fixing ICU-20897,
 * it should be deleted.
 */
class FormattedValueFieldPositionIteratorImpl : public UMemory, public FormattedValue {
public:

    /** @param initialFieldCapacity Initially allocate space for this many fields. */
    FormattedValueFieldPositionIteratorImpl(int32_t initialFieldCapacity, UErrorCode& status);

    virtual ~FormattedValueFieldPositionIteratorImpl();

    // Implementation of FormattedValue (const):

    UnicodeString toString(UErrorCode& status) const override;
    UnicodeString toTempString(UErrorCode& status) const override;
    Appendable& appendTo(Appendable& appendable, UErrorCode& status) const override;
    UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const override;

    // Additional methods used during construction phase only (non-const):

    FieldPositionIteratorHandler getHandler(UErrorCode& status);
    void appendString(UnicodeString string, UErrorCode& status);

    /**
     * Computes the spans for duplicated values.
     * For example, if the string has fields:
     * 
     *     ...aa..[b.cc]..d.[bb.e.c]..a..
     *
     * then the spans will be the bracketed regions.
     *
     * Assumes that the currently known fields are sorted
     * and all in the same category.
     */
    void addOverlapSpans(UFieldCategory spanCategory, int8_t firstIndex, UErrorCode& status);

    /**
     * Sorts the fields: start index first, length second.
     */
    void sort();

private:
    UnicodeString fString;
    UVector32 fFields;
};


// Internal struct that must be exported for MSVC
struct U_I18N_API SpanInfo {
    UFieldCategory category;
    int32_t spanValue;
    int32_t start;
    int32_t length;
};

// Export an explicit template instantiation of the MaybeStackArray that
//    is used as a data member of CEBuffer.
//
//    When building DLLs for Windows this is required even though
//    no direct access to the MaybeStackArray leaks out of the i18n library.
//
// See digitlst.h, pluralaffix.h, datefmt.h, and others for similar examples.
//
#if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
template class U_I18N_API MaybeStackArray<SpanInfo, 8>;
#endif

/**
 * Implementation of FormattedValue based on FormattedStringBuilder.
 *
 * The implementation currently revolves around numbers and number fields.
 * However, it can be generalized in the future when there is a need.
 *
 * @author sffc (Shane Carr)
 */
// Exported as U_I18N_API for tests
class U_I18N_API FormattedValueStringBuilderImpl : public UMemory, public FormattedValue {
public:

    FormattedValueStringBuilderImpl(FormattedStringBuilder::Field numericField);

    virtual ~FormattedValueStringBuilderImpl();

    FormattedValueStringBuilderImpl(FormattedValueStringBuilderImpl&&) = default;
    FormattedValueStringBuilderImpl& operator=(FormattedValueStringBuilderImpl&&) = default;

    // Implementation of FormattedValue (const):

    UnicodeString toString(UErrorCode& status) const override;
    UnicodeString toTempString(UErrorCode& status) const override;
    Appendable& appendTo(Appendable& appendable, UErrorCode& status) const override;
    UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const override;

    // Additional helper functions:
    UBool nextFieldPosition(FieldPosition& fp, UErrorCode& status) const;
    void getAllFieldPositions(FieldPositionIteratorHandler& fpih, UErrorCode& status) const;
    inline FormattedStringBuilder& getStringRef() {
        return fString;
    }
    inline const FormattedStringBuilder& getStringRef() const {
        return fString;
    }
    void resetString();

    /**
     * Adds additional metadata used for span fields.
     *
     * category: the category to use for the span field.
     * spanValue: the value of the span field: index of the list item, for example.
     * start: the start position within the string of the span. -1 if unknown.
     * length: the length of the span, used to split adjacent fields.
     */
    void appendSpanInfo(UFieldCategory category, int32_t spanValue, int32_t start, int32_t length, UErrorCode& status);
    void prependSpanInfo(UFieldCategory category, int32_t spanValue, int32_t start, int32_t length, UErrorCode& status);

private:
    FormattedStringBuilder fString;
    FormattedStringBuilder::Field fNumericField;
    MaybeStackArray<SpanInfo, 8> spanIndices;
    int32_t spanIndicesCount = 0;

    bool nextPositionImpl(ConstrainedFieldPosition& cfpos, FormattedStringBuilder::Field numericField, UErrorCode& status) const;
    static bool isIntOrGroup(FormattedStringBuilder::Field field);
    static bool isTrimmable(FormattedStringBuilder::Field field);
    int32_t trimBack(int32_t limit) const;
    int32_t trimFront(int32_t start) const;
};


// C API Helpers for FormattedValue
// Magic number as ASCII == "UFV"
struct UFormattedValueImpl;
typedef IcuCApiHelper<UFormattedValue, UFormattedValueImpl, 0x55465600> UFormattedValueApiHelper;
struct UFormattedValueImpl : public UMemory, public UFormattedValueApiHelper {
    // This pointer should be set by the child class.
    FormattedValue* fFormattedValue = nullptr;
};


/** Boilerplate to check for valid status before dereferencing the fData pointer. */
#define UPRV_FORMATTED_VALUE_METHOD_GUARD(returnExpression) \
    if (U_FAILURE(status)) { \
        return returnExpression; \
    } \
    if (fData == nullptr) { \
        status = fErrorCode; \
        return returnExpression; \
    } \


/** Implementation of the methods from U_FORMATTED_VALUE_SUBCLASS_AUTO. */
#define UPRV_FORMATTED_VALUE_SUBCLASS_AUTO_IMPL(Name) \
    Name::Name(Name&& src) noexcept \
            : fData(src.fData), fErrorCode(src.fErrorCode) { \
        src.fData = nullptr; \
        src.fErrorCode = U_INVALID_STATE_ERROR; \
    } \
    Name::~Name() { \
        delete fData; \
        fData = nullptr; \
    } \
    Name& Name::operator=(Name&& src) noexcept { \
        delete fData; \
        fData = src.fData; \
        src.fData = nullptr; \
        fErrorCode = src.fErrorCode; \
        src.fErrorCode = U_INVALID_STATE_ERROR; \
        return *this; \
    } \
    UnicodeString Name::toString(UErrorCode& status) const { \
        UPRV_FORMATTED_VALUE_METHOD_GUARD(ICU_Utility::makeBogusString()) \
        return fData->toString(status); \
    } \
    UnicodeString Name::toTempString(UErrorCode& status) const { \
        UPRV_FORMATTED_VALUE_METHOD_GUARD(ICU_Utility::makeBogusString()) \
        return fData->toTempString(status); \
    } \
    Appendable& Name::appendTo(Appendable& appendable, UErrorCode& status) const { \
        UPRV_FORMATTED_VALUE_METHOD_GUARD(appendable) \
        return fData->appendTo(appendable, status); \
    } \
    UBool Name::nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const { \
        UPRV_FORMATTED_VALUE_METHOD_GUARD(false) \
        return fData->nextPosition(cfpos, status); \
    }


/** Like UPRV_FORMATTED_VALUE_CAPI_AUTO_IMPL but without impl type declarations. */
#define UPRV_FORMATTED_VALUE_CAPI_NO_IMPLTYPE_AUTO_IMPL(CType, ImplType, HelperType, Prefix) \
    U_CAPI CType* U_EXPORT2 \
    Prefix ## _openResult (UErrorCode* ec) { \
        if (U_FAILURE(*ec)) { \
            return nullptr; \
        } \
        ImplType* impl = new ImplType(); \
        if (impl == nullptr) { \
            *ec = U_MEMORY_ALLOCATION_ERROR; \
            return nullptr; \
        } \
        return static_cast<HelperType*>(impl)->exportForC(); \
    } \
    U_CAPI const UFormattedValue* U_EXPORT2 \
    Prefix ## _resultAsValue (const CType* uresult, UErrorCode* ec) { \
        const ImplType* result = HelperType::validate(uresult, *ec); \
        if (U_FAILURE(*ec)) { return nullptr; } \
        return static_cast<const UFormattedValueApiHelper*>(result)->exportConstForC(); \
    } \
    U_CAPI void U_EXPORT2 \
    Prefix ## _closeResult (CType* uresult) { \
        UErrorCode localStatus = U_ZERO_ERROR; \
        const ImplType* impl = HelperType::validate(uresult, localStatus); \
        delete impl; \
    }


/**
 * Implementation of the standard methods for a UFormattedValue "subclass" C API.
 * @param CPPType The public C++ type, like FormattedList
 * @param CType The public C type, like UFormattedList
 * @param ImplType A name to use for the implementation class
 * @param HelperType A name to use for the "mixin" typedef for C API conversion
 * @param Prefix The C API prefix, like ulistfmt
 * @param MagicNumber A unique 32-bit number to use to identify this type
 */
#define UPRV_FORMATTED_VALUE_CAPI_AUTO_IMPL(CPPType, CType, ImplType, HelperType, Prefix, MagicNumber) \
    U_NAMESPACE_BEGIN \
    class ImplType; \
    typedef IcuCApiHelper<CType, ImplType, MagicNumber> HelperType; \
    class ImplType : public UFormattedValueImpl, public HelperType { \
    public: \
        ImplType(); \
        ~ImplType(); \
        CPPType fImpl; \
    }; \
    ImplType::ImplType() { \
        fFormattedValue = &fImpl; \
    } \
    ImplType::~ImplType() {} \
    U_NAMESPACE_END \
    UPRV_FORMATTED_VALUE_CAPI_NO_IMPLTYPE_AUTO_IMPL(CType, ImplType, HelperType, Prefix)


U_NAMESPACE_END

#endif /* #if !UCONFIG_NO_FORMATTING */
#endif // __FORMVAL_IMPL_H__