-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Update BigInteger and Complex to support UTF8 parsing and formatting #117745
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
tannergooding
wants to merge
9
commits into
dotnet:main
Choose a base branch
from
tannergooding:fix-81500
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e6c5f14
Update BigInteger and Complex to support UTF8 parsing and formatting
tannergooding 3655a67
Fix test ambiguity
tannergooding 0801b52
Add tests for BigInteger/Complex utf-8 parsing and formatting
tannergooding 589fc71
Ensure we can get a UTF-8 result for various number info queries
tannergooding 9e932c4
Merge remote-tracking branch 'dotnet/main' into fix-81500
tannergooding dd11409
Prefer Unsafe.BitCast over MemoryMarshal.Cast
tannergooding 88901be
Merge remote-tracking branch 'dotnet/main' into fix-81500
tannergooding 738bf05
Remove now unnecessary polyfills
tannergooding a7a1487
Merge branch 'main' into fix-81500
tannergooding File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
314 changes: 314 additions & 0 deletions
314
src/libraries/Common/src/System/Text/ValueStringBuilder_1.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,314 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Buffers; | ||
using System.Diagnostics; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
#nullable enable | ||
|
||
namespace System.Text | ||
{ | ||
internal ref partial struct ValueStringBuilder<TChar> | ||
where TChar : unmanaged | ||
{ | ||
private TChar[]? _arrayToReturnToPool; | ||
private Span<TChar> _chars; | ||
private int _pos; | ||
|
||
public ValueStringBuilder(Span<TChar> initialBuffer) | ||
{ | ||
Debug.Assert((typeof(TChar) == typeof(Utf8Char)) || (typeof(TChar) == typeof(Utf16Char))); | ||
|
||
_arrayToReturnToPool = null; | ||
_chars = initialBuffer; | ||
_pos = 0; | ||
} | ||
|
||
public ValueStringBuilder(int initialCapacity) | ||
{ | ||
Debug.Assert((typeof(TChar) == typeof(Utf8Char)) || (typeof(TChar) == typeof(Utf16Char))); | ||
|
||
_arrayToReturnToPool = ArrayPool<TChar>.Shared.Rent(initialCapacity); | ||
_chars = _arrayToReturnToPool; | ||
_pos = 0; | ||
} | ||
|
||
public int Length | ||
{ | ||
readonly get => _pos; | ||
set | ||
{ | ||
Debug.Assert(value >= 0); | ||
Debug.Assert(value <= _chars.Length); | ||
_pos = value; | ||
} | ||
} | ||
|
||
public readonly int Capacity => _chars.Length; | ||
|
||
public void EnsureCapacity(int capacity) | ||
{ | ||
// This is not expected to be called this with negative capacity | ||
Debug.Assert(capacity >= 0); | ||
|
||
// If the caller has a bug and calls this with negative capacity, make sure to call Grow to throw an exception. | ||
if ((uint)capacity > (uint)_chars.Length) | ||
Grow(capacity - _pos); | ||
} | ||
|
||
/// <summary> | ||
/// Get a pinnable reference to the builder. | ||
/// Does not ensure there is a null TChar after <see cref="Length"/> | ||
/// This overload is pattern matched in the C# 7.3+ compiler so you can omit | ||
/// the explicit method call, and write eg "fixed (TChar* c = builder)" | ||
/// </summary> | ||
public readonly ref TChar GetPinnableReference() | ||
{ | ||
return ref MemoryMarshal.GetReference(_chars); | ||
} | ||
|
||
/// <summary> | ||
/// Get a pinnable reference to the builder. | ||
/// </summary> | ||
/// <param name="terminate">Ensures that the builder has a null TChar after <see cref="Length"/></param> | ||
public ref TChar GetPinnableReference(bool terminate) | ||
{ | ||
if (terminate) | ||
{ | ||
EnsureCapacity(Length + 1); | ||
_chars[Length] = default; | ||
} | ||
return ref MemoryMarshal.GetReference(_chars); | ||
} | ||
|
||
public readonly ref TChar this[int index] | ||
{ | ||
get | ||
{ | ||
Debug.Assert(index < _pos); | ||
return ref _chars[index]; | ||
} | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
string result; | ||
Span<TChar> slice = _chars.Slice(0, _pos); | ||
|
||
if (typeof(TChar) == typeof(Utf8Char)) | ||
{ | ||
result = Encoding.UTF8.GetString(Unsafe.BitCast<ReadOnlySpan<TChar>, ReadOnlySpan<byte>>(slice)); | ||
} | ||
else | ||
{ | ||
Debug.Assert(typeof(TChar) == typeof(Utf16Char)); | ||
result = Unsafe.BitCast<ReadOnlySpan<TChar>, ReadOnlySpan<char>>(slice).ToString(); | ||
} | ||
|
||
Dispose(); | ||
return result; | ||
} | ||
|
||
/// <summary>Returns the underlying storage of the builder.</summary> | ||
public readonly Span<TChar> RawChars => _chars; | ||
|
||
/// <summary> | ||
/// Returns a span around the contents of the builder. | ||
/// </summary> | ||
/// <param name="terminate">Ensures that the builder has a null TChar after <see cref="Length"/></param> | ||
public ReadOnlySpan<TChar> AsSpan(bool terminate) | ||
{ | ||
if (terminate) | ||
{ | ||
EnsureCapacity(Length + 1); | ||
_chars[Length] = default; | ||
} | ||
return _chars.Slice(0, _pos); | ||
} | ||
|
||
public readonly ReadOnlySpan<TChar> AsSpan() => _chars.Slice(0, _pos); | ||
public readonly ReadOnlySpan<TChar> AsSpan(int start) => _chars.Slice(start, _pos - start); | ||
public readonly ReadOnlySpan<TChar> AsSpan(int start, int length) => _chars.Slice(start, length); | ||
|
||
public bool TryCopyTo(Span<TChar> destination, out int charsWritten) | ||
{ | ||
if (_chars.Slice(0, _pos).TryCopyTo(destination)) | ||
{ | ||
charsWritten = _pos; | ||
Dispose(); | ||
return true; | ||
} | ||
else | ||
{ | ||
charsWritten = 0; | ||
Dispose(); | ||
return false; | ||
} | ||
} | ||
|
||
public void Insert(int index, TChar value, int count) | ||
{ | ||
if (_pos > _chars.Length - count) | ||
{ | ||
Grow(count); | ||
} | ||
|
||
int remaining = _pos - index; | ||
_chars.Slice(index, remaining).CopyTo(_chars.Slice(index + count)); | ||
_chars.Slice(index, count).Fill(value); | ||
_pos += count; | ||
} | ||
|
||
public void Insert(int index, ReadOnlySpan<TChar> text) | ||
{ | ||
if (text.IsEmpty) | ||
{ | ||
return; | ||
} | ||
|
||
int count = text.Length; | ||
|
||
if (_pos > (_chars.Length - count)) | ||
{ | ||
Grow(count); | ||
} | ||
|
||
int remaining = _pos - index; | ||
_chars.Slice(index, remaining).CopyTo(_chars.Slice(index + count)); | ||
text.CopyTo(_chars.Slice(index)); | ||
_pos += count; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public void Append(TChar c) | ||
{ | ||
int pos = _pos; | ||
Span<TChar> chars = _chars; | ||
if ((uint)pos < (uint)chars.Length) | ||
{ | ||
chars[pos] = c; | ||
_pos = pos + 1; | ||
} | ||
else | ||
{ | ||
GrowAndAppend(c); | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public void Append(ReadOnlySpan<TChar> text) | ||
{ | ||
if (text.IsEmpty) | ||
{ | ||
return; | ||
} | ||
|
||
int pos = _pos; | ||
if (text.Length == 1 && (uint)pos < (uint)_chars.Length) // very common case, e.g. appending strings from NumberFormatInfo like separators, percent symbols, etc. | ||
{ | ||
_chars[pos] = text[0]; | ||
_pos = pos + 1; | ||
} | ||
else | ||
{ | ||
AppendSlow(text); | ||
} | ||
} | ||
|
||
private void AppendSlow(ReadOnlySpan<TChar> text) | ||
{ | ||
int pos = _pos; | ||
if (pos > _chars.Length - text.Length) | ||
{ | ||
Grow(text.Length); | ||
} | ||
|
||
text.CopyTo(_chars.Slice(pos)); | ||
_pos += text.Length; | ||
} | ||
|
||
public void Append(TChar c, int count) | ||
{ | ||
if (_pos > _chars.Length - count) | ||
{ | ||
Grow(count); | ||
} | ||
|
||
Span<TChar> dst = _chars.Slice(_pos, count); | ||
for (int i = 0; i < dst.Length; i++) | ||
{ | ||
dst[i] = c; | ||
} | ||
_pos += count; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public Span<TChar> AppendSpan(int length) | ||
{ | ||
int origPos = _pos; | ||
if (origPos > _chars.Length - length) | ||
{ | ||
Grow(length); | ||
} | ||
|
||
_pos = origPos + length; | ||
return _chars.Slice(origPos, length); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private void GrowAndAppend(TChar c) | ||
{ | ||
Grow(1); | ||
Append(c); | ||
} | ||
|
||
/// <summary> | ||
/// Resize the internal buffer either by doubling current buffer size or | ||
/// by adding <paramref name="additionalCapacityBeyondPos"/> to | ||
/// <see cref="_pos"/> whichever is greater. | ||
/// </summary> | ||
/// <param name="additionalCapacityBeyondPos"> | ||
/// Number of chars requested beyond current position. | ||
/// </param> | ||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private void Grow(int additionalCapacityBeyondPos) | ||
{ | ||
Debug.Assert(additionalCapacityBeyondPos > 0); | ||
Debug.Assert(_pos > _chars.Length - additionalCapacityBeyondPos, "Grow called incorrectly, no resize is needed."); | ||
|
||
const uint ArrayMaxLength = 0x7FFFFFC7; // same as Array.MaxLength | ||
|
||
// Increase to at least the required size (_pos + additionalCapacityBeyondPos), but try | ||
// to double the size if possible, bounding the doubling to not go beyond the max array length. | ||
int newCapacity = (int)Math.Max( | ||
(uint)(_pos + additionalCapacityBeyondPos), | ||
Math.Min((uint)_chars.Length * 2, ArrayMaxLength)); | ||
|
||
// Make sure to let Rent throw an exception if the caller has a bug and the desired capacity is negative. | ||
// This could also go negative if the actual required length wraps around. | ||
TChar[] poolArray = ArrayPool<TChar>.Shared.Rent(newCapacity); | ||
|
||
_chars.Slice(0, _pos).CopyTo(poolArray); | ||
|
||
TChar[]? toReturn = _arrayToReturnToPool; | ||
_chars = _arrayToReturnToPool = poolArray; | ||
if (toReturn != null) | ||
{ | ||
ArrayPool<TChar>.Shared.Return(toReturn); | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public void Dispose() | ||
{ | ||
TChar[]? toReturn = _arrayToReturnToPool; | ||
this = default; // for safety, to avoid using pooled array if this instance is erroneously appended to again | ||
if (toReturn != null) | ||
{ | ||
ArrayPool<TChar>.Shared.Return(toReturn); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is specific to these types that are defined just in System.Runtime.Numerics, and it's only used by System.Runtime.Numerics, we probably shouldn't have the file in Common?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not really specific to the numeric types and is usable by any other scenario using
ValueStringBuilder
that needs to support UTF-8 as well. So I put it in the place that people can find it if they're making similar changes to other paths in the future.More ideally we wouldn't need the
Utf8Char
shim in System.Runtime.Numerics and we could just usebyte
/char
directly. However, we currently need the directCastFrom
/CastTo
helpers to ensure the right codegen happens. I think we could probably fix this by special casing CreateFromTruncating in the JIT for the primitive types.