Merge pull request #889 from kamalmarhubi/next-power-of-two
Use checked_next_power_of_two from std instead of custom method
This commit is contained in:
commit
d3b18d0b45
2 changed files with 3 additions and 37 deletions
|
@ -15,7 +15,6 @@ use regex::Regex;
|
|||
|
||||
use Indent;
|
||||
use config::Config;
|
||||
use utils::round_up_to_power_of_two;
|
||||
|
||||
use MIN_STRING;
|
||||
|
||||
|
@ -41,7 +40,9 @@ pub fn rewrite_string<'a>(orig: &str, fmt: &StringFormat<'a>) -> Option<String>
|
|||
let punctuation = ":,;.";
|
||||
|
||||
let mut cur_start = 0;
|
||||
let mut result = String::with_capacity(round_up_to_power_of_two(stripped_str.len()));
|
||||
let mut result = String::with_capacity(stripped_str.len()
|
||||
.checked_next_power_of_two()
|
||||
.unwrap_or(usize::max_value()));
|
||||
result.push_str(fmt.opener);
|
||||
|
||||
let ender_length = fmt.line_end.len();
|
||||
|
|
35
src/utils.rs
35
src/utils.rs
|
@ -180,33 +180,6 @@ pub fn trim_newlines(input: &str) -> &str {
|
|||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[cfg(target_pointer_width="64")]
|
||||
// Based on the trick layed out at
|
||||
// http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
|
||||
pub fn round_up_to_power_of_two(mut x: usize) -> usize {
|
||||
x = x.wrapping_sub(1);
|
||||
x |= x >> 1;
|
||||
x |= x >> 2;
|
||||
x |= x >> 4;
|
||||
x |= x >> 8;
|
||||
x |= x >> 16;
|
||||
x |= x >> 32;
|
||||
x.wrapping_add(1)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[cfg(target_pointer_width="32")]
|
||||
pub fn round_up_to_power_of_two(mut x: usize) -> usize {
|
||||
x = x.wrapping_sub(1);
|
||||
x |= x >> 1;
|
||||
x |= x >> 2;
|
||||
x |= x >> 4;
|
||||
x |= x >> 8;
|
||||
x |= x >> 16;
|
||||
x.wrapping_add(1)
|
||||
}
|
||||
|
||||
// Macro for deriving implementations of Decodable for enums
|
||||
#[macro_export]
|
||||
macro_rules! impl_enum_decodable {
|
||||
|
@ -344,11 +317,3 @@ fn bin_search_test() {
|
|||
assert_eq!(Some(()), binary_search(4, 125, &closure));
|
||||
assert_eq!(None, binary_search(6, 100, &closure));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn power_rounding() {
|
||||
assert_eq!(0, round_up_to_power_of_two(0));
|
||||
assert_eq!(1, round_up_to_power_of_two(1));
|
||||
assert_eq!(64, round_up_to_power_of_two(33));
|
||||
assert_eq!(256, round_up_to_power_of_two(256));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue