镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

[API Proposal]: Add access to StringBuilder content by using ReadOnlySequence<char> #87362

Closed as duplicate

Description

@AlexRadch

Background and motivation

There are many methods for working with the contents by using the ReadOnlySequence<char> structure:

There are no such methods for the StringBuilder class, and it is often used as a buffer to create string content quickly and conveniently. Therefore, there is a desire to work with the contents of the StringBuilder class as quickly and conveniently as with the contents of the ReadOnlySequence<char> structure.

There are 3 ways to deal with the contents of a StringBuilder as ways as with the ReadOnlySequence<char> structure:

  1. Create a string and use it. This is not optimal.

  2. Use the StringBuilder.GetChunks() method and implement the required methods yourself, repeating the methods already written for the ReadOnlySequence<char> structure. This is probably the fastest in terms of code performance, but very costly to implement and test.

  3. Convert the StringBuilder.ChunkEnumerator structure to a ReadOnlySequence<char> structure and use it further in the already written methods for working with buffers and memory. Implementing such a conversion in third-party libraries will not be the most efficient without access to the internal fields of the StringBuilder class. This conversion can be done more efficiently by accessing the internal fields of the StringBuilder class.

I propose to add to the StringBuilder class (as an option in the extension methods) methods to get its contents as a ReadOnlySequence<char> structure.

API Proposal

namespace System.Text;

public class StringBuilder
{
    public ChunkReverseEnumerator GetReverseChunks(); // will allow us to create performance and memory-efficient code
}

// Assembly: System.Memory.dll
namespace System.Buffers;

public static class BuffersExtensions
{
    public static ReadOnlySequence<char> AsSequence(this StringBuilder builder); // OR GetSequence(); OR ToSequence();
    public static ReadOnlySequence<char> AsSequence(this StringBuilder builder, int startIndex, int length);
}

API Usage

Wherever the ReadOnlySequence<T> class is used:

Alternative Designs

Repeating the methods already written for the ReadOnlySequence<char> structure for the StringBuilder class.

Risks

The contents of the ReadOnlySequence<char> structure can be changed by changing the contents of the original StringBuilder instance.

Activity

  1. added
    api-suggestionEarly API idea and discussion, it is NOT ready for implementation
    on Jun 10, 2023
  2. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Jun 10, 2023
  3. huoyaoyuan commented on Jun 10, 2023

    @huoyaoyuan
    Member

    If we do this on StringBuilder itself, it would require folding ReadOnlySequence into CoreLib.
    Is this achievable with public api surface of StringBuilder, using GetChunks?

  4. AlexRadch commented on Jun 10, 2023

    @AlexRadch
    ContributorAuthor

    If we do this on StringBuilder itself, it would require folding ReadOnlySequence into CoreLib.

    Is System.Memory.dll not included in CoreLib?

    Is this achievable with the public API surface of StringBuilder, using GetChunks?

    GetChunks can be used, but such code will be less performance and consume more memory than code that has access to the StringBuilder private fields.

  5. huoyaoyuan commented on Jun 10, 2023

    @huoyaoyuan
    Member

    Is System.Memory.dll not included in CoreLib?

    CoreLib is referenced by everything, and can't reference anything else. It is a single assembly file and doesn't "include" anything. Don't confuse with shared framework.

  6. AlexRadch commented on Jun 10, 2023

    @AlexRadch
    ContributorAuthor

    Is this achievable with the public API surface of StringBuilder, using GetChunks?

    If add a new GetChunks method that will return chunks in reverse order, then such code can be created in any place and it will be performance and memory effective.

  7. AlexRadch commented on Jun 10, 2023

    @AlexRadch
    ContributorAuthor

    If we do this on StringBuilder itself, it would require folding ReadOnlySequence into CoreLib. Is this achievable with the public API surface of StringBuilder, using GetChunks?

    I rewrote [API Proposal] based on your comments. Thank you!

  8. added
    needs-further-triageIssue has been initially triaged, but needs deeper consideration or reconsideration
    and removed
    untriagedNew issue has not been triaged by the area owner
    on Jun 24, 2024
  9. added this to the Future milestone on Jul 19, 2024
  10. tannergooding commented on May 1, 2026

    @tannergooding
    Member

    Going to close this as a duplicate. We're unlikely to expose GetReverseChunks as we don't really expose reverse enumerators in general for any collection type. Then the other functionality is covered by #97570

  11. locked and limited conversation to collaborators on Jun 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-System.Runtimeneeds-further-triageIssue has been initially triaged, but needs deeper consideration or reconsideration

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions