@@ -93,29 +93,30 @@ pub struct LineIndex {
93
93
impl LineIndex {
94
94
/// Returns a `LineIndex` for the `text`.
95
95
pub fn new ( text : & str ) -> LineIndex {
96
+ let mut newlines = Vec :: with_capacity ( 16 ) ;
96
97
let mut line_wide_chars = IntMap :: default ( ) ;
98
+
97
99
let mut wide_chars = Vec :: new ( ) ;
100
+ let mut cur_row = TextSize :: from ( 0 ) ;
101
+ let mut cur_col = TextSize :: from ( 0 ) ;
102
+ let mut line = 0 ;
98
103
99
- let mut newlines = Vec :: with_capacity ( 16 ) ;
100
104
newlines. push ( TextSize :: from ( 0 ) ) ;
101
105
102
- let mut cur_row = 0 . into ( ) ;
103
- let mut cur_col = 0 . into ( ) ;
104
- let mut line = 0 ;
105
106
for c in text. chars ( ) {
106
107
let c_len = TextSize :: of ( c) ;
107
108
cur_row += c_len;
108
109
if c == '\n' {
109
110
newlines. push ( cur_row) ;
110
111
111
- // Save any utf-16 characters seen in the previous line
112
+ // Save any wide characters seen in the previous line
112
113
if !wide_chars. is_empty ( ) {
113
- line_wide_chars
114
- . insert ( line, std :: mem :: take ( & mut wide_chars ) . into_boxed_slice ( ) ) ;
114
+ let cs = std :: mem :: take ( & mut wide_chars ) . into_boxed_slice ( ) ;
115
+ line_wide_chars . insert ( line, cs ) ;
115
116
}
116
117
117
118
// Prepare for processing the next line
118
- cur_col = 0 . into ( ) ;
119
+ cur_col = TextSize :: from ( 0 ) ;
119
120
line += 1 ;
120
121
continue ;
121
122
}
@@ -127,7 +128,7 @@ impl LineIndex {
127
128
cur_col += c_len;
128
129
}
129
130
130
- // Save any utf-16 characters seen in the last line
131
+ // Save any wide characters seen in the last line
131
132
if !wide_chars. is_empty ( ) {
132
133
line_wide_chars. insert ( line, wide_chars. into_boxed_slice ( ) ) ;
133
134
}
@@ -136,6 +137,10 @@ impl LineIndex {
136
137
}
137
138
138
139
/// Transforms the `TextSize` into a `LineCol`.
140
+ ///
141
+ /// # Panics
142
+ ///
143
+ /// If the offset is invalid.
139
144
pub fn line_col ( & self , offset : TextSize ) -> LineCol {
140
145
let line = self . newlines . partition_point ( |& it| it <= offset) - 1 ;
141
146
let line_start_offset = self . newlines [ line] ;
0 commit comments