From: <jh...@us...> - 2009-09-23 00:54:23
|
Revision: 95 http://etch.svn.sourceforge.net/etch/?rev=95&view=rev Author: jheiss Date: 2009-09-23 00:54:12 +0000 (Wed, 23 Sep 2009) Log Message: ----------- Latest version from tpkg Modified Paths: -------------- trunk/server/lib/versiontype.rb Modified: trunk/server/lib/versiontype.rb =================================================================== --- trunk/server/lib/versiontype.rb 2009-09-23 00:53:21 UTC (rev 94) +++ trunk/server/lib/versiontype.rb 2009-09-23 00:54:12 UTC (rev 95) @@ -1,13 +1,11 @@ # This class stores numbers with multiple decimal points, a format # commonly used for version numbers. For example '2.5.1'. -require 'generator' # SyncEnumerator - class Version include Comparable def initialize(version) - @version = version + @version = version.to_s end def to_s @@ -24,16 +22,8 @@ fields[0] = '0' end end - # Pad with zeros so that '1' == '1.0', etc. - if ourfields.length != otherfields.length - larger = [ourfields.length, otherfields.length].max - # For the longer number this depends on something like (3...1).each - # doing nothing. That currently works, but is not documented behavior. - (ourfields.length...larger).each { ourfields << '0' } - (otherfields.length...larger).each { otherfields << '0' } - end - ourfields, otherfields = convert_and_split(ourfields, otherfields) + convert_and_split!(ourfields, otherfields) # Array conveniently implements <=> ourfields <=> otherfields @@ -42,18 +32,27 @@ # Private methods below private + # Loops over two arrays in parallel. If the entry at a given # position in both arrays is numeric it is converted from a string to # a number, or if either entry is a mixture of numeric and # non-numeric characters then both are split into an array consisting # of the numeric and non-numeric components. - def convert_and_split(fields1, fields2) + def convert_and_split!(fields0, fields1) + # Pad the shorter of two arrays with zeros so that both arrays are + # the same length. This ensures that '1' == '1.0', etc. + if fields0.length != fields1.length + larger = [fields0.length, fields1.length].max + # For the longer number this depends on something like (3...1).each + # doing nothing. That currently works, but is not documented behavior. + (fields0.length...larger).each { fields0 << '0' } + (fields1.length...larger).each { fields1 << '0' } + end + # Squish both arrays together bothfields = [] - sync = SyncEnumerator.new(fields1, fields2) - sync.each do |field1, field2| - bothfields << [field1, field2] - end + (0...fields0.length).each { |i| bothfields << [fields0[i], fields1[i]] } + bothfields.map! do |fields| # Convert fields of all digits from string to number to get a numeric # rather than string comparison. This ensures that 5.9 < 5.10 @@ -72,14 +71,14 @@ fields.map! { |f| f.scan(/\d+|\D+/) } # Pass back through this method to convert the numeric # entries to numbers - fields = convert_and_split(fields[0], fields[1]) + convert_and_split!(fields[0], fields[1]) end end fields end # Unsquish back to separate arrays - fields1 = bothfields.collect { |fields| fields[0] } - fields2 = bothfields.collect { |fields| fields[1] } - [fields1, fields2] + fields0.clear + fields1.clear + bothfields.each { |fields| fields0 << fields[0]; fields1 << fields[1] } end end This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |