315static inline bool isICUYearSymbol(UChar letter)
316{
317 return letter == 'y' || letter == 'Y';
318}
319
320static inline bool isICUMonthSymbol(UChar letter)
321{
322 return letter == 'M';
323}
324
325static inline bool isICUDayInMonthSymbol(UChar letter)
326{
327 return letter == 'd';
328}
329
330// Specification of the input:
331// http://icu-project.org/apiref/icu4c/classSimpleDateFormat.html#details
332static String localizeFormat(const Vector<UChar>& buffer)
333{
334 StringBuilder builder;
335 UChar lastChar = 0;
336 bool inQuote = false;
337 for (unsigned i = 0; i < buffer.size(); ++i) {
338 if (inQuote) {
339 if (buffer[i] == '\'') {
340 inQuote = false;
341 lastChar = 0;
342 ASSERT(i);
343 if (buffer[i - 1] == '\'')
344 builder.append('\'');
345 } else
346 builder.append(buffer[i]);
347 } else {
348 if (isASCIIAlpha(lastChar) && lastChar == buffer[i])
349 continue;
350 lastChar = buffer[i];
351 if (isICUYearSymbol(lastChar)) {
352 String text = dateFormatYearText();
353 builder.append(text.isEmpty() ? "Year" : text);
354 } else if (isICUMonthSymbol(lastChar)) {
355 String text = dateFormatMonthText();
356 builder.append(text.isEmpty() ? "Month" : text);
357 } else if (isICUDayInMonthSymbol(lastChar)) {
358 String text = dateFormatDayInMonthText();
359 builder.append(text.isEmpty() ? "Day" : text);
360 } else if (lastChar == '\'')
361 inQuote = true;
362 else
363 builder.append(lastChar);
364 }
365 }
366 return builder.toString();
367}
368
369void ICULocale::initializeLocalizedDateFormatText()
370{
371 if (!m_localizedDateFormatText.isNull())
372 return;
373 m_localizedDateFormatText = String("");
374 if (!initializeShortDateFormat())
375 return;
376 UErrorCode status = U_ZERO_ERROR;
377 int32_t length = udat_toPattern(m_shortDateFormat, TRUE, 0, 0, &status);
378 if (status != U_BUFFER_OVERFLOW_ERROR)
379 return;
380 Vector<UChar> buffer(length);
381 status = U_ZERO_ERROR;
382 udat_toPattern(m_shortDateFormat, TRUE, buffer.data(), length, &status);
383 if (U_FAILURE(status))
384 return;
385 m_localizedDateFormatText = localizeFormat(buffer);
386}
387
388String ICULocale::localizedDateFormatText()
389{
390 initializeLocalizedDateFormatText();
391 return m_localizedDateFormatText;
392}
393