From 809e86bbf8ed478e7aa7b9de8a3bfc3014289bd5 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Thu, 12 Nov 2015 22:57:06 +0700 Subject: Review of culture-independent String comparison methods --- crypto/src/util/Platform.cs | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) (limited to 'crypto/src/util/Platform.cs') diff --git a/crypto/src/util/Platform.cs b/crypto/src/util/Platform.cs index d4b18f182..361fe7536 100644 --- a/crypto/src/util/Platform.cs +++ b/crypto/src/util/Platform.cs @@ -13,6 +13,8 @@ namespace Org.BouncyCastle.Utilities { internal abstract class Platform { + private static readonly CompareInfo InvariantCompareInfo = CultureInfo.InvariantCulture.CompareInfo; + #if NETCF_1_0 || NETCF_2_0 private static string GetNewLine() { @@ -30,16 +32,12 @@ namespace Org.BouncyCastle.Utilities } #endif - internal static int CompareIgnoreCase(string a, string b) + internal static bool EqualsIgnoreCase(string a, string b) { -#if SILVERLIGHT - return String.Compare(a, b, StringComparison.InvariantCultureIgnoreCase); -#elif SYS_RUNTIME - return String.Compare(a, b, StringComparison.OrdinalIgnoreCase); -#elif PORTABLE - return String.Compare(a, b, CultureInfo.InvariantCulture, CompareOptions.IgnoreCase); +#if PORTABLE + return String.Equals(a, b, StringComparison.OrdinalIgnoreCase); #else - return String.Compare(a, b, true); + return ToUpperInvariant(a) == ToUpperInvariant(b); #endif } @@ -202,5 +200,25 @@ namespace Org.BouncyCastle.Utilities t.Close(); } #endif + + internal static int IndexOf(string source, string value) + { + return InvariantCompareInfo.IndexOf(source, value, CompareOptions.Ordinal); + } + + internal static int LastIndexOf(string source, string value) + { + return InvariantCompareInfo.LastIndexOf(source, value, CompareOptions.Ordinal); + } + + internal static bool StartsWith(string source, string prefix) + { + return InvariantCompareInfo.IsPrefix(source, prefix, CompareOptions.Ordinal); + } + + internal static bool EndsWith(string source, string suffix) + { + return InvariantCompareInfo.IsSuffix(source, suffix, CompareOptions.Ordinal); + } } } -- cgit 1.5.1