ErlangのUnicodeサポート


R 13 Aでは、ErlangがUnicodeのサポートに参加している.本明細書で説明するデータ型には、stdlib/unicode、stdlib/io、kerne l/fileを含むlist、binaryが含まれる.
Binary
Binaryのtype属性は、UTF 8、UTF 16、UTF 32のそれぞれに対応するutf関連type:utf 8、utf 16、utf 32を追加する.
Binary Constructing
Binary構築時にutf関連タイプを指定した場合、対応するintegerのValueは:0にある必要があります.16#D7FF, 16#E000..16#FFFD、または16#10000..16#10 FFFFの3区間のうち.そうでないと「bad argument」というメッセージが表示され、パラメータが間違っています.指定したutfタイプによって、同じデータで生成されるbinaryが異なります.
utf 8の場合、integerごとに1〜4文字が生成される.utf 16の場合、integerごとに2または4文字が生成される.utf 32の場合、integerごとに4文字が生成されます.
たとえば、unicodeが1024の文字Aを使用してbinaryを構築します.
1> <<1024/utf8>>.    
<<208,128>>
2> <<1024/utf16>>.
<<4,0>>
3> <<1024/utf32>>.
<<0,0,4,0>>

Binary Match
Binary Matchを実行する場合、utf関連のタイプを指定すると、変数が正常に一致すると、0が存在します.16#D7FF, 16#E000..16#FFFD、または16#10000..16#10 FFFFの3区間のinteger.
それはutfタイプによって異なり、bytesの数が異なる.
utf 8マッチング1-4 bytes(RFC-2279参照)
utf 16は2つまたは4つのbytesに一致する(RFC-2781参照)
utf 32マッチング4 bytes
例えば、上記の例を続けます.
4> Bin = <<1024/utf8>>.
<<208,128>>
5> <<U/utf8>> = Bin.
<<208,128>>
6> U.
1024

この例では,Uは2つのbytesに一致する.
utf関連タイプではunit specは指定できません
List
リストでは、各unicode文字はintegerで表されるため、latin 1のリストに比べてunicodeリストではelementの値が255より大きくてもよい.
次は有効なunicodeリストです:[10224,1025]
listからbinaryへの変換をunicodeモジュールで実現できます.
unicode module
まず、次のtype定義を参照してください.
unicode_binary() = binary() with characters encoded in UTF-8 coding standard
unicode_char() = integer() representing valid unicode codepoint
chardata() = charlist() | unicode_binary()
charlist() = [unicode_char() | unicode_binary() | charlist()]
a unicode_binary is allowed as the tail of the list
external_unicode_binary() = binary() with characters coded in a user specified Unicode encoding other than UTF-8 (UTF-16 or UTF-32)
external_chardata() = external_charlist() | external_unicode_binary()
external_charlist() = [unicode_char() | external_unicode_binary() | external_charlist()]
an external_unicode_binary is allowed as the tail of the list
latin1_binary() = binary() with characters coded in iso-latin-1
latin1_char() = integer() representing valid latin1 character (0-255)
latin1_chardata() = latin1_charlist() | latin1_binary()
latin1_charlist() = [latin1_char() | latin1_binary() | latin1_charlist()]
a latin1_binary is allowed as the tail of the list
unicode:charactersを呼び出すことができます.to_List/1 chardataまたはlatin 1_chardataまたはexternal_chardata()はunicode listに変換されます.
パラメータがlatin 1_の場合chardataでは、Dataパラメータはiodataです.返される結果リストでは、各elementはintegerです.デフォルトunicode:characters_to_List/1呼び出しunicode:characters_to_list(Data, unicode)
CharDataが他のタイプであれば、InEncoding typeを示すことができます.この関数が正常に実行された場合は{ok,List}を返し、失敗した場合は{error,list(),RestData}を返し、listは変換に成功した部分であり、RestDataはエラーが発生した位置である.
unicode:charactersを呼び出すこともできます.to_binary/1,chardataまたはlatin 1_chardataまたはexternal_chardata()はbinaryに変換されます.この関数とunicode:characters_to_Listは似ていますが、結果はbinaryとして保存されます.
データがlatin 1_の場合chardata,unicode:characters_to_binary/1とerlang:iolist_to_binary/1機能は同じ
unicodeモジュールにはbomに関連する2つの関数があり、bom指に基づいて対応するencodingタイプを返すか、encodingタイプに基づいて対応するbom値を生成することができます.ファイルを保存する際によく使用する.
Examples
1 utf 8で保存したファイルを開く
ファイルの内容は次の通りです.file:
[
{desc,「これはテストファイルです」},
{author, "litaocheng"}
].
フォーマットはerlang termで、保存時にutf 8符号化を選択します.
コードは次のとおりです.
%% read content from the file
test1() ->
    {ok, [Terms]} = file:consult("test.txt"),
    Desc = proplists:get_value(desc, Terms),
    _Author = proplists:get_value(author, Terms),
    
    % out put the Desc and Author
    DescUniBin = iolist_to_binary(Desc),
    DescUniList = unicode:characters_to_list(DescUniBin),
    io:format("desc bin : ~ts~ndesc bin : ~p~n",[DescUniBin, DescUniBin]),
    io:format("desc list: ~ts~ndesc list: ~p~n", [DescUniList, DescUniList]).

結果:
desc bin:これはテストファイルです.
desc bin : <<232,191,153,230,152,175,228,184,128,228,184,170,230,181,139,232,
             175,149,230,150,135,228,187,182>>
desc list:これはテストファイルです
desc list: [36825,26159,19968,20010,27979,35797,25991,20214]
まずリストからbinaryにコンテンツを変換し,DeschUniBinは対応するunicode binaryである.その後unicode:characters_to_List/1はunicode listの最終出力に変換されます.
unicode listのすべてのelementはintegerであり、unicode binaryのunicode stringはuft 8符号化を採用していることがわかります.
2、データをuft 8形式に保存する
%% save the binary in utf8 format
test2() ->
    [DescList] = io_lib:format("~ts", [" "]),
    DescBin = erlang:iolist_to_binary(DescList),
    DescList2 = unicode:characters_to_list(DescBin),
    List = lists:concat(["[{desc,\"", DescList2, "\"}, {author, \"litaocheng\"}]."]),
    Bin = unicode:characters_to_binary(List),
    io:format("bin is:~ts~n", [Bin]),
    file:write_file("test_out.txt", Bin).

Update:
2008.5.4:
[DescList] = io_lib:format("~ts",[「これはテストファイルです」)
erlang shellでのDescrListは、[36825261591996822001027979357972599120214]
ModuleファイルでDescrListは次のとおりです.
[232,191,153,230,152,175,228,184,128,228,184,170,230,181,139,232,175,
          149,230,150,135,228,187,182]