Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 /*
2 *******************************************************************************
3 * Copyright (C) 1997-2012, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
6 *
7 * File FORMAT.CPP
8 *
9 * Modification History:
10 *
11 * Date Name Description
12 * 02/19/97 aliu Converted from java.
13 * 03/17/97 clhuang Implemented with new APIs.
14 * 03/27/97 helena Updated to pass the simple test after code review.
15 * 07/20/98 stephen Added explicit init values for Field/ParsePosition
16 ********************************************************************************
17 */
18 // *****************************************************************************
19 // This file was generated from the java source file Format.java
20 // *****************************************************************************
22 #include "utypeinfo.h" // for 'typeid' to work
24 #include "unicode/utypes.h"
26 #ifndef U_I18N_IMPLEMENTATION
27 #error U_I18N_IMPLEMENTATION not set - must be set for all ICU source files in i18n/ - see http://userguide.icu-project.org/howtouseicu
28 #endif
30 /*
31 * Dummy code:
32 * If all modules in the I18N library are switched off, then there are no
33 * library exports and MSVC 6 writes a .dll but not a .lib file.
34 * Unless we export _something_ in that case...
35 */
36 #if UCONFIG_NO_COLLATION && UCONFIG_NO_FORMATTING && UCONFIG_NO_TRANSLITERATION
37 U_CAPI int32_t U_EXPORT2
38 uprv_icuin_lib_dummy(int32_t i) {
39 return -i;
40 }
41 #endif
43 /* Format class implementation ---------------------------------------------- */
45 #if !UCONFIG_NO_FORMATTING
47 #include "unicode/format.h"
48 #include "unicode/ures.h"
49 #include "cstring.h"
50 #include "locbased.h"
52 // *****************************************************************************
53 // class Format
54 // *****************************************************************************
56 U_NAMESPACE_BEGIN
58 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(FieldPosition)
60 FieldPosition::~FieldPosition() {}
62 FieldPosition *
63 FieldPosition::clone() const {
64 return new FieldPosition(*this);
65 }
67 // -------------------------------------
68 // default constructor
70 Format::Format()
71 : UObject()
72 {
73 *validLocale = *actualLocale = 0;
74 }
76 // -------------------------------------
78 Format::~Format()
79 {
80 }
82 // -------------------------------------
83 // copy constructor
85 Format::Format(const Format &that)
86 : UObject(that)
87 {
88 *this = that;
89 }
91 // -------------------------------------
92 // assignment operator
94 Format&
95 Format::operator=(const Format& that)
96 {
97 if (this != &that) {
98 uprv_strcpy(validLocale, that.validLocale);
99 uprv_strcpy(actualLocale, that.actualLocale);
100 }
101 return *this;
102 }
104 // -------------------------------------
105 // Formats the obj and append the result in the buffer, toAppendTo.
106 // This calls the actual implementation in the concrete subclasses.
108 UnicodeString&
109 Format::format(const Formattable& obj,
110 UnicodeString& toAppendTo,
111 UErrorCode& status) const
112 {
113 if (U_FAILURE(status)) return toAppendTo;
115 FieldPosition pos(FieldPosition::DONT_CARE);
117 return format(obj, toAppendTo, pos, status);
118 }
120 // -------------------------------------
121 // Default implementation sets unsupported error; subclasses should
122 // override.
124 UnicodeString&
125 Format::format(const Formattable& /* unused obj */,
126 UnicodeString& toAppendTo,
127 FieldPositionIterator* /* unused posIter */,
128 UErrorCode& status) const
129 {
130 if (!U_FAILURE(status)) {
131 status = U_UNSUPPORTED_ERROR;
132 }
133 return toAppendTo;
134 }
136 // -------------------------------------
137 // Parses the source string and create the corresponding
138 // result object. Checks the parse position for errors.
140 void
141 Format::parseObject(const UnicodeString& source,
142 Formattable& result,
143 UErrorCode& status) const
144 {
145 if (U_FAILURE(status)) return;
147 ParsePosition parsePosition(0);
148 parseObject(source, result, parsePosition);
149 if (parsePosition.getIndex() == 0) {
150 status = U_INVALID_FORMAT_ERROR;
151 }
152 }
154 // -------------------------------------
156 UBool
157 Format::operator==(const Format& that) const
158 {
159 // Subclasses: Call this method and then add more specific checks.
160 return typeid(*this) == typeid(that);
161 }
162 //---------------------------------------
164 /**
165 * Simple function for initializing a UParseError from a UnicodeString.
166 *
167 * @param pattern The pattern to copy into the parseError
168 * @param pos The position in pattern where the error occured
169 * @param parseError The UParseError object to fill in
170 * @draft ICU 2.4
171 */
172 void Format::syntaxError(const UnicodeString& pattern,
173 int32_t pos,
174 UParseError& parseError) {
175 parseError.offset = pos;
176 parseError.line=0; // we are not using line number
178 // for pre-context
179 int32_t start = (pos < U_PARSE_CONTEXT_LEN)? 0 : (pos - (U_PARSE_CONTEXT_LEN-1
180 /* subtract 1 so that we have room for null*/));
181 int32_t stop = pos;
182 pattern.extract(start,stop-start,parseError.preContext,0);
183 //null terminate the buffer
184 parseError.preContext[stop-start] = 0;
186 //for post-context
187 start = pos+1;
188 stop = ((pos+U_PARSE_CONTEXT_LEN)<=pattern.length()) ? (pos+(U_PARSE_CONTEXT_LEN-1)) :
189 pattern.length();
190 pattern.extract(start,stop-start,parseError.postContext,0);
191 //null terminate the buffer
192 parseError.postContext[stop-start]= 0;
193 }
195 Locale
196 Format::getLocale(ULocDataLocaleType type, UErrorCode& status) const {
197 U_LOCALE_BASED(locBased, *this);
198 return locBased.getLocale(type, status);
199 }
201 const char *
202 Format::getLocaleID(ULocDataLocaleType type, UErrorCode& status) const {
203 U_LOCALE_BASED(locBased, *this);
204 return locBased.getLocaleID(type, status);
205 }
207 void
208 Format::setLocaleIDs(const char* valid, const char* actual) {
209 U_LOCALE_BASED(locBased, *this);
210 locBased.setLocaleIDs(valid, actual);
211 }
213 U_NAMESPACE_END
215 #endif /* #if !UCONFIG_NO_FORMATTING */
217 //eof