Skip to content

Commit

Permalink
Convert while loops into for loops
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nyurik authored and danielrh committed Mar 18, 2024
1 parent b42042b commit 6fae4d3
Show file tree
Hide file tree
Showing 14 changed files with 828 additions and 1,266 deletions.
285 changes: 117 additions & 168 deletions src/enc/backward_references/hq.rs

Large diffs are not rendered by default.

28 changes: 10 additions & 18 deletions src/enc/bit_cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,28 +291,20 @@ pub fn BrotliPopulationCost<HistogramType: SliceWrapper<u32> + CostAccessors>(
if count == 4i32 {
let mut histo: [u32; 4] = [0; 4];

i = 0usize;
while i < 4usize {
{
histo[i] = histogram.slice()[s[i]];
}
i = i.wrapping_add(1);
for i in 0usize..4usize {
histo[i] = histogram.slice()[s[i]];
}
i = 0usize;
while i < 4usize {
{
let mut j: usize;
j = i.wrapping_add(1);
while j < 4usize {
{
if histo[j] > histo[i] {
histo.swap(j, i);
}
for i in 0usize..4usize {
let mut j: usize;
j = i.wrapping_add(1);
while j < 4usize {
{
if histo[j] > histo[i] {
histo.swap(j, i);
}
j = j.wrapping_add(1);
}
j = j.wrapping_add(1);
}
i = i.wrapping_add(1);
}
let h23: u32 = histo[2].wrapping_add(histo[3]);
let histomax: u32 = brotli_max_uint32_t(h23, histo[0]);
Expand Down
Loading

0 comments on commit 6fae4d3

Please sign in to comment.