4 ms·
Yes, unpaired surrogates are invalid UTF16. No, just because languages allow representing invalid strings in their type system doesn’t mean they use some other
by Const-me 8y ago
Yes, unpaired surrogates are invalid UTF16.
No, just because languages allow representing invalid strings in their type system doesn’t mean they use some other encoding.
Ability to represent invalid data is often a good thing. English language allows representing all kind of garbage, but this is OK, see e.g. Jabberwocky by Lewis Carroll.
There are programming languages who enforce strings encoding and other constraints with strict type systems and/or runtimes. They are only practical for very limited set of problems. The majority of real-world software has to deal with invalid data: compilers do because users type invalid programs, any sufficiently complex systems do because they receive data from external components written in other languages or from I/O. I think not being able to represent invalid strings is a bug, not feature.
Mainstream i.e. practical languages allow representing invalid strings, provide functionality to validate & normalize them, and often raise runtime errors when you try to use them in a way that makes this a problem. For example, .NET throws an ArgumentException saying "Invalid Unicode code point found at index ##" when you try to normalize an invalid string.