aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/stdlib/doc/src/epp.xml19
-rw-r--r--lib/stdlib/src/epp.erl37
-rw-r--r--lib/stdlib/test/epp_SUITE.erl9
3 files changed, 57 insertions, 8 deletions
diff --git a/lib/stdlib/doc/src/epp.xml b/lib/stdlib/doc/src/epp.xml
index 3e8aba2e5f..df7bf883fc 100644
--- a/lib/stdlib/doc/src/epp.xml
+++ b/lib/stdlib/doc/src/epp.xml
@@ -4,7 +4,7 @@
<erlref>
<header>
<copyright>
- <year>1996</year><year>2012</year>
+ <year>1996</year><year>2013</year>
<holder>Ericsson AB. All Rights Reserved.</holder>
</copyright>
<legalnotice>
@@ -109,7 +109,8 @@
<fsummary>Return a string representation of an encoding</fsummary>
<desc>
<p>Returns a string representation of an encoding. The string
- is recognized by <c>read_encoding/1,2</c> and
+ is recognized by <c>read_encoding/1,2</c>,
+ <c>read_encoding_from_binary/1,2</c>, and
<c>set_encoding/1</c> as a valid encoding.</p>
</desc>
</func>
@@ -128,6 +129,20 @@
</desc>
</func>
<func>
+ <name name="read_encoding_from_binary" arity="1"/>
+ <name name="read_encoding_from_binary" arity="2"/>
+ <fsummary>Read the encoding from a binary</fsummary>
+ <desc>
+ <p>Read the <seealso marker="#encoding">encoding</seealso> from
+ a binary. Returns the read encoding, or <c>none</c> if no
+ valid encoding was found.</p>
+ <p>The option <c>in_comment_only</c> is <c>true</c> by
+ default, which is correct for Erlang source files. If set to
+ <c>false</c> the encoding string does not necessarily have to
+ occur in a comment.</p>
+ </desc>
+ </func>
+ <func>
<name name="set_encoding" arity="1"/>
<fsummary>Read and set the encoding of an IO device</fsummary>
<desc>
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),
diff --git a/lib/stdlib/test/epp_SUITE.erl b/lib/stdlib/test/epp_SUITE.erl
index 606bbbcbb2..041d521514 100644
--- a/lib/stdlib/test/epp_SUITE.erl
+++ b/lib/stdlib/test/epp_SUITE.erl
@@ -1,7 +1,7 @@
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 1998-2012. All Rights Reserved.
+%% Copyright Ericsson AB 1998-2013. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
@@ -1342,6 +1342,8 @@ encoding(Enc, File) ->
E = encoding_nocom(Enc, File).
encoding_com(Enc, File) ->
+ B = list_to_binary(Enc),
+ E = epp:read_encoding_from_binary(B),
ok = file:write_file(File, Enc),
{ok, Fd} = file:open(File, [read]),
E = epp:set_encoding(Fd),
@@ -1349,10 +1351,13 @@ encoding_com(Enc, File) ->
E = epp:read_encoding(File).
encoding_nocom(Enc, File) ->
+ Options = [{in_comment_only, false}],
+ B = list_to_binary(Enc),
+ E = epp:read_encoding_from_binary(B, Options),
ok = file:write_file(File, Enc),
{ok, Fd} = file:open(File, [read]),
ok = file:close(Fd),
- epp:read_encoding(File, [{in_comment_only, false}]).
+ E = epp:read_encoding(File, Options).
check(Config, Tests) ->
eval_tests(Config, fun check_test/2, Tests).