@@ -37,23 +37,34 @@ macro_rules! static_cast_i8 {
3737 } ;
3838}
3939
40+ macro_rules! check_utf8 {
41+ ( $feat: expr, $t: ident) => {
42+ #[ target_feature( enable = $feat) ]
43+ #[ inline]
44+ unsafe fn check_utf8( & self , previous: & mut Utf8CheckingState <$t>) {
45+ if likely!( self . is_ascii( ) ) {
46+ previous. error =
47+ Utf8CheckingState :: <$t>:: check_eof( previous. error, previous. incomplete)
48+ } else {
49+ self . check_block( previous) ;
50+ }
51+ }
52+ } ;
53+ }
54+
4055/// check_bytes() strategy
4156macro_rules! check_bytes {
4257 ( $feat: expr, $t: ident) => {
4358 #[ target_feature( enable = $feat) ]
4459 #[ inline]
4560 unsafe fn check_bytes( current: $t, previous: & mut Utf8CheckingState <$t>) {
46- if likely!( Self :: is_ascii( current) ) {
47- previous. error = Self :: check_eof( previous. error, previous. incomplete)
48- } else {
49- let prev1 = Self :: prev1( current, previous. prev) ;
50- let sc = Self :: check_special_cases( current, prev1) ;
51- previous. error = Self :: or(
52- previous. error,
53- Self :: check_multibyte_lengths( current, previous. prev, sc) ,
54- ) ;
55- previous. incomplete = Self :: is_incomplete( current) ;
56- }
61+ let prev1 = Self :: prev1( current, previous. prev) ;
62+ let sc = Self :: check_special_cases( current, prev1) ;
63+ previous. error = Self :: or(
64+ previous. error,
65+ Self :: check_multibyte_lengths( current, previous. prev, sc) ,
66+ ) ;
67+ previous. incomplete = Self :: is_incomplete( current) ;
5768 previous. prev = current
5869 }
5970 } ;
@@ -79,12 +90,11 @@ macro_rules! validate_utf8_basic_simd {
7990 const SIMDINPUT_LENGTH : usize = 64 ;
8091 let len = input. len( ) ;
8192 let mut state = SimdInput :: new_utf8_checking_state( ) ;
82- let lenminus64: usize = if len < 64 { 0 } else { len as usize - 64 } ;
8393 let mut idx: usize = 0 ;
8494 let mut tmpbuf = $buf2type:: new( ) ;
8595
8696 let align: usize = core:: mem:: align_of:: <$buf2type>( ) ;
87- if lenminus64 >= 4096 {
97+ if len >= 4096 {
8898 let off = ( input. as_ptr( ) as usize ) % align;
8999 if off != 0 {
90100 let to_copy = align - off;
@@ -98,7 +108,10 @@ macro_rules! validate_utf8_basic_simd {
98108 idx += to_copy;
99109 }
100110 }
101- while idx < lenminus64 {
111+
112+ let rem = len - idx;
113+ let iter_lim = idx + ( rem - ( rem % 64 ) ) ;
114+ while idx < iter_lim {
102115 let input = SimdInput :: new( input. get_unchecked( idx as usize ..) ) ;
103116 input. check_utf8( & mut state) ;
104117 idx += SIMDINPUT_LENGTH ;
@@ -151,12 +164,11 @@ macro_rules! validate_utf8_compat_simd {
151164 const SIMDINPUT_LENGTH : usize = 64 ;
152165 let len = input. len( ) ;
153166 let mut state = SimdInput :: new_utf8_checking_state( ) ;
154- let lenminus64: usize = if len < 64 { 0 } else { len as usize - 64 } ;
155167 let mut idx: usize = 0 ;
156168 let mut tmpbuf = $buf2type:: new( ) ;
157169
158170 let align: usize = core:: mem:: align_of:: <$buf2type>( ) ;
159- if lenminus64 >= 4096 {
171+ if len >= 4096 {
160172 let off = ( input. as_ptr( ) as usize ) % align;
161173 if off != 0 {
162174 let to_copy = align - off;
@@ -174,7 +186,9 @@ macro_rules! validate_utf8_compat_simd {
174186 }
175187 }
176188
177- while idx < lenminus64 {
189+ let rem = len - idx;
190+ let iter_lim = idx + ( rem - ( rem % 64 ) ) ;
191+ while idx < iter_lim {
178192 let simd_input = SimdInput :: new( input. get_unchecked( idx as usize ..) ) ;
179193 simd_input. check_utf8( & mut state) ;
180194 if SimdInput :: check_utf8_errors( & state) {
0 commit comments