Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/UTF8.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ private static (int utfAdjust, int scalarAdjust) GetFinalScalarUtfAdjustments(by
} // Too short
// range check
codePoint = (uint)(firstByte & 0b00011111) << 6 | (uint)(buf[pos + 1] & 0b00111111);
if ((codePoint < 0x80) || (0x7ff < codePoint))
// codePoint is necessarily <= 0x7ff
if (codePoint < 0x80)
{
return buf + pos;
} // Overlong
Expand All @@ -141,7 +142,8 @@ private static (int utfAdjust, int scalarAdjust) GetFinalScalarUtfAdjustments(by
(uint)(buf[pos + 1] & 0b00111111) << 6 |
(uint)(buf[pos + 2] & 0b00111111);
// Either overlong or too large:
if ((codePoint < 0x800) || (0xffff < codePoint) ||
// codePoint is necessarily <= 0xffff
if ((codePoint < 0x800) ||
(0xd7ff < codePoint && codePoint < 0xe000))
{
return buf + pos;
Expand Down Expand Up @@ -238,7 +240,8 @@ private static (int utfAdjust, int scalarAdjust) GetFinalScalarUtfAdjustments(by
} // Too short
// range check
codePoint = (uint)(firstByte & 0b00011111) << 6 | (uint)(pInputBuffer[pos + 1] & 0b00111111);
if ((codePoint < 0x80) || (0x7ff < codePoint))
// codePoint is necessarily <= 0x7ff
if (codePoint < 0x80)
{
utf16CodeUnitCountAdjustment = TempUtf16CodeUnitCountAdjustment;
scalarCountAdjustment = TempScalarCountAdjustment;
Expand All @@ -261,7 +264,8 @@ private static (int utfAdjust, int scalarAdjust) GetFinalScalarUtfAdjustments(by
(uint)(pInputBuffer[pos + 1] & 0b00111111) << 6 |
(uint)(pInputBuffer[pos + 2] & 0b00111111);
// Either overlong or too large:
if ((codePoint < 0x800) || (0xffff < codePoint) ||
// codePoint is necessarily <= 0xffff
if ((codePoint < 0x800) ||
(0xd7ff < codePoint && codePoint < 0xe000))
{
utf16CodeUnitCountAdjustment = TempUtf16CodeUnitCountAdjustment;
Expand Down
3 changes: 2 additions & 1 deletion test/AsciiTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace tests;
namespace tests;

using System.Text;
using SimdUnicode;

Expand Down
6 changes: 4 additions & 2 deletions test/UTF8ValidationTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace tests;

using System.Text;
using SimdUnicode;
using System.Diagnostics;
Expand Down Expand Up @@ -1245,7 +1246,8 @@ public static bool ValidateUtf8Fuschia(byte[] data)
if ((data[pos + 1] & 0b11000000) != 0b10000000) return false;

codePoint = (uint)((byte1 & 0b00011111) << 6 | (data[pos + 1] & 0b00111111));
if (codePoint < 0x80 || 0x7ff < codePoint) return false;
// codePoint is necessarily <= 0x7ff
if (codePoint < 0x80) return false;
pos += 2;
}
else if ((byte1 & 0b11110000) == 0b11100000)
Expand All @@ -1255,7 +1257,7 @@ public static bool ValidateUtf8Fuschia(byte[] data)
if ((data[pos + 2] & 0b11000000) != 0b10000000) return false;

codePoint = (uint)((byte1 & 0b00001111) << 12 | (data[pos + 1] & 0b00111111) << 6 | (data[pos + 2] & 0b00111111));
if (codePoint < 0x800 || 0xffff < codePoint || (0xd7ff < codePoint && codePoint < 0xe000)) return false;
if (codePoint < 0x800 || (0xd7ff < codePoint && codePoint < 0xe000)) return false;
pos += 3;
}
else if ((byte1 & 0b11111000) == 0b11110000)
Expand Down
1 change: 1 addition & 0 deletions test/helpers/randomutf8.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace tests;

using System;
using System.Collections.Generic;
using System.Linq;
Expand Down
Loading