| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2011,2012 Google Inc. All rights reserved. | 2 * Copyright (C) 2011,2012 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 #include "config.h" | 31 #include "config.h" |
| 32 #include "core/platform/text/LocaleICU.h" | 32 #include "core/platform/text/LocaleICU.h" |
| 33 | 33 |
| 34 #include <unicode/udatpg.h> | 34 #include <unicode/udatpg.h> |
| 35 #include <unicode/uloc.h> | 35 #include <unicode/uloc.h> |
| 36 #include <limits> | 36 #include <limits> |
| 37 #include "wtf/DateMath.h" | 37 #include "wtf/DateMath.h" |
| 38 #include "wtf/PassOwnPtr.h" | 38 #include "wtf/PassOwnPtr.h" |
| 39 #include "wtf/text/StringBuffer.h" | |
| 40 #include "wtf/text/StringBuilder.h" | 39 #include "wtf/text/StringBuilder.h" |
| 41 | 40 |
| 42 using namespace icu; | 41 using namespace icu; |
| 43 using namespace std; | 42 using namespace std; |
| 44 | 43 |
| 45 namespace WebCore { | 44 namespace WebCore { |
| 46 | 45 |
| 47 PassOwnPtr<Locale> Locale::create(const AtomicString& locale) | 46 PassOwnPtr<Locale> Locale::create(const AtomicString& locale) |
| 48 { | 47 { |
| 49 return LocaleICU::create(locale.string().utf8().data()); | 48 return LocaleICU::create(locale.string().utf8().data()); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 77 return adoptPtr(new LocaleICU(localeString)); | 76 return adoptPtr(new LocaleICU(localeString)); |
| 78 } | 77 } |
| 79 | 78 |
| 80 String LocaleICU::decimalSymbol(UNumberFormatSymbol symbol) | 79 String LocaleICU::decimalSymbol(UNumberFormatSymbol symbol) |
| 81 { | 80 { |
| 82 UErrorCode status = U_ZERO_ERROR; | 81 UErrorCode status = U_ZERO_ERROR; |
| 83 int32_t bufferLength = unum_getSymbol(m_numberFormat, symbol, 0, 0, &status)
; | 82 int32_t bufferLength = unum_getSymbol(m_numberFormat, symbol, 0, 0, &status)
; |
| 84 ASSERT(U_SUCCESS(status) || status == U_BUFFER_OVERFLOW_ERROR); | 83 ASSERT(U_SUCCESS(status) || status == U_BUFFER_OVERFLOW_ERROR); |
| 85 if (U_FAILURE(status) && status != U_BUFFER_OVERFLOW_ERROR) | 84 if (U_FAILURE(status) && status != U_BUFFER_OVERFLOW_ERROR) |
| 86 return String(); | 85 return String(); |
| 87 StringBuffer<UChar> buffer(bufferLength); | 86 Vector<UChar> buffer(bufferLength); |
| 88 status = U_ZERO_ERROR; | 87 status = U_ZERO_ERROR; |
| 89 unum_getSymbol(m_numberFormat, symbol, buffer.characters(), bufferLength, &s
tatus); | 88 unum_getSymbol(m_numberFormat, symbol, buffer.data(), bufferLength, &status)
; |
| 90 if (U_FAILURE(status)) | 89 if (U_FAILURE(status)) |
| 91 return String(); | 90 return String(); |
| 92 return String::adopt(buffer); | 91 return String::adopt(buffer); |
| 93 } | 92 } |
| 94 | 93 |
| 95 String LocaleICU::decimalTextAttribute(UNumberFormatTextAttribute tag) | 94 String LocaleICU::decimalTextAttribute(UNumberFormatTextAttribute tag) |
| 96 { | 95 { |
| 97 UErrorCode status = U_ZERO_ERROR; | 96 UErrorCode status = U_ZERO_ERROR; |
| 98 int32_t bufferLength = unum_getTextAttribute(m_numberFormat, tag, 0, 0, &sta
tus); | 97 int32_t bufferLength = unum_getTextAttribute(m_numberFormat, tag, 0, 0, &sta
tus); |
| 99 ASSERT(U_SUCCESS(status) || status == U_BUFFER_OVERFLOW_ERROR); | 98 ASSERT(U_SUCCESS(status) || status == U_BUFFER_OVERFLOW_ERROR); |
| 100 if (U_FAILURE(status) && status != U_BUFFER_OVERFLOW_ERROR) | 99 if (U_FAILURE(status) && status != U_BUFFER_OVERFLOW_ERROR) |
| 101 return String(); | 100 return String(); |
| 102 StringBuffer<UChar> buffer(bufferLength); | 101 Vector<UChar> buffer(bufferLength); |
| 103 status = U_ZERO_ERROR; | 102 status = U_ZERO_ERROR; |
| 104 unum_getTextAttribute(m_numberFormat, tag, buffer.characters(), bufferLength
, &status); | 103 unum_getTextAttribute(m_numberFormat, tag, buffer.data(), bufferLength, &sta
tus); |
| 105 ASSERT(U_SUCCESS(status)); | 104 ASSERT(U_SUCCESS(status)); |
| 106 if (U_FAILURE(status)) | 105 if (U_FAILURE(status)) |
| 107 return String(); | 106 return String(); |
| 108 return String::adopt(buffer); | 107 return String::adopt(buffer); |
| 109 } | 108 } |
| 110 | 109 |
| 111 void LocaleICU::initializeLocaleData() | 110 void LocaleICU::initializeLocaleData() |
| 112 { | 111 { |
| 113 if (m_didCreateDecimalFormat) | 112 if (m_didCreateDecimalFormat) |
| 114 return; | 113 return; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 | 152 |
| 154 static String getDateFormatPattern(const UDateFormat* dateFormat) | 153 static String getDateFormatPattern(const UDateFormat* dateFormat) |
| 155 { | 154 { |
| 156 if (!dateFormat) | 155 if (!dateFormat) |
| 157 return emptyString(); | 156 return emptyString(); |
| 158 | 157 |
| 159 UErrorCode status = U_ZERO_ERROR; | 158 UErrorCode status = U_ZERO_ERROR; |
| 160 int32_t length = udat_toPattern(dateFormat, TRUE, 0, 0, &status); | 159 int32_t length = udat_toPattern(dateFormat, TRUE, 0, 0, &status); |
| 161 if (status != U_BUFFER_OVERFLOW_ERROR || !length) | 160 if (status != U_BUFFER_OVERFLOW_ERROR || !length) |
| 162 return emptyString(); | 161 return emptyString(); |
| 163 StringBuffer<UChar> buffer(length); | 162 Vector<UChar> buffer(length); |
| 164 status = U_ZERO_ERROR; | 163 status = U_ZERO_ERROR; |
| 165 udat_toPattern(dateFormat, TRUE, buffer.characters(), length, &status); | 164 udat_toPattern(dateFormat, TRUE, buffer.data(), length, &status); |
| 166 if (U_FAILURE(status)) | 165 if (U_FAILURE(status)) |
| 167 return emptyString(); | 166 return emptyString(); |
| 168 return String::adopt(buffer); | 167 return String::adopt(buffer); |
| 169 } | 168 } |
| 170 | 169 |
| 171 PassOwnPtr<Vector<String> > LocaleICU::createLabelVector(const UDateFormat* date
Format, UDateFormatSymbolType type, int32_t startIndex, int32_t size) | 170 PassOwnPtr<Vector<String> > LocaleICU::createLabelVector(const UDateFormat* date
Format, UDateFormatSymbolType type, int32_t startIndex, int32_t size) |
| 172 { | 171 { |
| 173 if (!dateFormat) | 172 if (!dateFormat) |
| 174 return PassOwnPtr<Vector<String> >(); | 173 return PassOwnPtr<Vector<String> >(); |
| 175 if (udat_countSymbols(dateFormat, type) != startIndex + size) | 174 if (udat_countSymbols(dateFormat, type) != startIndex + size) |
| 176 return PassOwnPtr<Vector<String> >(); | 175 return PassOwnPtr<Vector<String> >(); |
| 177 | 176 |
| 178 OwnPtr<Vector<String> > labels = adoptPtr(new Vector<String>()); | 177 OwnPtr<Vector<String> > labels = adoptPtr(new Vector<String>()); |
| 179 labels->reserveCapacity(size); | 178 labels->reserveCapacity(size); |
| 180 for (int32_t i = 0; i < size; ++i) { | 179 for (int32_t i = 0; i < size; ++i) { |
| 181 UErrorCode status = U_ZERO_ERROR; | 180 UErrorCode status = U_ZERO_ERROR; |
| 182 int32_t length = udat_getSymbols(dateFormat, type, startIndex + i, 0, 0,
&status); | 181 int32_t length = udat_getSymbols(dateFormat, type, startIndex + i, 0, 0,
&status); |
| 183 if (status != U_BUFFER_OVERFLOW_ERROR) | 182 if (status != U_BUFFER_OVERFLOW_ERROR) |
| 184 return PassOwnPtr<Vector<String> >(); | 183 return PassOwnPtr<Vector<String> >(); |
| 185 StringBuffer<UChar> buffer(length); | 184 Vector<UChar> buffer(length); |
| 186 status = U_ZERO_ERROR; | 185 status = U_ZERO_ERROR; |
| 187 udat_getSymbols(dateFormat, type, startIndex + i, buffer.characters(), l
ength, &status); | 186 udat_getSymbols(dateFormat, type, startIndex + i, buffer.data(), length,
&status); |
| 188 if (U_FAILURE(status)) | 187 if (U_FAILURE(status)) |
| 189 return PassOwnPtr<Vector<String> >(); | 188 return PassOwnPtr<Vector<String> >(); |
| 190 labels->append(String::adopt(buffer)); | 189 labels->append(String::adopt(buffer)); |
| 191 } | 190 } |
| 192 return labels.release(); | 191 return labels.release(); |
| 193 } | 192 } |
| 194 | 193 |
| 195 #if ENABLE(CALENDAR_PICKER) | 194 #if ENABLE(CALENDAR_PICKER) |
| 196 static PassOwnPtr<Vector<String> > createFallbackWeekDayShortLabels() | 195 static PassOwnPtr<Vector<String> > createFallbackWeekDayShortLabels() |
| 197 { | 196 { |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 String format = ASCIILiteral("yyyy-MM"); | 320 String format = ASCIILiteral("yyyy-MM"); |
| 322 UErrorCode status = U_ZERO_ERROR; | 321 UErrorCode status = U_ZERO_ERROR; |
| 323 UDateTimePatternGenerator* patternGenerator = udatpg_open(locale, &status); | 322 UDateTimePatternGenerator* patternGenerator = udatpg_open(locale, &status); |
| 324 if (!patternGenerator) | 323 if (!patternGenerator) |
| 325 return format; | 324 return format; |
| 326 status = U_ZERO_ERROR; | 325 status = U_ZERO_ERROR; |
| 327 Vector<UChar> skeletonCharacters; | 326 Vector<UChar> skeletonCharacters; |
| 328 skeleton.appendTo(skeletonCharacters); | 327 skeleton.appendTo(skeletonCharacters); |
| 329 int32_t length = udatpg_getBestPattern(patternGenerator, skeletonCharacters.
data(), skeletonCharacters.size(), 0, 0, &status); | 328 int32_t length = udatpg_getBestPattern(patternGenerator, skeletonCharacters.
data(), skeletonCharacters.size(), 0, 0, &status); |
| 330 if (status == U_BUFFER_OVERFLOW_ERROR && length) { | 329 if (status == U_BUFFER_OVERFLOW_ERROR && length) { |
| 331 StringBuffer<UChar> buffer(length); | 330 Vector<UChar> buffer(length); |
| 332 status = U_ZERO_ERROR; | 331 status = U_ZERO_ERROR; |
| 333 udatpg_getBestPattern(patternGenerator, skeletonCharacters.data(), skele
tonCharacters.size(), buffer.characters(), length, &status); | 332 udatpg_getBestPattern(patternGenerator, skeletonCharacters.data(), skele
tonCharacters.size(), buffer.data(), length, &status); |
| 334 if (U_SUCCESS(status)) | 333 if (U_SUCCESS(status)) |
| 335 format = String::adopt(buffer); | 334 format = String::adopt(buffer); |
| 336 } | 335 } |
| 337 udatpg_close(patternGenerator); | 336 udatpg_close(patternGenerator); |
| 338 return format; | 337 return format; |
| 339 } | 338 } |
| 340 | 339 |
| 341 String LocaleICU::monthFormat() | 340 String LocaleICU::monthFormat() |
| 342 { | 341 { |
| 343 if (!m_monthFormat.isNull()) | 342 if (!m_monthFormat.isNull()) |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 425 } | 424 } |
| 426 | 425 |
| 427 const Vector<String>& LocaleICU::timeAMPMLabels() | 426 const Vector<String>& LocaleICU::timeAMPMLabels() |
| 428 { | 427 { |
| 429 initializeDateTimeFormat(); | 428 initializeDateTimeFormat(); |
| 430 return m_timeAMPMLabels; | 429 return m_timeAMPMLabels; |
| 431 } | 430 } |
| 432 | 431 |
| 433 } // namespace WebCore | 432 } // namespace WebCore |
| 434 | 433 |
| OLD | NEW |