Sign Up
Log In
Log In
or
Sign Up
Places
All Projects
Status Monitor
Collapse sidebar
home:Ledest:erlang:23
erlang
6511-Add-hex-encoding-fonctions-on-the-binary-m...
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
File 6511-Add-hex-encoding-fonctions-on-the-binary-module.patch of Package erlang
From 46abba5683b9641676b00d91ce5592669e997944 Mon Sep 17 00:00:00 2001 From: gearnode <bryan@frimin.fr> Date: Fri, 29 Jan 2021 19:40:40 +0100 Subject: [PATCH 1/2] Add hex encoding fonctions on the binary module Dealing with hex format is frequent in many codebases. At this time, we all copy-paste similar helper in our codebases to encode and decode a hex-encoded binary. --- lib/stdlib/doc/src/binary.xml | 28 +++++++++++ lib/stdlib/src/binary.erl | 64 +++++++++++++++++++++++-- lib/stdlib/src/erl_stdlib_errors.erl | 19 ++++++++ lib/stdlib/test/binary_module_SUITE.erl | 45 +++++++++++++++-- lib/stdlib/test/error_info_lib.erl | 11 +++-- 5 files changed, 157 insertions(+), 10 deletions(-) diff --git a/lib/stdlib/doc/src/binary.xml b/lib/stdlib/doc/src/binary.xml index 412a0f5def..1426921ede 100644 --- a/lib/stdlib/doc/src/binary.xml +++ b/lib/stdlib/doc/src/binary.xml @@ -243,6 +243,34 @@ </func> <func> + <name name="encode_hex" arity="1" since=""/> + <fsummary>Encodes a binary into a hex encoded binary.</fsummary> + <desc> + <p>Encodes a binary into a hex encoded binary.</p> + + <p><em>Example:</em></p> + + <code> +1> binary:encode_hex(<<"f">>). +<<"66">></code> + </desc> + </func> + + <func> + <name name="decode_hex" arity="1" since=""/> + <fsummary>Decodes a hex encoded binary into a binary.</fsummary> + <desc> + <p>Decodes a hex encoded binary into a binary.</p> + + <p><em>Example</em></p> + + <code> +1> binary:decode_hex(<<"66">>). +<<"f">></code> + </desc> + </func> + + <func> <name name="first" arity="1" since="OTP R14B"/> <fsummary>Return the first byte of a binary.</fsummary> <desc> diff --git a/lib/stdlib/src/binary.erl b/lib/stdlib/src/binary.erl index eb5f225a98..73b619d192 100644 --- a/lib/stdlib/src/binary.erl +++ b/lib/stdlib/src/binary.erl @@ -20,7 +20,8 @@ -module(binary). %% %% Implemented in this module: --export([replace/3,replace/4]). +-export([replace/3,replace/4, + encode_hex/1, decode_hex/1]). -export_type([cp/0]). @@ -314,8 +315,6 @@ splitat(H,N,[]) -> splitat(H,N,[I|T]) -> [binary:part(H,{N,I-N})|splitat(H,I,T)]. - - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Simple helper functions %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -331,3 +330,61 @@ get_opts_replace([{insert_replaced,N} | T],{Part,Global,_Insert}) -> get_opts_replace(_,_) -> throw(badopt). +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% Hex encoding functions +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-spec encode_hex(Bin) -> Bin2 when + Bin :: binary(), + Bin2 :: binary(). +encode_hex(Bin) when is_binary(Bin) -> + encode_hex(Bin, <<>>); +encode_hex(Bin) -> + error(badarg, [Bin]). + +-spec encode_hex(Bin, Acc) -> Bin2 when + Bin :: binary(), + Acc :: binary(), + Bin2 :: Acc. +encode_hex(<<>>, Acc) -> + Acc; +encode_hex(<<A0:4, B0:4, Rest/binary>>, Acc) -> + A = encode_hex_digit(A0), + B = encode_hex_digit(B0), + encode_hex(Rest, <<Acc/binary, A, B>>). + +-spec encode_hex_digit(0..15) -> byte(). +encode_hex_digit(Char) when Char =< 9 -> + Char + $0; +encode_hex_digit(Char) when Char =< 15 -> + Char + $A - 10. + +-spec decode_hex(Bin) -> Bin2 when + Bin :: binary(), + Bin2 :: binary(). +decode_hex(Bin) when is_binary(Bin) -> + decode_hex(Bin, <<>>); +decode_hex(Bin) -> + error(badarg, [Bin]). + +-spec decode_hex(Bin, Acc) -> Bin2 when + Bin :: binary(), + Acc :: binary(), + Bin2 :: Acc. +decode_hex(<<>>, Acc) -> + Acc; +decode_hex(<<A0:8, B0:8, Rest/binary>>, Acc) -> + A = decode_hex_char(A0), + B = decode_hex_char(B0), + decode_hex(Rest, <<Acc/binary, A:4, B:4>>); +decode_hex(Bin, _Acc) -> + error(badarg, [Bin]). + +-spec decode_hex_char($A..$F | $a..$f | $0..$9) -> 0..15. +decode_hex_char(Char) when Char >= $a, Char =< $f -> + Char - $a + 10; +decode_hex_char(Char) when Char >= $A, Char =< $F -> + Char - $A + 10; +decode_hex_char(Char) when Char >= $0, Char =< $9 -> + Char - $0; +decode_hex_char(Char) -> + error(badarg, [Char]). diff --git a/lib/stdlib/test/binary_module_SUITE.erl b/lib/stdlib/test/binary_module_SUITE.erl index 2af4583d8b..70e100c9e6 100644 --- a/lib/stdlib/test/binary_module_SUITE.erl +++ b/lib/stdlib/test/binary_module_SUITE.erl @@ -23,7 +23,7 @@ interesting/1,scope_return/1,random_ref_comp/1,random_ref_sr_comp/1, random_ref_fla_comp/1,parts/1, bin_to_list/1, list_to_bin/1, copy/1, referenced/1,guard/1,encode_decode/1,badargs/1,longest_common_trap/1, - check_no_invalid_read_bug/1]). + check_no_invalid_read_bug/1, hex_encoding/1]). -export([random_number/1, make_unaligned/1]). @@ -37,12 +37,12 @@ all() -> [scope_return,interesting, random_ref_fla_comp, random_ref_sr_comp, random_ref_comp, parts, bin_to_list, list_to_bin, copy, referenced, guard, encode_decode, badargs, - longest_common_trap, check_no_invalid_read_bug]. + longest_common_trap, check_no_invalid_read_bug, + hex_encoding]. -define(MASK_ERROR(EXPR),mask_error((catch (EXPR)))). - %% Test various badarg exceptions in the module. badargs(Config) when is_list(Config) -> badarg = ?MASK_ERROR(binary:compile_pattern([<<1,2,3:3>>])), @@ -237,6 +237,12 @@ badargs(Config) when is_list(Config) -> badarg = ?MASK_ERROR( binary:at([1,2,4],2)), + + badarg = ?MASK_ERROR(binary:encode_hex("abc")), + badarg = ?MASK_ERROR(binary:encode_hex(123)), + badarg = ?MASK_ERROR(binary:encode_hex([])), + badarg = ?MASK_ERROR(binary:encode_hex(#{})), + badarg = ?MASK_ERROR(binary:encode_hex(foo)), ok. %% Whitebox test to force special trap conditions in @@ -1373,3 +1379,30 @@ check_no_invalid_read_bug(I) -> binary:encode_unsigned(N+N), binary:encode_unsigned(N+N, little), check_no_invalid_read_bug(I+1). + +hex_encoding(Config) when is_list(Config) -> + %% Vector test imported from the RFC 4648 section 10. + <<>> = binary:encode_hex(<<>>), + <<"66">> = binary:encode_hex(<<"f">>), + <<"666F">> = binary:encode_hex(<<"fo">>), + <<"666F6F">> = binary:encode_hex(<<"foo">>), + <<"666F6F62">> = binary:encode_hex(<<"foob">>), + <<"666F6F6261">> = binary:encode_hex(<<"fooba">>), + <<"666F6F626172">> = binary:encode_hex(<<"foobar">>), + + <<>> = binary:decode_hex(<<>>), + <<"f">> = binary:decode_hex(<<"66">>), + <<"fo">> = binary:decode_hex(<<"666F">>), + <<"foo">> = binary:decode_hex(<<"666F6F">>), + <<"foob">> = binary:decode_hex(<<"666F6F62">>), + <<"fooba">> = binary:decode_hex(<<"666F6F6261">>), + <<"foobar">> = binary:decode_hex(<<"666F6F626172">>), + + <<"fo">> = binary:decode_hex(<<"666f">>), + <<"foo">> = binary:decode_hex(<<"666f6f">>), + <<"foob">> = binary:decode_hex(<<"666f6f62">>), + <<"fooba">> = binary:decode_hex(<<"666f6f6261">>), + <<"foobar">> = binary:decode_hex(<<"666f6f626172">>), + + <<"foobar">> = binary:decode_hex(<<"666f6F626172">>), + ok. -- 2.35.3
Locations
Projects
Search
Status Monitor
Help
OpenBuildService.org
Documentation
API Documentation
Code of Conduct
Contact
Support
@OBShq
Terms
openSUSE Build Service is sponsored by
The Open Build Service is an
openSUSE project
.
Sign Up
Log In
Places
Places
All Projects
Status Monitor