📄 mbyte.texi
字号:
@node Extended Characters, Locales, String and Array Utilities, Top@chapter Extended CharactersA number of languages use character sets that are larger than the rangeof values of type @code{char}. Japanese and Chinese are probably themost familiar examples.The GNU C library includes support for two mechanisms for dealing withextended character sets: multibyte characters and wide characters. Thischapter describes how to use these mechanisms, and the functions forconverting between them.@cindex extended character setsThe behavior of the functions in this chapter is affected by the currentlocale for character classification---the @code{LC_CTYPE} category; see@ref{Locale Categories}. This choice of locale selects which multibytecode is used, and also controls the meanings and characteristics of widecharacter codes.@menu* Extended Char Intro:: Multibyte codes versus wide characters.* Locales and Extended Chars:: The locale selects the character codes.* Multibyte Char Intro:: How multibyte codes are represented.* Wide Char Intro:: How wide characters are represented.* Wide String Conversion:: Converting wide strings to multibyte code and vice versa.* Length of Char:: how many bytes make up one multibyte char.* Converting One Char:: Converting a string character by character.* Example of Conversion:: Example showing why converting one character at a time may be useful.* Shift State:: Multibyte codes with "shift characters".@end menu@node Extended Char Intro, Locales and Extended Chars, , Extended Characters@section Introduction to Extended CharactersYou can represent extended characters in either of two ways:@itemize @bullet@itemAs @dfn{multibyte characters} which can be embedded in an ordinarystring, an array of @code{char} objects. Their advantage is that manyprograms and operating systems can handle occasional multibytecharacters scattered among ordinary ASCII characters, without anychange.@item@cindex wide charactersAs @dfn{wide characters}, which are like ordinary characters except thatthey occupy more bits. The wide character data type, @code{wchar_t},has a range large enough to hold extended character codes as well asold-fashioned ASCII codes.An advantage of wide characters is that each character is a single dataobject, just like ordinary ASCII characters. There are a fewdisadvantages:@itemize @bullet@itemEach existing program must be modified and recompiled to make it usewide characters.@itemFiles of wide characters cannot be read by programs that expect ordinarycharacters.@end itemize@end itemizeTypically, you use the multibyte character representation as part of theexternal program interface, such as reading or writing text to files.However, it's usually easier to perform internal manipulations onstrings containing extended characters on arrays of @code{wchar_t}objects, since the uniform representation makes most editing operationseasier. If you do use multibyte characters for files and widecharacters for internal operations, you need to convert between themwhen you read and write data.If your system supports extended characters, then it supports them bothas multibyte characters and as wide characters. The library includesfunctions you can use to convert between the two representations.These functions are described in this chapter.@node Locales and Extended Chars, Multibyte Char Intro, Extended Char Intro, Extended Characters@section Locales and Extended CharactersA computer system can support more than one multibyte character code,and more than one wide character code. The user controls the choice ofcodes through the current locale for character classification(@pxref{Locales}). Each locale specifies a particular multibytecharacter code and a particular wide character code. The choice of localeinfluences the behavior of the conversion functions in the library.Some locales support neither wide characters nor nontrivial multibytecharacters. In these locales, the library conversion functions stillwork, even though what they do is basically trivial.If you select a new locale for character classification, the internalshift state maintained by these functions can become confused, so it'snot a good idea to change the locale while you are in the middle ofprocessing a string.@node Multibyte Char Intro, Wide Char Intro, Locales and Extended Chars, Extended Characters@section Multibyte Characters@cindex multibyte charactersIn the ordinary ASCII code, a sequence of characters is a sequence ofbytes, and each character is one byte. This is very simple, butallows for only 256 distinct characters.In a @dfn{multibyte character code}, a sequence of characters is asequence of bytes, but each character may occupy one or more consecutivebytes of the sequence.@cindex basic byte sequenceThere are many different ways of designing a multibyte character code;different systems use different codes. To specify a particular codemeans designating the @dfn{basic} byte sequences---those which representa single character---and what characters they stand for. A code that acomputer can actually use must have a finite number of these basicsequences, and typically none of them is more than a few characterslong.These sequences need not all have the same length. In fact, many ofthem are just one byte long. Because the basic ASCII characters in therange from @code{0} to @code{0177} are so important, they stand forthemselves in all multibyte character codes. That is to say, a bytewhose value is @code{0} through @code{0177} is always a character initself. The characters which are more than one byte must always startwith a byte in the range from @code{0200} through @code{0377}.The byte value @code{0} can be used to terminate a string, just as it isoften used in a string of ASCII characters.Specifying the basic byte sequences that represent single charactersautomatically gives meanings to many longer byte sequences, as more thanone character. For example, if the two byte sequence @code{0205 049}stands for the Greek letter alpha, then @code{0205 049 065} must standfor an alpha followed by an @samp{A} (ASCII code 065), and @code{0205 0490205 049} must stand for two alphas in a row.If any byte sequence can have more than one meaning as a sequence ofcharacters, then the multibyte code is ambiguous---and no good. Thecodes that systems actually use are all unambiguous.In most codes, there are certain sequences of bytes that have no meaningas a character or characters. These are called @dfn{invalid}.The simplest possible multibyte code is a trivial one:@quotationThe basic sequences consist of single bytes.@end quotationThis particular code is equivalent to not using multibyte characters atall. It has no invalid sequences. But it can handle only 256 differentcharacters.Here is another possible code which can handle 9376 differentcharacters:@quotationThe basic sequences consist of@itemize @bullet@itemsingle bytes with values in the range @code{0} through @code{0237}.@itemtwo-byte sequences, in which both of the bytes have values in the rangefrom @code{0240} through @code{0377}.@end itemize@end quotation@noindentThis code or a similar one is used on some systems to represent Japanesecharacters. The invalid sequences are those which consist of an oddnumber of consecutive bytes in the range from @code{0240} through@code{0377}.Here is another multibyte code which can handle more distinct extendedcharacters---in fact, almost thirty million:@quotationThe basic sequences consist of@itemize @bullet@itemsingle bytes with values in the range @code{0} through @code{0177}.@itemsequences of up to four bytes in which the first byte is in the rangefrom @code{0200} through @code{0237}, and the remaining bytes are in therange from @code{0240} through @code{0377}.@end itemize@end quotation@noindentIn this code, any sequence that starts with a byte in the rangefrom @code{0240} through @code{0377} is invalid.And here is another variant which has the advantage that removing thelast byte or bytes from a valid character can never produce anothervalid character. (This property is convenient when you want to searchstrings for particular characters.)@quotationThe basic sequences consist of@itemize @bullet@itemsingle bytes with values in the range @code{0} through @code{0177}.@itemtwo-byte sequences in which the first byte is in the range from@code{0200} through @code{0207}, and the second byte is in the rangefrom @code{0240} through @code{0377}.@itemthree-byte sequences in which the first byte is in the range from@code{0210} through @code{0217}, and the other bytes are in the rangefrom @code{0240} through @code{0377}.@itemfour-byte sequences in which the first byte is in the range from@code{0220} through @code{0227}, and the other bytes are in the rangefrom @code{0240} through @code{0377}.@end itemize@end quotation@noindentThe list of invalid sequences for this code is long and not worthstating in full; examples of invalid sequences include @code{0240} and@code{0220 0300 065}.The number of @emph{possible} multibyte codes is astronomical. But agiven computer system will support at most a few different codes. (Oneof these codes may allow for thousands of different characters.)Another computer system may support a completely different code. Thelibrary facilities described in this chapter are helpful because theypackage up the knowledge of the details of a particular computersystem's multibyte code, so your programs need not know them.You can use special standard macros to find out the maximum possiblenumber of bytes in a character in the currently selected multibytecode with @code{MB_CUR_MAX}, and the maximum for @emph{any} multibytecode supported on your computer with @code{MB_LEN_MAX}.@comment limits.h@comment ANSI@deftypevr Macro int MB_LEN_MAXThis is the maximum length of a multibyte character for any supportedlocale. It is defined in @file{limits.h}.@pindex limits.h@end deftypevr@comment stdlib.h@comment ANSI@deftypevr Macro int MB_CUR_MAXThis macro expands into a (possibly non-constant) positive integerexpression that is the maximum number of bytes in a multibyte characterin the current locale. The value is never greater than @code{MB_LEN_MAX}.@pindex stdlib.h@code{MB_CUR_MAX} is defined in @file{stdlib.h}.@end deftypevrNormally, each basic sequence in a particular character code stands forone character, the same character regardless of context. Some multibytecharacter codes have a concept of @dfn{shift state}; certain codes,called @dfn{shift sequences}, change to a different shift state, and themeaning of some or all basic sequences varies according to the currentshift state. In fact, the set of basic sequences might even bedifferent depending on the current shift state. @xref{Shift State}, formore information on handling this sort of code.What happens if you try to pass a string containing multibyte charactersto a function that doesn't know about them? Normally, such a functiontreats a string as a sequence of bytes, and interprets certain bytevalues specially; all other byte values are ``ordinary''. As long as amultibyte character doesn't contain any of the special byte values, thefunction should pass it through as if it were several ordinarycharacters.For example, let's figure out what happens if you use multibytecharacters in a file name. The functions such as @code{open} and@code{unlink} that operate on file names treat the name as a sequence ofbyte values, with @samp{/} as the only special value. Any other bytevalues are copied, or compared, in sequence, and all byte values aretreated alike. Thus, you may think of the file name as a sequence ofbytes or as a string containing multibyte characters; the same behaviormakes sense equally either way, provided no multibyte character containsa @samp{/}.@node Wide Char Intro, Wide String Conversion, Multibyte Char Intro, Extended Characters@section Wide Character Introduction@dfn{Wide characters} are much simpler than multibyte characters. Theyare simply characters with more than eight bits, so that they have roomfor more than 256 distinct codes. The wide character data type,@code{wchar_t}, has a range large enough to hold extended charactercodes as well as old-fashioned ASCII codes.An advantage of wide characters is that each character is a single dataobject, just like ordinary ASCII characters. Wide characters also havesome disadvantages:@itemize @bullet@itemA program must be modified and recompiled in order to use widecharacters at all.@itemFiles of wide characters cannot be read by programs that expect ordinarycharacters.@end itemizeWide character values @code{0} through @code{0177} are always identicalin meaning to the ASCII character codes. The wide character value zerois often used to terminate a string of wide characters, just as a singlebyte with value zero often terminates a string of ordinary characters.@comment stddef.h@comment ANSI@deftp {Data Type} wchar_tThis is the ``wide character'' type, an integer type whose range islarge enough to represent all distinct values in any extended characterset in the supported locales. @xref{Locales}, for more informationabout locales. This type is defined in the header file @file{stddef.h}.@pindex stddef.h@end deftpIf your system supports extended characters, then each extendedcharacter has both a wide character code and a corresponding multibytebasic sequence.@cindex code, character@cindex character codeIn this chapter, the term @dfn{code} is used to refer to a singleextended character object to emphasize the distinction from the@code{char} data type.@node Wide String Conversion, Length of Char, Wide Char Intro, Extended Characters@section Conversion of Extended Strings@cindex extended strings, converting representations@cindex converting extended strings@pindex stdlib.hThe @code{mbstowcs} function converts a string of multibyte characters
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -