]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/format.cpp
ICU-59117.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / format.cpp
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
b75a7d8f
A
3/*
4*******************************************************************************
51004dcb 5* Copyright (C) 1997-2012, International Business Machines Corporation and *
b75a7d8f
A
6* others. All Rights Reserved. *
7*******************************************************************************
8*
9* File FORMAT.CPP
10*
11* Modification History:
12*
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********************************************************************************
19*/
20// *****************************************************************************
21// This file was generated from the java source file Format.java
22// *****************************************************************************
729e4ab9 23
51004dcb 24#include "utypeinfo.h" // for 'typeid' to work
729e4ab9 25
b75a7d8f
A
26#include "unicode/utypes.h"
27
4388f060
A
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
30#endif
31
b75a7d8f
A
32/*
33 * Dummy code:
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...
37 */
38#if UCONFIG_NO_COLLATION && UCONFIG_NO_FORMATTING && UCONFIG_NO_TRANSLITERATION
39U_CAPI int32_t U_EXPORT2
40uprv_icuin_lib_dummy(int32_t i) {
41 return -i;
42}
43#endif
44
45/* Format class implementation ---------------------------------------------- */
46
47#if !UCONFIG_NO_FORMATTING
48
49#include "unicode/format.h"
374ca955
A
50#include "unicode/ures.h"
51#include "cstring.h"
52#include "locbased.h"
b75a7d8f
A
53
54// *****************************************************************************
55// class Format
56// *****************************************************************************
57
58U_NAMESPACE_BEGIN
59
374ca955
A
60UOBJECT_DEFINE_RTTI_IMPLEMENTATION(FieldPosition)
61
62FieldPosition::~FieldPosition() {}
63
64FieldPosition *
65FieldPosition::clone() const {
66 return new FieldPosition(*this);
67}
b75a7d8f
A
68
69// -------------------------------------
70// default constructor
71
72Format::Format()
73 : UObject()
74{
374ca955 75 *validLocale = *actualLocale = 0;
b75a7d8f
A
76}
77
78// -------------------------------------
79
80Format::~Format()
81{
82}
83
84// -------------------------------------
85// copy constructor
86
87Format::Format(const Format &that)
88 : UObject(that)
89{
374ca955 90 *this = that;
b75a7d8f
A
91}
92
93// -------------------------------------
94// assignment operator
95
96Format&
374ca955 97Format::operator=(const Format& that)
b75a7d8f 98{
729e4ab9
A
99 if (this != &that) {
100 uprv_strcpy(validLocale, that.validLocale);
101 uprv_strcpy(actualLocale, that.actualLocale);
102 }
b75a7d8f
A
103 return *this;
104}
105
106// -------------------------------------
107// Formats the obj and append the result in the buffer, toAppendTo.
108// This calls the actual implementation in the concrete subclasses.
729e4ab9 109
b75a7d8f 110UnicodeString&
729e4ab9
A
111Format::format(const Formattable& obj,
112 UnicodeString& toAppendTo,
b75a7d8f
A
113 UErrorCode& status) const
114{
115 if (U_FAILURE(status)) return toAppendTo;
116
374ca955 117 FieldPosition pos(FieldPosition::DONT_CARE);
b75a7d8f
A
118
119 return format(obj, toAppendTo, pos, status);
120}
729e4ab9 121
b75a7d8f 122// -------------------------------------
729e4ab9
A
123// Default implementation sets unsupported error; subclasses should
124// override.
125
126UnicodeString&
127Format::format(const Formattable& /* unused obj */,
128 UnicodeString& toAppendTo,
129 FieldPositionIterator* /* unused posIter */,
130 UErrorCode& status) const
131{
132 if (!U_FAILURE(status)) {
133 status = U_UNSUPPORTED_ERROR;
134 }
135 return toAppendTo;
136}
137
138// -------------------------------------
139// Parses the source string and create the corresponding
b75a7d8f 140// result object. Checks the parse position for errors.
729e4ab9 141
b75a7d8f 142void
729e4ab9
A
143Format::parseObject(const UnicodeString& source,
144 Formattable& result,
b75a7d8f
A
145 UErrorCode& status) const
146{
147 if (U_FAILURE(status)) return;
148
149 ParsePosition parsePosition(0);
150 parseObject(source, result, parsePosition);
151 if (parsePosition.getIndex() == 0) {
152 status = U_INVALID_FORMAT_ERROR;
153 }
154}
729e4ab9 155
b75a7d8f
A
156// -------------------------------------
157
158UBool
374ca955 159Format::operator==(const Format& that) const
b75a7d8f 160{
374ca955 161 // Subclasses: Call this method and then add more specific checks.
729e4ab9 162 return typeid(*this) == typeid(that);
b75a7d8f
A
163}
164//---------------------------------------
165
166/**
167 * Simple function for initializing a UParseError from a UnicodeString.
168 *
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
172 * @draft ICU 2.4
173 */
174void Format::syntaxError(const UnicodeString& pattern,
175 int32_t pos,
176 UParseError& parseError) {
177 parseError.offset = pos;
178 parseError.line=0; // we are not using line number
729e4ab9 179
b75a7d8f 180 // for pre-context
46f4442e 181 int32_t start = (pos < U_PARSE_CONTEXT_LEN)? 0 : (pos - (U_PARSE_CONTEXT_LEN-1
b75a7d8f
A
182 /* subtract 1 so that we have room for null*/));
183 int32_t stop = pos;
184 pattern.extract(start,stop-start,parseError.preContext,0);
185 //null terminate the buffer
186 parseError.preContext[stop-start] = 0;
729e4ab9 187
b75a7d8f
A
188 //for post-context
189 start = pos+1;
729e4ab9 190 stop = ((pos+U_PARSE_CONTEXT_LEN)<=pattern.length()) ? (pos+(U_PARSE_CONTEXT_LEN-1)) :
b75a7d8f
A
191 pattern.length();
192 pattern.extract(start,stop-start,parseError.postContext,0);
193 //null terminate the buffer
194 parseError.postContext[stop-start]= 0;
195}
196
729e4ab9 197Locale
374ca955
A
198Format::getLocale(ULocDataLocaleType type, UErrorCode& status) const {
199 U_LOCALE_BASED(locBased, *this);
200 return locBased.getLocale(type, status);
201}
202
203const char *
204Format::getLocaleID(ULocDataLocaleType type, UErrorCode& status) const {
205 U_LOCALE_BASED(locBased, *this);
206 return locBased.getLocaleID(type, status);
207}
208
209void
210Format::setLocaleIDs(const char* valid, const char* actual) {
211 U_LOCALE_BASED(locBased, *this);
212 locBased.setLocaleIDs(valid, actual);
213}
214
b75a7d8f
A
215U_NAMESPACE_END
216
217#endif /* #if !UCONFIG_NO_FORMATTING */
218
219//eof