diff --git a/src/Microsoft.Framework.WebEncoders/AllowedCharsBitmap.cs b/src/Microsoft.Framework.WebEncoders/AllowedCharsBitmap.cs index e05a917c56..3ad3312ae9 100644 --- a/src/Microsoft.Framework.WebEncoders/AllowedCharsBitmap.cs +++ b/src/Microsoft.Framework.WebEncoders/AllowedCharsBitmap.cs @@ -6,14 +6,15 @@ using System.Diagnostics; namespace Microsoft.Framework.WebEncoders { - internal class AllowedCharsBitmap + internal struct AllowedCharsBitmap { private const int ALLOWED_CHARS_BITMAP_LENGTH = 0x10000 / (8 * sizeof(uint)); - private uint[] _allowedCharsBitmap; + private readonly uint[] _allowedCharsBitmap; - public AllowedCharsBitmap() + private AllowedCharsBitmap(uint[] allowedCharsBitmap) { - _allowedCharsBitmap = new uint[ALLOWED_CHARS_BITMAP_LENGTH]; + Debug.Assert(allowedCharsBitmap != null); + _allowedCharsBitmap = allowedCharsBitmap; } // Marks a character as allowed (can be returned unencoded) @@ -34,9 +35,13 @@ namespace Microsoft.Framework.WebEncoders // Creates a deep copy of this bitmap public AllowedCharsBitmap Clone() { - var retVal = new AllowedCharsBitmap(); - retVal._allowedCharsBitmap = (uint[])this._allowedCharsBitmap.Clone(); - return retVal; + return new AllowedCharsBitmap((uint[])_allowedCharsBitmap.Clone()); + } + + // should be called in place of the ctor + public static AllowedCharsBitmap CreateNew() + { + return new AllowedCharsBitmap(new uint[ALLOWED_CHARS_BITMAP_LENGTH]); } // Marks a character as forbidden (must be returned encoded) @@ -47,7 +52,7 @@ namespace Microsoft.Framework.WebEncoders int offset = (int)(codePoint & 0x1FU); _allowedCharsBitmap[index] &= ~(0x1U << offset); } - + public void ForbidUndefinedCharacters() { // Forbid codepoints which aren't mapped to characters or which are otherwise always disallowed diff --git a/src/Microsoft.Framework.WebEncoders/CodePointFilter.cs b/src/Microsoft.Framework.WebEncoders/CodePointFilter.cs index f9c57f1873..da5991e7e2 100644 --- a/src/Microsoft.Framework.WebEncoders/CodePointFilter.cs +++ b/src/Microsoft.Framework.WebEncoders/CodePointFilter.cs @@ -19,7 +19,7 @@ namespace Microsoft.Framework.WebEncoders /// public CodePointFilter() { - _allowedCharsBitmap = new AllowedCharsBitmap(); + _allowedCharsBitmap = AllowedCharsBitmap.CreateNew(); } /// @@ -34,7 +34,7 @@ namespace Microsoft.Framework.WebEncoders } else { - _allowedCharsBitmap = new AllowedCharsBitmap(); + _allowedCharsBitmap = AllowedCharsBitmap.CreateNew(); AllowFilter(other); } } @@ -45,7 +45,7 @@ namespace Microsoft.Framework.WebEncoders /// public CodePointFilter(params UnicodeRange[] allowedRanges) { - _allowedCharsBitmap = new AllowedCharsBitmap(); + _allowedCharsBitmap = AllowedCharsBitmap.CreateNew(); AllowRanges(allowedRanges); } diff --git a/test/Microsoft.Framework.WebEncoders.Tests/AllowedCharsBitmapTests.cs b/test/Microsoft.Framework.WebEncoders.Tests/AllowedCharsBitmapTests.cs index 0810fb82d8..e26046b239 100644 --- a/test/Microsoft.Framework.WebEncoders.Tests/AllowedCharsBitmapTests.cs +++ b/test/Microsoft.Framework.WebEncoders.Tests/AllowedCharsBitmapTests.cs @@ -12,7 +12,7 @@ namespace Microsoft.Framework.WebEncoders public void Ctor_EmptyByDefault() { // Act - var bitmap = new AllowedCharsBitmap(); + var bitmap = AllowedCharsBitmap.CreateNew(); // Assert for (int i = 0; i <= Char.MaxValue; i++) @@ -25,7 +25,7 @@ namespace Microsoft.Framework.WebEncoders public void Allow_Forbid_ZigZag() { // Arrange - var bitmap = new AllowedCharsBitmap(); + var bitmap = AllowedCharsBitmap.CreateNew(); // Act // The only chars which are allowed are those whose code points are multiples of 3 or 7 @@ -58,7 +58,7 @@ namespace Microsoft.Framework.WebEncoders public void Clear_ForbidsEverything() { // Arrange - var bitmap = new AllowedCharsBitmap(); + var bitmap = AllowedCharsBitmap.CreateNew(); for (int i = 1; i <= Char.MaxValue; i++) { bitmap.AllowCharacter((char)i); @@ -78,7 +78,7 @@ namespace Microsoft.Framework.WebEncoders public void Clone_MakesDeepCopy() { // Arrange - var originalBitmap = new AllowedCharsBitmap(); + var originalBitmap = AllowedCharsBitmap.CreateNew(); originalBitmap.AllowCharacter('x'); // Act @@ -99,7 +99,7 @@ namespace Microsoft.Framework.WebEncoders // We only allow odd-numbered characters in this test so that // we can validate that we properly merged the two bitmaps together // rather than simply overwriting the target. - var bitmap = new AllowedCharsBitmap(); + var bitmap = AllowedCharsBitmap.CreateNew(); for (int i = 1; i <= Char.MaxValue; i += 2) { bitmap.AllowCharacter((char)i);