1 // © 2018 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
8 #include "unicode/formattedvalue.h"
9 #include "formattedval_impl.h"
10 #include "capi_helper.h"
15 ConstrainedFieldPosition::ConstrainedFieldPosition() {}
17 ConstrainedFieldPosition::~ConstrainedFieldPosition() {}
19 void ConstrainedFieldPosition::reset() {
24 fConstraint
= UCFPOS_CONSTRAINT_NONE
;
25 fCategory
= UFIELD_CATEGORY_UNDEFINED
;
28 void ConstrainedFieldPosition::constrainCategory(int32_t category
) {
29 fConstraint
= UCFPOS_CONSTRAINT_CATEGORY
;
33 void ConstrainedFieldPosition::constrainField(int32_t category
, int32_t field
) {
34 fConstraint
= UCFPOS_CONSTRAINT_FIELD
;
39 void ConstrainedFieldPosition::setInt64IterationContext(int64_t context
) {
43 UBool
ConstrainedFieldPosition::matchesField(int32_t category
, int32_t field
) const {
44 switch (fConstraint
) {
45 case UCFPOS_CONSTRAINT_NONE
:
47 case UCFPOS_CONSTRAINT_CATEGORY
:
48 return fCategory
== category
;
49 case UCFPOS_CONSTRAINT_FIELD
:
50 return fCategory
== category
&& fField
== field
;
56 void ConstrainedFieldPosition::setState(
68 FormattedValue::~FormattedValue() = default;
71 ///////////////////////
72 /// C API FUNCTIONS ///
73 ///////////////////////
75 struct UConstrainedFieldPositionImpl
: public UMemory
,
76 // Magic number as ASCII == "UCF"
77 public IcuCApiHelper
<UConstrainedFieldPosition
, UConstrainedFieldPositionImpl
, 0x55434600> {
78 ConstrainedFieldPosition fImpl
;
81 U_CAPI UConstrainedFieldPosition
* U_EXPORT2
82 ucfpos_open(UErrorCode
* ec
) {
83 auto* impl
= new UConstrainedFieldPositionImpl();
84 if (impl
== nullptr) {
85 *ec
= U_MEMORY_ALLOCATION_ERROR
;
88 return impl
->exportForC();
92 ucfpos_reset(UConstrainedFieldPosition
* ptr
, UErrorCode
* ec
) {
93 auto* impl
= UConstrainedFieldPositionImpl::validate(ptr
, *ec
);
100 U_CAPI
void U_EXPORT2
101 ucfpos_constrainCategory(UConstrainedFieldPosition
* ptr
, int32_t category
, UErrorCode
* ec
) {
102 auto* impl
= UConstrainedFieldPositionImpl::validate(ptr
, *ec
);
103 if (U_FAILURE(*ec
)) {
106 impl
->fImpl
.constrainCategory(category
);
109 U_CAPI
void U_EXPORT2
110 ucfpos_constrainField(UConstrainedFieldPosition
* ptr
, int32_t category
, int32_t field
, UErrorCode
* ec
) {
111 auto* impl
= UConstrainedFieldPositionImpl::validate(ptr
, *ec
);
112 if (U_FAILURE(*ec
)) {
115 impl
->fImpl
.constrainField(category
, field
);
118 U_CAPI
int32_t U_EXPORT2
119 ucfpos_getCategory(const UConstrainedFieldPosition
* ptr
, UErrorCode
* ec
) {
120 const auto* impl
= UConstrainedFieldPositionImpl::validate(ptr
, *ec
);
121 if (U_FAILURE(*ec
)) {
122 return UFIELD_CATEGORY_UNDEFINED
;
124 return impl
->fImpl
.getCategory();
127 U_CAPI
int32_t U_EXPORT2
128 ucfpos_getField(const UConstrainedFieldPosition
* ptr
, UErrorCode
* ec
) {
129 const auto* impl
= UConstrainedFieldPositionImpl::validate(ptr
, *ec
);
130 if (U_FAILURE(*ec
)) {
133 return impl
->fImpl
.getField();
136 U_CAPI
void U_EXPORT2
137 ucfpos_getIndexes(const UConstrainedFieldPosition
* ptr
, int32_t* pStart
, int32_t* pLimit
, UErrorCode
* ec
) {
138 const auto* impl
= UConstrainedFieldPositionImpl::validate(ptr
, *ec
);
139 if (U_FAILURE(*ec
)) {
142 *pStart
= impl
->fImpl
.getStart();
143 *pLimit
= impl
->fImpl
.getLimit();
146 U_CAPI
int64_t U_EXPORT2
147 ucfpos_getInt64IterationContext(const UConstrainedFieldPosition
* ptr
, UErrorCode
* ec
) {
148 const auto* impl
= UConstrainedFieldPositionImpl::validate(ptr
, *ec
);
149 if (U_FAILURE(*ec
)) {
152 return impl
->fImpl
.getInt64IterationContext();
155 U_CAPI
void U_EXPORT2
156 ucfpos_setInt64IterationContext(UConstrainedFieldPosition
* ptr
, int64_t context
, UErrorCode
* ec
) {
157 auto* impl
= UConstrainedFieldPositionImpl::validate(ptr
, *ec
);
158 if (U_FAILURE(*ec
)) {
161 impl
->fImpl
.setInt64IterationContext(context
);
164 U_CAPI UBool U_EXPORT2
165 ucfpos_matchesField(const UConstrainedFieldPosition
* ptr
, int32_t category
, int32_t field
, UErrorCode
* ec
) {
166 const auto* impl
= UConstrainedFieldPositionImpl::validate(ptr
, *ec
);
167 if (U_FAILURE(*ec
)) {
170 return impl
->fImpl
.matchesField(category
, field
);
173 U_CAPI
void U_EXPORT2
175 UConstrainedFieldPosition
* ptr
,
181 auto* impl
= UConstrainedFieldPositionImpl::validate(ptr
, *ec
);
182 if (U_FAILURE(*ec
)) {
185 impl
->fImpl
.setState(category
, field
, start
, limit
);
188 U_CAPI
void U_EXPORT2
189 ucfpos_close(UConstrainedFieldPosition
* ptr
) {
190 UErrorCode localStatus
= U_ZERO_ERROR
;
191 auto* impl
= UConstrainedFieldPositionImpl::validate(ptr
, localStatus
);
196 U_DRAFT
const UChar
* U_EXPORT2
198 const UFormattedValue
* ufmtval
,
201 const auto* impl
= UFormattedValueApiHelper::validate(ufmtval
, *ec
);
202 if (U_FAILURE(*ec
)) {
205 UnicodeString readOnlyAlias
= impl
->fFormattedValue
->toTempString(*ec
);
206 if (U_FAILURE(*ec
)) {
209 if (pLength
!= nullptr) {
210 *pLength
= readOnlyAlias
.length();
212 return readOnlyAlias
.getBuffer();
216 U_DRAFT UBool U_EXPORT2
217 ufmtval_nextPosition(
218 const UFormattedValue
* ufmtval
,
219 UConstrainedFieldPosition
* ucfpos
,
221 const auto* fmtval
= UFormattedValueApiHelper::validate(ufmtval
, *ec
);
222 auto* cfpos
= UConstrainedFieldPositionImpl::validate(ucfpos
, *ec
);
223 if (U_FAILURE(*ec
)) {
226 return fmtval
->fFormattedValue
->nextPosition(cfpos
->fImpl
, *ec
);
232 #endif /* #if !UCONFIG_NO_FORMATTING */