diff options
author | Marek Safar <marek.safar@gmail.com> | 2012-02-09 14:55:58 +0400 |
---|---|---|
committer | Marek Safar <marek.safar@gmail.com> | 2012-02-09 14:55:58 +0400 |
commit | 0e72f48e8155abb012e5429fbf879f3720f863e6 (patch) | |
tree | 5dafad26793e11cd773c06c61a3d3b946f2fca08 /mcs/class | |
parent | 79f12832b4461622a7c92497e7d14bd0dfcb87af (diff) |
Fixes cloning of DateTimeFormatInfo, added more arguments checks. Fixes #3279
Diffstat (limited to 'mcs/class')
-rw-r--r-- | mcs/class/corlib/System.Globalization/DateTimeFormatInfo.cs | 163 | ||||
-rw-r--r-- | mcs/class/corlib/Test/System.Globalization/DateTimeFormatInfoTest.cs | 16 |
2 files changed, 108 insertions, 71 deletions
diff --git a/mcs/class/corlib/System.Globalization/DateTimeFormatInfo.cs b/mcs/class/corlib/System.Globalization/DateTimeFormatInfo.cs index 695510f4547..6ad391d1115 100644 --- a/mcs/class/corlib/System.Globalization/DateTimeFormatInfo.cs +++ b/mcs/class/corlib/System.Globalization/DateTimeFormatInfo.cs @@ -1,14 +1,13 @@ -// System.Globalization.DateTimeFormatInfo // -// Some useful functions are missing in the ECMA specs. -// They have been added following MS SDK Beta2 +// System.Globalization.DateTimeFormatInfo.cs // -// Martin Weindel (martin.weindel@t-online.de) +// Authors: +// Martin Weindel (martin.weindel@t-online.de) +// Marek Safar (marek.safar@gmail.com) // // (C) Martin Weindel (martin.weindel@t-online.de) - -// // Copyright (C) 2004 Novell, Inc (http://www.novell.com) +// Copyright (C) 2012 Xamarin Inc (http://www.xamarin.com) // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -49,10 +48,9 @@ namespace System.Globalization [Serializable] [ComVisible (true)] [StructLayout (LayoutKind.Sequential)] - public sealed class DateTimeFormatInfo : ICloneable, IFormatProvider { + public sealed class DateTimeFormatInfo : ICloneable, IFormatProvider + { private static readonly string MSG_READONLY = "This instance is read only"; - private static readonly string MSG_ARRAYSIZE_MONTH = "An array with exactly 13 elements is needed"; - private static readonly string MSG_ARRAYSIZE_DAY = "An array with exactly 7 elements is needed"; private static readonly string[] INVARIANT_ABBREVIATED_DAY_NAMES = new string[7] { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; private static readonly string[] INVARIANT_DAY_NAMES @@ -266,11 +264,8 @@ namespace System.Globalization { return abbreviatedDayNames; } - set - { - if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY); - if (value == null) throw new ArgumentNullException(); - if (value.GetLength(0) != 7) throw new ArgumentException(MSG_ARRAYSIZE_DAY); + set { + CheckDaysValue (value); abbreviatedDayNames = (string[]) value.Clone(); } } @@ -287,11 +282,8 @@ namespace System.Globalization { return abbreviatedMonthNames; } - set - { - if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY); - if (value == null) throw new ArgumentNullException(); - if (value.GetLength(0) != 13) throw new ArgumentException(MSG_ARRAYSIZE_MONTH); + set { + CheckMonthsValue (value); abbreviatedMonthNames = (string[]) value.Clone(); } } @@ -308,11 +300,8 @@ namespace System.Globalization { return dayNames; } - set - { - if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY); - if (value == null) throw new ArgumentNullException(); - if (value.GetLength(0) != 7) throw new ArgumentException(MSG_ARRAYSIZE_DAY); + set { + CheckDaysValue (value); dayNames = (string[]) value.Clone(); } } @@ -329,14 +318,55 @@ namespace System.Globalization { return monthNames; } - set - { - if (IsReadOnly) throw new InvalidOperationException(MSG_READONLY); - if (value == null) throw new ArgumentNullException(); - if (value.GetLength(0) != 13) throw new ArgumentException(MSG_ARRAYSIZE_MONTH); + set { + CheckMonthsValue (value); monthNames = (string[]) value.Clone(); } } + + [MonoLimitation ("Returns only the English month abbreviated names")] + [ComVisible (false)] + public string[] AbbreviatedMonthGenitiveNames { + get { + return (string[]) m_genitiveAbbreviatedMonthNames.Clone (); + } + set { + CheckMonthsValue (value); + m_genitiveAbbreviatedMonthNames = value; + } + } + + [MonoLimitation ("Returns only the English month names")] + [ComVisible (false)] + public string[] MonthGenitiveNames { + get { + return (string[]) genitiveMonthNames.Clone (); + } + set { + CheckMonthsValue (value); + genitiveMonthNames = value; + } + } + + [MonoLimitation ("Returns an empty string as if the calendar name wasn't available")] + [ComVisible (false)] + public string NativeCalendarName { + get { + return String.Empty; + } + } + + [ComVisible (false)] + public string[] ShortestDayNames { + get { + return (string[]) shortDayNames.Clone (); + } + + set { + CheckDaysValue (value); + shortDayNames = value; + } + } public string AMDesignator { @@ -751,7 +781,7 @@ namespace System.Globalization yearMonthPatterns = new string [] {"yyyy MMMM"}; } - private string [] PopulateCombinedList (string [] dates, string [] times) + static string [] PopulateCombinedList (string [] dates, string [] times) { if (dates != null && times != null) { string [] list = new string [dates.Length * times.Length]; @@ -764,47 +794,6 @@ namespace System.Globalization return null; } - [MonoLimitation ("Returns only the English month abbreviated names")] - [ComVisible (false)] - public string [] AbbreviatedMonthGenitiveNames { - get { return m_genitiveAbbreviatedMonthNames; } - set { m_genitiveAbbreviatedMonthNames = value; } - } - - [MonoLimitation ("Returns only the English month names")] - [ComVisible (false)] - public string [] MonthGenitiveNames { - get { return genitiveMonthNames; } - set { genitiveMonthNames = value; } - } - - [MonoLimitation ("Returns an empty string as if the calendar name wasn't available")] - [ComVisible (false)] - public string NativeCalendarName { - get { return String.Empty; } - } - - [ComVisible (false)] - public string [] ShortestDayNames { - get { - return shortDayNames; - } - - set { - if (value == null) - throw new ArgumentNullException (); - - if (value.Length != 7) - throw new ArgumentException ("Array must have 7 entries"); - - for (int i = 0; i < 7; i++) - if (value [i] == null) - throw new ArgumentNullException (String.Format ("Element {0} is null", i)); - - shortDayNames = value; - } - } - [ComVisible (false)] public string GetShortestDayName (DayOfWeek dayOfWeek) { @@ -853,5 +842,37 @@ namespace System.Globalization throw new ArgumentException ("format", "Format specifier is invalid"); } } + + void CheckDaysValue (string[] value) + { + if (IsReadOnly) + throw new InvalidOperationException (MSG_READONLY); + + if (value == null) + throw new ArgumentNullException (); + + if (value.Length != 7) + throw new ArgumentException ("An array with exactly 7 elements is required"); + + int ni = Array.IndexOf (value, null); + if (ni >= 0) + throw new ArgumentNullException (string.Format ("Element at index {0} is null", ni)); + } + + void CheckMonthsValue (string[] value) + { + if (IsReadOnly) + throw new InvalidOperationException (MSG_READONLY); + + if (value == null) + throw new ArgumentNullException (); + + if (value.Length != 13) + throw new ArgumentException ("An array with exactly 13 elements is required"); + + int ni = Array.IndexOf (value, null); + if (ni >= 0) + throw new ArgumentNullException (string.Format ("Element at index {0} is null", ni)); + } } } diff --git a/mcs/class/corlib/Test/System.Globalization/DateTimeFormatInfoTest.cs b/mcs/class/corlib/Test/System.Globalization/DateTimeFormatInfoTest.cs index 21bfa319d71..1258f15c9a9 100644 --- a/mcs/class/corlib/Test/System.Globalization/DateTimeFormatInfoTest.cs +++ b/mcs/class/corlib/Test/System.Globalization/DateTimeFormatInfoTest.cs @@ -5,6 +5,7 @@ // Ben Maurer <bmaurer@andrew.cmu.edu> // // Copyright (C) 2005 Novell (http://www.novell.com) +// Copyright (C) 2012 Xamarin Inc (http://www.xamarin.com) // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -100,6 +101,21 @@ namespace MonoTests.System.Globalization Assert.AreEqual ("dd MMMM", di.MonthDayPattern, "#5"); } + [Test] + public void Clone () + { + DateTimeFormatInfo dfi = (DateTimeFormatInfo) DateTimeFormatInfo.InvariantInfo.Clone (); + dfi.MonthNames[0] = "foo"; + dfi.AbbreviatedDayNames[0] = "b1"; + dfi.AbbreviatedMonthGenitiveNames[0] = "b2"; + + + Assert.IsFalse (dfi.IsReadOnly, "#0"); + Assert.AreEqual ("January", DateTimeFormatInfo.InvariantInfo.MonthNames [0], "#1"); + Assert.AreEqual ("Sun", DateTimeFormatInfo.InvariantInfo.AbbreviatedDayNames[0], "#2"); + Assert.AreEqual ("Jan", DateTimeFormatInfo.InvariantInfo.AbbreviatedMonthGenitiveNames[0], "#3"); + } + #if !TARGET_JVM [Test] public void Bug78569 () |