@@ -500,6 +500,9 @@ public static class UTF8
500
500
int TailScalarCodeUnitCountAdjustment = 0 ;
501
501
int TailUtf16CodeUnitCountAdjustment = 0 ;
502
502
503
+ bool prevWasSimd = false ;
504
+
505
+
503
506
if ( pInputBuffer == null || inputLength <= 0 )
504
507
{
505
508
utf16CodeUnitCountAdjustment = TempUtf16CodeUnitCountAdjustment ;
@@ -606,7 +609,6 @@ public static class UTF8
606
609
Vector256 < byte > v0f = Vector256 . Create ( ( byte ) 0x0F ) ;
607
610
Vector256 < byte > v80 = Vector256 . Create ( ( byte ) 0x80 ) ;
608
611
609
- bool prevWasSimd = false ;
610
612
611
613
for ( ; processedLength + 32 <= inputLength ; processedLength += 32 )
612
614
{
@@ -938,6 +940,29 @@ public static class UTF8
938
940
Console . WriteLine ( "TempUTF16 after tail remaining check:" + TempUtf16CodeUnitCountAdjustment ) ;
939
941
Console . WriteLine ( "TempScalar '' '' '':" + TempScalarCountAdjustment ) ;
940
942
943
+ } else if ( processedLength == inputLength && prevWasSimd ) {
944
+ for ( int k = 0 ; k < 3 ; k ++ )
945
+ {
946
+ // There is no error here hence the loop is straigthforward and we avoid double counting every byte
947
+ int candidateByte = pInputBuffer [ processedLength - k ] ;
948
+ if ( ( candidateByte & 0b11000000 ) == 0b11000000 )
949
+ {
950
+ if ( ( candidateByte & 0b11100000 ) == 0b11000000 ) // Start of a 2-byte sequence
951
+ {
952
+ TempUtf16CodeUnitCountAdjustment -= 1 ;
953
+ }
954
+ if ( ( candidateByte & 0b11110000 ) == 0b11100000 ) // Start of a 3-byte sequence
955
+ {
956
+ TempUtf16CodeUnitCountAdjustment -= 2 ;
957
+ }
958
+ if ( ( candidateByte & 0b11111000 ) == 0b11110000 ) // Start of a 4-byte sequence
959
+ {
960
+ TempUtf16CodeUnitCountAdjustment -= 2 ;
961
+ TempScalarCountAdjustment -= 1 ;
962
+ }
963
+ break ;
964
+ }
965
+ }
941
966
}
942
967
943
968
utf16CodeUnitCountAdjustment = TempUtf16CodeUnitCountAdjustment + TailUtf16CodeUnitCountAdjustment ;
0 commit comments