Concatenate ReadOnlySpan<char>
Here's an example of how the .NET team internally handles this for Path.Join:
private static unsafe string JoinInternal(ReadOnlySpan<char> first, ReadOnlySpan<char> second)
{
Debug.Assert(first.Length > 0 && second.Length > 0, "should have dealt with empty paths");
bool hasSeparator = PathInternal.IsDirectorySeparator(first[first.Length - 1])
|| PathInternal.IsDirectorySeparator(second[0]);
fixed (char* f = &MemoryMarshal.GetReference(first), s = &MemoryMarshal.GetReference(second))
{
return string.Create(
first.Length + second.Length + (hasSeparator ? 0 : 1),
(First: (IntPtr)f, FirstLength: first.Length, Second: (IntPtr)s, SecondLength: second.Length, HasSeparator: hasSeparator),
(destination, state) =>
{
new Span<char>((char*)state.First, state.FirstLength).CopyTo(destination);
if (!state.HasSeparator)
destination[state.FirstLength] = PathInternal.DirectorySeparatorChar;
new Span<char>((char*)state.Second, state.SecondLength).CopyTo(destination.Slice(state.FirstLength + (state.HasSeparator ? 0 : 1)));
});
}
}
If you'd like to avoid using unsafe
and use something that's maybe easier to read, you could use something like:
public static ReadOnlySpan<char> Concat(this ReadOnlySpan<char> first, ReadOnlySpan<char> second)
{
return new string(first.ToArray().Concat(second.ToArray()).ToArray()).AsSpan();
}
public static ReadOnlySpan<char> Concat(this string first, ReadOnlySpan<char> second)
{
return new string(first.ToArray().Concat(second.ToArray()).ToArray()).ToArray();
}
Using ReadOnlySpan
is pretty low level and optimized for speed and so how you do it will likely depend on your own situation. But in many situations, it's probably fine to go back to string
interpolation and StringBuilder
(or don't convert to ReadOnlySpan
at all). So
var sb = new StringBuilder();
return sb
.Append(hello)
.Append(space)
.Append(world)
.ToString();
or
return $"{hello.ToString()}{space.ToString()}{world.ToString()}";
You can acheive that with a buffer like this =>
var hello = "Hello".AsSpan();
var space = " ".AsSpan();
var world = "World".AsSpan();
// First allocate the buffer with the target size
char[] buffer = new char[hello.Length + space.Length + world.Length];
// "Convert" it to writable Span<char>
var span = new Span<char>(buffer);
// Then copy each span at the right position in the buffer
int index = 0;
hello.CopyTo(span.Slice(index, hello.Length));
index += hello.Length;
space.CopyTo(span.Slice(index, space.Length));
index += space.Length;
world.CopyTo(span.Slice(index, world.Length));
// Finality get back the string
string result = span.ToString();
You can optimize one again by using an arraypool to reuse the buffer
char[] buffer = ArrayPool<char>.Shared.Rent(hello.Length + space.Length + world.Length);
// ...
ArrayPool<char>.Shared.Return(buffer);
I think it's worth mentioning that an overload for concatenating spans was added in .NET Core 3 and that support for .NET Core 2.1 ends soon(-ish) anyway on August 21, 2021 [src]. If you upgrade now then you can simply use String.Concat.
https://docs.microsoft.com/en-us/dotnet/api/system.string.concat?view=netcore-3.1#System_String_Concat_System_ReadOnlySpan_System_Char__System_ReadOnlySpan_System_Char__System_ReadOnlySpan_System_Char__
var hello = "Hello".AsSpan();
var space = " ".AsSpan();
var world = "World".AsSpan();
// .NET Core 3+
var result = string.Concat(hello, space, world);