Coverage Report

Created: 2025-06-13 06:38

/src/icu/icu4c/source/i18n/format.cpp
Line
Count
Source (jump to first uncovered line)
1
// © 2016 and later: Unicode, Inc. and others.
2
// License & terms of use: http://www.unicode.org/copyright.html
3
/*
4
*******************************************************************************
5
* Copyright (C) 1997-2012, International Business Machines Corporation and    *
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
// *****************************************************************************
23
24
#include "utypeinfo.h"  // for 'typeid' to work
25
26
#include "unicode/utypes.h"
27
#include "charstr.h"
28
29
#ifndef U_I18N_IMPLEMENTATION
30
#error U_I18N_IMPLEMENTATION not set - must be set for all ICU source files in i18n/ - see https://unicode-org.github.io/icu/userguide/howtouseicu
31
#endif
32
33
/*
34
 * Dummy code:
35
 * If all modules in the I18N library are switched off, then there are no
36
 * library exports and MSVC 6 writes a .dll but not a .lib file.
37
 * Unless we export _something_ in that case...
38
 */
39
#if UCONFIG_NO_COLLATION && UCONFIG_NO_FORMATTING && UCONFIG_NO_TRANSLITERATION
40
U_CAPI int32_t U_EXPORT2
41
uprv_icuin_lib_dummy(int32_t i) {
42
    return -i;
43
}
44
#endif
45
46
/* Format class implementation ---------------------------------------------- */
47
48
#if !UCONFIG_NO_FORMATTING
49
50
#include "unicode/format.h"
51
#include "unicode/ures.h"
52
#include "cstring.h"
53
#include "locbased.h"
54
55
// *****************************************************************************
56
// class Format
57
// *****************************************************************************
58
59
U_NAMESPACE_BEGIN
60
61
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(FieldPosition)
62
63
31.7k
FieldPosition::~FieldPosition() {}
64
65
FieldPosition *
66
0
FieldPosition::clone() const {
67
0
    return new FieldPosition(*this);
68
0
}
69
70
// -------------------------------------
71
// default constructor
72
73
Format::Format()
74
438k
    : UObject()
75
438k
{
76
438k
}
77
78
// -------------------------------------
79
80
Format::~Format()
81
583k
{
82
583k
    delete actualLocale;
83
583k
    delete validLocale;
84
583k
}
85
86
// -------------------------------------
87
// copy constructor
88
89
Format::Format(const Format &that)
90
146k
    : UObject(that)
91
146k
{
92
146k
    *this = that;
93
146k
}
94
95
// -------------------------------------
96
// assignment operator
97
98
Format&
99
Format::operator=(const Format& that)
100
294k
{
101
294k
    if (this != &that) {
102
294k
        UErrorCode status = U_ZERO_ERROR;
103
294k
        U_LOCALE_BASED(locBased, *this);
104
294k
        locBased.setLocaleIDs(that.validLocale, that.actualLocale, status);
105
294k
        U_ASSERT(U_SUCCESS(status));
106
294k
    }
107
294k
    return *this;
108
294k
}
109
110
// -------------------------------------
111
// Formats the obj and append the result in the buffer, toAppendTo.
112
// This calls the actual implementation in the concrete subclasses.
113
114
UnicodeString&
115
Format::format(const Formattable& obj,
116
               UnicodeString& toAppendTo,
117
               UErrorCode& status) const
118
0
{
119
0
    if (U_FAILURE(status)) return toAppendTo;
120
121
0
    FieldPosition pos(FieldPosition::DONT_CARE);
122
123
0
    return format(obj, toAppendTo, pos, status);
124
0
}
125
126
// -------------------------------------
127
// Default implementation sets unsupported error; subclasses should
128
// override.
129
130
UnicodeString&
131
Format::format(const Formattable& /* unused obj */,
132
               UnicodeString& toAppendTo,
133
               FieldPositionIterator* /* unused posIter */,
134
               UErrorCode& status) const
135
0
{
136
0
    if (!U_FAILURE(status)) {
137
0
      status = U_UNSUPPORTED_ERROR;
138
0
    }
139
0
    return toAppendTo;
140
0
}
141
142
// -------------------------------------
143
// Parses the source string and create the corresponding
144
// result object.  Checks the parse position for errors.
145
146
void
147
Format::parseObject(const UnicodeString& source,
148
                    Formattable& result,
149
                    UErrorCode& status) const
150
0
{
151
0
    if (U_FAILURE(status)) return;
152
153
0
    ParsePosition parsePosition(0);
154
0
    parseObject(source, result, parsePosition);
155
0
    if (parsePosition.getIndex() == 0) {
156
0
        status = U_INVALID_FORMAT_ERROR;
157
0
    }
158
0
}
159
160
// -------------------------------------
161
162
bool
163
Format::operator==(const Format& that) const
164
0
{
165
    // Subclasses: Call this method and then add more specific checks.
166
0
    return typeid(*this) == typeid(that);
167
0
}
168
//---------------------------------------
169
170
/**
171
 * Simple function for initializing a UParseError from a UnicodeString.
172
 *
173
 * @param pattern The pattern to copy into the parseError
174
 * @param pos The position in pattern where the error occurred
175
 * @param parseError The UParseError object to fill in
176
 * @draft ICU 2.4
177
 */
178
void Format::syntaxError(const UnicodeString& pattern,
179
                         int32_t pos,
180
0
                         UParseError& parseError) {
181
0
    parseError.offset = pos;
182
0
    parseError.line=0;  // we are not using line number
183
184
    // for pre-context
185
0
    int32_t start = (pos < U_PARSE_CONTEXT_LEN)? 0 : (pos - (U_PARSE_CONTEXT_LEN-1
186
0
                                                             /* subtract 1 so that we have room for null*/));
187
0
    int32_t stop  = pos;
188
0
    pattern.extract(start,stop-start,parseError.preContext,0);
189
    //null terminate the buffer
190
0
    parseError.preContext[stop-start] = 0;
191
192
    //for post-context
193
0
    start = pos+1;
194
0
    stop  = ((pos+U_PARSE_CONTEXT_LEN)<=pattern.length()) ? (pos+(U_PARSE_CONTEXT_LEN-1)) :
195
0
        pattern.length();
196
0
    pattern.extract(start,stop-start,parseError.postContext,0);
197
    //null terminate the buffer
198
0
    parseError.postContext[stop-start]= 0;
199
0
}
200
201
Locale
202
11.1k
Format::getLocale(ULocDataLocaleType type, UErrorCode& status) const {
203
11.1k
    return LocaleBased::getLocale(validLocale, actualLocale, type, status);
204
11.1k
}
205
206
const char *
207
0
Format::getLocaleID(ULocDataLocaleType type, UErrorCode& status) const {
208
0
    return LocaleBased::getLocaleID(validLocale,actualLocale, type, status);
209
0
}
210
211
void
212
123k
Format::setLocaleIDs(const char* valid, const char* actual) {
213
123k
    U_LOCALE_BASED(locBased, *this);
214
123k
    UErrorCode status = U_ZERO_ERROR;
215
123k
    locBased.setLocaleIDs(valid, actual, status);
216
123k
    U_ASSERT(U_SUCCESS(status));
217
123k
}
218
219
U_NAMESPACE_END
220
221
#endif /* #if !UCONFIG_NO_FORMATTING */
222
223
//eof