Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/Polyfill/Polyfill_StringBuilder_CopyTo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Polyfills;

using System;
using System.Buffers;
using System.Text;

static partial class Polyfill
Expand Down Expand Up @@ -37,9 +38,27 @@ public static void CopyTo(
throw new ArgumentException("The destination span is too short to hold the requested characters.", nameof(destination));
}

for (var index = 0; index < count; index++)
if (count == 0)
{
destination[index] = target[sourceIndex + index];
return;
}

// The indexer walks the chunk list on every call, so copy in blocks through the array overload instead.
var buffer = ArrayPool<char>.Shared.Rent(Math.Min(count, 16384));
try
{
var copied = 0;
while (copied < count)
{
var length = Math.Min(count - copied, buffer.Length);
target.CopyTo(sourceIndex + copied, buffer, 0, length);
buffer.AsSpan(0, length).CopyTo(destination.Slice(copied));
copied += length;
}
}
finally
{
ArrayPool<char>.Shared.Return(buffer);
}
}
}
Expand Down
21 changes: 19 additions & 2 deletions src/Split/net461/Polyfill_StringBuilder_CopyTo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#if FeatureMemory
namespace Polyfills;
using System;
using System.Buffers;
using System.Text;
static partial class Polyfill
{
Expand Down Expand Up @@ -31,9 +32,25 @@ public static void CopyTo(
{
throw new ArgumentException("The destination span is too short to hold the requested characters.", nameof(destination));
}
for (var index = 0; index < count; index++)
if (count == 0)
{
destination[index] = target[sourceIndex + index];
return;
}
var buffer = ArrayPool<char>.Shared.Rent(Math.Min(count, 16384));
try
{
var copied = 0;
while (copied < count)
{
var length = Math.Min(count - copied, buffer.Length);
target.CopyTo(sourceIndex + copied, buffer, 0, length);
buffer.AsSpan(0, length).CopyTo(destination.Slice(copied));
copied += length;
}
}
finally
{
ArrayPool<char>.Shared.Return(buffer);
}
}
}
Expand Down
21 changes: 19 additions & 2 deletions src/Split/net462/Polyfill_StringBuilder_CopyTo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#if FeatureMemory
namespace Polyfills;
using System;
using System.Buffers;
using System.Text;
static partial class Polyfill
{
Expand Down Expand Up @@ -31,9 +32,25 @@ public static void CopyTo(
{
throw new ArgumentException("The destination span is too short to hold the requested characters.", nameof(destination));
}
for (var index = 0; index < count; index++)
if (count == 0)
{
destination[index] = target[sourceIndex + index];
return;
}
var buffer = ArrayPool<char>.Shared.Rent(Math.Min(count, 16384));
try
{
var copied = 0;
while (copied < count)
{
var length = Math.Min(count - copied, buffer.Length);
target.CopyTo(sourceIndex + copied, buffer, 0, length);
buffer.AsSpan(0, length).CopyTo(destination.Slice(copied));
copied += length;
}
}
finally
{
ArrayPool<char>.Shared.Return(buffer);
}
}
}
Expand Down
21 changes: 19 additions & 2 deletions src/Split/net47/Polyfill_StringBuilder_CopyTo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#if FeatureMemory
namespace Polyfills;
using System;
using System.Buffers;
using System.Text;
static partial class Polyfill
{
Expand Down Expand Up @@ -31,9 +32,25 @@ public static void CopyTo(
{
throw new ArgumentException("The destination span is too short to hold the requested characters.", nameof(destination));
}
for (var index = 0; index < count; index++)
if (count == 0)
{
destination[index] = target[sourceIndex + index];
return;
}
var buffer = ArrayPool<char>.Shared.Rent(Math.Min(count, 16384));
try
{
var copied = 0;
while (copied < count)
{
var length = Math.Min(count - copied, buffer.Length);
target.CopyTo(sourceIndex + copied, buffer, 0, length);
buffer.AsSpan(0, length).CopyTo(destination.Slice(copied));
copied += length;
}
}
finally
{
ArrayPool<char>.Shared.Return(buffer);
}
}
}
Expand Down
21 changes: 19 additions & 2 deletions src/Split/net471/Polyfill_StringBuilder_CopyTo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#if FeatureMemory
namespace Polyfills;
using System;
using System.Buffers;
using System.Text;
static partial class Polyfill
{
Expand Down Expand Up @@ -31,9 +32,25 @@ public static void CopyTo(
{
throw new ArgumentException("The destination span is too short to hold the requested characters.", nameof(destination));
}
for (var index = 0; index < count; index++)
if (count == 0)
{
destination[index] = target[sourceIndex + index];
return;
}
var buffer = ArrayPool<char>.Shared.Rent(Math.Min(count, 16384));
try
{
var copied = 0;
while (copied < count)
{
var length = Math.Min(count - copied, buffer.Length);
target.CopyTo(sourceIndex + copied, buffer, 0, length);
buffer.AsSpan(0, length).CopyTo(destination.Slice(copied));
copied += length;
}
}
finally
{
ArrayPool<char>.Shared.Return(buffer);
}
}
}
Expand Down
21 changes: 19 additions & 2 deletions src/Split/net472/Polyfill_StringBuilder_CopyTo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#if FeatureMemory
namespace Polyfills;
using System;
using System.Buffers;
using System.Text;
static partial class Polyfill
{
Expand Down Expand Up @@ -31,9 +32,25 @@ public static void CopyTo(
{
throw new ArgumentException("The destination span is too short to hold the requested characters.", nameof(destination));
}
for (var index = 0; index < count; index++)
if (count == 0)
{
destination[index] = target[sourceIndex + index];
return;
}
var buffer = ArrayPool<char>.Shared.Rent(Math.Min(count, 16384));
try
{
var copied = 0;
while (copied < count)
{
var length = Math.Min(count - copied, buffer.Length);
target.CopyTo(sourceIndex + copied, buffer, 0, length);
buffer.AsSpan(0, length).CopyTo(destination.Slice(copied));
copied += length;
}
}
finally
{
ArrayPool<char>.Shared.Return(buffer);
}
}
}
Expand Down
21 changes: 19 additions & 2 deletions src/Split/net48/Polyfill_StringBuilder_CopyTo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#if FeatureMemory
namespace Polyfills;
using System;
using System.Buffers;
using System.Text;
static partial class Polyfill
{
Expand Down Expand Up @@ -31,9 +32,25 @@ public static void CopyTo(
{
throw new ArgumentException("The destination span is too short to hold the requested characters.", nameof(destination));
}
for (var index = 0; index < count; index++)
if (count == 0)
{
destination[index] = target[sourceIndex + index];
return;
}
var buffer = ArrayPool<char>.Shared.Rent(Math.Min(count, 16384));
try
{
var copied = 0;
while (copied < count)
{
var length = Math.Min(count - copied, buffer.Length);
target.CopyTo(sourceIndex + copied, buffer, 0, length);
buffer.AsSpan(0, length).CopyTo(destination.Slice(copied));
copied += length;
}
}
finally
{
ArrayPool<char>.Shared.Return(buffer);
}
}
}
Expand Down
21 changes: 19 additions & 2 deletions src/Split/net481/Polyfill_StringBuilder_CopyTo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#if FeatureMemory
namespace Polyfills;
using System;
using System.Buffers;
using System.Text;
static partial class Polyfill
{
Expand Down Expand Up @@ -31,9 +32,25 @@ public static void CopyTo(
{
throw new ArgumentException("The destination span is too short to hold the requested characters.", nameof(destination));
}
for (var index = 0; index < count; index++)
if (count == 0)
{
destination[index] = target[sourceIndex + index];
return;
}
var buffer = ArrayPool<char>.Shared.Rent(Math.Min(count, 16384));
try
{
var copied = 0;
while (copied < count)
{
var length = Math.Min(count - copied, buffer.Length);
target.CopyTo(sourceIndex + copied, buffer, 0, length);
buffer.AsSpan(0, length).CopyTo(destination.Slice(copied));
copied += length;
}
}
finally
{
ArrayPool<char>.Shared.Return(buffer);
}
}
}
Expand Down
21 changes: 19 additions & 2 deletions src/Split/netcoreapp2.0/Polyfill_StringBuilder_CopyTo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#if FeatureMemory
namespace Polyfills;
using System;
using System.Buffers;
using System.Text;
static partial class Polyfill
{
Expand Down Expand Up @@ -31,9 +32,25 @@ public static void CopyTo(
{
throw new ArgumentException("The destination span is too short to hold the requested characters.", nameof(destination));
}
for (var index = 0; index < count; index++)
if (count == 0)
{
destination[index] = target[sourceIndex + index];
return;
}
var buffer = ArrayPool<char>.Shared.Rent(Math.Min(count, 16384));
try
{
var copied = 0;
while (copied < count)
{
var length = Math.Min(count - copied, buffer.Length);
target.CopyTo(sourceIndex + copied, buffer, 0, length);
buffer.AsSpan(0, length).CopyTo(destination.Slice(copied));
copied += length;
}
}
finally
{
ArrayPool<char>.Shared.Return(buffer);
}
}
}
Expand Down
21 changes: 19 additions & 2 deletions src/Split/netstandard2.0/Polyfill_StringBuilder_CopyTo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#if FeatureMemory
namespace Polyfills;
using System;
using System.Buffers;
using System.Text;
static partial class Polyfill
{
Expand Down Expand Up @@ -31,9 +32,25 @@ public static void CopyTo(
{
throw new ArgumentException("The destination span is too short to hold the requested characters.", nameof(destination));
}
for (var index = 0; index < count; index++)
if (count == 0)
{
destination[index] = target[sourceIndex + index];
return;
}
var buffer = ArrayPool<char>.Shared.Rent(Math.Min(count, 16384));
try
{
var copied = 0;
while (copied < count)
{
var length = Math.Min(count - copied, buffer.Length);
target.CopyTo(sourceIndex + copied, buffer, 0, length);
buffer.AsSpan(0, length).CopyTo(destination.Slice(copied));
copied += length;
}
}
finally
{
ArrayPool<char>.Shared.Return(buffer);
}
}
}
Expand Down
21 changes: 19 additions & 2 deletions src/Split/uap10.0/Polyfill_StringBuilder_CopyTo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#if FeatureMemory
namespace Polyfills;
using System;
using System.Buffers;
using System.Text;
static partial class Polyfill
{
Expand Down Expand Up @@ -31,9 +32,25 @@ public static void CopyTo(
{
throw new ArgumentException("The destination span is too short to hold the requested characters.", nameof(destination));
}
for (var index = 0; index < count; index++)
if (count == 0)
{
destination[index] = target[sourceIndex + index];
return;
}
var buffer = ArrayPool<char>.Shared.Rent(Math.Min(count, 16384));
try
{
var copied = 0;
while (copied < count)
{
var length = Math.Min(count - copied, buffer.Length);
target.CopyTo(sourceIndex + copied, buffer, 0, length);
buffer.AsSpan(0, length).CopyTo(destination.Slice(copied));
copied += length;
}
}
finally
{
ArrayPool<char>.Shared.Return(buffer);
}
}
}
Expand Down
Loading
Loading