Skip to content

Commit db8069d

Browse files
committed
Add from_tuple method
1 parent fcb3eb9 commit db8069d

File tree

6 files changed

+58
-1
lines changed

6 files changed

+58
-1
lines changed

CHANGES

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ This is not a complete list of changes. See repository for full details:
55
https://github.com/Perl/version.pm
66

77
* Add to_decimal and to_dotted_decimal methods
8-
* Add tuple method
8+
* Add tuple and from_tuple methods
99

1010
0.9931
1111
* Fix definitions of LIKELY and UNLIKELY on older perls

lib/version.pm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ our (@ISA, $STRICT, $LAX);
3131
*version::to_decimal = \&version::vpp::to_decimal;
3232
*version::to_dotted_decimal = \&version::vpp::to_dotted_decimal;
3333
*version::tuple = \&version::vpp::tuple;
34+
*version::from_tuple = \&version::vpp::from_tuple;
3435
if ($] >= 5.009000) {
3536
no strict 'refs';
3637
*version::stringify = \&version::vpp::stringify;
@@ -53,6 +54,7 @@ our (@ISA, $STRICT, $LAX);
5354
*version::to_decimal = \&version::vxs::to_decimal;
5455
*version::to_dotted_decimal = \&version::vxs::to_dotted_decimal;
5556
*version::tuple = \&version::vxs::tuple;
57+
*version::from_tuple = \&version::vxs::from_tuple;
5658
if ($] >= 5.009000) {
5759
no strict 'refs';
5860
*version::stringify = \&version::vxs::stringify;

lib/version.pod

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,12 @@ This turns the components of the version into a list. E.g.
289289

290290
version->parse('1.2.3')->tuple; # (1, 2, 3)
291291

292+
=head2 from_tuple(...)
293+
294+
This takes a list of components and creates a dotted decimal version out of it. E.g.
295+
296+
version->from_tuple(1, 2, 3) # v1.2.3
297+
292298
=head2 stringify()
293299

294300
Returns a string that is as close to the original representation as possible.

t/coretests.pm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ sub BaseTests {
5151

5252
my $version = $CLASS->$method("v1.2.3.4");
5353
is_deeply ([ $version->tuple ], [1, 2, 3, 4], 'Tuple seems to work');
54+
is_deeply ($version, $CLASS->from_tuple(1, 2, 3, 4), 'Equals from_tuple');
5455

5556
# test illegal formats
5657
eval {my $version = $CLASS->$method("1.2_3_4")};

vperl/vpp.pm

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,19 @@ sub tuple {
880880
return @{ $self->{version} };
881881
}
882882

883+
sub from_tuple {
884+
my ($proto, @args) = @_;
885+
my $class = ref($proto) || $proto;
886+
887+
my @version = map 0+$_, @args;
888+
die if @args < 1;
889+
return bless {
890+
version => \@version,
891+
qv => !!1,
892+
'v' . join('.', @version),
893+
}, $class;
894+
}
895+
883896
sub _verify {
884897
my ($self) = @_;
885898
if ( ref($self)

vutil/vxs.inc

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
{VXS_CLASS "::declare", VXSp(version_qv), VXSXSDP(NULL)},
7474
{VXS_CLASS "::is_qv", VXSp(version_is_qv), VXSXSDP(NULL)},
7575
{VXS_CLASS "::tuple", VXSp(version_tuple), VXSXSDP(NULL)},
76+
{VXS_CLASS "::from_tuple", VXSp(version_from_tuple), VXSXSDP(NULL)},
7677
#else
7778

7879
#ifndef dVAR
@@ -509,4 +510,38 @@ VXS(version_tuple)
509510
}
510511
}
511512

513+
VXS(version_from_tuple)
514+
{
515+
dXSARGS;
516+
SV *lobj;
517+
int i;
518+
if (items < 2)
519+
croak_xs_usage(cv, "lobj, ...");
520+
lobj = ST(0);
521+
SP -= items;
522+
523+
AV* versions = newAV();
524+
SV* original = newSVpvs("v");
525+
526+
for (i = 1; i < items; ++i) {
527+
if (SvIV(ST(i)) < 0)
528+
Perl_croak(aTHX_ "Value %d in version is negative", SvIV(ST(i)));
529+
UV value = SvUV(ST(i));
530+
av_push(versions, newSVuv(value));
531+
if (i != 1)
532+
sv_catpvs(original, ".");
533+
sv_catpvf(original, "%" UVuf, value);
534+
}
535+
536+
HV* hash = newHV();
537+
(void)hv_stores(hash, "version", newRV_noinc(MUTABLE_SV(versions)));
538+
(void)hv_stores(hash, "original", original);
539+
(void)hv_stores(hash, "qv", newSVsv(&PL_sv_yes));
540+
541+
HV* stash = SvROK(lobj) ? SvSTASH(lobj) : gv_stashsv(lobj, GV_ADD);
542+
SV* result = sv_bless(newRV_noinc(MUTABLE_SV(hash)), stash);
543+
XPUSHs(result);
544+
PUTBACK;
545+
}
546+
512547
#endif

0 commit comments

Comments
 (0)