Skip to content

Instantly share code, notes, and snippets.

@terrbear
Created September 12, 2018 23:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save terrbear/8d308e42d7a2ae3bb7dec1e5b1c2d5fd to your computer and use it in GitHub Desktop.
Save terrbear/8d308e42d7a2ae3bb7dec1e5b1c2d5fd to your computer and use it in GitHub Desktop.
def deb_compare(left, right)
mine = left.scan(/\d+|[^\d]+/)
theirs = right.scan(/\d+|[^\d]+/)
lhs = mine.shift
rhs = theirs.shift
while lhs
if rhs.nil?
if lhs == '~'
return 1
end
return -1
end
if lhs =~ /[^\d]+/ && rhs =~ /[^\d]+/
if lhs.length >= rhs.length
lhs.scan(/./).zip(rhs.scan(/./)).each do |chars|
lc, rc = order(chars.first), order(chars.last)
if lc != rc
return lc <=> rc
end
end
else
rhs.scan(/./).zip(lhs.scan(/./)).each do |chars|
rc, lc = order(chars.first), order(chars.last)
if lc != rc
return lc <=> rc
end
end
end
end
if lhs =~ /\d+/ && rhs =~ /\d+/
if lhs != rhs
return lhs.to_i <=> rhs.to_i
end
elsif lhs =~ /\d+/
return -1
elsif rhs =~ /\d+/
return 1
end
lhs = mine.shift
rhs = theirs.shift
end
# other is longer
if rhs
last = left.scan(/\d+|[^\d]+/)
if last == '~'
return -1
end
return 1
end
return 0
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment