Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Commit

Permalink
Completed Int32 APIs (#2353)
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysztofCwalina authored Jun 7, 2018
1 parent f801662 commit 47ce583
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 19 deletions.
54 changes: 54 additions & 0 deletions src/System.Memory.Polyfill/System/Numbers.cs
Original file line number Diff line number Diff line change
@@ -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<char> 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<char> 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<char> 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<char> destination, out int charsWritten, ReadOnlySpan<char> 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
}
}
}
18 changes: 0 additions & 18 deletions src/System.Memory.Polyfill/System/Parse.cs

This file was deleted.

30 changes: 29 additions & 1 deletion tests/System.Memory.Polyfill.Tests/Int32.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
}
}
}

0 comments on commit 47ce583

Please sign in to comment.