Unicode Encoding in C 11 String Literals
C 11 introduced new character types and string literals to facilitate Unicode encoding. This article explores the usage and semantics of these new features, addressing the following questions:
Character References and String Types
Are "x/u/U" character references combinable with all string types?
Answer: No. "x" can be used in all strings, while "u" and "U" are restricted to UTF-encoded strings.
Fixed-Width Strings
Are string types fixed-width, or do "x/u/U" references expand the number of bytes?
Answer: While the number of code units in a string depends on the encoding, the number of elements in the array remains fixed.
UTF-Encoding Semantics for "u"" and "u8"" Strings
Do "u"" and "u8"" strings have encoding semantics, allowing for encoding of non-BMP code points?
Answer: Yes, "u"" creates UTF-16 encoded strings, while "u8"" creates UTF-8 encoded strings. Non-BMP code points will be encoded accordingly.
Use of Lone Surrogates with "u"
Can lone surrogates be written using "u"?
Answer: No, the specification prohibits using UTF-16 surrogate pairs (0xD800-0xDFFF) as code points for "u" or "U".
Encoding Awareness in String Functions
Are string functions encoding aware?
Answer: No, the standard string functions are not encoding aware and consider Unicode strings as sequences of code units, not code points. They cannot detect invalid byte sequences.
Conclusion
This exploration of Unicode encoding in C 11 string literals provides a comprehensive overview of the new character types, string literals, and their encoding semantics. It clarifies their usage and limitations, empowering developers to effectively utilize Unicode support in C 11.
The above is the detailed content of How Do C 11 String Literals Handle Unicode Encoding?. For more information, please follow other related articles on the PHP Chinese website!