ruby / csv

CSV Reading and Writing
https://ruby.github.io/csv/
BSD 2-Clause "Simplified" License
178 stars 113 forks source link

Simplify odd division by two in Parser#parse_quoted_column_value #249

Closed Maumagnaguagno closed 2 years ago

Maumagnaguagno commented 2 years ago

Division by two of odd n_quotes (integer >= 0) does not need to decrement one to generate the same result as even numbers. Division by two already removes the least significant bit that is being subtracted: (0bxxx1 - 1) / 2 => 0bxxx0 / 2 => 0bxxx.

olleolleolle commented 2 years ago

@Maumagnaguagno They may give the same result, but my guess is that the one operating on even numbers works quicker.

Maumagnaguagno commented 2 years ago

Yes, the formula operating on even numbers is "quicker", n_quotes / 2, than the one for odds, (n_quotes - 1) / 2, which is one of the reasons I want to use the even formula for both evens and odds. The main reason is to simplify the code and make clearer that what is pushed to value is the same for both cases.

kou commented 2 years ago

Thanks!