Skip to content

Commit

Permalink
Natural ordering of @goal @glob is broken #64
Browse files Browse the repository at this point in the history
  • Loading branch information
xonixx committed Oct 11, 2021
1 parent 696b853 commit 2830076
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions makesure.awk
Original file line number Diff line number Diff line change
Expand Up @@ -651,25 +651,20 @@ function dl(url, dest, verbose) {
# s1 > s2 -> 1
# s1== s2 -> 0
# s1 < s2 -> -1
function natOrder(s1,s2, i1,i2, c1, c2, n1,n2, l1, l2) {
l1 = length(s1); l2 = length(s2)
function natOrder(s1,s2, i1,i2, c1, c2, n1,n2) {
if (_digit(c1 = substr(s1,i1,1)) && _digit(c2 = substr(s2,i2,1))) {
n1 = +c1; while(_digit(c1 = substr(s1,++i1,1))) { n1 = n1 * 10 + c1 }
n2 = +c2; while(_digit(c2 = substr(s2,++i2,1))) { n2 = n2 * 10 + c2 }

if (i1 == l1+1 || i2 == l2+1)
return _cmp(l1-i1, l2-i2)
return n1 == n2 ? natOrder(s1, s2, i1, i2) : _cmp(n1, n2)
}

while ((c1 = substr(s1,i1,1)) == (c2 = substr(s2,i2,1))) {
# consume till equal substrings
while ((c1 = substr(s1,i1,1)) == (c2 = substr(s2,i2,1)) && c1 != "") {
i1++; i2++
if (i1>l1 || i2>l2)
return _cmp(l1-i1, l2-i2)
}

if (!_digit(c1) || !_digit(c2))
return _cmp(c1, c2)

n1 = 0; while(_digit(c1 = substr(s1,i1++,1))) { n1 = n1 * 10 + c1 }
n2 = 0; while(_digit(c2 = substr(s2,i2++,1))) { n2 = n2 * 10 + c2 }

return n1 == n2 ? natOrder(s1, s2, i1, i2) : _cmp(n1, n2)
return _digit(c1) && _digit(c2) ? natOrder(s1, s2, i1, i2) : _cmp(c1, c2)
}
function _cmp(v1, v2) { return v1 > v2 ? 1 : v1 < v2 ? -1 : 0 }
function _digit(c) { return c >= "0" && c <= "9" }
Expand All @@ -679,7 +674,7 @@ function quicksort(data, left, right, i, last) {
quicksortSwap(data, left, int((left + right) / 2))
last = left
for (i = left + 1; i <= right; i++)
if (natOrder(data[i], data[left]) < 1)
if (natOrder(data[i], data[left],1,1) < 1)
quicksortSwap(data, ++last, i)
quicksortSwap(data, left, last)
quicksort(data, left, last - 1)
Expand Down

0 comments on commit 2830076

Please sign in to comment.