diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2015-11-12 22:57:06 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2015-11-12 22:57:06 +0700 |
commit | 809e86bbf8ed478e7aa7b9de8a3bfc3014289bd5 (patch) | |
tree | 9287ae725992c5972bed155ec4f4773fdace7b22 /crypto/src/util | |
parent | Refactoring of "unused bits" changes (diff) | |
download | BouncyCastle.NET-ed25519-809e86bbf8ed478e7aa7b9de8a3bfc3014289bd5.tar.xz |
Review of culture-independent String comparison methods
Diffstat (limited to 'crypto/src/util')
-rw-r--r-- | crypto/src/util/Platform.cs | 34 | ||||
-rw-r--r-- | crypto/src/util/io/pem/PemReader.cs | 10 | ||||
-rw-r--r-- | crypto/src/util/net/IPAddress.cs | 4 |
3 files changed, 34 insertions, 14 deletions
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); + } } } diff --git a/crypto/src/util/io/pem/PemReader.cs b/crypto/src/util/io/pem/PemReader.cs index b3284705d..bf712b6de 100644 --- a/crypto/src/util/io/pem/PemReader.cs +++ b/crypto/src/util/io/pem/PemReader.cs @@ -35,7 +35,7 @@ namespace Org.BouncyCastle.Utilities.IO.Pem { string line = reader.ReadLine(); - if (line != null && line.StartsWith(BeginString)) + if (line != null && Platform.StartsWith(line, BeginString)) { line = line.Substring(BeginString.Length); int index = line.IndexOf('-'); @@ -56,7 +56,7 @@ namespace Org.BouncyCastle.Utilities.IO.Pem string line; while ((line = reader.ReadLine()) != null - && line.IndexOf(endMarker) == -1) + && Platform.IndexOf(line, endMarker) == -1) { int colonPos = line.IndexOf(':'); @@ -69,8 +69,10 @@ namespace Org.BouncyCastle.Utilities.IO.Pem // Process field string fieldName = line.Substring(0, colonPos).Trim(); - if (fieldName.StartsWith("X-")) - fieldName = fieldName.Substring(2); + if (Platform.StartsWith(fieldName, "X-")) + { + fieldName = fieldName.Substring(2); + } string fieldValue = line.Substring(colonPos + 1).Trim(); diff --git a/crypto/src/util/net/IPAddress.cs b/crypto/src/util/net/IPAddress.cs index 2a30a15f0..38c124590 100644 --- a/crypto/src/util/net/IPAddress.cs +++ b/crypto/src/util/net/IPAddress.cs @@ -85,7 +85,7 @@ namespace Org.BouncyCastle.Utilities.Net public static bool IsValidIPv4WithNetmask( string address) { - int index = address.IndexOf("/"); + int index = address.IndexOf('/'); string mask = address.Substring(index + 1); return (index > 0) && IsValidIPv4(address.Substring(0, index)) @@ -95,7 +95,7 @@ namespace Org.BouncyCastle.Utilities.Net public static bool IsValidIPv6WithNetmask( string address) { - int index = address.IndexOf("/"); + int index = address.IndexOf('/'); string mask = address.Substring(index + 1); return (index > 0) && (IsValidIPv6(address.Substring(0, index)) |