Rollup merge of #132473 - ZhekaS:core_fmt_radix_no_panic, r=joboet

[core/fmt] Replace checked slice indexing by unchecked to support panic-free code

Fixes #126425

Replace the potentially panicking `[]` indexing with `get_unchecked()` to prevent linking with panic-related code.
This commit is contained in:
Jubilee 2024-11-05 01:34:23 -08:00 committed by GitHub
commit 57f64c67e0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -88,7 +88,10 @@ fn fmt_int<T: DisplayInt>(&self, mut x: T, f: &mut fmt::Formatter<'_>) -> fmt::R
};
}
}
let buf = &buf[curr..];
// SAFETY: `curr` is initialized to `buf.len()` and is only decremented, so it can't overflow. It is
// decremented exactly once for each digit. Since u128 is the widest fixed width integer format supported,
// the maximum number of digits (bits) is 128 for base-2, so `curr` won't underflow as well.
let buf = unsafe { buf.get_unchecked(curr..) };
// SAFETY: The only chars in `buf` are created by `Self::digit` which are assumed to be
// valid UTF-8
let buf = unsafe {