@@ -509,7 +509,7 @@ impl<'a> Rope<'a> {
509
509
Lines {
510
510
iter : match & self . repr {
511
511
Repr :: Light ( s) => LinesEnum :: Light ( s) ,
512
- Repr :: Full ( data) => LinesEnum :: Complex {
512
+ Repr :: Full ( data) => LinesEnum :: Full {
513
513
iter : data,
514
514
in_chunk_byte_idx : 0 ,
515
515
chunk_idx : 0 ,
@@ -554,7 +554,7 @@ impl Hash for Rope<'_> {
554
554
555
555
enum LinesEnum < ' a , ' b > {
556
556
Light ( & ' b str ) ,
557
- Complex {
557
+ Full {
558
558
iter : & ' a Vec < ( & ' b str , usize ) > ,
559
559
in_chunk_byte_idx : usize ,
560
560
chunk_idx : usize ,
@@ -605,7 +605,7 @@ impl<'a> Iterator for Lines<'_, 'a> {
605
605
}
606
606
Lines {
607
607
iter :
608
- LinesEnum :: Complex {
608
+ LinesEnum :: Full {
609
609
iter : chunks,
610
610
ref mut in_chunk_byte_idx,
611
611
ref mut chunk_idx,
@@ -777,6 +777,39 @@ impl Iterator for CharIndices<'_, '_> {
777
777
}
778
778
}
779
779
}
780
+
781
+ #[ inline( always) ]
782
+ fn size_hint ( & self ) -> ( usize , Option < usize > ) {
783
+ match & self . iter {
784
+ CharIndicesEnum :: Light { iter } => ( 0 , None ) ,
785
+ CharIndicesEnum :: Full {
786
+ chunks,
787
+ char_indices,
788
+ chunk_index,
789
+ } => {
790
+ ( 0 , None )
791
+ // if *chunk_index >= chunks.len() {
792
+ // if char_indices.is_empty() {
793
+ // return (0, Some(0));
794
+ // }
795
+ // return (char_indices.len(), Some(char_indices.len()));
796
+ // }
797
+ //
798
+ // let Some(total) = chunks.last().map(|(s, l)| *l + s.len()) else {
799
+ // return (0, Some(0));
800
+ // };
801
+ //
802
+ // let (_, prev) = chunks[*chunk_index];
803
+ // // SAFETY: The previous length is guaranteed be less than or equal to the latter one.
804
+ // let remaining = total - prev;
805
+ //
806
+ // (
807
+ // (remaining + 3) / 4 + char_indices.len(),
808
+ // Some(remaining + char_indices.len()),
809
+ // )
810
+ }
811
+ }
812
+ }
780
813
}
781
814
782
815
impl Default for Rope < ' _ > {
@@ -1226,25 +1259,38 @@ mod tests {
1226
1259
1227
1260
#[ test]
1228
1261
fn char_indices ( ) {
1262
+ // The algorithm is the same as the one used in `std::str::CharIndices::size_hint`.
1263
+ macro_rules! lo {
1264
+ ( $expr: expr) => {
1265
+ ( $expr + 3 ) / 4
1266
+ } ;
1267
+ }
1268
+
1229
1269
let mut a = Rope :: new ( ) ;
1230
1270
a. add ( "abc" ) ;
1231
1271
a. add ( "def" ) ;
1232
1272
assert_eq ! (
1233
1273
a. char_indices( ) . collect:: <Vec <_>>( ) ,
1234
1274
"abcdef" . char_indices( ) . collect:: <Vec <_>>( )
1235
1275
) ;
1276
+ let len = "abcdef" . char_indices ( ) . size_hint ( ) . 1 . unwrap ( ) ;
1277
+ assert_eq ! ( a. char_indices( ) . size_hint( ) , ( lo!( len) , Some ( len) ) ) ;
1236
1278
1237
1279
let mut a = Rope :: new ( ) ;
1238
1280
a. add ( "こんにちは" ) ;
1239
1281
assert_eq ! (
1240
1282
a. char_indices( ) . collect:: <Vec <_>>( ) ,
1241
1283
"こんにちは" . char_indices( ) . collect:: <Vec <_>>( )
1242
1284
) ;
1285
+ let len = "こんにちは" . char_indices ( ) . size_hint ( ) . 1 . unwrap ( ) ;
1286
+ assert_eq ! ( a. char_indices( ) . size_hint( ) , ( lo!( len) , Some ( len) ) ) ;
1243
1287
a. add ( "世界" ) ;
1244
1288
assert_eq ! (
1245
1289
a. char_indices( ) . collect:: <Vec <_>>( ) ,
1246
1290
"こんにちは世界" . char_indices( ) . collect:: <Vec <_>>( )
1247
1291
) ;
1292
+ let len = "こんにちは世界" . char_indices ( ) . size_hint ( ) . 1 . unwrap ( ) ;
1293
+ assert_eq ! ( a. char_indices( ) . size_hint( ) , ( lo!( len) , Some ( len) ) ) ;
1248
1294
}
1249
1295
1250
1296
#[ test]
0 commit comments