@@ -8,9 +8,7 @@ use syntax_pos::{BytePos, Pos, Span};
88use rustc_lexer:: Base ;
99use rustc_lexer:: unescape;
1010
11- use std:: borrow:: Cow ;
1211use std:: char;
13- use std:: iter;
1412use std:: convert:: TryInto ;
1513use rustc_data_structures:: sync:: Lrc ;
1614use log:: debug;
@@ -181,18 +179,7 @@ impl<'a> StringReader<'a> {
181179 let string = self . str_from ( start) ;
182180 // comments with only more "/"s are not doc comments
183181 let tok = if is_doc_comment ( string) {
184- let mut idx = 0 ;
185- loop {
186- idx = match string[ idx..] . find ( '\r' ) {
187- None => break ,
188- Some ( it) => idx + it + 1
189- } ;
190- if string[ idx..] . chars ( ) . next ( ) != Some ( '\n' ) {
191- self . err_span_ ( start + BytePos ( idx as u32 - 1 ) ,
192- start + BytePos ( idx as u32 ) ,
193- "bare CR not allowed in doc-comment" ) ;
194- }
195- }
182+ self . forbid_bare_cr ( start, string, "bare CR not allowed in doc-comment" ) ;
196183 token:: DocComment ( Symbol :: intern ( string) )
197184 } else {
198185 token:: Comment
@@ -217,15 +204,10 @@ impl<'a> StringReader<'a> {
217204 }
218205
219206 let tok = if is_doc_comment {
220- let has_cr = string. contains ( '\r' ) ;
221- let string = if has_cr {
222- self . translate_crlf ( start,
223- string,
224- "bare CR not allowed in block doc-comment" )
225- } else {
226- string. into ( )
227- } ;
228- token:: DocComment ( Symbol :: intern ( & string[ ..] ) )
207+ self . forbid_bare_cr ( start,
208+ string,
209+ "bare CR not allowed in block doc-comment" ) ;
210+ token:: DocComment ( Symbol :: intern ( string) )
229211 } else {
230212 token:: Comment
231213 } ;
@@ -516,49 +498,16 @@ impl<'a> StringReader<'a> {
516498 & self . src [ self . src_index ( start) ..self . src_index ( end) ]
517499 }
518500
519- /// Converts CRLF to LF in the given string, raising an error on bare CR.
520- fn translate_crlf < ' b > ( & self , start : BytePos , s : & ' b str , errmsg : & ' b str ) -> Cow < ' b , str > {
521- let mut chars = s. char_indices ( ) . peekable ( ) ;
522- while let Some ( ( i, ch) ) = chars. next ( ) {
523- if ch == '\r' {
524- if let Some ( ( lf_idx, '\n' ) ) = chars. peek ( ) {
525- return translate_crlf_ ( self , start, s, * lf_idx, chars, errmsg) . into ( ) ;
526- }
527- let pos = start + BytePos ( i as u32 ) ;
528- let end_pos = start + BytePos ( ( i + ch. len_utf8 ( ) ) as u32 ) ;
529- self . err_span_ ( pos, end_pos, errmsg) ;
530- }
531- }
532- return s. into ( ) ;
533-
534- fn translate_crlf_ ( rdr : & StringReader < ' _ > ,
535- start : BytePos ,
536- s : & str ,
537- mut j : usize ,
538- mut chars : iter:: Peekable < impl Iterator < Item = ( usize , char ) > > ,
539- errmsg : & str )
540- -> String {
541- let mut buf = String :: with_capacity ( s. len ( ) ) ;
542- // Skip first CR
543- buf. push_str ( & s[ .. j - 1 ] ) ;
544- while let Some ( ( i, ch) ) = chars. next ( ) {
545- if ch == '\r' {
546- if j < i {
547- buf. push_str ( & s[ j..i] ) ;
548- }
549- let next = i + ch. len_utf8 ( ) ;
550- j = next;
551- if chars. peek ( ) . map ( |( _, ch) | * ch) != Some ( '\n' ) {
552- let pos = start + BytePos ( i as u32 ) ;
553- let end_pos = start + BytePos ( next as u32 ) ;
554- rdr. err_span_ ( pos, end_pos, errmsg) ;
555- }
556- }
557- }
558- if j < s. len ( ) {
559- buf. push_str ( & s[ j..] ) ;
560- }
561- buf
501+ fn forbid_bare_cr ( & self , start : BytePos , s : & str , errmsg : & str ) {
502+ let mut idx = 0 ;
503+ loop {
504+ idx = match s[ idx..] . find ( '\r' ) {
505+ None => break ,
506+ Some ( it) => idx + it + 1
507+ } ;
508+ self . err_span_ ( start + BytePos ( idx as u32 - 1 ) ,
509+ start + BytePos ( idx as u32 ) ,
510+ errmsg) ;
562511 }
563512 }
564513
0 commit comments