1- use crate :: { LineIndex , TextSize , WideChar } ;
1+ use crate :: { LineCol , LineIndex , TextSize , WideChar , WideEncoding , WideLineCol } ;
22
33macro_rules! test {
44 (
@@ -102,7 +102,7 @@ test!(
102102 case: multi_byte_with_new_lines,
103103 text: "01\t 345\n 789abcΔf01234567\u{07} 9\n bcΔf" ,
104104 lines: vec![ 7 , 27 ] ,
105- multi_byte_chars: vec![ ( 1 , ( 13 , 15 ) ) , ( 2 , ( 29 , 31 ) ) ] ,
105+ multi_byte_chars: vec![ ( 1 , ( 6 , 8 ) ) , ( 2 , ( 2 , 4 ) ) ] ,
106106) ;
107107
108108test ! (
@@ -118,3 +118,27 @@ test!(
118118 lines: vec![ 16 ] ,
119119 multi_byte_chars: vec![ ] ,
120120) ;
121+
122+ #[ test]
123+ fn test_try_line_col ( ) {
124+ let text = "\n \n \n \n \n 宽3456" ;
125+ assert_eq ! ( & text[ 5 ..8 ] , "宽" ) ;
126+ assert_eq ! ( & text[ 11 ..12 ] , "6" ) ;
127+ let line_index = LineIndex :: new ( text) ;
128+ let before_6 = TextSize :: from ( 11 ) ;
129+ let line_col = line_index. try_line_col ( before_6) ;
130+ assert_eq ! ( line_col, Some ( LineCol { line: 5 , col: 6 } ) ) ;
131+ }
132+
133+ #[ test]
134+ fn test_to_wide ( ) {
135+ let text = "\n \n \n \n \n 宽3456" ;
136+ assert_eq ! ( & text[ 5 ..8 ] , "宽" ) ;
137+ assert_eq ! ( & text[ 11 ..12 ] , "6" ) ;
138+ let line_index = LineIndex :: new ( text) ;
139+ let before_6 = TextSize :: from ( 11 ) ;
140+ let line_col = line_index. try_line_col ( before_6) ;
141+ assert_eq ! ( line_col, Some ( LineCol { line: 5 , col: 6 } ) ) ;
142+ let wide_line_col = line_index. to_wide ( WideEncoding :: Utf16 , line_col. unwrap ( ) ) ;
143+ assert_eq ! ( wide_line_col, Some ( WideLineCol { line: 5 , col: 4 } ) ) ;
144+ }
0 commit comments