Created
September 12, 2018 23:48
-
-
Save terrbear/8d308e42d7a2ae3bb7dec1e5b1c2d5fd to your computer and use it in GitHub Desktop.
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
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