Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit c9309a4

Browse files
Merge pull request #2100 from KrzysztofCwalina/APIReviewChanges
First round of changes based on API review feedback
2 parents d421196 + d7d0ccf commit c9309a4

14 files changed

+442
-571
lines changed

src/System.Text.Encodings.Web/src/System.Text.Encodings.Web.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@
2121
<Compile Include="System\Text\Encodings\Web\CodePointFilter.cs" />
2222
<Compile Include="System\Text\Encodings\Web\HexUtil.cs" />
2323
<Compile Include="System\Text\Encodings\Web\HtmlEncoder.cs" />
24-
<Compile Include="System\Text\Encodings\Web\ICodePointFilter.cs" />
2524
<Compile Include="System\Text\Encodings\Web\JavaScriptStringEncoder.cs" />
26-
<Compile Include="System\Text\Encodings\Web\TextEncoderExtensions.cs" />
2725
<Compile Include="System\Text\Encodings\Web\TextEncoder.cs" />
2826
<Compile Include="System\Text\Encodings\Web\UnicodeHelpers.cs" />
2927
<Compile Include="System\Text\Encodings\Web\UnicodeRange.cs" />

src/System.Text.Encodings.Web/src/System/Text/Encodings/Web/CodePointFilter.cs

Lines changed: 18 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace System.Text.Encodings.Web
1010
/// <summary>
1111
/// Represents a filter which allows only certain Unicode code points through.
1212
/// </summary>
13-
public sealed class CodePointFilter : ICodePointFilter
13+
public class CodePointFilter
1414
{
1515
private AllowedCharactersBitmap _allowedCharactersBitmap;
1616

@@ -23,20 +23,12 @@ public CodePointFilter()
2323
}
2424

2525
/// <summary>
26-
/// Instantiates the filter by cloning the allow list of another <see cref="ICodePointFilter"/>.
26+
/// Instantiates the filter by cloning the allow list of another <see cref="CodePointFilter"/>.
2727
/// </summary>
28-
public CodePointFilter(ICodePointFilter other)
28+
public CodePointFilter(CodePointFilter other)
2929
{
30-
CodePointFilter otherAsCodePointFilter = other as CodePointFilter;
31-
if (otherAsCodePointFilter != null)
32-
{
33-
_allowedCharactersBitmap = otherAsCodePointFilter.GetAllowedCharacters();
34-
}
35-
else
36-
{
37-
_allowedCharactersBitmap = AllowedCharactersBitmap.CreateNew();
38-
AllowFilter(other);
39-
}
30+
_allowedCharactersBitmap = AllowedCharactersBitmap.CreateNew();
31+
AllowFilter(other);
4032
}
4133

4234
/// <summary>
@@ -56,22 +48,15 @@ public CodePointFilter(params UnicodeRange[] allowedRanges)
5648
/// <summary>
5749
/// Allows the character specified by <paramref name="character"/> through the filter.
5850
/// </summary>
59-
/// <returns>
60-
/// The 'this' instance.
61-
/// </returns>
62-
public CodePointFilter AllowCharacter(char character)
51+
public virtual void AllowCharacter(char character)
6352
{
6453
_allowedCharactersBitmap.AllowCharacter(character);
65-
return this;
6654
}
6755

6856
/// <summary>
6957
/// Allows all characters specified by <paramref name="characters"/> through the filter.
7058
/// </summary>
71-
/// <returns>
72-
/// The 'this' instance.
73-
/// </returns>
74-
public CodePointFilter AllowCharacters(params char[] characters)
59+
public virtual void AllowCharacters(params char[] characters)
7560
{
7661
if (characters == null)
7762
{
@@ -82,37 +67,12 @@ public CodePointFilter AllowCharacters(params char[] characters)
8267
{
8368
_allowedCharactersBitmap.AllowCharacter(characters[i]);
8469
}
85-
86-
return this;
87-
}
88-
89-
/// <summary>
90-
/// Allows all characters in the string <paramref name="characters"/> through the filter.
91-
/// </summary>
92-
/// <returns>
93-
/// The 'this' instance.
94-
/// </returns>
95-
public CodePointFilter AllowCharacters(string characters)
96-
{
97-
if (characters == null)
98-
{
99-
throw new ArgumentNullException("characters");
100-
}
101-
102-
for (int i = 0; i < characters.Length; i++)
103-
{
104-
_allowedCharactersBitmap.AllowCharacter(characters[i]);
105-
}
106-
return this;
10770
}
10871

10972
/// <summary>
11073
/// Allows all characters specified by <paramref name="filter"/> through the filter.
11174
/// </summary>
112-
/// <returns>
113-
/// The 'this' instance.
114-
/// </returns>
115-
public CodePointFilter AllowFilter(ICodePointFilter filter)
75+
public virtual void AllowFilter(CodePointFilter filter)
11676
{
11777
if (filter == null)
11878
{
@@ -128,16 +88,12 @@ public CodePointFilter AllowFilter(ICodePointFilter filter)
12888
_allowedCharactersBitmap.AllowCharacter(codePointAsChar);
12989
}
13090
}
131-
return this;
13291
}
13392

13493
/// <summary>
13594
/// Allows all characters specified by <paramref name="range"/> through the filter.
13695
/// </summary>
137-
/// <returns>
138-
/// The 'this' instance.
139-
/// </returns>
140-
public CodePointFilter AllowRange(UnicodeRange range)
96+
public virtual void AllowRange(UnicodeRange range)
14197
{
14298
if (range == null)
14399
{
@@ -150,16 +106,12 @@ public CodePointFilter AllowRange(UnicodeRange range)
150106
{
151107
_allowedCharactersBitmap.AllowCharacter((char)(firstCodePoint + i));
152108
}
153-
return this;
154109
}
155110

156111
/// <summary>
157112
/// Allows all characters specified by <paramref name="ranges"/> through the filter.
158113
/// </summary>
159-
/// <returns>
160-
/// The 'this' instance.
161-
/// </returns>
162-
public CodePointFilter AllowRanges(params UnicodeRange[] ranges)
114+
public virtual void AllowRanges(params UnicodeRange[] ranges)
163115
{
164116
if (ranges == null)
165117
{
@@ -170,62 +122,28 @@ public CodePointFilter AllowRanges(params UnicodeRange[] ranges)
170122
{
171123
AllowRange(ranges[i]);
172124
}
173-
174-
return this;
175125
}
176126

177127
/// <summary>
178128
/// Resets this filter by disallowing all characters.
179129
/// </summary>
180-
/// <returns>
181-
/// The 'this' instance.
182-
/// </returns>
183-
public CodePointFilter Clear()
130+
public virtual void Clear()
184131
{
185132
_allowedCharactersBitmap.Clear();
186-
return this;
187133
}
188134

189135
/// <summary>
190136
/// Disallows the character <paramref name="character"/> through the filter.
191137
/// </summary>
192-
/// <returns>
193-
/// The 'this' instance.
194-
/// </returns>
195-
public CodePointFilter ForbidCharacter(char character)
138+
public virtual void ForbidCharacter(char character)
196139
{
197140
_allowedCharactersBitmap.ForbidCharacter(character);
198-
return this;
199141
}
200142

201143
/// <summary>
202144
/// Disallows all characters specified by <paramref name="characters"/> through the filter.
203145
/// </summary>
204-
/// <returns>
205-
/// The 'this' instance.
206-
/// </returns>
207-
public CodePointFilter ForbidCharacters(params char[] characters)
208-
{
209-
if (characters == null)
210-
{
211-
throw new ArgumentNullException("characters");
212-
}
213-
214-
for (int i = 0; i < characters.Length; i++)
215-
{
216-
_allowedCharactersBitmap.ForbidCharacter(characters[i]);
217-
}
218-
219-
return this;
220-
}
221-
222-
/// <summary>
223-
/// Disallows all characters in the string <paramref name="characters"/> through the filter.
224-
/// </summary>
225-
/// <returns>
226-
/// The 'this' instance.
227-
/// </returns>
228-
public CodePointFilter ForbidCharacters(string characters)
146+
public virtual void ForbidCharacters(params char[] characters)
229147
{
230148
if (characters == null)
231149
{
@@ -236,16 +154,12 @@ public CodePointFilter ForbidCharacters(string characters)
236154
{
237155
_allowedCharactersBitmap.ForbidCharacter(characters[i]);
238156
}
239-
return this;
240157
}
241158

242159
/// <summary>
243160
/// Disallows all characters specified by <paramref name="range"/> through the filter.
244161
/// </summary>
245-
/// <returns>
246-
/// The 'this' instance.
247-
/// </returns>
248-
public CodePointFilter ForbidRange(UnicodeRange range)
162+
public virtual void ForbidRange(UnicodeRange range)
249163
{
250164
if (range == null)
251165
{
@@ -258,16 +172,12 @@ public CodePointFilter ForbidRange(UnicodeRange range)
258172
{
259173
_allowedCharactersBitmap.ForbidCharacter((char)(firstCodePoint + i));
260174
}
261-
return this;
262175
}
263176

264177
/// <summary>
265178
/// Disallows all characters specified by <paramref name="ranges"/> through the filter.
266179
/// </summary>
267-
/// <returns>
268-
/// The 'this' instance.
269-
/// </returns>
270-
public CodePointFilter ForbidRanges(params UnicodeRange[] ranges)
180+
public virtual void ForbidRanges(params UnicodeRange[] ranges)
271181
{
272182
if (ranges == null)
273183
{
@@ -278,8 +188,6 @@ public CodePointFilter ForbidRanges(params UnicodeRange[] ranges)
278188
{
279189
ForbidRange(ranges[i]);
280190
}
281-
282-
return this;
283191
}
284192

285193
/// <summary>
@@ -294,7 +202,7 @@ internal AllowedCharactersBitmap GetAllowedCharacters()
294202
/// <summary>
295203
/// Gets an enumeration of all allowed code points.
296204
/// </summary>
297-
public IEnumerable<int> GetAllowedCodePoints()
205+
public virtual IEnumerable<int> GetAllowedCodePoints()
298206
{
299207
for (int i = 0; i < 0x10000; i++)
300208
{
@@ -308,15 +216,15 @@ public IEnumerable<int> GetAllowedCodePoints()
308216
/// <summary>
309217
/// Returns a value stating whether the character <paramref name="character"/> is allowed through the filter.
310218
/// </summary>
311-
public bool IsCharacterAllowed(char character)
219+
public virtual bool IsCharacterAllowed(char character)
312220
{
313221
return _allowedCharactersBitmap.IsCharacterAllowed(character);
314222
}
315223

316224
/// <summary>
317225
/// Wraps the provided filter as a CodePointFilter, avoiding the clone if possible.
318226
/// </summary>
319-
internal static CodePointFilter Wrap(ICodePointFilter filter)
227+
internal static CodePointFilter Wrap(CodePointFilter filter)
320228
{
321229
return (filter as CodePointFilter) ?? new CodePointFilter(filter);
322230
}

src/System.Text.Encodings.Web/src/System/Text/Encodings/Web/HtmlEncoder.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4+
using System.ComponentModel;
45
using System.Diagnostics;
56
using System.Runtime.CompilerServices;
67
using System.Text.Internal;
@@ -24,7 +25,7 @@ public static HtmlEncoder Create(params UnicodeRange[] allowedRanges)
2425
}
2526
}
2627

27-
public sealed class DefaultHtmlEncoder : HtmlEncoder
28+
internal sealed class DefaultHtmlEncoder : HtmlEncoder
2829
{
2930
private AllowedCharactersBitmap _allowedCharacters;
3031
internal readonly static DefaultHtmlEncoder Singleton = new DefaultHtmlEncoder(new CodePointFilter(UnicodeRanges.BasicLatin));
@@ -65,7 +66,6 @@ public override bool Encodes(int unicodeScalar)
6566
return !_allowedCharacters.IsUnicodeScalarAllowed(unicodeScalar);
6667
}
6768

68-
[CLSCompliant(false)]
6969
[MethodImpl(MethodImplOptions.AggressiveInlining)]
7070
public unsafe override int FindFirstCharacterToEncode(char* text, int textLength)
7171
{
@@ -77,24 +77,23 @@ public override int MaxOutputCharactersPerInputCharacter
7777
get { return 8; } // "&#xFFFF;" is the longest encoded form
7878
}
7979

80-
static readonly char[] quote = "&quot;".ToCharArray();
81-
static readonly char[] ampersand = "&amp;".ToCharArray();
82-
static readonly char[] lessthan = "&lt;".ToCharArray();
83-
static readonly char[] greaterthan = "&gt;".ToCharArray();
80+
static readonly char[] s_quote = "&quot;".ToCharArray();
81+
static readonly char[] s_ampersand = "&amp;".ToCharArray();
82+
static readonly char[] s_lessthan = "&lt;".ToCharArray();
83+
static readonly char[] s_greaterthan = "&gt;".ToCharArray();
8484

85-
[CLSCompliant(false)]
8685
public unsafe override bool TryEncodeUnicodeScalar(int unicodeScalar, char* buffer, int bufferLength, out int numberOfCharactersWritten)
8786
{
8887
if (buffer == null)
8988
{
9089
throw new ArgumentNullException("buffer");
9190
}
9291

93-
if (!Encodes(unicodeScalar)) { return unicodeScalar.TryWriteScalarAsChar(buffer, bufferLength, out numberOfCharactersWritten); }
94-
else if (unicodeScalar == '\"') { return quote.TryCopyCharacters(buffer, bufferLength, out numberOfCharactersWritten); }
95-
else if (unicodeScalar == '&') { return ampersand.TryCopyCharacters(buffer, bufferLength, out numberOfCharactersWritten); }
96-
else if (unicodeScalar == '<') { return lessthan.TryCopyCharacters(buffer, bufferLength, out numberOfCharactersWritten); }
97-
else if (unicodeScalar == '>') { return greaterthan.TryCopyCharacters(buffer, bufferLength, out numberOfCharactersWritten); }
92+
if (!Encodes(unicodeScalar)) { return TryWriteScalarAsChar(unicodeScalar, buffer, bufferLength, out numberOfCharactersWritten); }
93+
else if (unicodeScalar == '\"') { return TryCopyCharacters(s_quote, buffer, bufferLength, out numberOfCharactersWritten); }
94+
else if (unicodeScalar == '&') { return TryCopyCharacters(s_ampersand, buffer, bufferLength, out numberOfCharactersWritten); }
95+
else if (unicodeScalar == '<') { return TryCopyCharacters(s_lessthan, buffer, bufferLength, out numberOfCharactersWritten); }
96+
else if (unicodeScalar == '>') { return TryCopyCharacters(s_greaterthan, buffer, bufferLength, out numberOfCharactersWritten); }
9897
else { return TryWriteEncodedScalarAsNumericEntity(unicodeScalar, buffer, bufferLength, out numberOfCharactersWritten); }
9998
}
10099

src/System.Text.Encodings.Web/src/System/Text/Encodings/Web/ICodePointFilter.cs

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)