| 1 | package irelate |
| 2 | |
| 3 | // This is taken from vbom.ml/util/sortorder/natsort.go and put here due to |
| 4 | // Installation issues on go 1.5. Here is the License from that repo: |
| 5 | /* |
| 6 | The MIT License (MIT) |
| 7 | Copyright (c) 2015 Frits van Bommel |
| 8 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | of this software and associated documentation files (the "Software"), to deal |
| 10 | in the Software without restriction, including without limitation the rights |
| 11 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | copies of the Software, and to permit persons to whom the Software is |
| 13 | furnished to do so, subject to the following conditions: |
| 14 | The above copyright notice and this permission notice shall be included in all |
| 15 | copies or substantial portions of the Software. |
| 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | func isdigit(b byte) bool { return '0' <= b && b <= '9' } |
| 26 | |
| 27 | // NaturalLess compares two strings using natural ordering. This means that e.g. |
| 28 | // "abc2" < "abc12". |
| 29 | // |
| 30 | // Non-digit sequences and numbers are compared separately. The former are |
| 31 | // compared bytewise, while the latter are compared numerically (except that |
| 32 | // the number of leading zeros is used as a tie-breaker, so e.g. "2" < "02") |
| 33 | // |
| 34 | // Limitation: only ASCII digits (0-9) are considered. |
| 35 | func NaturalLess(str1, str2 string) bool { |
| 36 | idx1, idx2 := 0, 0 |
| 37 | for idx1 < len(str1) && idx2 < len(str2) { |
| 38 | c1, c2 := str1[idx1], str2[idx2] |
| 39 | dig1, dig2 := isdigit(c1), isdigit(c2) |
| 40 | switch { |
| 41 | case dig1 != dig2: // Digits before other characters. |
| 42 | return dig1 // True if LHS is a digit, false if the RHS is one. |
| 43 | case !dig1: // && !dig2, because dig1 == dig2 |
| 44 | // UTF-8 compares bytewise-lexicographically, no need to decode |
| 45 | // codepoints. |
| 46 | if c1 != c2 { |
| 47 | return c1 < c2 |
| 48 | } |
| 49 | idx1++ |
| 50 | idx2++ |
| 51 | default: // Digits |
| 52 | // Eat zeros. |
| 53 | for ; idx1 < len(str1) && str1[idx1] == '0'; idx1++ { |
| 54 | } |
| 55 | for ; idx2 < len(str2) && str2[idx2] == '0'; idx2++ { |
| 56 | } |
| 57 | // Eat all digits. |
| 58 | nonZero1, nonZero2 := idx1, idx2 |
| 59 | for ; idx1 < len(str1) && isdigit(str1[idx1]); idx1++ { |
| 60 | } |
| 61 | for ; idx2 < len(str2) && isdigit(str2[idx2]); idx2++ { |
| 62 | } |
| 63 | // If lengths of numbers with non-zero prefix differ, the shorter |
| 64 | // one is less. |
| 65 | if len1, len2 := idx1-nonZero1, idx2-nonZero2; len1 != len2 { |
| 66 | return len1 < len2 |
| 67 | } |
| 68 | // If they're not equal, string comparison is correct. |
| 69 | if nr1, nr2 := str1[nonZero1:idx1], str2[nonZero2:idx2]; nr1 != nr2 { |
| 70 | return nr1 < nr2 |
| 71 | } |
| 72 | // Otherwise, the one with less zeros is less. |
| 73 | // Because everything up to the number is equal, comparing the index |
| 74 | // after the zeros is sufficient. |
| 75 | if nonZero1 != nonZero2 { |
| 76 | return nonZero1 < nonZero2 |
| 77 | } |
| 78 | } |
| 79 | // They're identical so far, so continue comparing. |
| 80 | } |
| 81 | // So far they are identical. At least one is ended. If the other continues, |
| 82 | // it sorts last. |
| 83 | return len(str1) < len(str2) |
| 84 | } |