Author: bodewig Date: Wed Sep 7 04:26:03 2016 New Revision: 1759541 URL: http://svn.apache.org/viewvc?rev=1759541&view=rev Log: add a null-safe EqualsIgnoringCase method
patch by Jonas Baehr Modified: logging/log4net/trunk/src/Util/PatternStringConverters/NewLinePatternConverter.cs logging/log4net/trunk/src/Util/SystemInfo.cs logging/log4net/trunk/tests/src/Util/SystemInfoTest.cs Modified: logging/log4net/trunk/src/Util/PatternStringConverters/NewLinePatternConverter.cs URL: http://svn.apache.org/viewvc/logging/log4net/trunk/src/Util/PatternStringConverters/NewLinePatternConverter.cs?rev=1759541&r1=1759540&r2=1759541&view=diff ============================================================================== --- logging/log4net/trunk/src/Util/PatternStringConverters/NewLinePatternConverter.cs (original) +++ logging/log4net/trunk/src/Util/PatternStringConverters/NewLinePatternConverter.cs Wed Sep 7 04:26:03 2016 @@ -67,11 +67,11 @@ namespace log4net.Util.PatternStringConv /// </remarks> public void ActivateOptions() { - if (Option.ToUpperInvariant() == "DOS") + if (SystemInfo.EqualsIgnoringCase(Option, "DOS")) { Option = "\r\n"; } - else if (Option.ToUpperInvariant() == "UNIX") + else if (SystemInfo.EqualsIgnoringCase(Option, "UNIX")) { Option = "\n"; } Modified: logging/log4net/trunk/src/Util/SystemInfo.cs URL: http://svn.apache.org/viewvc/logging/log4net/trunk/src/Util/SystemInfo.cs?rev=1759541&r1=1759540&r2=1759541&view=diff ============================================================================== --- logging/log4net/trunk/src/Util/SystemInfo.cs (original) +++ logging/log4net/trunk/src/Util/SystemInfo.cs Wed Sep 7 04:26:03 2016 @@ -18,7 +18,9 @@ #endregion using System; -#if !NETSTANDARD1_3 +#if NETSTANDARD1_3 +using System.Globalization; +#else using System.Configuration; #endif using System.Reflection; @@ -1057,6 +1059,29 @@ namespace log4net.Util #endif } + /// <summary> + /// Tests two strings for equality, the ignoring case. + /// </summary> + /// <remarks> + /// If the platform permits, culture information is ignored completely (ordinal comparison). + /// The aim of this method is to provide a fast comparison that deals with <c>null</c> and ignores different casing. + /// It is not supposed to deal with various, culture-specific habits. + /// Use it to compare against pure ASCII constants, like keywords etc. + /// </remarks> + /// <param name="a">The one string.</param> + /// <param name="b">The other string.</param> + /// <returns><c>true</c> if the strings are equal, <c>false</c> otherwise.</returns> + public static Boolean EqualsIgnoringCase(String a, String b) + { +#if NET_1_0 || NET_1_1 || NETCF_1_0 + return string.Compare(a, b, true, System.Globalization.CultureInfo.InvariantCulture) == 0 +#elif NETSTANDARD1_3 + return CultureInfo.InvariantCulture.CompareInfo.Compare(a, b, CompareOptions.IgnoreCase) == 0; +#else // >= .NET-2.0 + return String.Equals(a, b, StringComparison.OrdinalIgnoreCase); +#endif + } + #endregion Public Static Methods #region Private Static Methods Modified: logging/log4net/trunk/tests/src/Util/SystemInfoTest.cs URL: http://svn.apache.org/viewvc/logging/log4net/trunk/tests/src/Util/SystemInfoTest.cs?rev=1759541&r1=1759540&r2=1759541&view=diff ============================================================================== --- logging/log4net/trunk/tests/src/Util/SystemInfoTest.cs (original) +++ logging/log4net/trunk/tests/src/Util/SystemInfoTest.cs Wed Sep 7 04:26:03 2016 @@ -170,5 +170,41 @@ namespace log4net.Tests.Util return SystemInfo.GetTypeFromString(typeName, throwOnError, ignoreCase); #endif } + + [Test] + public void EqualsIgnoringCase_BothNull_true() + { + Assert.True(SystemInfo.EqualsIgnoringCase(null, null)); + } + + [Test] + public void EqualsIgnoringCase_LeftNull_false() + { + Assert.False(SystemInfo.EqualsIgnoringCase(null, "foo")); + } + + [Test] + public void EqualsIgnoringCase_RightNull_false() + { + Assert.False(SystemInfo.EqualsIgnoringCase("foo", null)); + } + + [Test] + public void EqualsIgnoringCase_SameStringsSameCase_true() + { + Assert.True(SystemInfo.EqualsIgnoringCase("foo", "foo")); + } + + [Test] + public void EqualsIgnoringCase_SameStringsDifferentCase_true() + { + Assert.True(SystemInfo.EqualsIgnoringCase("foo", "FOO")); + } + + [Test] + public void EqualsIgnoringCase_DifferentStrings_false() + { + Assert.False(SystemInfo.EqualsIgnoringCase("foo", "foobar")); + } } }