<PackageReference Include="System.Text.Encodings.Web" Version="9.0.3" />

TextEncoderSettings

public class TextEncoderSettings
Represents a filter that allows only certain Unicode code points.
using System.Collections.Generic; using System.Runtime.CompilerServices; using System.Text.Unicode; namespace System.Text.Encodings.Web { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] public class TextEncoderSettings { private AllowedBmpCodePointsBitmap _allowedCodePointsBitmap; public TextEncoderSettings() { } public TextEncoderSettings(TextEncoderSettings other) { if (other == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.other); _allowedCodePointsBitmap = other.GetAllowedCodePointsBitmap(); } public TextEncoderSettings(params UnicodeRange[] allowedRanges) { if (allowedRanges == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.allowedRanges); AllowRanges(allowedRanges); } public virtual void AllowCharacter(char character) { _allowedCodePointsBitmap.AllowChar(character); } public virtual void AllowCharacters(params char[] characters) { if (characters == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.characters); for (int i = 0; i < characters.Length; i++) { _allowedCodePointsBitmap.AllowChar(characters[i]); } } public virtual void AllowCodePoints(IEnumerable<int> codePoints) { if (codePoints == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.codePoints); foreach (int codePoint in codePoints) { if (System.Text.UnicodeUtility.IsBmpCodePoint((uint)codePoint)) _allowedCodePointsBitmap.AllowChar((char)codePoint); } } public virtual void AllowRange(UnicodeRange range) { if (range == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.range); int firstCodePoint = range.FirstCodePoint; int length = range.Length; for (int i = 0; i < length; i++) { int num = firstCodePoint + i; _allowedCodePointsBitmap.AllowChar((char)num); } } public virtual void AllowRanges(params UnicodeRange[] ranges) { if (ranges == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.ranges); for (int i = 0; i < ranges.Length; i++) { AllowRange(ranges[i]); } } public virtual void Clear() { _allowedCodePointsBitmap = default(AllowedBmpCodePointsBitmap); } public virtual void ForbidCharacter(char character) { _allowedCodePointsBitmap.ForbidChar(character); } public virtual void ForbidCharacters(params char[] characters) { if (characters == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.characters); for (int i = 0; i < characters.Length; i++) { _allowedCodePointsBitmap.ForbidChar(characters[i]); } } public virtual void ForbidRange(UnicodeRange range) { if (range == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.range); int firstCodePoint = range.FirstCodePoint; int length = range.Length; for (int i = 0; i < length; i++) { int num = firstCodePoint + i; _allowedCodePointsBitmap.ForbidChar((char)num); } } public virtual void ForbidRanges(params UnicodeRange[] ranges) { if (ranges == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.ranges); for (int i = 0; i < ranges.Length; i++) { ForbidRange(ranges[i]); } } public virtual IEnumerable<int> GetAllowedCodePoints() { for (int i = 0; i <= 65535; i++) { if (_allowedCodePointsBitmap.IsCharAllowed((char)i)) yield return i; } } [return: System.Runtime.CompilerServices.IsReadOnly] internal ref AllowedBmpCodePointsBitmap GetAllowedCodePointsBitmap() { if (GetType() == typeof(TextEncoderSettings)) return ref _allowedCodePointsBitmap; StrongBox<AllowedBmpCodePointsBitmap> strongBox = new StrongBox<AllowedBmpCodePointsBitmap>(); foreach (int allowedCodePoint in GetAllowedCodePoints()) { if ((uint)allowedCodePoint <= 65535) strongBox.Value.AllowChar((char)allowedCodePoint); } return ref strongBox.Value; } } }