So my algorithm could be:
A)..
C) If there are 1, 2 or 3 spaces BEFORE that TABULATION,
replace that TABULATION by 3,2 or 1 space(s)
C1)if TABULATION is the first or last character in a line
- replace it by 4 spaces
D)..
You HAVE to take into account the "visual" position any tab character is on. Especially your C1) remark may not be correct: if the last character on a line is a tab, but is it not on the first tab visual position, it it not a full tab.
Incrementally, you can replace tab characters only by the number of space characters up to the next visual tab position. A full tab can only be on position 1, ts + 1, 2 * ts + 1, n * ts + 1, where ts = tab size, and each next tab character can only be replaced if you take into account all visual positions that you have claimed by replacing each previous tab by the correct number of space characters.
Put simply, if your file exists of a letter and then a tab character, which is the last character, you replace that tab character by the (maximum/set) tab size minus 1.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You HAVE to take into account the "visual" position any tab character is on. Especially your C1) remark may not be correct: if the last character on a line is a tab, but is it not on the first tab visual position, it it not a full tab.
Incrementally, you can replace tab characters only by the number of space characters up to the next visual tab position. A full tab can only be on position 1, ts + 1, 2 * ts + 1, n * ts + 1, where ts = tab size, and each next tab character can only be replaced if you take into account all visual positions that you have claimed by replacing each previous tab by the correct number of space characters.
Put simply, if your file exists of a letter and then a tab character, which is the last character, you replace that tab character by the (maximum/set) tab size minus 1.