Skip to content

Commit d3b18d0

Browse files
committed
Merge pull request #889 from kamalmarhubi/next-power-of-two
Use checked_next_power_of_two from std instead of custom method
2 parents 11391df + 8168c7c commit d3b18d0

File tree

2 files changed

+3
-37
lines changed

2 files changed

+3
-37
lines changed

src/string.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use regex::Regex;
1515

1616
use Indent;
1717
use config::Config;
18-
use utils::round_up_to_power_of_two;
1918

2019
use MIN_STRING;
2120

@@ -41,7 +40,9 @@ pub fn rewrite_string<'a>(orig: &str, fmt: &StringFormat<'a>) -> Option<String>
4140
let punctuation = ":,;.";
4241

4342
let mut cur_start = 0;
44-
let mut result = String::with_capacity(round_up_to_power_of_two(stripped_str.len()));
43+
let mut result = String::with_capacity(stripped_str.len()
44+
.checked_next_power_of_two()
45+
.unwrap_or(usize::max_value()));
4546
result.push_str(fmt.opener);
4647

4748
let ender_length = fmt.line_end.len();

src/utils.rs

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -180,33 +180,6 @@ pub fn trim_newlines(input: &str) -> &str {
180180
}
181181
}
182182

183-
#[inline]
184-
#[cfg(target_pointer_width="64")]
185-
// Based on the trick layed out at
186-
// http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
187-
pub fn round_up_to_power_of_two(mut x: usize) -> usize {
188-
x = x.wrapping_sub(1);
189-
x |= x >> 1;
190-
x |= x >> 2;
191-
x |= x >> 4;
192-
x |= x >> 8;
193-
x |= x >> 16;
194-
x |= x >> 32;
195-
x.wrapping_add(1)
196-
}
197-
198-
#[inline]
199-
#[cfg(target_pointer_width="32")]
200-
pub fn round_up_to_power_of_two(mut x: usize) -> usize {
201-
x = x.wrapping_sub(1);
202-
x |= x >> 1;
203-
x |= x >> 2;
204-
x |= x >> 4;
205-
x |= x >> 8;
206-
x |= x >> 16;
207-
x.wrapping_add(1)
208-
}
209-
210183
// Macro for deriving implementations of Decodable for enums
211184
#[macro_export]
212185
macro_rules! impl_enum_decodable {
@@ -344,11 +317,3 @@ fn bin_search_test() {
344317
assert_eq!(Some(()), binary_search(4, 125, &closure));
345318
assert_eq!(None, binary_search(6, 100, &closure));
346319
}
347-
348-
#[test]
349-
fn power_rounding() {
350-
assert_eq!(0, round_up_to_power_of_two(0));
351-
assert_eq!(1, round_up_to_power_of_two(1));
352-
assert_eq!(64, round_up_to_power_of_two(33));
353-
assert_eq!(256, round_up_to_power_of_two(256));
354-
}

0 commit comments

Comments
 (0)