From 47ce583b35bb547bbaa0dd8489519711e2590c62 Mon Sep 17 00:00:00 2001 From: Krzysztof Cwalina Date: Thu, 7 Jun 2018 11:13:57 -0700 Subject: [PATCH] Completed Int32 APIs (#2353) --- .../System/{Stream.cs => MemoryPolyfill.cs} | 0 src/System.Memory.Polyfill/System/Numbers.cs | 54 +++++++++++++++++++ src/System.Memory.Polyfill/System/Parse.cs | 18 ------- tests/System.Memory.Polyfill.Tests/Int32.cs | 30 ++++++++++- 4 files changed, 83 insertions(+), 19 deletions(-) rename src/System.Memory.Polyfill/System/{Stream.cs => MemoryPolyfill.cs} (100%) create mode 100644 src/System.Memory.Polyfill/System/Numbers.cs delete mode 100644 src/System.Memory.Polyfill/System/Parse.cs diff --git a/src/System.Memory.Polyfill/System/Stream.cs b/src/System.Memory.Polyfill/System/MemoryPolyfill.cs similarity index 100% rename from src/System.Memory.Polyfill/System/Stream.cs rename to src/System.Memory.Polyfill/System/MemoryPolyfill.cs diff --git a/src/System.Memory.Polyfill/System/Numbers.cs b/src/System.Memory.Polyfill/System/Numbers.cs new file mode 100644 index 00000000000..582429b73c3 --- /dev/null +++ b/src/System.Memory.Polyfill/System/Numbers.cs @@ -0,0 +1,54 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Globalization; + +namespace System +{ + public static partial class Int32Polyfill + { + public static bool TryParse(ReadOnlySpan s, out int value) + { +#if NETCOREAPP2_1 + return int.TryParse(s, out value); +#else + return int.TryParse(s.ToString(), out value); +#endif + } + + public static bool TryParse(ReadOnlySpan s, NumberStyles style, IFormatProvider provider, out int value) + { +#if NETCOREAPP2_1 + return int.TryParse(s, style, provider, out value); +#else + return int.TryParse(s.ToString(), style, provider, out value); +#endif + } + + public static int Parse(ReadOnlySpan s, NumberStyles style, IFormatProvider provider = null) + { +#if NETCOREAPP2_1 + return int.Parse(s, style, provider); +#else + return int.Parse(s.ToString(), style, provider); +#endif + } + + public static bool TryFormat(this int value, Span destination, out int charsWritten, ReadOnlySpan format = default, IFormatProvider provider = null) + { +#if NETCOREAPP2_1 + return value.TryFormat(destination, out charsWritten, format, provider); +#else + var str = value.ToString(format.ToString(), provider); + if (str.AsSpan().TryCopyTo(destination)) + { + charsWritten = str.Length; + return true; + } + charsWritten = 0; + return false; +#endif + } + } +} diff --git a/src/System.Memory.Polyfill/System/Parse.cs b/src/System.Memory.Polyfill/System/Parse.cs deleted file mode 100644 index ba210af9cca..00000000000 --- a/src/System.Memory.Polyfill/System/Parse.cs +++ /dev/null @@ -1,18 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -namespace System -{ - public static partial class Int32Polyfill - { - public static bool TryParse(ReadOnlySpan buffer, out int value) - { -#if NETCOREAPP2_1 - return int.TryParse(buffer, out value); -#else - return int.TryParse(buffer.ToString(), out value); -#endif - } - } -} diff --git a/tests/System.Memory.Polyfill.Tests/Int32.cs b/tests/System.Memory.Polyfill.Tests/Int32.cs index 81336866031..ac9d59d9b1f 100644 --- a/tests/System.Memory.Polyfill.Tests/Int32.cs +++ b/tests/System.Memory.Polyfill.Tests/Int32.cs @@ -1,7 +1,7 @@ // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -using System.IO; +using System.Globalization; using Xunit; namespace System.Polyfill.Tests @@ -15,5 +15,33 @@ public void Int32TryParse() Assert.True(Int32Polyfill.TryParse(span, out int value)); Assert.Equal(int.MaxValue, value); } + + [Fact] + public void Int32TryParseCulture() + { + var culture = CultureInfo.CreateSpecificCulture("pl-PL"); + var formatted = (" " + int.MaxValue.ToString("N", culture)); + Assert.True(Int32Polyfill.TryParse(formatted.AsSpan(), NumberStyles.AllowLeadingWhite | NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands, culture, out int value)); + Assert.Equal(int.MaxValue, value); + } + + [Fact] + public void Int32ParseCulture() + { + var culture = CultureInfo.CreateSpecificCulture("pl-PL"); + var formatted = (" " + int.MaxValue.ToString("N", culture)); + var value = Int32Polyfill.Parse(formatted.AsSpan(), NumberStyles.AllowLeadingWhite | NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands, culture); + Assert.Equal(int.MaxValue, value); + } + + [Fact] + public void Int32TryFormat() + { + var culture = CultureInfo.CreateSpecificCulture("pl-PL"); + var buffer = new char[100]; + Assert.True(int.MinValue.TryFormat(buffer.AsSpan(), out int written, "N".AsSpan(), culture)); + var result = buffer.AsSpan(0, written).ToString(); + Assert.Equal(int.MinValue.ToString("N", culture), result); + } } }