/src/icu/source/i18n/formattedvalue.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // © 2018 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 | | |
8 | | #include "unicode/formattedvalue.h" |
9 | | #include "formattedval_impl.h" |
10 | | #include "capi_helper.h" |
11 | | |
12 | | U_NAMESPACE_BEGIN |
13 | | |
14 | | |
15 | 0 | ConstrainedFieldPosition::ConstrainedFieldPosition() {} |
16 | | |
17 | 0 | ConstrainedFieldPosition::~ConstrainedFieldPosition() {} |
18 | | |
19 | 0 | void ConstrainedFieldPosition::reset() { |
20 | 0 | fContext = 0LL; |
21 | 0 | fField = 0; |
22 | 0 | fStart = 0; |
23 | 0 | fLimit = 0; |
24 | 0 | fConstraint = UCFPOS_CONSTRAINT_NONE; |
25 | 0 | fCategory = UFIELD_CATEGORY_UNDEFINED; |
26 | 0 | } |
27 | | |
28 | 0 | void ConstrainedFieldPosition::constrainCategory(int32_t category) { |
29 | 0 | fConstraint = UCFPOS_CONSTRAINT_CATEGORY; |
30 | 0 | fCategory = category; |
31 | 0 | } |
32 | | |
33 | 0 | void ConstrainedFieldPosition::constrainField(int32_t category, int32_t field) { |
34 | 0 | fConstraint = UCFPOS_CONSTRAINT_FIELD; |
35 | 0 | fCategory = category; |
36 | 0 | fField = field; |
37 | 0 | } |
38 | | |
39 | 0 | void ConstrainedFieldPosition::setInt64IterationContext(int64_t context) { |
40 | 0 | fContext = context; |
41 | 0 | } |
42 | | |
43 | 0 | UBool ConstrainedFieldPosition::matchesField(int32_t category, int32_t field) const { |
44 | 0 | switch (fConstraint) { |
45 | 0 | case UCFPOS_CONSTRAINT_NONE: |
46 | 0 | return TRUE; |
47 | 0 | case UCFPOS_CONSTRAINT_CATEGORY: |
48 | 0 | return fCategory == category; |
49 | 0 | case UCFPOS_CONSTRAINT_FIELD: |
50 | 0 | return fCategory == category && fField == field; |
51 | 0 | default: |
52 | 0 | UPRV_UNREACHABLE; |
53 | 0 | } |
54 | 0 | } |
55 | | |
56 | | void ConstrainedFieldPosition::setState( |
57 | | int32_t category, |
58 | | int32_t field, |
59 | | int32_t start, |
60 | 0 | int32_t limit) { |
61 | 0 | fCategory = category; |
62 | 0 | fField = field; |
63 | 0 | fStart = start; |
64 | 0 | fLimit = limit; |
65 | 0 | } |
66 | | |
67 | | |
68 | 0 | FormattedValue::~FormattedValue() = default; |
69 | | |
70 | | |
71 | | /////////////////////// |
72 | | /// C API FUNCTIONS /// |
73 | | /////////////////////// |
74 | | |
75 | | struct UConstrainedFieldPositionImpl : public UMemory, |
76 | | // Magic number as ASCII == "UCF" |
77 | | public IcuCApiHelper<UConstrainedFieldPosition, UConstrainedFieldPositionImpl, 0x55434600> { |
78 | | ConstrainedFieldPosition fImpl; |
79 | | }; |
80 | | |
81 | | U_CAPI UConstrainedFieldPosition* U_EXPORT2 |
82 | 0 | ucfpos_open(UErrorCode* ec) { |
83 | 0 | auto* impl = new UConstrainedFieldPositionImpl(); |
84 | 0 | if (impl == nullptr) { |
85 | 0 | *ec = U_MEMORY_ALLOCATION_ERROR; |
86 | 0 | return nullptr; |
87 | 0 | } |
88 | 0 | return impl->exportForC(); |
89 | 0 | } |
90 | | |
91 | | U_CAPI void U_EXPORT2 |
92 | 0 | ucfpos_reset(UConstrainedFieldPosition* ptr, UErrorCode* ec) { |
93 | 0 | auto* impl = UConstrainedFieldPositionImpl::validate(ptr, *ec); |
94 | 0 | if (U_FAILURE(*ec)) { |
95 | 0 | return; |
96 | 0 | } |
97 | 0 | impl->fImpl.reset(); |
98 | 0 | } |
99 | | |
100 | | U_CAPI void U_EXPORT2 |
101 | 0 | ucfpos_constrainCategory(UConstrainedFieldPosition* ptr, int32_t category, UErrorCode* ec) { |
102 | 0 | auto* impl = UConstrainedFieldPositionImpl::validate(ptr, *ec); |
103 | 0 | if (U_FAILURE(*ec)) { |
104 | 0 | return; |
105 | 0 | } |
106 | 0 | impl->fImpl.constrainCategory(category); |
107 | 0 | } |
108 | | |
109 | | U_CAPI void U_EXPORT2 |
110 | 0 | ucfpos_constrainField(UConstrainedFieldPosition* ptr, int32_t category, int32_t field, UErrorCode* ec) { |
111 | 0 | auto* impl = UConstrainedFieldPositionImpl::validate(ptr, *ec); |
112 | 0 | if (U_FAILURE(*ec)) { |
113 | 0 | return; |
114 | 0 | } |
115 | 0 | impl->fImpl.constrainField(category, field); |
116 | 0 | } |
117 | | |
118 | | U_CAPI int32_t U_EXPORT2 |
119 | 0 | ucfpos_getCategory(const UConstrainedFieldPosition* ptr, UErrorCode* ec) { |
120 | 0 | const auto* impl = UConstrainedFieldPositionImpl::validate(ptr, *ec); |
121 | 0 | if (U_FAILURE(*ec)) { |
122 | 0 | return UFIELD_CATEGORY_UNDEFINED; |
123 | 0 | } |
124 | 0 | return impl->fImpl.getCategory(); |
125 | 0 | } |
126 | | |
127 | | U_CAPI int32_t U_EXPORT2 |
128 | 0 | ucfpos_getField(const UConstrainedFieldPosition* ptr, UErrorCode* ec) { |
129 | 0 | const auto* impl = UConstrainedFieldPositionImpl::validate(ptr, *ec); |
130 | 0 | if (U_FAILURE(*ec)) { |
131 | 0 | return 0; |
132 | 0 | } |
133 | 0 | return impl->fImpl.getField(); |
134 | 0 | } |
135 | | |
136 | | U_CAPI void U_EXPORT2 |
137 | 0 | ucfpos_getIndexes(const UConstrainedFieldPosition* ptr, int32_t* pStart, int32_t* pLimit, UErrorCode* ec) { |
138 | 0 | const auto* impl = UConstrainedFieldPositionImpl::validate(ptr, *ec); |
139 | 0 | if (U_FAILURE(*ec)) { |
140 | 0 | return; |
141 | 0 | } |
142 | 0 | *pStart = impl->fImpl.getStart(); |
143 | 0 | *pLimit = impl->fImpl.getLimit(); |
144 | 0 | } |
145 | | |
146 | | U_CAPI int64_t U_EXPORT2 |
147 | 0 | ucfpos_getInt64IterationContext(const UConstrainedFieldPosition* ptr, UErrorCode* ec) { |
148 | 0 | const auto* impl = UConstrainedFieldPositionImpl::validate(ptr, *ec); |
149 | 0 | if (U_FAILURE(*ec)) { |
150 | 0 | return 0; |
151 | 0 | } |
152 | 0 | return impl->fImpl.getInt64IterationContext(); |
153 | 0 | } |
154 | | |
155 | | U_CAPI void U_EXPORT2 |
156 | 0 | ucfpos_setInt64IterationContext(UConstrainedFieldPosition* ptr, int64_t context, UErrorCode* ec) { |
157 | 0 | auto* impl = UConstrainedFieldPositionImpl::validate(ptr, *ec); |
158 | 0 | if (U_FAILURE(*ec)) { |
159 | 0 | return; |
160 | 0 | } |
161 | 0 | impl->fImpl.setInt64IterationContext(context); |
162 | 0 | } |
163 | | |
164 | | U_CAPI UBool U_EXPORT2 |
165 | 0 | ucfpos_matchesField(const UConstrainedFieldPosition* ptr, int32_t category, int32_t field, UErrorCode* ec) { |
166 | 0 | const auto* impl = UConstrainedFieldPositionImpl::validate(ptr, *ec); |
167 | 0 | if (U_FAILURE(*ec)) { |
168 | 0 | return 0; |
169 | 0 | } |
170 | 0 | return impl->fImpl.matchesField(category, field); |
171 | 0 | } |
172 | | |
173 | | U_CAPI void U_EXPORT2 |
174 | | ucfpos_setState( |
175 | | UConstrainedFieldPosition* ptr, |
176 | | int32_t category, |
177 | | int32_t field, |
178 | | int32_t start, |
179 | | int32_t limit, |
180 | 0 | UErrorCode* ec) { |
181 | 0 | auto* impl = UConstrainedFieldPositionImpl::validate(ptr, *ec); |
182 | 0 | if (U_FAILURE(*ec)) { |
183 | 0 | return; |
184 | 0 | } |
185 | 0 | impl->fImpl.setState(category, field, start, limit); |
186 | 0 | } |
187 | | |
188 | | U_CAPI void U_EXPORT2 |
189 | 0 | ucfpos_close(UConstrainedFieldPosition* ptr) { |
190 | 0 | UErrorCode localStatus = U_ZERO_ERROR; |
191 | 0 | auto* impl = UConstrainedFieldPositionImpl::validate(ptr, localStatus); |
192 | 0 | delete impl; |
193 | 0 | } |
194 | | |
195 | | |
196 | | U_CAPI const UChar* U_EXPORT2 |
197 | | ufmtval_getString( |
198 | | const UFormattedValue* ufmtval, |
199 | | int32_t* pLength, |
200 | 0 | UErrorCode* ec) { |
201 | 0 | const auto* impl = UFormattedValueApiHelper::validate(ufmtval, *ec); |
202 | 0 | if (U_FAILURE(*ec)) { |
203 | 0 | return nullptr; |
204 | 0 | } |
205 | 0 | UnicodeString readOnlyAlias = impl->fFormattedValue->toTempString(*ec); |
206 | 0 | if (U_FAILURE(*ec)) { |
207 | 0 | return nullptr; |
208 | 0 | } |
209 | 0 | if (pLength != nullptr) { |
210 | 0 | *pLength = readOnlyAlias.length(); |
211 | 0 | } |
212 | 0 | return readOnlyAlias.getBuffer(); |
213 | 0 | } |
214 | | |
215 | | |
216 | | U_CAPI UBool U_EXPORT2 |
217 | | ufmtval_nextPosition( |
218 | | const UFormattedValue* ufmtval, |
219 | | UConstrainedFieldPosition* ucfpos, |
220 | 0 | UErrorCode* ec) { |
221 | 0 | const auto* fmtval = UFormattedValueApiHelper::validate(ufmtval, *ec); |
222 | 0 | auto* cfpos = UConstrainedFieldPositionImpl::validate(ucfpos, *ec); |
223 | 0 | if (U_FAILURE(*ec)) { |
224 | 0 | return FALSE; |
225 | 0 | } |
226 | 0 | return fmtval->fFormattedValue->nextPosition(cfpos->fImpl, *ec); |
227 | 0 | } |
228 | | |
229 | | |
230 | | U_NAMESPACE_END |
231 | | |
232 | | #endif /* #if !UCONFIG_NO_FORMATTING */ |