<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    ivaneeo's blog

    自由的力量,自由的生活。

      BlogJava :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
      669 Posts :: 0 Stories :: 64 Comments :: 0 Trackbacks

    勤奮的 litaocheng 同學(xué),在每日超負(fù)荷的加班工作之余,仍然刻苦學(xué)習(xí)筆耕不輟,為我們不斷帶來(lái)勁爆文章,這一篇《Erlang的Unicode支持》為我們介紹了 R13 的最新特性,也是最被大家期望的特性——內(nèi)置的 Unicode 支持。廢話少說(shuō),直接上正文。

    在R13A中, Erlang加入了對(duì)Unicode的支持。本文涉及到的數(shù)據(jù)類型包括:list, binary, 涉及到的模塊包括stdlib/unicode, stdlib/io, kernel/file。

    Binary

    Binary的type屬性增加了utf相關(guān)的type:utf8, utf16, utf32,其分別對(duì)應(yīng)UTF8, UTF16,UTF32編碼。

    Binary Constructing

    在Binary構(gòu)建時(shí), 如果指定了utf相關(guān)類型,那么對(duì)應(yīng)的integer的Value必須位于:0..16#D7FF, 16#E000..16#FFFD, 或者 16#10000..16#10FFFF這三個(gè)區(qū)間中。否則將會(huì)提示’bad argument’,參數(shù)錯(cuò)誤。根據(jù)指定的的utf類型不同,同一個(gè)數(shù)據(jù)產(chǎn)生的binary不同。

    對(duì)于utf8,每個(gè)integer生成1到4個(gè)字符;對(duì)于utf16,每個(gè)integer生成2或4個(gè)字符;對(duì)于utf32,每個(gè)integer生成4個(gè)字符。

    比如, 使用unicode為1024的字符A, 構(gòu)建一個(gè)binary:

    1<<1024/utf8>>.    
    <<208,128>>
    2<<1024/utf16>>.
    <<4,0>>
    3<<1024/utf32>>.
    <<0,0,4,0>>

    Binary Match

    當(dāng)進(jìn)行Binary Match時(shí),如果指定utf相關(guān)類型,變量成功匹配后,將擁有一個(gè)位于:0..16#D7FF, 16#E000..16#FFFD, 或者 16#10000..16#10FFFF這三個(gè)區(qū)間中的integer。

    其更具utf類型的不同,消耗(match)不同數(shù)目的bytes。

    • utf8匹配1-4個(gè)bytes(參考RFC-2279)
    • utf16匹配2 或 4 個(gè)bytes (參考 RFC-2781)
    • utf32匹配4個(gè) bytes

    比如:繼續(xù)我們上面的例子

    4Bin = <<1024/utf8>>.
    <<208,128>>
    5<<U/utf8>> = Bin.
    <<208,128>>
    6U.
    1024

    這個(gè)例子中,U匹配了2個(gè)bytes。對(duì)于utf相關(guān)類型,不能指定unit spec。

    List

    在list中,每個(gè)unicode字符采用integer來(lái)表示,因此與latin1的list相比,unicode list中,element的數(shù)值可以大于255。下面就是一個(gè)有效的unicode list: [1024, 1025]

    我們可以通過(guò)unicode 模塊實(shí)現(xiàn) list到binary的轉(zhuǎn)換。

    unicode module

    首先請(qǐng)參看下面的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

    我們可以調(diào)用unicode:characters_to_list/1 將chardata或latin1_chardata或external_chardata()轉(zhuǎn)化成一個(gè)unicode list。

    如果參數(shù)為latin1_chardata,那么Data參數(shù)就是一個(gè)iodata. 返回的結(jié)果list中,每個(gè)element為一個(gè)integer。默認(rèn)情況 unicode:characters_to_list/1調(diào)用unicode:characters_to_list(Data, unicode)

    如果我們的CharData為其他類型,我們可以指明InEncoding type。如果此函數(shù)執(zhí)行成功,返回{ok, List},如果失敗返回{error, list(), RestData}, 其中l(wèi)ist為轉(zhuǎn)化成功的部分,RestData為發(fā)生錯(cuò)誤的位置。

    我們也可以調(diào)用unicode:characters_to_binary/1,將chardata或latin1_chardata或 external_chardata()轉(zhuǎn)化成一個(gè)binary。這個(gè)函數(shù)和unicode:characters_to_list類似,只是結(jié)果保存為 binary。

    如果Data為latin1_chardata, 那么unicode:characters_to_binary/1和 erlang:iolist_to_binary/1功能相同

    unicode模塊中,還有兩個(gè)于bom相關(guān)的函數(shù),可以根據(jù)bom指返回對(duì)應(yīng)的encoding類型,也可以根據(jù)encoding類型生成對(duì)應(yīng)的bom值。其在保存文件時(shí),經(jīng)常使用.

    Examples

    1, 打開(kāi)utf8保存的文件

    文件內(nèi)容如下test.file:

    [
    desc"這是一個(gè)測(cè)試文件"},
    {
    author"litaocheng"}
    ].

    其格式為erlang term,保存時(shí)選擇utf8編碼。
    代碼如下:

    1. %% read content from the file
    2. test1() ->
    3.     {ok[Terms]} = file:consult("test.txt"),
    4.     Desc = proplists:get_value(descTerms),
    5.     _Author = proplists:get_value(authorTerms),
    6.    
    7.     % out put the Desc and Author
    8.     DescUniBin = iolist_to_binary(Desc),
    9.     DescUniList = unicode:characters_to_list(DescUniBin),
    10.     io:format("desc bin : ~ts~ndesc bin : ~p~n",[DescUniBin,DescUniBin]),
    11.     io:format("desc list: ~ts~ndesc list: ~p~n"[DescUniList,DescUniList]).

    結(jié)果:

    desc bin : 這是一個(gè)測(cè)試文件
    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: 這是一個(gè)測(cè)試文件
    desc list: [36825,26159,19968,20010,27979,35797,25991,20214]

    首先將內(nèi)容從list轉(zhuǎn)換為binary, DescUniBin 便是對(duì)應(yīng)的unicode binary。隨后通過(guò)unicode:characters_to_list/1轉(zhuǎn)化為unicode list最后輸出。
    我們可以看到 unicode list中所有的element為integer, unicode binary中unicode string采用uft8編碼。

    2, 將數(shù)據(jù)保存成uft8格式

    1. %% save the binary in utf8 format
    2. test2() ->
    3.     [DescList] = io_lib:format("~ts"["這是一個(gè)測(cè)試文件"]),
    4.     DescBin = erlang:iolist_to_binary(DescList),
    5.     DescList2 = unicode:characters_to_list(DescBin),
    6.     List = lists:concat(["[{desc,\""DescList2"\"}, {author,\"litaocheng\"}]."]),
    7.     Bin = unicode:characters_to_binary(List),
    8.     io:format("bin is:~ts~n"[Bin]),
    9.     file:write_file("test_out.txt"Bin).

    這個(gè)例子的完整代碼如下:

    1. -module(unicode_test).
    2. -compile([export_all]).
    3.  
    4. %%
    5. %% the test.txt content:
    6. %% [
    7. %% {desc, "這是一個(gè)測(cè)試文件"},
    8. %% {author, "litaocheng"}
    9. %% ].
    10. %%
    11.  
    12. test() ->
    13.     test2(),
    14.     test1().
    15.  
    16. %% read content from the file
    17. test1() ->
    18.     {ok[Term]} = file:consult("test_out.txt"),
    19.     Desc = proplists:get_value(descTerm),
    20.     _Author = proplists:get_value(authorTerm),
    21.    
    22.     % out put the Desc and Author
    23.     DescUniBin = iolist_to_binary(Desc),
    24.     DescUniList = unicode:characters_to_list(DescUniBin),
    25.     io:format("desc bin : ~ts~ndesc bin : ~p~n",[DescUniBin,DescUniBin]),
    26.     io:format("desc list: ~ts~ndesc list: ~p~n"[DescUniList,DescUniList]).
    27.  
    28.  
    29. %% save the binary in utf8 format
    30. test2() ->
    31.     [DescList] = io_lib:format("~ts"["這是一個(gè)測(cè)試文件"]),
    32.     DescBin = erlang:iolist_to_binary(DescList),
    33.     DescList2 = unicode:characters_to_list(DescBin),
    34.     List = lists:concat(["[{desc,\""DescList2"\"}, {author,\"litaocheng\"}]."]),
    35.     Bin = unicode:characters_to_binary(List),
    36.     io:format("bin is:~ts~n"[Bin]),
    37.     file:write_file("test_out.txt"Bin).
    posted on 2009-10-28 15:19 ivaneeo 閱讀(1410) 評(píng)論(1)  編輯  收藏 所屬分類: erlang-分布式語(yǔ)言
    主站蜘蛛池模板: 久久久久成人片免费观看蜜芽 | 亚洲AV无码一区二区三区在线| 91香蕉成人免费网站| 亚洲日本VA中文字幕久久道具| 免费观看午夜在线欧差毛片| 国产成人精品免费久久久久| 亚洲色大情网站www| 伊人久久精品亚洲午夜| 久久国内免费视频| 久青草国产免费观看| 亚洲国产福利精品一区二区| 亚洲精品成人片在线观看| 欧洲一级毛片免费| 4hu四虎免费影院www| 亚洲一久久久久久久久| 亚洲Av无码专区国产乱码DVD| 狠狠久久永久免费观看| 一级毛片在线观看免费| 猫咪免费人成网站在线观看入口 | 国内大片在线免费看| 无码精品人妻一区二区三区免费看 | 亚洲片国产一区一级在线观看 | 88av免费观看| 亚洲一级片免费看| 亚洲AV成人无码网天堂| 亚洲人成影院午夜网站| 久久亚洲国产精品一区二区| 免费一级毛片不卡在线播放| 一个人免费观看在线视频www| 亚洲a一级免费视频| caoporn国产精品免费| 小说区亚洲自拍另类| 67194在线午夜亚洲| 亚洲人成影院在线| 亚洲色大成网站WWW久久九九| 国产免费看插插插视频| 大学生美女毛片免费视频| 亚洲一级毛片免费观看| 久草福利资源网站免费| 丰满人妻一区二区三区免费视频| 美女视频黄a视频全免费网站色 |