Last active
August 10, 2016 23:32
-
-
Save larryv/9e7c8f19e3e0abe9f7c82c7c66c3fc78 to your computer and use it in GitHub Desktop.
Iterations on a subroutine for changing 0.000-style Perl module versions into 0.0.0-style versions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
proc perl5_convert_version {vers} { | |
if {[string index $vers 0] eq "v"} { | |
set start 1 | |
} else { | |
set start 0 | |
} | |
set index [string first . $vers] | |
set other_dot [string first . [string range $vers [expr {$index + 1}] end]] | |
if {$index == -1 || $other_dot != -1} { | |
return [string range $vers $start end] | |
} | |
set ret [string range $vers $start [expr {$index - 1}]] | |
incr index | |
set fractional [string range $vers $index end] | |
set index 0 | |
while {$index < [string length $fractional] || $index < 6} { | |
set sub [string range $fractional $index [expr {$index + 2}]] | |
if {[string length $sub] < 3} { | |
append sub [string repeat "0" [expr {3 - [string length $sub]}]] | |
} | |
append ret ".[scan $sub %u]" | |
incr index 3 | |
} | |
return $ret | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
proc perl5_normalize_version {rawvers} { | |
set vers [string trimleft $rawvers v] | |
if {![regexp {^(\d+)\.(\d+)$} $vers -> int frac]} { | |
return $vers | |
} | |
set fraclen [string length $frac] | |
set padlen [expr {max(6 - $fraclen, (3 - $fraclen) % 3)}] | |
append frac [string repeat 0 $padlen] | |
set normvers [scan $int %u] | |
for {set i 0} {$i < $fraclen + $padlen} {incr i 3} { | |
append normvers .[scan [string range $frac $i $i+2] %u] | |
} | |
return $normvers | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
proc perl5_normalize_version {rawvers} { | |
set vers [string trimleft $rawvers v] | |
if {![regexp {^\d+\.\d+$} $vers]} { | |
return $vers | |
} | |
set frac [lindex [split $vers .] 1] | |
set fraclen [string length $frac] | |
set padlen [expr {max(6 - $fraclen, (3 - $fraclen) % 3)}] | |
append frac [string repeat 0 $padlen] | |
scan $vers %u normvers | |
for {set i 0} {$i < $fraclen + $padlen} {incr i 3} { | |
append normvers .[scan [string range $frac $i $i+2] %u] | |
} | |
return $normvers | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment