| 1 |
. |
| 2 |
|
| 3 |
|
| 4 |
#ifndef __FORMVAL_IMPL_H__ |
| 5 |
#define __FORMVAL_IMPL_H__ |
| 6 |
|
| 7 |
#include "unicode/utypes.h" |
| 8 |
#if !UCONFIG_NO_FORMATTING |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "unicode/formattedvalue.h" |
| 17 |
#include "capi_helper.h" |
| 18 |
#include "fphdlimp.h" |
| 19 |
#include "util.h" |
| 20 |
#include "uvectr32.h" |
| 21 |
#include "formatted_string_builder.h" |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
typedef enum UCFPosConstraintType { |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
UCFPOS_CONSTRAINT_NONE = 0, |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
UCFPOS_CONSTRAINT_CATEGORY, |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
UCFPOS_CONSTRAINT_FIELD |
| 64 |
} UCFPosConstraintType; |
| 65 |
|
| 66 |
|
| 67 |
U_NAMESPACE_BEGIN |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
class FormattedValueFieldPositionIteratorImpl : public UMemory, public FormattedValue { |
| 77 |
public: |
| 78 |
|
| 79 |
|
| 80 |
FormattedValueFieldPositionIteratorImpl(int32_t initialFieldCapacity, UErrorCode& status); |
| 81 |
|
| 82 |
virtual ~FormattedValueFieldPositionIteratorImpl(); |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
UnicodeString toString(UErrorCode& status) const override; |
| 87 |
UnicodeString toTempString(UErrorCode& status) const override; |
| 88 |
Appendable& appendTo(Appendable& appendable, UErrorCode& status) const override; |
| 89 |
UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const override; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
FieldPositionIteratorHandler getHandler(UErrorCode& status); |
| 94 |
void appendString(UnicodeString string, UErrorCode& status); |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
void addOverlapSpans(UFieldCategory spanCategory, int8_t firstIndex, UErrorCode& status); |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
void sort(); |
| 113 |
|
| 114 |
private: |
| 115 |
UnicodeString fString; |
| 116 |
UVector32 fFields; |
| 117 |
}; |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
struct U_I18N_API SpanInfo { |
| 122 |
UFieldCategory category; |
| 123 |
int32_t spanValue; |
| 124 |
int32_t start; |
| 125 |
int32_t length; |
| 126 |
}; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
#if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN |
| 137 |
template class U_I18N_API MaybeStackArray<SpanInfo, 8>; |
| 138 |
#endif |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
class U_I18N_API FormattedValueStringBuilderImpl : public UMemory, public FormattedValue { |
| 150 |
public: |
| 151 |
|
| 152 |
FormattedValueStringBuilderImpl(FormattedStringBuilder::Field numericField); |
| 153 |
|
| 154 |
virtual ~FormattedValueStringBuilderImpl(); |
| 155 |
|
| 156 |
FormattedValueStringBuilderImpl(FormattedValueStringBuilderImpl&&) = default; |
| 157 |
FormattedValueStringBuilderImpl& operator=(FormattedValueStringBuilderImpl&&) = default; |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
UnicodeString toString(UErrorCode& status) const override; |
| 162 |
UnicodeString toTempString(UErrorCode& status) const override; |
| 163 |
Appendable& appendTo(Appendable& appendable, UErrorCode& status) const override; |
| 164 |
UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const override; |
| 165 |
|
| 166 |
|
| 167 |
UBool nextFieldPosition(FieldPosition& fp, UErrorCode& status) const; |
| 168 |
void getAllFieldPositions(FieldPositionIteratorHandler& fpih, UErrorCode& status) const; |
| 169 |
inline FormattedStringBuilder& getStringRef() { |
| 170 |
return fString; |
| 171 |
} |
| 172 |
inline const FormattedStringBuilder& getStringRef() const { |
| 173 |
return fString; |
| 174 |
} |
| 175 |
void resetString(); |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
void appendSpanInfo(UFieldCategory category, int32_t spanValue, int32_t start, int32_t length, UErrorCode& status); |
| 186 |
void prependSpanInfo(UFieldCategory category, int32_t spanValue, int32_t start, int32_t length, UErrorCode& status); |
| 187 |
|
| 188 |
private: |
| 189 |
FormattedStringBuilder fString; |
| 190 |
FormattedStringBuilder::Field fNumericField; |
| 191 |
MaybeStackArray<SpanInfo, 8> spanIndices; |
| 192 |
int32_t spanIndicesCount = 0; |
| 193 |
|
| 194 |
bool nextPositionImpl(ConstrainedFieldPosition& cfpos, FormattedStringBuilder::Field numericField, UErrorCode& status) const; |
| 195 |
static bool isIntOrGroup(FormattedStringBuilder::Field field); |
| 196 |
static bool isTrimmable(FormattedStringBuilder::Field field); |
| 197 |
int32_t trimBack(int32_t limit) const; |
| 198 |
int32_t trimFront(int32_t start) const; |
| 199 |
}; |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
struct UFormattedValueImpl; |
| 205 |
typedef IcuCApiHelper<UFormattedValue, UFormattedValueImpl, 0x55465600> UFormattedValueApiHelper; |
| 206 |
struct UFormattedValueImpl : public UMemory, public UFormattedValueApiHelper { |
| 207 |
|
| 208 |
FormattedValue* fFormattedValue = nullptr; |
| 209 |
}; |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
#define UPRV_FORMATTED_VALUE_METHOD_GUARD(returnExpression) \ |
| 214 |
if (U_FAILURE(status)) { \ |
| 215 |
return returnExpression; \ |
| 216 |
} \ |
| 217 |
if (fData == nullptr) { \ |
| 218 |
status = fErrorCode; \ |
| 219 |
return returnExpression; \ |
| 220 |
} \ |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
#define UPRV_FORMATTED_VALUE_SUBCLASS_AUTO_IMPL(Name) \ |
| 225 |
Name::Name(Name&& src) noexcept \ |
| 226 |
: fData(src.fData), fErrorCode(src.fErrorCode) { \ |
| 227 |
src.fData = nullptr; \ |
| 228 |
src.fErrorCode = U_INVALID_STATE_ERROR; \ |
| 229 |
} \ |
| 230 |
Name::~Name() { \ |
| 231 |
delete fData; \ |
| 232 |
fData = nullptr; \ |
| 233 |
} \ |
| 234 |
Name& Name::operator=(Name&& src) noexcept { \ |
| 235 |
delete fData; \ |
| 236 |
fData = src.fData; \ |
| 237 |
src.fData = nullptr; \ |
| 238 |
fErrorCode = src.fErrorCode; \ |
| 239 |
src.fErrorCode = U_INVALID_STATE_ERROR; \ |
| 240 |
return *this; \ |
| 241 |
} \ |
| 242 |
UnicodeString Name::toString(UErrorCode& status) const { \ |
| 243 |
UPRV_FORMATTED_VALUE_METHOD_GUARD(ICU_Utility::makeBogusString()) \ |
| 244 |
return fData->toString(status); \ |
| 245 |
} \ |
| 246 |
UnicodeString Name::toTempString(UErrorCode& status) const { \ |
| 247 |
UPRV_FORMATTED_VALUE_METHOD_GUARD(ICU_Utility::makeBogusString()) \ |
| 248 |
return fData->toTempString(status); \ |
| 249 |
} \ |
| 250 |
Appendable& Name::appendTo(Appendable& appendable, UErrorCode& status) const { \ |
| 251 |
UPRV_FORMATTED_VALUE_METHOD_GUARD(appendable) \ |
| 252 |
return fData->appendTo(appendable, status); \ |
| 253 |
} \ |
| 254 |
UBool Name::nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const { \ |
| 255 |
UPRV_FORMATTED_VALUE_METHOD_GUARD(false) \ |
| 256 |
return fData->nextPosition(cfpos, status); \ |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
#define UPRV_FORMATTED_VALUE_CAPI_NO_IMPLTYPE_AUTO_IMPL(CType, ImplType, HelperType, Prefix) \ |
| 262 |
U_CAPI CType* U_EXPORT2 \ |
| 263 |
Prefix ## _openResult (UErrorCode* ec) { \ |
| 264 |
if (U_FAILURE(*ec)) { \ |
| 265 |
return nullptr; \ |
| 266 |
} \ |
| 267 |
ImplType* impl = new ImplType(); \ |
| 268 |
if (impl == nullptr) { \ |
| 269 |
*ec = U_MEMORY_ALLOCATION_ERROR; \ |
| 270 |
return nullptr; \ |
| 271 |
} \ |
| 272 |
return static_cast<HelperType*>(impl)->exportForC(); \ |
| 273 |
} \ |
| 274 |
U_CAPI const UFormattedValue* U_EXPORT2 \ |
| 275 |
Prefix ## _resultAsValue (const CType* uresult, UErrorCode* ec) { \ |
| 276 |
const ImplType* result = HelperType::validate(uresult, *ec); \ |
| 277 |
if (U_FAILURE(*ec)) { return nullptr; } \ |
| 278 |
return static_cast<const UFormattedValueApiHelper*>(result)->exportConstForC(); \ |
| 279 |
} \ |
| 280 |
U_CAPI void U_EXPORT2 \ |
| 281 |
Prefix ## _closeResult (CType* uresult) { \ |
| 282 |
UErrorCode localStatus = U_ZERO_ERROR; \ |
| 283 |
const ImplType* impl = HelperType::validate(uresult, localStatus); \ |
| 284 |
delete impl; \ |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
#define UPRV_FORMATTED_VALUE_CAPI_AUTO_IMPL(CPPType, CType, ImplType, HelperType, Prefix, MagicNumber) \ |
| 298 |
U_NAMESPACE_BEGIN \ |
| 299 |
class ImplType; \ |
| 300 |
typedef IcuCApiHelper<CType, ImplType, MagicNumber> HelperType; \ |
| 301 |
class ImplType : public UFormattedValueImpl, public HelperType { \ |
| 302 |
public: \ |
| 303 |
ImplType(); \ |
| 304 |
~ImplType(); \ |
| 305 |
CPPType fImpl; \ |
| 306 |
}; \ |
| 307 |
ImplType::ImplType() { \ |
| 308 |
fFormattedValue = &fImpl; \ |
| 309 |
} \ |
| 310 |
ImplType::~ImplType() {} \ |
| 311 |
U_NAMESPACE_END \ |
| 312 |
UPRV_FORMATTED_VALUE_CAPI_NO_IMPLTYPE_AUTO_IMPL(CType, ImplType, HelperType, Prefix) |
| 313 |
|
| 314 |
|
| 315 |
U_NAMESPACE_END |
| 316 |
|
| 317 |
#endif |
| 318 |
#endif |