@@ -22,15 +22,15 @@ pub struct TextIndex {
2222 /// Byte offset from the start of the document
2323 pub utf8 : usize ,
2424 /// Line number (0-based)
25- pub line : usize ,
25+ pub line : u32 ,
2626 /// Column offset in bytes (0-based)
27- pub col_utf8 : usize ,
27+ pub col_utf8 : u32 ,
2828 /// Column offset in UTF-16 code units (0-based)
29- pub col_utf16 : usize ,
29+ pub col_utf16 : u32 ,
3030 /// Column offset in UTF-32 code points (0-based)
3131 ///
3232 /// This is the same as Rust's [`char`] count.
33- pub col_utf32 : usize ,
33+ pub col_utf32 : u32 ,
3434}
3535
3636impl TextIndex {
@@ -81,9 +81,10 @@ impl TextIndex {
8181 self . col_utf16 = 0 ;
8282 self . col_utf32 = 0 ;
8383 }
84+ #[ expect( clippy:: cast_possible_truncation) ]
8485 _ => {
85- self . col_utf8 += bytes;
86- self . col_utf16 += c. len_utf16 ( ) ;
86+ self . col_utf8 += bytes as u32 ;
87+ self . col_utf16 += c. len_utf16 ( ) as u32 ;
8788 self . col_utf32 += 1 ;
8889 }
8990 }
@@ -103,9 +104,10 @@ impl TextIndex {
103104 self . col_utf16 = 0 ;
104105 self . col_utf32 = 0 ;
105106 }
107+ #[ expect( clippy:: cast_possible_truncation) ]
106108 _ => {
107- self . col_utf8 += bytes;
108- self . col_utf16 += c. len_utf16 ( ) ;
109+ self . col_utf8 += bytes as u32 ;
110+ self . col_utf16 += c. len_utf16 ( ) as u32 ;
109111 self . col_utf32 += 1 ;
110112 }
111113 }
@@ -114,7 +116,7 @@ impl TextIndex {
114116 /// Advance this index according to the `Advance` parameter.
115117 #[ inline]
116118 fn advance_by ( & mut self , advance : & Advance ) {
117- self . utf8 += advance. bytes ;
119+ self . utf8 += advance. bytes as usize ;
118120 self . line += advance. lines ;
119121 // ASCII-only path: 1 byte = 1 UTF-16 code unit = 1 code point
120122 match advance. column {
@@ -153,15 +155,15 @@ impl Ord for TextIndex {
153155/// The type of operation to perform on the `TextIndex`'s `column` field
154156#[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
155157enum Column {
156- Increment ( usize ) ,
157- Set ( usize ) ,
158+ Increment ( u32 ) ,
159+ Set ( u32 ) ,
158160}
159161
160162/// An update to perform on `TextIndex` after scanning a chunk of the input text
161163#[ derive( Debug , Clone , PartialEq , Eq ) ]
162164struct Advance {
163- bytes : usize ,
164- lines : usize ,
165+ bytes : u32 ,
166+ lines : u32 ,
165167 column : Column ,
166168}
167169
@@ -233,18 +235,17 @@ impl From<[i8; SIMD_LANES]> for Advance {
233235 /// increment the column count) from the number of bytes on the last line which we calculated before. This number
234236 /// is the new value of the `column` field of `TextIndex`.
235237 #[ inline]
238+ #[ expect( clippy:: cast_possible_truncation, clippy:: cast_possible_wrap) ]
236239 fn from ( chunk : [ i8 ; SIMD_LANES ] ) -> Self {
237240 let bytes = i8x32:: new ( chunk) ;
238241 let nonascii_mask = bytes. simd_lt ( i8x32:: ZERO ) . to_bitmask ( ) ;
239- #[ expect( clippy:: cast_possible_wrap) ]
240242 let lf_bytes = i8x32:: splat ( b'\n' as i8 ) ;
241243 let mut lf_mask = bytes. simd_eq ( lf_bytes) . to_bitmask ( ) ;
242- #[ expect( clippy:: cast_possible_wrap) ]
243244 let cr_bytes = i8x32:: splat ( b'\r' as i8 ) ;
244245 let mut cr_mask = bytes. simd_eq ( cr_bytes) . to_bitmask ( ) ;
245246
246247 // ignore non-ASCII characters at the end
247- let n_ascii = nonascii_mask. trailing_zeros ( ) as usize ;
248+ let n_ascii = nonascii_mask. trailing_zeros ( ) ;
248249 if n_ascii == 0 {
249250 // there are not ASCII bytes at the start of the chunk
250251 return Advance {
@@ -253,15 +254,15 @@ impl From<[i8; SIMD_LANES]> for Advance {
253254 lines : 0 ,
254255 } ;
255256 }
256- let shift = SIMD_LANES - n_ascii; // this is < SIMD_LANES
257+ let shift = SIMD_LANES as u32 - n_ascii; // this is < SIMD_LANES
257258 lf_mask <<= shift;
258259 cr_mask <<= shift;
259260
260261 let mut n_lines = 0 ;
261262 let column = if lf_mask > 0 {
262263 // the chunk contains multiple lines, we ignore everything but the last line
263- n_lines = lf_mask. count_ones ( ) as usize ;
264- let n_last_line = lf_mask. leading_zeros ( ) as usize ;
264+ n_lines = lf_mask. count_ones ( ) ;
265+ let n_last_line = lf_mask. leading_zeros ( ) ;
265266 if n_last_line == 0 {
266267 // edge case where the last byte is \n
267268 return Advance {
@@ -271,10 +272,10 @@ impl From<[i8; SIMD_LANES]> for Advance {
271272 } ;
272273 }
273274 // we ignore the \r in the last line for the columns count
274- cr_mask >>= SIMD_LANES - n_last_line; // the shift amount is < SIMD_LANES
275- Column :: Set ( n_last_line - cr_mask. count_ones ( ) as usize )
275+ cr_mask >>= SIMD_LANES as u32 - n_last_line; // the shift amount is < SIMD_LANES
276+ Column :: Set ( n_last_line - cr_mask. count_ones ( ) )
276277 } else {
277- Column :: Increment ( n_ascii - cr_mask. count_ones ( ) as usize )
278+ Column :: Increment ( n_ascii - cr_mask. count_ones ( ) )
278279 } ;
279280 Advance {
280281 bytes : n_ascii,
0 commit comments