Skip to content

Commit 6fae4d3

Browse files
nyurikdanielrh
authored andcommitted
Convert while loops into for loops
I used a very powerful tool [coccinelle](https://gitlab.inria.fr/coccinelle/coccinelleforrust) (its C version is used for Linux kernel refactorings). Highly recommend installing it as it has a potential to simplify refactorings while not dealing with regex errors. To install it, run ```sh cargo install --git https://gitlab.inria.fr/coccinelle/coccinelleforrust ``` Afterward, create this file as `target/repl2.cocci` with the following content. Using `target/` ensures that it will be ignored by git. ```diff @@ expression i, end; @@ -i = 0usize; -while i < end +for i in 0usize..end -{ { ... } - i = i.wrapping_add(1); -} ``` and run this command from the root of the repo: ```shell cfr -c target/repl2.cocci src/ --apply cargo fmt --all ``` Afterwards, I compiled and removed all unused index variables.
1 parent b42042b commit 6fae4d3

14 files changed

+828
-1266
lines changed

src/enc/backward_references/hq.rs

Lines changed: 117 additions & 168 deletions
Large diffs are not rendered by default.

src/enc/bit_cost.rs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -291,28 +291,20 @@ pub fn BrotliPopulationCost<HistogramType: SliceWrapper<u32> + CostAccessors>(
291291
if count == 4i32 {
292292
let mut histo: [u32; 4] = [0; 4];
293293

294-
i = 0usize;
295-
while i < 4usize {
296-
{
297-
histo[i] = histogram.slice()[s[i]];
298-
}
299-
i = i.wrapping_add(1);
294+
for i in 0usize..4usize {
295+
histo[i] = histogram.slice()[s[i]];
300296
}
301-
i = 0usize;
302-
while i < 4usize {
303-
{
304-
let mut j: usize;
305-
j = i.wrapping_add(1);
306-
while j < 4usize {
307-
{
308-
if histo[j] > histo[i] {
309-
histo.swap(j, i);
310-
}
297+
for i in 0usize..4usize {
298+
let mut j: usize;
299+
j = i.wrapping_add(1);
300+
while j < 4usize {
301+
{
302+
if histo[j] > histo[i] {
303+
histo.swap(j, i);
311304
}
312-
j = j.wrapping_add(1);
313305
}
306+
j = j.wrapping_add(1);
314307
}
315-
i = i.wrapping_add(1);
316308
}
317309
let h23: u32 = histo[2].wrapping_add(histo[3]);
318310
let histomax: u32 = brotli_max_uint32_t(h23, histo[0]);

0 commit comments

Comments
 (0)