Coverage Report

Created: 2026-06-07 06:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/icu/icu4c/source/i18n/format.cpp
Line
Count
Source
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
28
#ifndef U_I18N_IMPLEMENTATION
29
#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
30
#endif
31
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
39
U_CAPI int32_t U_EXPORT2
40
uprv_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"
50
#include "unicode/ures.h"
51
#include "cstring.h"
52
#include "locbased.h"
53
54
// *****************************************************************************
55
// class Format
56
// *****************************************************************************
57
58
U_NAMESPACE_BEGIN
59
60
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(FieldPosition)
61
62
30.8k
FieldPosition::~FieldPosition() {}
63
64
FieldPosition *
65
0
FieldPosition::clone() const {
66
0
    return new FieldPosition(*this);
67
0
}
68
69
// -------------------------------------
70
// default constructor
71
72
Format::Format()
73
324k
    : UObject(), actualLocale(Locale::getRoot()), validLocale(Locale::getRoot())
74
324k
{
75
324k
}
76
77
// -------------------------------------
78
79
Format::~Format()
80
490k
{
81
490k
}
82
83
// -------------------------------------
84
// copy constructor
85
86
Format::Format(const Format &that)
87
166k
    : UObject(that)
88
166k
{
89
166k
    *this = that;
90
166k
}
91
92
// -------------------------------------
93
// assignment operator
94
95
Format&
96
Format::operator=(const Format& that)
97
334k
{
98
334k
    if (this != &that) {
99
334k
        actualLocale = that.actualLocale;
100
334k
        validLocale = that.validLocale;
101
334k
    }
102
334k
    return *this;
103
334k
}
104
105
// -------------------------------------
106
// Formats the obj and append the result in the buffer, toAppendTo.
107
// This calls the actual implementation in the concrete subclasses.
108
109
UnicodeString&
110
Format::format(const Formattable& obj,
111
               UnicodeString& toAppendTo,
112
               UErrorCode& status) const
113
0
{
114
0
    if (U_FAILURE(status)) return toAppendTo;
115
116
0
    FieldPosition pos(FieldPosition::DONT_CARE);
117
118
0
    return format(obj, toAppendTo, pos, status);
119
0
}
120
121
// -------------------------------------
122
// Default implementation sets unsupported error; subclasses should
123
// override.
124
125
UnicodeString&
126
Format::format(const Formattable& /* unused obj */,
127
               UnicodeString& toAppendTo,
128
               FieldPositionIterator* /* unused posIter */,
129
               UErrorCode& status) const
130
0
{
131
0
    if (!U_FAILURE(status)) {
132
0
      status = U_UNSUPPORTED_ERROR;
133
0
    }
134
0
    return toAppendTo;
135
0
}
136
137
// -------------------------------------
138
// Parses the source string and create the corresponding
139
// result object.  Checks the parse position for errors.
140
141
void
142
Format::parseObject(const UnicodeString& source,
143
                    Formattable& result,
144
                    UErrorCode& status) const
145
0
{
146
0
    if (U_FAILURE(status)) return;
147
148
0
    ParsePosition parsePosition(0);
149
0
    parseObject(source, result, parsePosition);
150
0
    if (parsePosition.getIndex() == 0) {
151
0
        status = U_INVALID_FORMAT_ERROR;
152
0
    }
153
0
}
154
155
// -------------------------------------
156
157
bool
158
Format::operator==(const Format& that) const
159
0
{
160
    // Subclasses: Call this method and then add more specific checks.
161
0
    return typeid(*this) == typeid(that);
162
0
}
163
//---------------------------------------
164
165
/**
166
 * Simple function for initializing a UParseError from a UnicodeString.
167
 *
168
 * @param pattern The pattern to copy into the parseError
169
 * @param pos The position in pattern where the error occurred
170
 * @param parseError The UParseError object to fill in
171
 * @draft ICU 2.4
172
 */
173
void Format::syntaxError(const UnicodeString& pattern,
174
                         int32_t pos,
175
0
                         UParseError& parseError) {
176
0
    parseError.offset = pos;
177
0
    parseError.line=0;  // we are not using line number
178
179
    // for pre-context
180
0
    int32_t start = (pos < U_PARSE_CONTEXT_LEN)? 0 : (pos - (U_PARSE_CONTEXT_LEN-1
181
0
                                                             /* subtract 1 so that we have room for null*/));
182
0
    int32_t stop  = pos;
183
0
    pattern.extract(start,stop-start,parseError.preContext,0);
184
    //null terminate the buffer
185
0
    parseError.preContext[stop-start] = 0;
186
187
    //for post-context
188
0
    start = pos+1;
189
0
    stop  = ((pos+U_PARSE_CONTEXT_LEN)<=pattern.length()) ? (pos+(U_PARSE_CONTEXT_LEN-1)) :
190
0
        pattern.length();
191
0
    pattern.extract(start,stop-start,parseError.postContext,0);
192
    //null terminate the buffer
193
0
    parseError.postContext[stop-start]= 0;
194
0
}
195
196
Locale
197
12.0k
Format::getLocale(ULocDataLocaleType type, UErrorCode& status) const {
198
12.0k
    return LocaleBased::getLocale(validLocale, actualLocale, type, status);
199
12.0k
}
200
201
const char *
202
0
Format::getLocaleID(ULocDataLocaleType type, UErrorCode& status) const {
203
0
    return LocaleBased::getLocaleID(validLocale,actualLocale, type, status);
204
0
}
205
206
void
207
104k
Format::setLocaleIDs(const char* valid, const char* actual) {
208
104k
    actualLocale = Locale(actual);
209
104k
    validLocale = Locale(valid);
210
104k
}
211
212
U_NAMESPACE_END
213
214
#endif /* #if !UCONFIG_NO_FORMATTING */
215
216
//eof