Skip to content

Commit

Permalink
- Added the String hexlify and unhexlify methods.
Browse files Browse the repository at this point in the history
Example:

	say "foobar".hexlify		#=> 666f6f626172
	say "666f6f626172".unhexlify	#=> foobar

Also aliased as `ascii2hex` and `hex2ascii`.
  • Loading branch information
trizen committed Jan 4, 2022
1 parent 53a286f commit 4613a60
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
22 changes: 18 additions & 4 deletions lib/Sidef/Types/String/String.pm
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,22 @@ package Sidef::Types::String::String {
Sidef::Types::Number::Number->new($$self, 16);
}

sub hexlify {
my ($self) = @_;
my $r = CORE::unpack("H*", $$self);
bless \$r;
}

*ascii2hex = \&hexlify;

sub unhexlify {
my ($self) = @_;
my $r = CORE::pack("H*", $$self);
bless \$r;
}

*hex2ascii = \&unhexlify;

sub decode_base64 {
state $x = require MIME::Base64;
bless \MIME::Base64::decode_base64(${$_[0]});
Expand Down Expand Up @@ -1003,10 +1019,8 @@ package Sidef::Types::String::String {
}

my $tries = (
CORE::join(
'|', map { CORE::quotemeta($_) }
sort { length($b) <=> length($a) } CORE::keys(%map)
)
CORE::join('|', map { CORE::quotemeta($_) }
sort { length($b) <=> length($a) } CORE::keys(%map))
);

$self->new($$self =~ s{($tries)}{$map{$1}}gr);
Expand Down
20 changes: 20 additions & 0 deletions lib/Sidef/Types/String/String.pod
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,26 @@ Returns the

=cut

=head2 hex2ascii

String.hex2ascii()

Returns the

Aliases: I<unhexlify>

=cut

=head2 hexlify

String.hexlify()

Returns the

Aliases: I<ascii2hex>

=cut

=head2 index

String.index()
Expand Down

0 comments on commit 4613a60

Please sign in to comment.