Skip to content

Commit 7a82c0a

Browse files
committed
Add to_decimal and to_dotted_decimal methods
1 parent e011c73 commit 7a82c0a

File tree

6 files changed

+74
-0
lines changed

6 files changed

+74
-0
lines changed

CHANGES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ This is not a complete list of changes. See repository for full details:
44

55
https://github.com/Perl/version.pm
66

7+
* Add to_decimal and to_dotted_decimal methods
8+
79
0.9931
810
* Fix definitions of LIKELY and UNLIKELY on older perls
911

lib/version.pm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ our (@ISA, $STRICT, $LAX);
2828
*version::new = \&version::vpp::new;
2929
*version::numify = \&version::vpp::numify;
3030
*version::normal = \&version::vpp::normal;
31+
*version::to_decimal = \&version::vpp::to_decimal;
32+
*version::to_dotted_decimal = \&version::vpp::to_dotted_decimal;
3133
if ($] >= 5.009000) {
3234
no strict 'refs';
3335
*version::stringify = \&version::vpp::stringify;
@@ -47,6 +49,8 @@ our (@ISA, $STRICT, $LAX);
4749
*version::new = \&version::vxs::new;
4850
*version::numify = \&version::vxs::numify;
4951
*version::normal = \&version::vxs::normal;
52+
*version::to_decimal = \&version::vxs::to_decimal;
53+
*version::to_dotted_decimal = \&version::vxs::to_dotted_decimal;
5054
if ($] >= 5.009000) {
5155
no strict 'refs';
5256
*version::stringify = \&version::vxs::stringify;

lib/version.pod

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,18 @@ Returns a value representing the object in a pure decimal.
271271
version->declare('v1.2')->numify; # 1.002000
272272
version->parse('1.2')->numify; # 1.200
273273

274+
=head2 to_decimal
275+
276+
This returns a new version object for the numified version, much like C<< version->parse($v->numify) >> would.
277+
278+
version->parse('v1.2')->to_decimal; # 1.002000
279+
280+
=head2 to_dotted_decimal
281+
282+
This returns a new version object for the normalized version, much like C<< version->parse($v->normal) >> would.
283+
284+
version->parse('1.002')->to_dotted_decimal; # v1.2.0
285+
274286
=head2 stringify()
275287

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

t/coretests.pm

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,19 @@ SKIP: {
633633
$v = $CLASS->new("1.02_003");
634634
is $v->numify, '1.020030', 'Ignore underscores for numify';
635635
}
636+
637+
{
638+
$v = $CLASS->parse("v1.2.3");
639+
$v2 = $v->to_decimal;
640+
isa_ok $v2, $CLASS;
641+
is $v2, "1.002003";
642+
}
643+
{
644+
$v = $CLASS->parse("1.002003");
645+
$v2 = $v->to_dotted_decimal;
646+
isa_ok $v2, $CLASS;
647+
is "$v2", "v1.2.3";
648+
}
636649
}
637650

638651
1;

vperl/vpp.pm

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,16 @@ sub stringify {
775775
: $self->numify;
776776
}
777777

778+
sub to_decimal {
779+
my ($self) = @_;
780+
return ref($self)->new($self->numify);
781+
}
782+
783+
sub to_dotted_decimal {
784+
my ($self) = @_;
785+
return ref($self)->new($self->normal);
786+
}
787+
778788
sub vcmp {
779789
my ($left,$right,$swap) = @_;
780790
die "Usage: version::vcmp(lobj, robj, ...)" if @_ < 2;

vutil/vxs.inc

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
{VXS_CLASS "::(0+", VXSp(version_numify), VXSXSDP(NULL)},
4747
{VXS_CLASS "::numify", VXSp(version_numify), VXSXSDP(NULL)},
4848
{VXS_CLASS "::normal", VXSp(version_normal), VXSXSDP(NULL)},
49+
{VXS_CLASS "::to_decimal", VXSp(version_to_decimal), VXSXSDP(NULL)},
50+
{VXS_CLASS "::to_dotted_decimal", VXSp(version_to_dotted_decimal), VXSXSDP(NULL)},
4951
{VXS_CLASS "::(cmp", VXSp(version_vcmp), VXSXSDP(NULL)},
5052
{VXS_CLASS "::(<=>", VXSp(version_vcmp), VXSXSDP(NULL)},
5153
# ifdef PERL_CORE
@@ -296,6 +298,37 @@ VXS(version_normal)
296298
}
297299
}
298300

301+
VXS(version_to_decimal)
302+
{
303+
dXSARGS;
304+
SV* self = ST(0);
305+
if (items < 1)
306+
croak_xs_usage(cv, "lobj, ...");
307+
SP -= items;
308+
{
309+
SV *lobj, *rv;
310+
VTYPECHECK(lobj, self, "lobj");
311+
rv = NEW_VERSION(VNUMIFY(lobj));
312+
VXS_RETURN_M_SV(sv_bless(rv, SvSTASH(SvRV(self))));
313+
}
314+
}
315+
316+
VXS(version_to_dotted_decimal)
317+
{
318+
dXSARGS;
319+
SV* self = ST(0);
320+
if (items != 1)
321+
croak_xs_usage(cv, "ver");
322+
SP -= items;
323+
{
324+
SV *lobj, *rv;
325+
VTYPECHECK(lobj, self, "lobj");
326+
rv = NEW_VERSION(VNORMAL(lobj));
327+
sv_bless(rv, SvSTASH(SvRV(self)));
328+
VXS_RETURN_M_SV(sv_bless(rv, SvSTASH(SvRV(self))));
329+
}
330+
}
331+
299332
VXS(version_vcmp)
300333
{
301334
dXSARGS;

0 commit comments

Comments
 (0)