What I think in addition to what Atkinso writes: If you just strip arbitrary bytes that happen to be equal in value to the numeric value of ASCII control characters or whitespace, how can you be sure that you don’t destroy valid non-whitespace unicode symbols?

You can’t! This will work only of you have actually ASCII input.

    • HaraldvonBlauzahn@feddit.orgOP
      link
      fedilink
      arrow-up
      1
      arrow-down
      3
      ·
      4 days ago

      You are right with this. But still, in Rust, a vector of u8 is different from a sequence of unicode characters. This would not work in Python3 either, while it’d work in Python2.

      • brian@programming.dev
        link
        fedilink
        arrow-up
        2
        ·
        edit-2
        4 days ago

        yeah it’s incorrect bc it destroys multibyte characters, but no idea what you’re saying about u8 being a different type from unicode. the original code was reading bytes and converting them too? the typing isn’t the issue, you can still store utf8 as a series of bytes

        • Markaos@discuss.tchncs.de
          link
          fedilink
          arrow-up
          1
          ·
          4 days ago

          it’s incorrect bc it destroys multibyte characters

          It doesn’t. As the poster two levels up said, all bytes that don’t represent an ASCII character have the high bit set, even the follow-up bytes in multibyte sequences. So the condition b >= 32 will match and preserve them.

      • FizzyOrange@programming.dev
        link
        fedilink
        arrow-up
        1
        ·
        4 days ago

        No, the filter is correct even for UTF-8. Any ASCII character is exactly unchanged in UTF-8 (part of the reason it is popular). Since this code only filters out ASCII characters it works fine with ASCII or UTF-8.