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