tr: Fix multiple ranges with different lengths (Michael Forney)

See his description below. Thanks Michael!

---
A bug was introduced in bc4c293fe5 causing the
range length for the next set to be used instead of the first one. This causes
issues when choosing the replacement rune when the ranges are of different
lengths.

Current behavior:

$ echo 1234 | tr 'a-f1-4' '1-6a-d'
56ab

Correct behavior:

$ echo 1234 | tr 'a-f1-4' '1-6a-d'
abcd

This also fixes range expressions in the form [a-z], which get encoded as four
ranges '[', 'a'..'z', ']', causing all a-z characters to get mapped to ']'. This
form is occasionally used in shell scripts, including the syscalltbl.sh script
used to build linux.
---
This commit is contained in:
Laslo Hunhold 2016-11-18 12:45:59 +01:00
parent d24ef864cb
commit fb11173926

2
tr.c
View File

@ -213,7 +213,7 @@ read:
}
if (argc == 1 && sflag)
goto write;
for (i = 0, off1 = 0; i < set1ranges; i++, off1 += rangelen(set1[i])) {
for (i = 0, off1 = 0; i < set1ranges; off1 += rangelen(set1[i]), i++) {
if (set1[i].start <= r && r <= set1[i].end) {
if (dflag) {
if (cflag)