-
-
Save gdamjan/2258609 to your computer and use it in GitHub Desktop.
Yet Another Erlang/Unicode Fuckup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(reduced_test_case). | |
-define(NODEBUG, 1). | |
-include_lib("eunit/include/eunit.hrl"). | |
-behavior(gen_server). | |
%-compile(export_all). | |
-export([init/1, code_change/3, handle_call/3, handle_cast/2, handle_info/2, terminate/2]). | |
-export([start/0]). | |
-define(SERVER, ?MODULE). | |
start() -> | |
{ok, PID} = gen_server:start_link({local, ?SERVER}, ?MODULE, [], []), | |
PID ! {hi, [1040,1044]}, % try a string/list | |
PID ! {hi, <<"\xa9">>}, % try a latin1 binary | |
PID ! {hi, <<"\xd0\x90\xd0\x94">>}, % try a utf8 binary | |
PID ! {hi, [[1040,1044], <<"\xa9">>, <<"\xd0\x90\xd0\x94">>]}, % all together in a iolist | |
{ok, PID}. | |
to_binary(IoList) when is_binary(IoList) -> | |
IoList; | |
to_binary(IoList) -> | |
to_binary(IoList, []). | |
to_binary([], Acc) -> | |
Acc; | |
to_binary([Head | Tail], Acc) when is_binary(Head) -> | |
to_binary(Tail, [Acc | Head]); | |
to_binary(IoList, Acc) -> | |
case unicode:characters_to_binary(IoList) of | |
{error, Bin, Rest} -> | |
to_binary(Rest, [Acc|Bin]); | |
{incomplete, Bin1, Bin2} -> | |
[Acc | [Bin1, Bin2]]; | |
Bin -> | |
Bin | |
end. | |
print(Data) -> | |
port_command(stdout, [to_binary(Data), "\n"]). | |
init(_) -> | |
StdOut = open_port("/dev/stdout", [binary, out]), | |
register(stdout, StdOut), | |
{ok, []}. | |
handle_info({hi, Data}, State) -> | |
print(Data), | |
{noreply, State}; | |
handle_info(_Msg, State) -> {noreply, State}. | |
handle_call(_Msg, _Caller, State) -> {noreply, State}. | |
handle_cast(_Msg, State) -> {noreply, State}. | |
terminate(_Reason, _State) -> ok. | |
code_change(_OldVersion, State, _Extra) -> {ok, State}. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
echo "Reduced test case:" | |
cat reduced_test_case.erl | |
erlc reduced_test_case.erl | |
echo | |
echo "===" | |
echo "Running erl -eval \"reduced_test_case:start()\" with keys" | |
echo "* -run init stop" | |
echo | |
echo "<output>" | |
erl -eval "reduced_test_case:start()" -run init stop | |
echo "</output>" | |
echo | |
echo | |
echo "===" | |
echo "Running erl -eval \"reduced_test_case:start()\" with keys" | |
echo "* -run init stop -noshell" | |
echo | |
echo "<output>" | |
erl -eval "reduced_test_case:start()" -run init stop -noshell | |
echo "</output>" | |
echo | |
echo | |
echo | |
echo "Feel the difference." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
be careful though, that to_binary recursive function has a bug where it enters an infinite loop in some cases. In the end I've decided not to use it.