2011年3月10日木曜日

Erlang binary_to_atom

binary_to_atom(Binary, Encoding) -> atom()
Types:
Binary = binary()
Encoding = latin1 | utf8 | unicode

Returns the atom whose text representation is Binary.
(バイナリで表されたものをアトムにして戻す)

If Encoding is latin1, no translation of bytes in the binary is done.
(エンコードがlatin1なら、バイナリの各バイトはなんら置換されない)

If Encoding is utf8 or unicode, the binary must contain valid UTF-8 sequences;
(もし、utf8 or unicodeなら、バイナリはutf8の文字列になる)

furthermore, only Unicode characters up to 0xFF are allowed.
それ以上に、16進数でFFまでならUnicodeが許容される)


Note:
(参考)
binary_to_atom(Binary, utf8) will fail if the binary contains Unicode characters greater than 16#FF.
(binary_to_atom(Binary, utf8)は落ちる。16進数のFFより大きなUnicode文字が含まれていると)

In a future release,
(将来的に)

such Unicode characters might be allowed and binary_to_atom(Binary, utf8) will not fail in that case.
(そのようなUnicode文字は許容される。そして、binary_to_atom(Binary, utf8)は落ちることがないようになる)

実験結果:

7> binary_to_atom(<<100,102,103>>,latin1).
dfg
8> binary_to_atom(<<1,2,3>>,latin1).    
'\001\002\003'
9> binary_to_atom(<<257,2,3>>,latin1).
'\001\002\003'
10> binary_to_atom(<<256,2,3>>,latin1).
'\000\002\003'
11> binary_to_atom(<<"Erlang">>,latin1).
'Erlang'
12> binary_to_atom(<<1024/utf8>>, utf8).
** exception error: bad argument
     in function  binary_to_atom/2
        called as binary_to_atom(<<208,128>>,utf8)
13> binary_to_atom(<<1024/unicode>>, utf8).
* 1: bit type unicode undefined
14> binary_to_atom(<<10/unicode>>, utf8).
* 1: bit type unicode undefined
15> binary_to_atom(<<10/unicode>>, unicode).
* 1: bit type unicode undefined
16> binary_to_atom(<<192>>, unicode).    

** exception error: bad argument
     in function  binary_to_atom/2
        called as binary_to_atom(<<"À">>,unicode)
17> binary_to_atom(<<192>>, latin1).
'À'
18> binary_to_atom(<<102/utf8>>, utf8).
f
19> binary_to_atom(<<16#FF>>, utf8).  
** exception error: bad argument
     in function  binary_to_atom/2
        called as binary_to_atom(<<"ÿ">>,utf8)
20> binary_to_atom(<<16#FF>>, unicode).
** exception error: bad argument
     in function  binary_to_atom/2
        called as binary_to_atom(<<"ÿ">>,unicode)
21> binary_to_atom(<<16#1A>>, unicode).
'\032'
22> binary_to_atom(<<16#41>>, unicode).
'A'
23> binary_to_atom(<<16#FE>>, unicode).
** exception error: bad argument
     in function  binary_to_atom/2
        called as binary_to_atom(<<"þ">>,unicode)
24> binary_to_atom(<<16#256>>, unicode).
'V'
25> binary_to_atom(<<256>>, unicode).   
'\000'
26> binary_to_atom(<<255>>, unicode).
** exception error: bad argument
     in function  binary_to_atom/2
        called as binary_to_atom(<<"ÿ">>,unicode)
27> binary_to_atom(<<128>>, unicode).
** exception error: bad argument
     in function  binary_to_atom/2
        called as binary_to_atom(<<128>>,unicode)
28> binary_to_atom(<<127>>, unicode).
'\d'
29> binary_to_atom(<<16#7F>>, unicode).
'\d'
30> binary_to_atom(<<16#7E>>, unicode).
'~'
31> binary_to_atom(<<16#80>>, unicode).
** exception error: bad argument
     in function  binary_to_atom/2
        called as binary_to_atom(<<128>>,unicode)
32> binary_to_atom(<<16#7F>>, latin1). 
'\d'
33> binary_to_atom(<<16#80>>, latin1).
'\200'
34> binary_to_atom(<<16#0>>, latin1). 
'\000'
35> binary_to_atom(<<16#80>>, utf8).  
** exception error: bad argument
     in function  binary_to_atom/2
        called as binary_to_atom(<<128>>,utf8)
36> 

1.unicodeで16#FFまでOKってあると思ったけど、16#7Fまでしかできないみたい。
2.latin1の場合、256=0なので N % 256なのでしょう。

ちょっと長くてすみません。