1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
5 * Copyright (C) 1997-2012, International Business Machines Corporation and *
6 * others. All Rights Reserved. *
7 *******************************************************************************
11 * Modification History:
13 * Date Name Description
14 * 02/19/97 aliu Converted from java.
15 * 03/17/97 clhuang Implemented with new APIs.
16 * 03/27/97 helena Updated to pass the simple test after code review.
17 * 07/20/98 stephen Added explicit init values for Field/ParsePosition
18 ********************************************************************************
20 // *****************************************************************************
21 // This file was generated from the java source file Format.java
22 // *****************************************************************************
24 #include "utypeinfo.h" // for 'typeid' to work
26 #include "unicode/utypes.h"
28 #ifndef U_I18N_IMPLEMENTATION
29 #error U_I18N_IMPLEMENTATION not set - must be set for all ICU source files in i18n/ - see http://userguide.icu-project.org/howtouseicu
34 * If all modules in the I18N library are switched off, then there are no
35 * library exports and MSVC 6 writes a .dll but not a .lib file.
36 * Unless we export _something_ in that case...
38 #if UCONFIG_NO_COLLATION && UCONFIG_NO_FORMATTING && UCONFIG_NO_TRANSLITERATION
39 U_CAPI int32_t U_EXPORT2
40 uprv_icuin_lib_dummy(int32_t i) {
45 /* Format class implementation ---------------------------------------------- */
47 #if !UCONFIG_NO_FORMATTING
49 #include "unicode/format.h"
50 #include "unicode/ures.h"
54 // *****************************************************************************
56 // *****************************************************************************
60 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(FieldPosition)
62 FieldPosition::~FieldPosition() {}
65 FieldPosition::clone() const {
66 return new FieldPosition(*this);
69 // -------------------------------------
70 // default constructor
75 *validLocale = *actualLocale = 0;
78 // -------------------------------------
84 // -------------------------------------
87 Format::Format(const Format &that)
93 // -------------------------------------
94 // assignment operator
97 Format::operator=(const Format& that)
100 uprv_strcpy(validLocale, that.validLocale);
101 uprv_strcpy(actualLocale, that.actualLocale);
106 // -------------------------------------
107 // Formats the obj and append the result in the buffer, toAppendTo.
108 // This calls the actual implementation in the concrete subclasses.
111 Format::format(const Formattable& obj,
112 UnicodeString& toAppendTo,
113 UErrorCode& status) const
115 if (U_FAILURE(status)) return toAppendTo;
117 FieldPosition pos(FieldPosition::DONT_CARE);
119 return format(obj, toAppendTo, pos, status);
122 // -------------------------------------
123 // Default implementation sets unsupported error; subclasses should
127 Format::format(const Formattable& /* unused obj */,
128 UnicodeString& toAppendTo,
129 FieldPositionIterator* /* unused posIter */,
130 UErrorCode& status) const
132 if (!U_FAILURE(status)) {
133 status = U_UNSUPPORTED_ERROR;
138 // -------------------------------------
139 // Parses the source string and create the corresponding
140 // result object. Checks the parse position for errors.
143 Format::parseObject(const UnicodeString& source,
145 UErrorCode& status) const
147 if (U_FAILURE(status)) return;
149 ParsePosition parsePosition(0);
150 parseObject(source, result, parsePosition);
151 if (parsePosition.getIndex() == 0) {
152 status = U_INVALID_FORMAT_ERROR;
156 // -------------------------------------
159 Format::operator==(const Format& that) const
161 // Subclasses: Call this method and then add more specific checks.
162 return typeid(*this) == typeid(that);
164 //---------------------------------------
167 * Simple function for initializing a UParseError from a UnicodeString.
169 * @param pattern The pattern to copy into the parseError
170 * @param pos The position in pattern where the error occured
171 * @param parseError The UParseError object to fill in
174 void Format::syntaxError(const UnicodeString& pattern,
176 UParseError& parseError) {
177 parseError.offset = pos;
178 parseError.line=0; // we are not using line number
181 int32_t start = (pos < U_PARSE_CONTEXT_LEN)? 0 : (pos - (U_PARSE_CONTEXT_LEN-1
182 /* subtract 1 so that we have room for null*/));
184 pattern.extract(start,stop-start,parseError.preContext,0);
185 //null terminate the buffer
186 parseError.preContext[stop-start] = 0;
190 stop = ((pos+U_PARSE_CONTEXT_LEN)<=pattern.length()) ? (pos+(U_PARSE_CONTEXT_LEN-1)) :
192 pattern.extract(start,stop-start,parseError.postContext,0);
193 //null terminate the buffer
194 parseError.postContext[stop-start]= 0;
198 Format::getLocale(ULocDataLocaleType type, UErrorCode& status) const {
199 U_LOCALE_BASED(locBased, *this);
200 return locBased.getLocale(type, status);
204 Format::getLocaleID(ULocDataLocaleType type, UErrorCode& status) const {
205 U_LOCALE_BASED(locBased, *this);
206 return locBased.getLocaleID(type, status);
210 Format::setLocaleIDs(const char* valid, const char* actual) {
211 U_LOCALE_BASED(locBased, *this);
212 locBased.setLocaleIDs(valid, actual);
217 #endif /* #if !UCONFIG_NO_FORMATTING */