<commit_before>-module(shackle_udp).
-include("shackle_internal.hrl").

-compile(inline).
-compile({inline_size, 512}).

-export([
    close/1,
    header/2,
    new/3,
    send/3
]).

%% public
-spec close(inet:socket()) ->
    ok.

close(Socket) ->
    gen_udp:close(Socket).

-spec header(inet:ip_address(), inet:port_number()) -> iodata().

header({A, B, C, D}, Port) ->
    [[((Port) bsr 8) band 16#ff, (Port) band 16#ff],
        [A band 16#ff, B band 16#ff, C band 16#ff, D band 16#ff]].

-spec new(inet:ip_address(), inet:port_number(), [gen_udp:option()]) ->
    {ok, inet:socket()} | {error, term()}.

new(_Ip, _Port, Options) ->
    gen_udp:open(0, Options).

-spec send(inet:socket(), iodata(), iodata()) ->
    ok | {error, term()}.

send(Socket, Header, Data) ->
    try
        true = erlang:port_command(Socket, [Header, Data]),
        ok
    catch
        Error:Reason ->
            {error, {Error, Reason}}
    end.<commit_msg>Cleanup UDP header generation<commit_after>-module(shackle_udp).
-include("shackle_internal.hrl").

-compile(inline).
-compile({inline_size, 512}).

-export([
    close/1,
    header/2,
    new/3,
    send/3
]).

-define(INT16(X), [((X) bsr 8) band 16#ff, (X) band 16#ff]).

%% public
-spec close(inet:socket()) ->
    ok.

close(Socket) ->
    gen_udp:close(Socket).

-spec header(inet:ip_address(), inet:port_number()) -> iodata().

header(IP, Port) ->
    [?INT16(Port), ip4_to_bytes(IP)].

-spec new(inet:ip_address(), inet:port_number(), [gen_udp:option()]) ->
    {ok, inet:socket()} | {error, term()}.

new(_Ip, _Port, Options) ->
    gen_udp:open(0, Options).

-spec send(inet:socket(), iodata(), iodata()) ->
    ok | {error, term()}.

send(Socket, Header, Data) ->
    try
        true = erlang:port_command(Socket, [Header, Data]),
        ok
    catch
        Error:Reason ->
            {error, {Error, Reason}}
    end.

%% private
ip4_to_bytes({A, B, C, D}) ->
    [A band 16#ff, B band 16#ff, C band 16#ff, D band 16#ff].
{'fraction_non_alphanumeric': 0.20514112903225806, 'fraction_numerical': 0.024193548387096774, 'mean_word_length': 3.109730848861284, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4583815', 'n_tokens_mistral': 806, 'n_tokens_neox': 761, 'n_words': 167}