/src/icu/icu4c/source/common/simpleformatter.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) 2014-2016, International Business Machines |
6 | | * Corporation and others. All Rights Reserved. |
7 | | ****************************************************************************** |
8 | | * simpleformatter.cpp |
9 | | */ |
10 | | |
11 | | #include "unicode/utypes.h" |
12 | | #include "unicode/simpleformatter.h" |
13 | | #include "unicode/unistr.h" |
14 | | #include "uassert.h" |
15 | | |
16 | | U_NAMESPACE_BEGIN |
17 | | |
18 | | namespace { |
19 | | |
20 | | /** |
21 | | * Argument numbers must be smaller than this limit. |
22 | | * Text segment lengths are offset by this much. |
23 | | * This is currently the only unused char value in compiled patterns, |
24 | | * except it is the maximum value of the first unit (max arg +1). |
25 | | */ |
26 | | const int32_t ARG_NUM_LIMIT = 0x100; |
27 | | /** |
28 | | * Initial and maximum char/char16_t value set for a text segment. |
29 | | * Segment length char values are from ARG_NUM_LIMIT+1 to this value here. |
30 | | * Normally 0xffff, but can be as small as ARG_NUM_LIMIT+1 for testing. |
31 | | */ |
32 | | const char16_t SEGMENT_LENGTH_PLACEHOLDER_CHAR = 0xffff; |
33 | | /** |
34 | | * Maximum length of a text segment. Longer segments are split into shorter ones. |
35 | | */ |
36 | | const int32_t MAX_SEGMENT_LENGTH = SEGMENT_LENGTH_PLACEHOLDER_CHAR - ARG_NUM_LIMIT; |
37 | | |
38 | | enum { |
39 | | APOS = 0x27, |
40 | | DIGIT_ZERO = 0x30, |
41 | | DIGIT_ONE = 0x31, |
42 | | DIGIT_NINE = 0x39, |
43 | | OPEN_BRACE = 0x7b, |
44 | | CLOSE_BRACE = 0x7d |
45 | | }; |
46 | | |
47 | 44.9k | inline UBool isInvalidArray(const void *array, int32_t length) { |
48 | 44.9k | return (length < 0 || (array == nullptr && length != 0)); |
49 | 44.9k | } |
50 | | |
51 | | } // namespace |
52 | | |
53 | 0 | SimpleFormatter &SimpleFormatter::operator=(const SimpleFormatter& other) { |
54 | 0 | if (this == &other) { |
55 | 0 | return *this; |
56 | 0 | } |
57 | 0 | compiledPattern = other.compiledPattern; |
58 | 0 | return *this; |
59 | 0 | } |
60 | | |
61 | 156k | SimpleFormatter::~SimpleFormatter() {} |
62 | | |
63 | | UBool SimpleFormatter::applyPatternMinMaxArguments( |
64 | | const UnicodeString &pattern, |
65 | | int32_t min, int32_t max, |
66 | 191k | UErrorCode &errorCode) { |
67 | 191k | if (U_FAILURE(errorCode)) { |
68 | 1.32k | return false; |
69 | 1.32k | } |
70 | | // Parse consistent with MessagePattern, but |
71 | | // - support only simple numbered arguments |
72 | | // - build a simple binary structure into the result string |
73 | 190k | const char16_t *patternBuffer = pattern.getBuffer(); |
74 | 190k | int32_t patternLength = pattern.length(); |
75 | | // Reserve the first char for the number of arguments. |
76 | 190k | compiledPattern.setTo(static_cast<char16_t>(0)); |
77 | 190k | int32_t textLength = 0; |
78 | 190k | int32_t maxArg = -1; |
79 | 190k | UBool inQuote = false; |
80 | 2.12M | for (int32_t i = 0; i < patternLength;) { |
81 | 1.93M | char16_t c = patternBuffer[i++]; |
82 | 1.93M | if (c == APOS) { |
83 | 2.35k | if (i < patternLength && (c = patternBuffer[i]) == APOS) { |
84 | | // double apostrophe, skip the second one |
85 | 0 | ++i; |
86 | 2.35k | } else if (inQuote) { |
87 | | // skip the quote-ending apostrophe |
88 | 0 | inQuote = false; |
89 | 0 | continue; |
90 | 2.35k | } else if (c == OPEN_BRACE || c == CLOSE_BRACE) { |
91 | | // Skip the quote-starting apostrophe, find the end of the quoted literal text. |
92 | 0 | ++i; |
93 | 0 | inQuote = true; |
94 | 2.35k | } else { |
95 | | // The apostrophe is part of literal text. |
96 | 2.35k | c = APOS; |
97 | 2.35k | } |
98 | 1.93M | } else if (!inQuote && c == OPEN_BRACE) { |
99 | 217k | if (textLength > 0) { |
100 | 133k | compiledPattern.setCharAt(compiledPattern.length() - textLength - 1, |
101 | 133k | static_cast<char16_t>(ARG_NUM_LIMIT + textLength)); |
102 | 133k | textLength = 0; |
103 | 133k | } |
104 | 217k | int32_t argNumber; |
105 | 217k | if ((i + 1) < patternLength && |
106 | 217k | 0 <= (argNumber = patternBuffer[i] - DIGIT_ZERO) && argNumber <= 9 && |
107 | 217k | patternBuffer[i + 1] == CLOSE_BRACE) { |
108 | 217k | i += 2; |
109 | 217k | } else { |
110 | | // Multi-digit argument number (no leading zero) or syntax error. |
111 | | // MessagePattern permits PatternProps.skipWhiteSpace(pattern, index) |
112 | | // around the number, but this class does not. |
113 | 0 | argNumber = -1; |
114 | 0 | if (i < patternLength && DIGIT_ONE <= (c = patternBuffer[i++]) && c <= DIGIT_NINE) { |
115 | 0 | argNumber = c - DIGIT_ZERO; |
116 | 0 | while (i < patternLength && |
117 | 0 | DIGIT_ZERO <= (c = patternBuffer[i++]) && c <= DIGIT_NINE) { |
118 | 0 | argNumber = argNumber * 10 + (c - DIGIT_ZERO); |
119 | 0 | if (argNumber >= ARG_NUM_LIMIT) { |
120 | 0 | break; |
121 | 0 | } |
122 | 0 | } |
123 | 0 | } |
124 | 0 | if (argNumber < 0 || c != CLOSE_BRACE) { |
125 | 0 | errorCode = U_ILLEGAL_ARGUMENT_ERROR; |
126 | 0 | return false; |
127 | 0 | } |
128 | 0 | } |
129 | 217k | if (argNumber > maxArg) { |
130 | 194k | maxArg = argNumber; |
131 | 194k | } |
132 | 217k | compiledPattern.append(static_cast<char16_t>(argNumber)); |
133 | 217k | continue; |
134 | 217k | } // else: c is part of literal text |
135 | | // Append c and track the literal-text segment length. |
136 | 1.71M | if (textLength == 0) { |
137 | | // Reserve a char for the length of a new text segment, preset the maximum length. |
138 | 298k | compiledPattern.append(SEGMENT_LENGTH_PLACEHOLDER_CHAR); |
139 | 298k | } |
140 | 1.71M | compiledPattern.append(c); |
141 | 1.71M | if (++textLength == MAX_SEGMENT_LENGTH) { |
142 | 0 | textLength = 0; |
143 | 0 | } |
144 | 1.71M | } |
145 | 190k | if (textLength > 0) { |
146 | 164k | compiledPattern.setCharAt(compiledPattern.length() - textLength - 1, |
147 | 164k | static_cast<char16_t>(ARG_NUM_LIMIT + textLength)); |
148 | 164k | } |
149 | 190k | int32_t argCount = maxArg + 1; |
150 | 190k | if (argCount < min || max < argCount) { |
151 | 22 | errorCode = U_ILLEGAL_ARGUMENT_ERROR; |
152 | 22 | return false; |
153 | 22 | } |
154 | 190k | compiledPattern.setCharAt(0, static_cast<char16_t>(argCount)); |
155 | 190k | return true; |
156 | 190k | } |
157 | | |
158 | | UnicodeString& SimpleFormatter::format( |
159 | | const UnicodeString &value0, |
160 | 0 | UnicodeString &appendTo, UErrorCode &errorCode) const { |
161 | 0 | const UnicodeString *values[] = { &value0 }; |
162 | 0 | return formatAndAppend(values, 1, appendTo, nullptr, 0, errorCode); |
163 | 0 | } |
164 | | |
165 | | UnicodeString& SimpleFormatter::format( |
166 | | const UnicodeString &value0, |
167 | | const UnicodeString &value1, |
168 | 14.7k | UnicodeString &appendTo, UErrorCode &errorCode) const { |
169 | 14.7k | const UnicodeString *values[] = { &value0, &value1 }; |
170 | 14.7k | return formatAndAppend(values, 2, appendTo, nullptr, 0, errorCode); |
171 | 14.7k | } |
172 | | |
173 | | UnicodeString& SimpleFormatter::format( |
174 | | const UnicodeString &value0, |
175 | | const UnicodeString &value1, |
176 | | const UnicodeString &value2, |
177 | 0 | UnicodeString &appendTo, UErrorCode &errorCode) const { |
178 | 0 | const UnicodeString *values[] = { &value0, &value1, &value2 }; |
179 | 0 | return formatAndAppend(values, 3, appendTo, nullptr, 0, errorCode); |
180 | 0 | } |
181 | | |
182 | | UnicodeString& SimpleFormatter::formatAndAppend( |
183 | | const UnicodeString *const *values, int32_t valuesLength, |
184 | | UnicodeString &appendTo, |
185 | 14.7k | int32_t *offsets, int32_t offsetsLength, UErrorCode &errorCode) const { |
186 | 14.7k | if (U_FAILURE(errorCode)) { |
187 | 1.34k | return appendTo; |
188 | 1.34k | } |
189 | 13.3k | if (isInvalidArray(values, valuesLength) || isInvalidArray(offsets, offsetsLength) || |
190 | 13.3k | valuesLength < getArgumentLimit()) { |
191 | 0 | errorCode = U_ILLEGAL_ARGUMENT_ERROR; |
192 | 0 | return appendTo; |
193 | 0 | } |
194 | 13.3k | return format(compiledPattern.getBuffer(), compiledPattern.length(), values, |
195 | 13.3k | appendTo, nullptr, true, |
196 | 13.3k | offsets, offsetsLength, errorCode); |
197 | 13.3k | } |
198 | | |
199 | | UnicodeString &SimpleFormatter::formatAndReplace( |
200 | | const UnicodeString *const *values, int32_t valuesLength, |
201 | | UnicodeString &result, |
202 | 9.13k | int32_t *offsets, int32_t offsetsLength, UErrorCode &errorCode) const { |
203 | 9.13k | if (U_FAILURE(errorCode)) { |
204 | 0 | return result; |
205 | 0 | } |
206 | 9.13k | if (isInvalidArray(values, valuesLength) || isInvalidArray(offsets, offsetsLength)) { |
207 | 0 | errorCode = U_ILLEGAL_ARGUMENT_ERROR; |
208 | 0 | return result; |
209 | 0 | } |
210 | 9.13k | const char16_t *cp = compiledPattern.getBuffer(); |
211 | 9.13k | int32_t cpLength = compiledPattern.length(); |
212 | 9.13k | if (valuesLength < getArgumentLimit(cp, cpLength)) { |
213 | 0 | errorCode = U_ILLEGAL_ARGUMENT_ERROR; |
214 | 0 | return result; |
215 | 0 | } |
216 | | |
217 | | // If the pattern starts with an argument whose value is the same object |
218 | | // as the result, then we keep the result contents and append to it. |
219 | | // Otherwise we replace its contents. |
220 | 9.13k | int32_t firstArg = -1; |
221 | | // If any non-initial argument value is the same object as the result, |
222 | | // then we first copy its contents and use that instead while formatting. |
223 | 9.13k | UnicodeString resultCopy; |
224 | 9.13k | if (getArgumentLimit(cp, cpLength) > 0) { |
225 | 50.4k | for (int32_t i = 1; i < cpLength;) { |
226 | 41.2k | int32_t n = cp[i++]; |
227 | 41.2k | if (n < ARG_NUM_LIMIT) { |
228 | 22.8k | if (values[n] == &result) { |
229 | 9.13k | if (i == 2) { |
230 | 8.68k | firstArg = n; |
231 | 8.68k | } else if (resultCopy.isEmpty() && !result.isEmpty()) { |
232 | 442 | resultCopy = result; |
233 | 442 | } |
234 | 9.13k | } |
235 | 22.8k | } else { |
236 | 18.3k | i += n - ARG_NUM_LIMIT; |
237 | 18.3k | } |
238 | 41.2k | } |
239 | 9.13k | } |
240 | 9.13k | if (firstArg < 0) { |
241 | 445 | result.remove(); |
242 | 445 | } |
243 | 9.13k | return format(cp, cpLength, values, |
244 | 9.13k | result, &resultCopy, false, |
245 | 9.13k | offsets, offsetsLength, errorCode); |
246 | 9.13k | } |
247 | | |
248 | | UnicodeString SimpleFormatter::getTextWithNoArguments( |
249 | | const char16_t *compiledPattern, |
250 | | int32_t compiledPatternLength, |
251 | | int32_t* offsets, |
252 | 29.0k | int32_t offsetsLength) { |
253 | 87.1k | for (int32_t i = 0; i < offsetsLength; i++) { |
254 | 58.0k | offsets[i] = -1; |
255 | 58.0k | } |
256 | 29.0k | int32_t capacity = compiledPatternLength - 1 - |
257 | 29.0k | getArgumentLimit(compiledPattern, compiledPatternLength); |
258 | 29.0k | UnicodeString sb(capacity, 0, 0); // Java: StringBuilder |
259 | 116k | for (int32_t i = 1; i < compiledPatternLength;) { |
260 | 87.4k | int32_t n = compiledPattern[i++]; |
261 | 87.4k | if (n > ARG_NUM_LIMIT) { |
262 | 29.3k | n -= ARG_NUM_LIMIT; |
263 | 29.3k | sb.append(compiledPattern + i, n); |
264 | 29.3k | i += n; |
265 | 58.0k | } else if (n < offsetsLength) { |
266 | | // TODO(ICU-20406): This does not distinguish between "{0}{1}" and "{1}{0}". |
267 | | // Consider removing this function and replacing it with an iterator interface. |
268 | 58.0k | offsets[n] = sb.length(); |
269 | 58.0k | } |
270 | 87.4k | } |
271 | 29.0k | return sb; |
272 | 29.0k | } |
273 | | |
274 | | UnicodeString &SimpleFormatter::format( |
275 | | const char16_t *compiledPattern, int32_t compiledPatternLength, |
276 | | const UnicodeString *const *values, |
277 | | UnicodeString &result, const UnicodeString *resultCopy, UBool forbidResultAsValue, |
278 | | int32_t *offsets, int32_t offsetsLength, |
279 | 22.4k | UErrorCode &errorCode) { |
280 | 22.4k | if (U_FAILURE(errorCode)) { |
281 | 0 | return result; |
282 | 0 | } |
283 | 22.4k | for (int32_t i = 0; i < offsetsLength; i++) { |
284 | 0 | offsets[i] = -1; |
285 | 0 | } |
286 | 103k | for (int32_t i = 1; i < compiledPatternLength;) { |
287 | 81.3k | int32_t n = compiledPattern[i++]; |
288 | 81.3k | if (n < ARG_NUM_LIMIT) { |
289 | 49.5k | const UnicodeString *value = values[n]; |
290 | 49.5k | if (value == nullptr) { |
291 | 0 | errorCode = U_ILLEGAL_ARGUMENT_ERROR; |
292 | 0 | return result; |
293 | 0 | } |
294 | 49.5k | if (value == &result) { |
295 | 9.13k | if (forbidResultAsValue) { |
296 | 0 | errorCode = U_ILLEGAL_ARGUMENT_ERROR; |
297 | 0 | return result; |
298 | 0 | } |
299 | 9.13k | if (i == 2) { |
300 | | // We are appending to result which is also the first value object. |
301 | 8.68k | if (n < offsetsLength) { |
302 | 0 | offsets[n] = 0; |
303 | 0 | } |
304 | 8.68k | } else { |
305 | 445 | if (n < offsetsLength) { |
306 | 0 | offsets[n] = result.length(); |
307 | 0 | } |
308 | 445 | result.append(*resultCopy); |
309 | 445 | } |
310 | 40.4k | } else { |
311 | 40.4k | if (n < offsetsLength) { |
312 | 0 | offsets[n] = result.length(); |
313 | 0 | } |
314 | 40.4k | result.append(*value); |
315 | 40.4k | } |
316 | 49.5k | } else { |
317 | 31.8k | int32_t length = n - ARG_NUM_LIMIT; |
318 | 31.8k | result.append(compiledPattern + i, length); |
319 | 31.8k | i += length; |
320 | 31.8k | } |
321 | 81.3k | } |
322 | 22.4k | return result; |
323 | 22.4k | } |
324 | | |
325 | | U_NAMESPACE_END |