Title: [131293] trunk/Source/WebCore
Revision
131293
Author
tk...@chromium.org
Date
2012-10-15 03:00:43 -0700 (Mon, 15 Oct 2012)

Log Message

Improve code of LocaleMac.mm
https://bugs.webkit.org/show_bug.cgi?id=98383

Reviewed by Darin Adler.

- Use isNull instead of isEmpty because isNull is more efficient.
- create*Formatter should return RetainPtr to avoid to retain in each of callsites.
- Explicit String conversion is not needed.

No new tests. This doesn't change any behavior.

* platform/text/mac/LocaleMac.h:
(LocaleMac):
- Change the return types of createShortDateFormatter,
createTimeFormatter, and createShortTimeFormatter from NSDateFormatter*
to RetainPtr<NSDateFormatter>.
- Omit 'create' from their names.
- Add m_gregorianCalendar.
* platform/text/mac/LocaleMac.mm:
(WebCore::createDateTimeFormatter):
Change the return type from NSDateFormatter* to RetainPtr<NSDateFormatter>.
Add an NSCalendar argument.
(WebCore::LocaleMac::shortDateFormatter):
Renamed from createShortTimeFormatter.
Change the return type from NSDateFormatter* to RetainPtr<NSDateFormatter>.
(WebCore::LocaleMac::parseDateTime): Follow the return type change and the renaming.
(WebCore::LocaleMac::formatDateTime): Ditto. Also, remove explicit String construction.
(WebCore::LocaleMac::dateFormatText): Ditto. Also, use isNull.
(WebCore::LocaleMac::monthLabels): Follow the return type change and the renaming.
(WebCore::LocaleMac::weekDayShortLabels): Ditto.
(WebCore::LocaleMac::timeFormatter):
Change the return type from NSDateFormatter* to RetainPtr<NSDateFormatter>.
Renamed from createTimeFormatter.
(WebCore::LocaleMac::shortTimeFormatter): Ditto.
(WebCore::LocaleMac::dateFormat):
Use isNull. Remove explicit String construction. Follow the return type
change and the renaming.
(WebCore::LocaleMac::timeFormat): ditto.
(WebCore::LocaleMac::shortTimeFormat): ditto.
(WebCore::LocaleMac::timeAMPMLabels):
Remove explicit String construction. Follow the return type change.
(WebCore::LocaleMac::initializeLocalizerData):
Remove explicit String construction.

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (131292 => 131293)


--- trunk/Source/WebCore/ChangeLog	2012-10-15 09:50:04 UTC (rev 131292)
+++ trunk/Source/WebCore/ChangeLog	2012-10-15 10:00:43 UTC (rev 131293)
@@ -1,3 +1,49 @@
+2012-10-15  Kent Tamura  <tk...@chromium.org>
+
+        Improve code of LocaleMac.mm
+        https://bugs.webkit.org/show_bug.cgi?id=98383
+
+        Reviewed by Darin Adler.
+
+        - Use isNull instead of isEmpty because isNull is more efficient.
+        - create*Formatter should return RetainPtr to avoid to retain in each of callsites.
+        - Explicit String conversion is not needed.
+
+        No new tests. This doesn't change any behavior.
+
+        * platform/text/mac/LocaleMac.h:
+        (LocaleMac):
+        - Change the return types of createShortDateFormatter,
+        createTimeFormatter, and createShortTimeFormatter from NSDateFormatter*
+        to RetainPtr<NSDateFormatter>.
+        - Omit 'create' from their names.
+        - Add m_gregorianCalendar.
+        * platform/text/mac/LocaleMac.mm:
+        (WebCore::createDateTimeFormatter):
+        Change the return type from NSDateFormatter* to RetainPtr<NSDateFormatter>.
+        Add an NSCalendar argument.
+        (WebCore::LocaleMac::shortDateFormatter):
+        Renamed from createShortTimeFormatter.
+        Change the return type from NSDateFormatter* to RetainPtr<NSDateFormatter>.
+        (WebCore::LocaleMac::parseDateTime): Follow the return type change and the renaming.
+        (WebCore::LocaleMac::formatDateTime): Ditto. Also, remove explicit String construction.
+        (WebCore::LocaleMac::dateFormatText): Ditto. Also, use isNull.
+        (WebCore::LocaleMac::monthLabels): Follow the return type change and the renaming.
+        (WebCore::LocaleMac::weekDayShortLabels): Ditto.
+        (WebCore::LocaleMac::timeFormatter):
+        Change the return type from NSDateFormatter* to RetainPtr<NSDateFormatter>.
+        Renamed from createTimeFormatter.
+        (WebCore::LocaleMac::shortTimeFormatter): Ditto.
+        (WebCore::LocaleMac::dateFormat):
+        Use isNull. Remove explicit String construction. Follow the return type
+        change and the renaming.
+        (WebCore::LocaleMac::timeFormat): ditto.
+        (WebCore::LocaleMac::shortTimeFormat): ditto.
+        (WebCore::LocaleMac::timeAMPMLabels):
+        Remove explicit String construction. Follow the return type change.
+        (WebCore::LocaleMac::initializeLocalizerData):
+        Remove explicit String construction.
+
 2012-10-12  Ilya Tikhonovsky  <loi...@chromium.org>
 
         Web Inspector: NMI remove hand made object builders with automatic builder.

Modified: trunk/Source/WebCore/platform/text/mac/LocaleMac.h (131292 => 131293)


--- trunk/Source/WebCore/platform/text/mac/LocaleMac.h	2012-10-15 09:50:04 UTC (rev 131292)
+++ trunk/Source/WebCore/platform/text/mac/LocaleMac.h	2012-10-15 10:00:43 UTC (rev 131293)
@@ -37,6 +37,7 @@
 #include <wtf/Vector.h>
 #include <wtf/text/WTFString.h>
 
+OBJC_CLASS NSCalendar;
 OBJC_CLASS NSDateFormatter;
 OBJC_CLASS NSLocale;
 
@@ -70,18 +71,19 @@
 
 private:
     explicit LocaleMac(NSLocale*);
-    NSDateFormatter *createShortDateFormatter();
+    RetainPtr<NSDateFormatter> shortDateFormatter();
     virtual void initializeLocalizerData() OVERRIDE;
 
     RetainPtr<NSLocale> m_locale;
+    RetainPtr<NSCalendar> m_gregorianCalendar;
 #if ENABLE(CALENDAR_PICKER)
     String m_localizedDateFormatText;
     Vector<String> m_monthLabels;
     Vector<String> m_weekDayShortLabels;
 #endif
 #if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
-    NSDateFormatter *createTimeFormatter();
-    NSDateFormatter *createShortTimeFormatter();
+    RetainPtr<NSDateFormatter> timeFormatter();
+    RetainPtr<NSDateFormatter> shortTimeFormatter();
 
     String m_dateFormat;
     String m_localizedTimeFormatText;

Modified: trunk/Source/WebCore/platform/text/mac/LocaleMac.mm (131292 => 131293)


--- trunk/Source/WebCore/platform/text/mac/LocaleMac.mm	2012-10-15 09:50:04 UTC (rev 131292)
+++ trunk/Source/WebCore/platform/text/mac/LocaleMac.mm	2012-10-15 10:00:43 UTC (rev 131293)
@@ -70,19 +70,20 @@
     return LocaleMac::create(determineLocale(locale.string()));
 }
 
-static NSDateFormatter* createDateTimeFormatter(NSLocale* locale, NSDateFormatterStyle dateStyle, NSDateFormatterStyle timeStyle)
+static RetainPtr<NSDateFormatter> createDateTimeFormatter(NSLocale* locale, NSCalendar* calendar, NSDateFormatterStyle dateStyle, NSDateFormatterStyle timeStyle)
 {
     NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
     [formatter setLocale:locale];
     [formatter setDateStyle:dateStyle];
     [formatter setTimeStyle:timeStyle];
     [formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
-    [formatter setCalendar:[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]];
-    return formatter;
+    [formatter setCalendar:calendar];
+    return adoptNS(formatter);
 }
 
 LocaleMac::LocaleMac(NSLocale* locale)
     : m_locale(locale)
+    , m_gregorianCalendar(AdoptNS, [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar])
     , m_didInitializeNumberData(false)
 {
     NSArray* availableLanguages = [NSLocale ISOLanguageCodes];
@@ -90,6 +91,7 @@
     NSString* language = [m_locale.get() objectForKey:NSLocaleLanguageCode];
     if ([availableLanguages indexOfObject:language] == NSNotFound)
         m_locale = [[NSLocale alloc] initWithLocaleIdentifier:defaultLanguage()];
+    [m_gregorianCalendar.get() setLocale:m_locale.get()];
 }
 
 LocaleMac::~LocaleMac()
@@ -112,17 +114,16 @@
     return currentLocale;
 }
 
-NSDateFormatter* LocaleMac::createShortDateFormatter()
+RetainPtr<NSDateFormatter> LocaleMac::shortDateFormatter()
 {
-    return createDateTimeFormatter(m_locale.get(), NSDateFormatterShortStyle, NSDateFormatterNoStyle);
+    return createDateTimeFormatter(m_locale.get(), m_gregorianCalendar.get(), NSDateFormatterShortStyle, NSDateFormatterNoStyle);
 }
 
 double LocaleMac::parseDateTime(const String& input, DateComponents::Type type)
 {
     if (type != DateComponents::Date)
         return std::numeric_limits<double>::quiet_NaN();
-    RetainPtr<NSDateFormatter> formatter(AdoptNS, createShortDateFormatter());
-    NSDate *date = [formatter.get() dateFromString:input];
+    NSDate *date = [shortDateFormatter().get() dateFromString:input];
     if (!date)
         return std::numeric_limits<double>::quiet_NaN();
     return [date timeIntervalSince1970] * msPerSecond;
@@ -132,9 +133,8 @@
 {
     if (dateComponents.type() != DateComponents::Date)
         return Localizer::formatDateTime(dateComponents, formatType);
-    RetainPtr<NSDateFormatter> formatter(AdoptNS, createShortDateFormatter());
     NSTimeInterval interval = dateComponents.millisecondsSinceEpoch() / msPerSecond;
-    return String([formatter.get() stringFromDate:[NSDate dateWithTimeIntervalSince1970:interval]]);
+    return [shortDateFormatter().get() stringFromDate:[NSDate dateWithTimeIntervalSince1970:interval]];
 }
 
 #if ENABLE(CALENDAR_PICKER)
@@ -187,10 +187,9 @@
 
 String LocaleMac::dateFormatText()
 {
-    if (!m_localizedDateFormatText.isEmpty())
+    if (!m_localizedDateFormatText.isNull())
         return m_localizedDateFormatText;
-    RetainPtr<NSDateFormatter> formatter(AdoptNS, createShortDateFormatter());
-    m_localizedDateFormatText = localizeDateFormat(String([formatter.get() dateFormat]));
+    m_localizedDateFormatText = localizeDateFormat([shortDateFormatter().get() dateFormat]);
     return  m_localizedDateFormatText;
 }
 
@@ -199,8 +198,7 @@
     if (!m_monthLabels.isEmpty())
         return m_monthLabels;
     m_monthLabels.reserveCapacity(12);
-    RetainPtr<NSDateFormatter> formatter(AdoptNS, createShortDateFormatter());
-    NSArray *array = [formatter.get() monthSymbols];
+    NSArray *array = [shortDateFormatter().get() monthSymbols];
     if ([array count] == 12) {
         for (unsigned i = 0; i < 12; ++i)
             m_monthLabels.append(String([array objectAtIndex:i]));
@@ -216,8 +214,7 @@
     if (!m_weekDayShortLabels.isEmpty())
         return m_weekDayShortLabels;
     m_weekDayShortLabels.reserveCapacity(7);
-    RetainPtr<NSDateFormatter> formatter(AdoptNS, createShortDateFormatter());
-    NSArray *array = [formatter.get() shortWeekdaySymbols];
+    NSArray *array = [shortDateFormatter().get() shortWeekdaySymbols];
     if ([array count] == 7) {
         for (unsigned i = 0; i < 7; ++i)
             m_weekDayShortLabels.append(String([array objectAtIndex:i]));
@@ -232,12 +229,10 @@
 
 unsigned LocaleMac::firstDayOfWeek()
 {
-    RetainPtr<NSCalendar> calendar(AdoptNS, [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]);
-    [calendar.get() setLocale:m_locale.get()];
     // The document for NSCalendar - firstWeekday doesn't have an explanation of
     // firstWeekday value. We can guess it by the document of NSDateComponents -
     // weekDay, so it can be 1 through 7 and 1 is Sunday.
-    return [calendar.get() firstWeekday] - 1;
+    return [m_gregorianCalendar.get() firstWeekday] - 1;
 }
 
 bool LocaleMac::isRTL()
@@ -247,40 +242,37 @@
 #endif
 
 #if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
-NSDateFormatter* LocaleMac::createTimeFormatter()
+RetainPtr<NSDateFormatter> LocaleMac::timeFormatter()
 {
-    return createDateTimeFormatter(m_locale.get(), NSDateFormatterNoStyle, NSDateFormatterMediumStyle);
+    return createDateTimeFormatter(m_locale.get(), m_gregorianCalendar.get(), NSDateFormatterNoStyle, NSDateFormatterMediumStyle);
 }
 
-NSDateFormatter* LocaleMac::createShortTimeFormatter()
+RetainPtr<NSDateFormatter> LocaleMac::shortTimeFormatter()
 {
-    return createDateTimeFormatter(m_locale.get(), NSDateFormatterNoStyle, NSDateFormatterShortStyle);
+    return createDateTimeFormatter(m_locale.get(), m_gregorianCalendar.get(), NSDateFormatterNoStyle, NSDateFormatterShortStyle);
 }
 
 String LocaleMac::dateFormat()
 {
-    if (!m_dateFormat.isEmpty())
+    if (!m_dateFormat.isNull())
         return m_dateFormat;
-    RetainPtr<NSDateFormatter> formatter(AdoptNS, createShortDateFormatter());
-    m_dateFormat = String([formatter.get() dateFormat]);
+    m_dateFormat = [shortDateFormatter().get() dateFormat];
     return m_dateFormat;
 }
 
 String LocaleMac::timeFormat()
 {
-    if (!m_localizedTimeFormatText.isEmpty())
+    if (!m_localizedTimeFormatText.isNull())
         return m_localizedTimeFormatText;
-    RetainPtr<NSDateFormatter> formatter(AdoptNS, createTimeFormatter());
-    m_localizedTimeFormatText = String([formatter.get() dateFormat]);
+    m_localizedTimeFormatText = [timeFormatter().get() dateFormat];
     return m_localizedTimeFormatText;
 }
 
 String LocaleMac::shortTimeFormat()
 {
-    if (!m_localizedShortTimeFormatText.isEmpty())
+    if (!m_localizedShortTimeFormatText.isNull())
         return m_localizedShortTimeFormatText;
-    RetainPtr<NSDateFormatter> formatter(AdoptNS, createShortTimeFormatter());
-    m_localizedShortTimeFormatText = String([formatter.get() dateFormat]);
+    m_localizedShortTimeFormatText = [shortTimeFormatter().get() dateFormat];
     return m_localizedShortTimeFormatText;
 }
 
@@ -289,9 +281,9 @@
     if (!m_timeAMPMLabels.isEmpty())
         return m_timeAMPMLabels;
     m_timeAMPMLabels.reserveCapacity(2);
-    RetainPtr<NSDateFormatter> formatter(AdoptNS, createShortTimeFormatter());
-    m_timeAMPMLabels.append(String([formatter.get() AMSymbol]));
-    m_timeAMPMLabels.append(String([formatter.get() PMSymbol]));
+    RetainPtr<NSDateFormatter> formatter = shortTimeFormatter();
+    m_timeAMPMLabels.append([formatter.get() AMSymbol]);
+    m_timeAMPMLabels.append([formatter.get() PMSymbol]);
     return m_timeAMPMLabels;
 }
 #endif
@@ -315,9 +307,9 @@
     for (unsigned i = 0; i < 10; ++i)
         symbols.append(nineToZero.substring(9 - i, 1));
     ASSERT(symbols.size() == DecimalSeparatorIndex);
-    symbols.append(String([formatter.get() decimalSeparator]));
+    symbols.append([formatter.get() decimalSeparator]);
     ASSERT(symbols.size() == GroupSeparatorIndex);
-    symbols.append(String([formatter.get() groupingSeparator]));
+    symbols.append([formatter.get() groupingSeparator]);
     ASSERT(symbols.size() == DecimalSymbolsSize);
 
     String positivePrefix([formatter.get() positivePrefix]);
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
http://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to