TabStop Puzzle

Given a string that contains characters, spaces (0x20) and tabs (0x09) where a tab displays as moving to the next tabstop (which are equally spaced N characters apart)

Problem #1 :
change multiple spaces to the correct number of tabs, so that the VISUAL display remains the same

Problem #2 :
change single or multiple tabs to the correct number of spaces , so that the VISUAL display remains the same

These are two different transformations that could be applied to the text
Because the visual display needs to remain the same, any given tab may need to be converted to anywhere from 1 to N spaces, and vice-versa… So it it not a simple replaceall

LOL… seems I asked this same question over 4 years ago on TOF… but didn’t post the solution, nor reference to even what project it was… but did have enough clues to find the C version .

Now to convert to Swift, and hopefully make it work on a selected part of the entire file.