diff options
author | Hans Bolinder <[email protected]> | 2013-01-15 08:41:05 +0100 |
---|---|---|
committer | Hans Bolinder <[email protected]> | 2013-01-25 12:54:27 +0100 |
commit | acd0ee9d1301a96f6f2fe1b19c12b3639c77c59e (patch) | |
tree | e7c91b2397fb065122c906d01d81e957645aa3d2 /lib/stdlib/src | |
parent | b2b0feab152063f71c4bf58f985cd52fc9e0105a (diff) | |
download | otp-acd0ee9d1301a96f6f2fe1b19c12b3639c77c59e.tar.gz otp-acd0ee9d1301a96f6f2fe1b19c12b3639c77c59e.tar.bz2 otp-acd0ee9d1301a96f6f2fe1b19c12b3639c77c59e.zip |
[stdlib] Introduce new functions epp:read_encoding_from_binary/1,2
Diffstat (limited to 'lib/stdlib/src')
-rw-r--r-- | lib/stdlib/src/epp.erl | 37 |
1 files changed, 33 insertions, 4 deletions
diff --git a/lib/stdlib/src/epp.erl b/lib/stdlib/src/epp.erl index 475b1bf933..afa39c3fb9 100644 --- a/lib/stdlib/src/epp.erl +++ b/lib/stdlib/src/epp.erl @@ -24,7 +24,8 @@ -export([scan_erl_form/1,parse_erl_form/1,macro_defs/1]). -export([parse_file/1, parse_file/3]). -export([default_encoding/0, encoding_to_string/1, - read_encoding/1, read_encoding/2, set_encoding/1]). + read_encoding_from_binary/1, read_encoding_from_binary/2, + set_encoding/1, read_encoding/1, read_encoding/2]). -export([interpret_file_attribute/1]). -export([normalize_typed_record_fields/1,restore_typed_record_fields/1]). @@ -265,13 +266,41 @@ set_encoding(File) -> ok = io:setopts(File, [{encoding, Enc}]), Encoding. --spec read_encoding_from_file(File, InComment) -> source_encoding() | none when - File :: io:device(), - InComment :: boolean(). +-spec read_encoding_from_binary(Binary) -> source_encoding() | none when + Binary :: binary(). -define(ENC_CHUNK, 32). -define(N_ENC_CHUNK, 16). % a total of 512 bytes +read_encoding_from_binary(Binary) -> + read_encoding_from_binary(Binary, []). + +-spec read_encoding_from_binary(Binary, Options) -> + source_encoding() | none when + Binary :: binary(), + Options :: [Option], + Option :: {in_comment_only, boolean()}. + +read_encoding_from_binary(Binary, Options) -> + InComment = proplists:get_value(in_comment_only, Options, true), + try + com_nl(Binary, fake_reader(0), 0, InComment) + catch + throw:no -> + none + end. + +fake_reader(N) -> + fun() when N =:= ?N_ENC_CHUNK -> + throw(no); + () -> + {<<>>, fake_reader(N+1)} + end. + +-spec read_encoding_from_file(File, InComment) -> source_encoding() | none when + File :: io:device(), + InComment :: boolean(). + read_encoding_from_file(File, InComment) -> {ok, Pos0} = file:position(File, cur), Opts = io:getopts(File), |