1
1
use std:: assert_matches:: debug_assert_matches;
2
2
use std:: fmt:: { self , Display , Write as _} ;
3
- use std:: mem;
4
3
use std:: sync:: LazyLock as Lazy ;
4
+ use std:: { ascii, mem} ;
5
5
6
6
use rustc_ast:: tokenstream:: TokenTree ;
7
7
use rustc_hir:: def:: { DefKind , Res } ;
@@ -24,7 +24,7 @@ use crate::clean::{
24
24
clean_middle_ty, inline,
25
25
} ;
26
26
use crate :: core:: DocContext ;
27
- use crate :: display:: Joined as _;
27
+ use crate :: display:: { Joined as _, MaybeDisplay as _ } ;
28
28
29
29
#[ cfg( test) ]
30
30
mod tests;
@@ -254,14 +254,7 @@ pub(crate) fn qpath_to_string(p: &hir::QPath<'_>) -> String {
254
254
fmt:: from_fn ( |f| {
255
255
segments
256
256
. iter ( )
257
- . map ( |seg| {
258
- fmt:: from_fn ( |f| {
259
- if seg. ident . name != kw:: PathRoot {
260
- write ! ( f, "{}" , seg. ident) ?;
261
- }
262
- Ok ( ( ) )
263
- } )
264
- } )
257
+ . map ( |seg| ( seg. ident . name != kw:: PathRoot ) . then_some ( seg. ident ) . maybe_display ( ) )
265
258
. joined ( "::" , f)
266
259
} )
267
260
. to_string ( )
@@ -391,30 +384,12 @@ pub(crate) fn print_evaluated_const(
391
384
} )
392
385
}
393
386
394
- fn format_integer_with_underscore_sep ( num : & str ) -> String {
395
- let num_chars: Vec < _ > = num. chars ( ) . collect ( ) ;
396
- let mut num_start_index = if num_chars. first ( ) == Some ( & '-' ) { 1 } else { 0 } ;
397
- let chunk_size = match & num. as_bytes ( ) [ num_start_index..] {
398
- [ b'0' , b'b' | b'x' , ..] => {
399
- num_start_index += 2 ;
400
- 4
401
- }
402
- [ b'0' , b'o' , ..] => {
403
- num_start_index += 2 ;
404
- let remaining_chars = num_chars. len ( ) - num_start_index;
405
- if remaining_chars <= 6 {
406
- // don't add underscores to Unix permissions like 0755 or 100755
407
- return num. to_string ( ) ;
408
- }
409
- 3
410
- }
411
- _ => 3 ,
412
- } ;
413
-
414
- num_chars[ ..num_start_index]
415
- . iter ( )
416
- . chain ( num_chars[ num_start_index..] . rchunks ( chunk_size) . rev ( ) . intersperse ( & [ '_' ] ) . flatten ( ) )
417
- . collect ( )
387
+ fn format_integer_with_underscore_sep ( num : u128 , is_negative : bool ) -> String {
388
+ let num = num. to_string ( ) ;
389
+ let chars = num. as_ascii ( ) . unwrap ( ) ;
390
+ let mut result = if is_negative { "-" . to_string ( ) } else { String :: new ( ) } ;
391
+ result. extend ( chars. rchunks ( 3 ) . rev ( ) . intersperse ( & [ ascii:: Char :: LowLine ] ) . flatten ( ) ) ;
392
+ result
418
393
}
419
394
420
395
fn print_const_with_custom_print_scalar < ' tcx > (
@@ -428,7 +403,10 @@ fn print_const_with_custom_print_scalar<'tcx>(
428
403
match ( ct, ct. ty ( ) . kind ( ) ) {
429
404
( mir:: Const :: Val ( mir:: ConstValue :: Scalar ( int) , _) , ty:: Uint ( ui) ) => {
430
405
let mut output = if with_underscores {
431
- format_integer_with_underscore_sep ( & int. to_string ( ) )
406
+ format_integer_with_underscore_sep (
407
+ int. assert_scalar_int ( ) . to_bits_unchecked ( ) ,
408
+ false ,
409
+ )
432
410
} else {
433
411
int. to_string ( )
434
412
} ;
@@ -445,7 +423,10 @@ fn print_const_with_custom_print_scalar<'tcx>(
445
423
. size ;
446
424
let sign_extended_data = int. assert_scalar_int ( ) . to_int ( size) ;
447
425
let mut output = if with_underscores {
448
- format_integer_with_underscore_sep ( & sign_extended_data. to_string ( ) )
426
+ format_integer_with_underscore_sep (
427
+ sign_extended_data. unsigned_abs ( ) ,
428
+ sign_extended_data. is_negative ( ) ,
429
+ )
449
430
} else {
450
431
sign_extended_data. to_string ( )
451
432
} ;
0 commit comments