Skip to content

Utf16ValueStringBuilder.Insert runs into System.ArgumentException: Destination is too short. #125

@JohannesDeml

Description

@JohannesDeml

I wanted to benchmark the library and ran into an issue for the StringBuilder.Insert equivalent method. Here is a simple program that replicated the issue:

using System;
using Cysharp.Text;

public static class Program
{
	private static int Main(string[] args)
	{
		string result = ZStringBuilderInsert();
		return 0;
	}

	private static string ZStringBuilderInsert()
	{
		var stringArray = new string[100];
		for (int i = 0; i < stringArray.Length; i++)
		{
			stringArray[i] = new string('A', 1000);
		}

		using (Utf16ValueStringBuilder sb = ZString.CreateStringBuilder())
		{
			for (var i = stringArray.Length - 1; i >= 0; i--)
			{
				var part = stringArray[i];
				try
				{
					sb.Insert(0, part);
				}
				catch (Exception e)
				{
					Console.WriteLine($"Error during string concatenation #{i}: " + e.Message);
					throw;
				}
			}

			return sb.ToString();
		}
	}
}

This results in the following error:

MicroBenchmarks/bin/Release/net8.0/MicroBenchmarks
Error during string concatenation #67: Destination is too short. (Parameter 'destination')
Unhandled exception. System.ArgumentException: Destination is too short. (Parameter 'destination')
   at Cysharp.Text.Utf16ValueStringBuilder.Insert(Int32 index, ReadOnlySpan`1 value, Int32 count)
   at Cysharp.Text.Utf16ValueStringBuilder.Insert(Int32 index, String value)
   at MicroBenchmarks.Program.ZStringBuilderInsert() in /Users/johannes.deml/Documents/CSharp/MicroBenchmarksDotNet/MicroBenchmarks/Program.cs:line 39
   at MicroBenchmarks.Program.Main(String[] args) in /Users/johannes.deml/Documents/CSharp/MicroBenchmarksDotNet/MicroBenchmarks/Program.cs:line 20

Process finished with exit code 134.

I guess there is an issue with resizing the buffer? I didn't look too deep into the issue, but I didn't find it tracked here, so I thought it is a good idea to open up an issue for the exception.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions