Skip to content

Use checked_next_power_of_two from std instead of custom method #889

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use regex::Regex;

use Indent;
use config::Config;
use utils::round_up_to_power_of_two;

use MIN_STRING;

Expand All @@ -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();
Expand Down
35 changes: 0 additions & 35 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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));
}