Skip to content

API proposal: expose System.Text.Encoding.Latin1 getter publicly #31549

@GrabYourPitchforks

Description

@GrabYourPitchforks

The ISO-8859-1 encoding is used somewhat commonly in web scenarios since it's the encoding normally specified for headers. We should consider having a fast-access property for this encoding just like we have for Encoding.UTF8.

The workaround for now is to use Encoding.GetEncoding("iso-8859-1", ...), which works but is not convenient or optimized.

API Proposal

namespace System.Text
{
    public class Encoding
    {
        /* NEW static propery getter */
        public static Encoding Latin1 { get; }
    }
}

The return value of this would be equivalent to calling Encoding.GetEncoding("iso-8859-1"). The byte to char conversion is a naive widening operation. The char to byte conversion is a naive narrowing operation, with any char values above 0x007F being best-fit mapped to an ASCII byte. If a best-fit mapping is unavailable then the character is converted to (byte)'?'.

If the developer wishes to change the fallback behavior or to enable best fit mapping, they can call Encoding.GetEncoding("iso-8859-1", ...) and pass custom fallback behaviors as needed for the newly-created instance.

This proposal does not intend on making the existing internal Latin1Encoding class public.

/cc @davidfowl

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions