/src/icu/source/i18n/umsg.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 | | * |
6 | | * Copyright (C) 1999-2012, International Business Machines |
7 | | * Corporation and others. All Rights Reserved. |
8 | | * |
9 | | ******************************************************************************* |
10 | | * file name: umsg.cpp |
11 | | * encoding: UTF-8 |
12 | | * tab size: 8 (not used) |
13 | | * indentation:4 |
14 | | * |
15 | | * This is a C wrapper to MessageFormat C++ API. |
16 | | * |
17 | | * Change history: |
18 | | * |
19 | | * 08/5/2001 Ram Added C wrappers for C++ API. Changed implementation of old API's |
20 | | * Removed pattern parser. |
21 | | * |
22 | | */ |
23 | | |
24 | | #include "unicode/utypes.h" |
25 | | |
26 | | #if !UCONFIG_NO_FORMATTING |
27 | | |
28 | | #include "unicode/umsg.h" |
29 | | #include "unicode/ustring.h" |
30 | | #include "unicode/fmtable.h" |
31 | | #include "unicode/msgfmt.h" |
32 | | #include "unicode/unistr.h" |
33 | | #include "cpputils.h" |
34 | | #include "uassert.h" |
35 | | #include "ustr_imp.h" |
36 | | |
37 | | U_NAMESPACE_BEGIN |
38 | | /** |
39 | | * This class isolates our access to private internal methods of |
40 | | * MessageFormat. It is never instantiated; it exists only for C++ |
41 | | * access management. |
42 | | */ |
43 | | class MessageFormatAdapter { |
44 | | public: |
45 | | static const Formattable::Type* getArgTypeList(const MessageFormat& m, |
46 | | int32_t& count); |
47 | 0 | static UBool hasArgTypeConflicts(const MessageFormat& m) { |
48 | 0 | return m.hasArgTypeConflicts; |
49 | 0 | } |
50 | | }; |
51 | | const Formattable::Type* |
52 | | MessageFormatAdapter::getArgTypeList(const MessageFormat& m, |
53 | 0 | int32_t& count) { |
54 | 0 | return m.getArgTypeList(count); |
55 | 0 | } |
56 | | U_NAMESPACE_END |
57 | | |
58 | | U_NAMESPACE_USE |
59 | | |
60 | | U_CAPI int32_t |
61 | | u_formatMessage(const char *locale, |
62 | | const UChar *pattern, |
63 | | int32_t patternLength, |
64 | | UChar *result, |
65 | | int32_t resultLength, |
66 | | UErrorCode *status, |
67 | | ...) |
68 | 0 | { |
69 | 0 | va_list ap; |
70 | 0 | int32_t actLen; |
71 | | //argument checking deferred to subsequent method calls |
72 | | // start vararg processing |
73 | 0 | va_start(ap, status); |
74 | |
|
75 | 0 | actLen = u_vformatMessage(locale,pattern,patternLength,result,resultLength,ap,status); |
76 | | // end vararg processing |
77 | 0 | va_end(ap); |
78 | |
|
79 | 0 | return actLen; |
80 | 0 | } |
81 | | |
82 | | U_CAPI int32_t U_EXPORT2 |
83 | | u_vformatMessage( const char *locale, |
84 | | const UChar *pattern, |
85 | | int32_t patternLength, |
86 | | UChar *result, |
87 | | int32_t resultLength, |
88 | | va_list ap, |
89 | | UErrorCode *status) |
90 | | |
91 | 0 | { |
92 | | //argument checking deferred to subsequent method calls |
93 | 0 | UMessageFormat *fmt = umsg_open(pattern,patternLength,locale,NULL,status); |
94 | 0 | int32_t retVal = umsg_vformat(fmt,result,resultLength,ap,status); |
95 | 0 | umsg_close(fmt); |
96 | 0 | return retVal; |
97 | 0 | } |
98 | | |
99 | | U_CAPI int32_t |
100 | | u_formatMessageWithError(const char *locale, |
101 | | const UChar *pattern, |
102 | | int32_t patternLength, |
103 | | UChar *result, |
104 | | int32_t resultLength, |
105 | | UParseError *parseError, |
106 | | UErrorCode *status, |
107 | | ...) |
108 | 0 | { |
109 | 0 | va_list ap; |
110 | 0 | int32_t actLen; |
111 | | //argument checking deferred to subsequent method calls |
112 | | // start vararg processing |
113 | 0 | va_start(ap, status); |
114 | |
|
115 | 0 | actLen = u_vformatMessageWithError(locale,pattern,patternLength,result,resultLength,parseError,ap,status); |
116 | | |
117 | | // end vararg processing |
118 | 0 | va_end(ap); |
119 | 0 | return actLen; |
120 | 0 | } |
121 | | |
122 | | U_CAPI int32_t U_EXPORT2 |
123 | | u_vformatMessageWithError( const char *locale, |
124 | | const UChar *pattern, |
125 | | int32_t patternLength, |
126 | | UChar *result, |
127 | | int32_t resultLength, |
128 | | UParseError *parseError, |
129 | | va_list ap, |
130 | | UErrorCode *status) |
131 | | |
132 | 0 | { |
133 | | //argument checking deferred to subsequent method calls |
134 | 0 | UMessageFormat *fmt = umsg_open(pattern,patternLength,locale,parseError,status); |
135 | 0 | int32_t retVal = umsg_vformat(fmt,result,resultLength,ap,status); |
136 | 0 | umsg_close(fmt); |
137 | 0 | return retVal; |
138 | 0 | } |
139 | | |
140 | | |
141 | | // For parse, do the reverse of format: |
142 | | // 1. Call through to the C++ APIs |
143 | | // 2. Just assume the user passed in enough arguments. |
144 | | // 3. Iterate through each formattable returned, and assign to the arguments |
145 | | U_CAPI void |
146 | | u_parseMessage( const char *locale, |
147 | | const UChar *pattern, |
148 | | int32_t patternLength, |
149 | | const UChar *source, |
150 | | int32_t sourceLength, |
151 | | UErrorCode *status, |
152 | | ...) |
153 | 0 | { |
154 | 0 | va_list ap; |
155 | | //argument checking deferred to subsequent method calls |
156 | | |
157 | | // start vararg processing |
158 | 0 | va_start(ap, status); |
159 | |
|
160 | 0 | u_vparseMessage(locale,pattern,patternLength,source,sourceLength,ap,status); |
161 | | // end vararg processing |
162 | 0 | va_end(ap); |
163 | 0 | } |
164 | | |
165 | | U_CAPI void U_EXPORT2 |
166 | | u_vparseMessage(const char *locale, |
167 | | const UChar *pattern, |
168 | | int32_t patternLength, |
169 | | const UChar *source, |
170 | | int32_t sourceLength, |
171 | | va_list ap, |
172 | | UErrorCode *status) |
173 | 0 | { |
174 | | //argument checking deferred to subsequent method calls |
175 | 0 | UMessageFormat *fmt = umsg_open(pattern,patternLength,locale,NULL,status); |
176 | 0 | int32_t count = 0; |
177 | 0 | umsg_vparse(fmt,source,sourceLength,&count,ap,status); |
178 | 0 | umsg_close(fmt); |
179 | 0 | } |
180 | | |
181 | | U_CAPI void |
182 | | u_parseMessageWithError(const char *locale, |
183 | | const UChar *pattern, |
184 | | int32_t patternLength, |
185 | | const UChar *source, |
186 | | int32_t sourceLength, |
187 | | UParseError *error, |
188 | | UErrorCode *status, |
189 | | ...) |
190 | 0 | { |
191 | 0 | va_list ap; |
192 | | |
193 | | //argument checking deferred to subsequent method calls |
194 | | |
195 | | // start vararg processing |
196 | 0 | va_start(ap, status); |
197 | |
|
198 | 0 | u_vparseMessageWithError(locale,pattern,patternLength,source,sourceLength,ap,error,status); |
199 | | // end vararg processing |
200 | 0 | va_end(ap); |
201 | 0 | } |
202 | | U_CAPI void U_EXPORT2 |
203 | | u_vparseMessageWithError(const char *locale, |
204 | | const UChar *pattern, |
205 | | int32_t patternLength, |
206 | | const UChar *source, |
207 | | int32_t sourceLength, |
208 | | va_list ap, |
209 | | UParseError *error, |
210 | | UErrorCode* status) |
211 | 0 | { |
212 | | //argument checking deferred to subsequent method calls |
213 | 0 | UMessageFormat *fmt = umsg_open(pattern,patternLength,locale,error,status); |
214 | 0 | int32_t count = 0; |
215 | 0 | umsg_vparse(fmt,source,sourceLength,&count,ap,status); |
216 | 0 | umsg_close(fmt); |
217 | 0 | } |
218 | | ////////////////////////////////////////////////////////////////////////////////// |
219 | | // |
220 | | // Message format C API |
221 | | // |
222 | | ///////////////////////////////////////////////////////////////////////////////// |
223 | | |
224 | | |
225 | | U_CAPI UMessageFormat* U_EXPORT2 |
226 | | umsg_open( const UChar *pattern, |
227 | | int32_t patternLength, |
228 | | const char *locale, |
229 | | UParseError *parseError, |
230 | | UErrorCode *status) |
231 | 0 | { |
232 | | //check arguments |
233 | 0 | if(status==NULL || U_FAILURE(*status)) |
234 | 0 | { |
235 | 0 | return 0; |
236 | 0 | } |
237 | 0 | if(pattern==NULL||patternLength<-1){ |
238 | 0 | *status=U_ILLEGAL_ARGUMENT_ERROR; |
239 | 0 | return 0; |
240 | 0 | } |
241 | | |
242 | 0 | UParseError tErr; |
243 | 0 | if(parseError==NULL) |
244 | 0 | { |
245 | 0 | parseError = &tErr; |
246 | 0 | } |
247 | |
|
248 | 0 | int32_t len = (patternLength == -1 ? u_strlen(pattern) : patternLength); |
249 | 0 | UnicodeString patString(patternLength == -1, pattern, len); |
250 | |
|
251 | 0 | MessageFormat* retVal = new MessageFormat(patString,Locale(locale),*parseError,*status); |
252 | 0 | if(retVal == NULL) { |
253 | 0 | *status = U_MEMORY_ALLOCATION_ERROR; |
254 | 0 | return NULL; |
255 | 0 | } |
256 | 0 | if (U_SUCCESS(*status) && MessageFormatAdapter::hasArgTypeConflicts(*retVal)) { |
257 | 0 | *status = U_ARGUMENT_TYPE_MISMATCH; |
258 | 0 | } |
259 | 0 | return (UMessageFormat*)retVal; |
260 | 0 | } |
261 | | |
262 | | U_CAPI void U_EXPORT2 |
263 | | umsg_close(UMessageFormat* format) |
264 | 0 | { |
265 | | //check arguments |
266 | 0 | if(format==NULL){ |
267 | 0 | return; |
268 | 0 | } |
269 | 0 | delete (MessageFormat*) format; |
270 | 0 | } |
271 | | |
272 | | U_CAPI UMessageFormat U_EXPORT2 |
273 | | umsg_clone(const UMessageFormat *fmt, |
274 | | UErrorCode *status) |
275 | 0 | { |
276 | | //check arguments |
277 | 0 | if(status==NULL || U_FAILURE(*status)){ |
278 | 0 | return NULL; |
279 | 0 | } |
280 | 0 | if(fmt==NULL){ |
281 | 0 | *status = U_ILLEGAL_ARGUMENT_ERROR; |
282 | 0 | return NULL; |
283 | 0 | } |
284 | 0 | UMessageFormat retVal = (UMessageFormat)((MessageFormat*)fmt)->clone(); |
285 | 0 | if(retVal == 0) { |
286 | 0 | *status = U_MEMORY_ALLOCATION_ERROR; |
287 | 0 | return 0; |
288 | 0 | } |
289 | 0 | return retVal; |
290 | 0 | } |
291 | | |
292 | | U_CAPI void U_EXPORT2 |
293 | | umsg_setLocale(UMessageFormat *fmt, const char* locale) |
294 | 0 | { |
295 | | //check arguments |
296 | 0 | if(fmt==NULL){ |
297 | 0 | return; |
298 | 0 | } |
299 | 0 | ((MessageFormat*)fmt)->setLocale(Locale(locale)); |
300 | 0 | } |
301 | | |
302 | | U_CAPI const char* U_EXPORT2 |
303 | | umsg_getLocale(const UMessageFormat *fmt) |
304 | 0 | { |
305 | | //check arguments |
306 | 0 | if(fmt==NULL){ |
307 | 0 | return ""; |
308 | 0 | } |
309 | 0 | return ((const MessageFormat*)fmt)->getLocale().getName(); |
310 | 0 | } |
311 | | |
312 | | U_CAPI void U_EXPORT2 |
313 | | umsg_applyPattern(UMessageFormat *fmt, |
314 | | const UChar* pattern, |
315 | | int32_t patternLength, |
316 | | UParseError* parseError, |
317 | | UErrorCode* status) |
318 | 0 | { |
319 | | //check arguments |
320 | 0 | UParseError tErr; |
321 | 0 | if(status ==NULL||U_FAILURE(*status)){ |
322 | 0 | return ; |
323 | 0 | } |
324 | 0 | if(fmt==NULL || (pattern==NULL && patternLength!=0) || patternLength<-1) { |
325 | 0 | *status=U_ILLEGAL_ARGUMENT_ERROR; |
326 | 0 | return ; |
327 | 0 | } |
328 | | |
329 | 0 | if(parseError==NULL){ |
330 | 0 | parseError = &tErr; |
331 | 0 | } |
332 | | |
333 | | // UnicodeString(pattern, -1) calls u_strlen(). |
334 | 0 | ((MessageFormat*)fmt)->applyPattern(UnicodeString(pattern,patternLength),*parseError,*status); |
335 | 0 | } |
336 | | |
337 | | U_CAPI int32_t U_EXPORT2 |
338 | | umsg_toPattern(const UMessageFormat *fmt, |
339 | | UChar* result, |
340 | | int32_t resultLength, |
341 | | UErrorCode* status) |
342 | 0 | { |
343 | | //check arguments |
344 | 0 | if(status ==NULL||U_FAILURE(*status)){ |
345 | 0 | return -1; |
346 | 0 | } |
347 | 0 | if(fmt==NULL||resultLength<0 || (resultLength>0 && result==0)){ |
348 | 0 | *status=U_ILLEGAL_ARGUMENT_ERROR; |
349 | 0 | return -1; |
350 | 0 | } |
351 | | |
352 | | |
353 | 0 | UnicodeString res; |
354 | 0 | if(!(result==NULL && resultLength==0)) { |
355 | | // NULL destination for pure preflighting: empty dummy string |
356 | | // otherwise, alias the destination buffer |
357 | 0 | res.setTo(result, 0, resultLength); |
358 | 0 | } |
359 | 0 | ((const MessageFormat*)fmt)->toPattern(res); |
360 | 0 | return res.extract(result, resultLength, *status); |
361 | 0 | } |
362 | | |
363 | | U_CAPI int32_t |
364 | | umsg_format( const UMessageFormat *fmt, |
365 | | UChar *result, |
366 | | int32_t resultLength, |
367 | | UErrorCode *status, |
368 | | ...) |
369 | 0 | { |
370 | 0 | va_list ap; |
371 | 0 | int32_t actLen; |
372 | | //argument checking deferred to last method call umsg_vformat which |
373 | | //saves time when arguments are valid and we don't care when arguments are not |
374 | | //since we return an error anyway |
375 | | |
376 | | |
377 | | // start vararg processing |
378 | 0 | va_start(ap, status); |
379 | |
|
380 | 0 | actLen = umsg_vformat(fmt,result,resultLength,ap,status); |
381 | | |
382 | | // end vararg processing |
383 | 0 | va_end(ap); |
384 | |
|
385 | 0 | return actLen; |
386 | 0 | } |
387 | | |
388 | | U_CAPI int32_t U_EXPORT2 |
389 | | umsg_vformat( const UMessageFormat *fmt, |
390 | | UChar *result, |
391 | | int32_t resultLength, |
392 | | va_list ap, |
393 | | UErrorCode *status) |
394 | 0 | { |
395 | | //check arguments |
396 | 0 | if(status==0 || U_FAILURE(*status)) |
397 | 0 | { |
398 | 0 | return -1; |
399 | 0 | } |
400 | 0 | if(fmt==NULL||resultLength<0 || (resultLength>0 && result==0)) { |
401 | 0 | *status=U_ILLEGAL_ARGUMENT_ERROR; |
402 | 0 | return -1; |
403 | 0 | } |
404 | | |
405 | 0 | int32_t count =0; |
406 | 0 | const Formattable::Type* argTypes = |
407 | 0 | MessageFormatAdapter::getArgTypeList(*(const MessageFormat*)fmt, count); |
408 | | // Allocate at least one element. Allocating an array of length |
409 | | // zero causes problems on some platforms (e.g. Win32). |
410 | 0 | Formattable* args = new Formattable[count ? count : 1]; |
411 | | |
412 | | // iterate through the vararg list, and get the arguments out |
413 | 0 | for(int32_t i = 0; i < count; ++i) { |
414 | | |
415 | 0 | UChar *stringVal; |
416 | 0 | double tDouble=0; |
417 | 0 | int32_t tInt =0; |
418 | 0 | int64_t tInt64 = 0; |
419 | 0 | UDate tempDate = 0; |
420 | 0 | switch(argTypes[i]) { |
421 | 0 | case Formattable::kDate: |
422 | 0 | tempDate = va_arg(ap, UDate); |
423 | 0 | args[i].setDate(tempDate); |
424 | 0 | break; |
425 | | |
426 | 0 | case Formattable::kDouble: |
427 | 0 | tDouble =va_arg(ap, double); |
428 | 0 | args[i].setDouble(tDouble); |
429 | 0 | break; |
430 | | |
431 | 0 | case Formattable::kLong: |
432 | 0 | tInt = va_arg(ap, int32_t); |
433 | 0 | args[i].setLong(tInt); |
434 | 0 | break; |
435 | | |
436 | 0 | case Formattable::kInt64: |
437 | 0 | tInt64 = va_arg(ap, int64_t); |
438 | 0 | args[i].setInt64(tInt64); |
439 | 0 | break; |
440 | | |
441 | 0 | case Formattable::kString: |
442 | | // For some reason, a temporary is needed |
443 | 0 | stringVal = va_arg(ap, UChar*); |
444 | 0 | if(stringVal){ |
445 | 0 | args[i].setString(UnicodeString(stringVal)); |
446 | 0 | }else{ |
447 | 0 | *status=U_ILLEGAL_ARGUMENT_ERROR; |
448 | 0 | } |
449 | 0 | break; |
450 | | |
451 | 0 | case Formattable::kArray: |
452 | | // throw away this argument |
453 | | // this is highly platform-dependent, and probably won't work |
454 | | // so, if you try to skip arguments in the list (and not use them) |
455 | | // you'll probably crash |
456 | 0 | va_arg(ap, int); |
457 | 0 | break; |
458 | | |
459 | 0 | case Formattable::kObject: |
460 | | // Unused argument number. Read and ignore a pointer argument. |
461 | 0 | va_arg(ap, void*); |
462 | 0 | break; |
463 | | |
464 | 0 | default: |
465 | | // Unknown/unsupported argument type. |
466 | 0 | UPRV_UNREACHABLE; |
467 | 0 | } |
468 | 0 | } |
469 | 0 | UnicodeString resultStr; |
470 | 0 | FieldPosition fieldPosition(FieldPosition::DONT_CARE); |
471 | | |
472 | | /* format the message */ |
473 | 0 | ((const MessageFormat*)fmt)->format(args,count,resultStr,fieldPosition,*status); |
474 | |
|
475 | 0 | delete[] args; |
476 | |
|
477 | 0 | if(U_FAILURE(*status)){ |
478 | 0 | return -1; |
479 | 0 | } |
480 | | |
481 | 0 | return resultStr.extract(result, resultLength, *status); |
482 | 0 | } |
483 | | |
484 | | U_CAPI void |
485 | | umsg_parse( const UMessageFormat *fmt, |
486 | | const UChar *source, |
487 | | int32_t sourceLength, |
488 | | int32_t *count, |
489 | | UErrorCode *status, |
490 | | ...) |
491 | 0 | { |
492 | 0 | va_list ap; |
493 | | //argument checking deferred to last method call umsg_vparse which |
494 | | //saves time when arguments are valid and we don't care when arguments are not |
495 | | //since we return an error anyway |
496 | | |
497 | | // start vararg processing |
498 | 0 | va_start(ap, status); |
499 | |
|
500 | 0 | umsg_vparse(fmt,source,sourceLength,count,ap,status); |
501 | | |
502 | | // end vararg processing |
503 | 0 | va_end(ap); |
504 | 0 | } |
505 | | |
506 | | U_CAPI void U_EXPORT2 |
507 | | umsg_vparse(const UMessageFormat *fmt, |
508 | | const UChar *source, |
509 | | int32_t sourceLength, |
510 | | int32_t *count, |
511 | | va_list ap, |
512 | | UErrorCode *status) |
513 | 0 | { |
514 | | //check arguments |
515 | 0 | if(status==NULL||U_FAILURE(*status)) |
516 | 0 | { |
517 | 0 | return; |
518 | 0 | } |
519 | 0 | if(fmt==NULL||source==NULL || sourceLength<-1 || count==NULL){ |
520 | 0 | *status=U_ILLEGAL_ARGUMENT_ERROR; |
521 | 0 | return; |
522 | 0 | } |
523 | 0 | if(sourceLength==-1){ |
524 | 0 | sourceLength=u_strlen(source); |
525 | 0 | } |
526 | |
|
527 | 0 | UnicodeString srcString(source,sourceLength); |
528 | 0 | Formattable *args = ((const MessageFormat*)fmt)->parse(srcString,*count,*status); |
529 | 0 | UDate *aDate; |
530 | 0 | double *aDouble; |
531 | 0 | UChar *aString; |
532 | 0 | int32_t* aInt; |
533 | 0 | int64_t* aInt64; |
534 | 0 | UnicodeString temp; |
535 | 0 | int len =0; |
536 | | // assign formattables to varargs |
537 | 0 | for(int32_t i = 0; i < *count; i++) { |
538 | 0 | switch(args[i].getType()) { |
539 | | |
540 | 0 | case Formattable::kDate: |
541 | 0 | aDate = va_arg(ap, UDate*); |
542 | 0 | if(aDate){ |
543 | 0 | *aDate = args[i].getDate(); |
544 | 0 | }else{ |
545 | 0 | *status=U_ILLEGAL_ARGUMENT_ERROR; |
546 | 0 | } |
547 | 0 | break; |
548 | | |
549 | 0 | case Formattable::kDouble: |
550 | 0 | aDouble = va_arg(ap, double*); |
551 | 0 | if(aDouble){ |
552 | 0 | *aDouble = args[i].getDouble(); |
553 | 0 | }else{ |
554 | 0 | *status=U_ILLEGAL_ARGUMENT_ERROR; |
555 | 0 | } |
556 | 0 | break; |
557 | | |
558 | 0 | case Formattable::kLong: |
559 | 0 | aInt = va_arg(ap, int32_t*); |
560 | 0 | if(aInt){ |
561 | 0 | *aInt = (int32_t) args[i].getLong(); |
562 | 0 | }else{ |
563 | 0 | *status=U_ILLEGAL_ARGUMENT_ERROR; |
564 | 0 | } |
565 | 0 | break; |
566 | | |
567 | 0 | case Formattable::kInt64: |
568 | 0 | aInt64 = va_arg(ap, int64_t*); |
569 | 0 | if(aInt64){ |
570 | 0 | *aInt64 = args[i].getInt64(); |
571 | 0 | }else{ |
572 | 0 | *status=U_ILLEGAL_ARGUMENT_ERROR; |
573 | 0 | } |
574 | 0 | break; |
575 | | |
576 | 0 | case Formattable::kString: |
577 | 0 | aString = va_arg(ap, UChar*); |
578 | 0 | if(aString){ |
579 | 0 | args[i].getString(temp); |
580 | 0 | len = temp.length(); |
581 | 0 | temp.extract(0,len,aString); |
582 | 0 | aString[len]=0; |
583 | 0 | }else{ |
584 | 0 | *status= U_ILLEGAL_ARGUMENT_ERROR; |
585 | 0 | } |
586 | 0 | break; |
587 | | |
588 | 0 | case Formattable::kObject: |
589 | | // This will never happen because MessageFormat doesn't |
590 | | // support kObject. When MessageFormat is changed to |
591 | | // understand MeasureFormats, modify this code to do the |
592 | | // right thing. [alan] |
593 | 0 | UPRV_UNREACHABLE; |
594 | | |
595 | | // better not happen! |
596 | 0 | case Formattable::kArray: |
597 | 0 | UPRV_UNREACHABLE; |
598 | 0 | } |
599 | 0 | } |
600 | | |
601 | | // clean up |
602 | 0 | delete [] args; |
603 | 0 | } |
604 | | |
605 | 0 | #define SINGLE_QUOTE ((UChar)0x0027) |
606 | 0 | #define CURLY_BRACE_LEFT ((UChar)0x007B) |
607 | 0 | #define CURLY_BRACE_RIGHT ((UChar)0x007D) |
608 | | |
609 | 0 | #define STATE_INITIAL 0 |
610 | 0 | #define STATE_SINGLE_QUOTE 1 |
611 | 0 | #define STATE_IN_QUOTE 2 |
612 | 0 | #define STATE_MSG_ELEMENT 3 |
613 | | |
614 | 0 | #define MAppend(c) if (len < destCapacity) dest[len++] = c; else len++ |
615 | | |
616 | | int32_t umsg_autoQuoteApostrophe(const UChar* pattern, |
617 | | int32_t patternLength, |
618 | | UChar* dest, |
619 | | int32_t destCapacity, |
620 | | UErrorCode* ec) |
621 | 0 | { |
622 | 0 | int32_t state = STATE_INITIAL; |
623 | 0 | int32_t braceCount = 0; |
624 | 0 | int32_t len = 0; |
625 | |
|
626 | 0 | if (ec == NULL || U_FAILURE(*ec)) { |
627 | 0 | return -1; |
628 | 0 | } |
629 | | |
630 | 0 | if (pattern == NULL || patternLength < -1 || (dest == NULL && destCapacity > 0)) { |
631 | 0 | *ec = U_ILLEGAL_ARGUMENT_ERROR; |
632 | 0 | return -1; |
633 | 0 | } |
634 | 0 | U_ASSERT(destCapacity >= 0); |
635 | |
|
636 | 0 | if (patternLength == -1) { |
637 | 0 | patternLength = u_strlen(pattern); |
638 | 0 | } |
639 | |
|
640 | 0 | for (int i = 0; i < patternLength; ++i) { |
641 | 0 | UChar c = pattern[i]; |
642 | 0 | switch (state) { |
643 | 0 | case STATE_INITIAL: |
644 | 0 | switch (c) { |
645 | 0 | case SINGLE_QUOTE: |
646 | 0 | state = STATE_SINGLE_QUOTE; |
647 | 0 | break; |
648 | 0 | case CURLY_BRACE_LEFT: |
649 | 0 | state = STATE_MSG_ELEMENT; |
650 | 0 | ++braceCount; |
651 | 0 | break; |
652 | 0 | } |
653 | 0 | break; |
654 | | |
655 | 0 | case STATE_SINGLE_QUOTE: |
656 | 0 | switch (c) { |
657 | 0 | case SINGLE_QUOTE: |
658 | 0 | state = STATE_INITIAL; |
659 | 0 | break; |
660 | 0 | case CURLY_BRACE_LEFT: |
661 | 0 | case CURLY_BRACE_RIGHT: |
662 | 0 | state = STATE_IN_QUOTE; |
663 | 0 | break; |
664 | 0 | default: |
665 | 0 | MAppend(SINGLE_QUOTE); |
666 | 0 | state = STATE_INITIAL; |
667 | 0 | break; |
668 | 0 | } |
669 | 0 | break; |
670 | | |
671 | 0 | case STATE_IN_QUOTE: |
672 | 0 | switch (c) { |
673 | 0 | case SINGLE_QUOTE: |
674 | 0 | state = STATE_INITIAL; |
675 | 0 | break; |
676 | 0 | } |
677 | 0 | break; |
678 | | |
679 | 0 | case STATE_MSG_ELEMENT: |
680 | 0 | switch (c) { |
681 | 0 | case CURLY_BRACE_LEFT: |
682 | 0 | ++braceCount; |
683 | 0 | break; |
684 | 0 | case CURLY_BRACE_RIGHT: |
685 | 0 | if (--braceCount == 0) { |
686 | 0 | state = STATE_INITIAL; |
687 | 0 | } |
688 | 0 | break; |
689 | 0 | } |
690 | 0 | break; |
691 | | |
692 | 0 | default: // Never happens. |
693 | 0 | break; |
694 | 0 | } |
695 | | |
696 | 0 | U_ASSERT(len >= 0); |
697 | 0 | MAppend(c); |
698 | 0 | } |
699 | | |
700 | | // End of scan |
701 | 0 | if (state == STATE_SINGLE_QUOTE || state == STATE_IN_QUOTE) { |
702 | 0 | MAppend(SINGLE_QUOTE); |
703 | 0 | } |
704 | |
|
705 | 0 | return u_terminateUChars(dest, destCapacity, len, ec); |
706 | 0 | } |
707 | | |
708 | | #endif /* #if !UCONFIG_NO_FORMATTING */ |