[API Proposal]: Add StringBuilder overload for WebUtility.HtmlEncode

Open
#109,591 8 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
38/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp

Research direction

Start with src/libraries/System.Private.CoreLib/src/System/Net/WebUtility.cs and compare the existing TextWriter and private StringBuilder implementations linked in the issue. Confirm the proposed HtmlEncode and HtmlDecode signatures and their expected behavior; this is done when the public API design is accepted and the overloads have appropriate coverage.

Written by the indexing model from the issue text.

Description

api-suggestion area-System.Net
Background and motivation

I suggest adding StringBuilder overloads in WebUtility similar to TextWriter one. Actually one very similar already exists for private implementation.

Motivation: in Oxpecker framework (which is currently one of the top frameworks at Techempower benchmarks) I'm using StringBuilder for ViewEngine rather than TextWriter because it gives about 10% performance increase for rendering HTML views to string. One downside is that HtmlEncode method doesn't allow for StringBuilder, so I'm allocating strings each time encoding is required.
If overload is added, it can behave faster than TextWriter since no intermediate ValueStringBuilder instances are requried.

API Proposal
public static class WebUtility
{
    public static void HtmlEncode(string? value, StringBuilder output)
    public static void HtmlDecode(string? value, StringBuilder output)
}
API Usage
var sb = new StringBuilder();
WebUtility.HtmlEncode("myString", sb);
Alternative Designs

No response

Risks

No response

Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/runtime

All issues in dotnet/runtime

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.