diff options
| author | Jack Nagel | 2015-03-27 20:07:50 -0400 |
|---|---|---|
| committer | Jack Nagel | 2015-03-27 20:07:50 -0400 |
| commit | 1304a2b29a3de5da2b31e1656bf772eea2a78dc1 (patch) | |
| tree | d3465e1e0acefc65101f1be6d56d8e8e882c41ee /Library | |
| parent | 7c48b549c4182941f29eee0e34f844e7e9ed31e1 (diff) | |
| download | homebrew-1304a2b29a3de5da2b31e1656bf772eea2a78dc1.tar.bz2 | |
Make Version#<=> allocation-free
Diffstat (limited to 'Library')
| -rw-r--r-- | Library/Homebrew/version.rb | 47 |
1 files changed, 32 insertions, 15 deletions
diff --git a/Library/Homebrew/version.rb b/Library/Homebrew/version.rb index 5dcfced59..cb1502070 100644 --- a/Library/Homebrew/version.rb +++ b/Library/Homebrew/version.rb @@ -201,8 +201,33 @@ class Version return 1 if head? && !other.head? return -1 if !head? && other.head? - max = [tokens.length, other.tokens.length].max - pad_to(max) <=> other.pad_to(max) + ltokens = tokens + rtokens = other.tokens + max = max(ltokens.length, rtokens.length) + l = r = 0 + + while l < max + a = ltokens[l] || NULL_TOKEN + b = rtokens[r] || NULL_TOKEN + + if a == b + l += 1 + r += 1 + next + elsif a.numeric? && b.numeric? + return a <=> b + elsif a.numeric? + return 1 if a > NULL_TOKEN + l += 1 + elsif b.numeric? + return -1 if b > NULL_TOKEN + r += 1 + else + return a <=> b + end + end + + return 0 end alias_method :eql?, :== @@ -219,22 +244,14 @@ class Version attr_reader :version - def begins_with_numeric? - tokens.first.numeric? + def tokens + @tokens ||= tokenize end - def pad_to(length) - if begins_with_numeric? - nums, rest = tokens.partition(&:numeric?) - nums.fill(NULL_TOKEN, nums.length, length - tokens.length) - nums.concat(rest) - else - tokens.dup.fill(NULL_TOKEN, tokens.length, length - tokens.length) - end - end + private - def tokens - @tokens ||= tokenize + def max(a, b) + a > b ? a : b end def tokenize |
