I have received a few close requests says its unclear what I am asking. For me its extremely clear what I am asking and I might have added a few extra thoughts on the issue, but my question is extremely direct. Here is the goal/thesis of my question:

How does one detects if a string is binary safe or not in go.

A function like:

IsBinarySafe(str) //returns true if its safe and false if its not.

Please ask clarifying comments if there is something unclear. I always take a lot of care and time to make good question, so put as much effort as I do to my question and help me make it better so everyone can get as much benefit as I do.

Any comment after this are just things I have thought or attempted to solve this:


I assumed that there must exist a library that already does this but had a tough time finding it. If there isn't one, how do you implement this?

I was thinking of some solution but wasn't really convinced they were good solutions. One of them was to iterate over the bytes, and have a hash map of all the illegal byte sequences. I also thought of maybe writing a regex with all the illegal strings but wasn't sure if that was a good solution. I also was not sure if a sequence of bytes from other languages counted as binary safe. Say the typical golang example:

世界

Would:

IsBinarySafe(世界) //true or false?

Would it return true or false? I was assuming that all binary safe string should only use 1 byte. So iterating over it in the following way:

const nihongo = "日本語abc日本語"
    for i, w := 0, 0; i < len(nihongo); i += w {
        runeValue, width := utf8.DecodeRuneInString(nihongo[i:])
        fmt.Printf("%#U starts at byte position %d
", runeValue, i)
        w = width
    }

and returning false whenever the width was great than 1. These are just some ideas I had just in case there wasn't a library for something like this already but I wasn't sure.