1+ use std:: borrow:: Cow ;
2+ use std:: io:: BufRead ;
3+
14use memchr:: memchr_iter;
25
36use crate :: comment_block:: CommentBlock ;
47use crate :: parser_state:: line_difference_requires_newline;
58use crate :: types:: { LineNumber , SourceOffset } ;
6- use crate :: util:: u8_to_string;
79
810/// A vector of offsets in the source code where lines start, which
911/// we use to detect what line a given offset is one.
@@ -51,7 +53,7 @@ impl LineIndex {
5153pub struct FileComments {
5254 start_of_file_contiguous_comment_lines : Option < CommentBlock > ,
5355 /// A list of comments, sorted in order by `LineNumber`
54- other_comments : Vec < ( LineNumber , String ) > ,
56+ other_comments : Vec < ( LineNumber , Vec < u8 > ) > ,
5557 /// Sorted list of line numbers that contain Ruby code (not comments/blank)
5658 lines_with_ruby : Vec < LineNumber > ,
5759 last_lineno : LineNumber ,
@@ -96,7 +98,7 @@ impl FileComments {
9698 for comment in comments {
9799 file_comments. push_comment (
98100 line_index. get_line_number ( comment. location ( ) . start_offset ( ) ) as u64 ,
99- u8_to_string ( comment. text ( ) . trim_ascii_end ( ) ) ,
101+ comment. text ( ) . trim_ascii_end ( ) . to_vec ( ) ,
100102 ) ;
101103 file_comments
102104 . comment_start_offsets
@@ -146,7 +148,7 @@ impl FileComments {
146148 /// Add a new comment. If the beginning of this file is a comment block,
147149 /// each of those comment lines must be pushed before any other line, or
148150 /// the end of the block from the start of the file will be incorrectly calculated.
149- fn push_comment ( & mut self , line_number : u64 , l : String ) {
151+ fn push_comment ( & mut self , line_number : u64 , l : Vec < u8 > ) {
150152 match (
151153 & mut self . start_of_file_contiguous_comment_lines ,
152154 line_number,
@@ -158,10 +160,10 @@ impl FileComments {
158160 otherwise we won't know where the last line is" ,
159161 ) ;
160162 self . start_of_file_contiguous_comment_lines =
161- Some ( CommentBlock :: new ( 1 ..2 , vec ! [ l] ) ) ;
163+ Some ( CommentBlock :: new ( 1 ..2 , vec ! [ l. into ( ) ] ) ) ;
162164 }
163165 ( Some ( sled) , _) if sled. following_line_number ( ) == line_number => {
164- sled. add_line ( l) ;
166+ sled. add_line ( l. into ( ) ) ;
165167 }
166168 _ => {
167169 debug_assert ! (
@@ -220,21 +222,22 @@ impl FileComments {
220222 . other_comments
221223 . partition_point ( |( ln, _) | * ln <= line_number) ;
222224
223- let mut comment_block_with_spaces: Vec < String > = Vec :: new ( ) ;
225+ let mut comment_block_with_spaces = Vec :: new ( ) ;
224226 let mut last_line = None ;
225227
226228 if line_difference_requires_newline (
227229 self . other_comments . first ( ) . unwrap ( ) . 0 ,
228230 starting_line_number,
229231 ) {
230- comment_block_with_spaces. push ( String :: new ( ) ) ;
232+ comment_block_with_spaces. push ( b"" . into ( ) ) ;
231233 }
232234
233235 for ( index, comment_contents) in self . other_comments . drain ( ..split_point) {
236+ let comment_contents: Cow < ' _ , [ u8 ] > = Cow :: Owned ( comment_contents) ;
234237 if let Some ( last_line) = last_line
235238 && line_difference_requires_newline ( index, last_line)
236239 {
237- comment_block_with_spaces. push ( String :: new ( ) ) ;
240+ comment_block_with_spaces. push ( b"" . into ( ) ) ;
238241 }
239242 let line_count = comment_contents. lines ( ) . count ( ) as u64 ;
240243 last_line = Some ( index + line_count - 1 ) ;
@@ -243,7 +246,7 @@ impl FileComments {
243246
244247 if line_number > last_line. unwrap ( ) + 1 {
245248 last_line = Some ( line_number) ;
246- comment_block_with_spaces. push ( String :: new ( ) ) ;
249+ comment_block_with_spaces. push ( b"" . into ( ) ) ;
247250 }
248251
249252 Some ( (
0 commit comments