Ed Tanous | a8544a5 | 2021-09-29 14:31:13 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Ed Tanous | bb05f22 | 2022-01-24 18:56:47 -0800 | [diff] [blame] | 3 | #include <charconv> |
Ed Tanous | a8544a5 | 2021-09-29 14:31:13 -0700 | [diff] [blame] | 4 | #include <string_view> |
| 5 | |
| 6 | namespace details |
| 7 | { |
| 8 | |
| 9 | // This implementation avoids the complexity of using std::isdigit, which pulls |
| 10 | // in all of <locale>, and likely has other consequences. |
| 11 | inline bool simpleIsDigit(const char c) |
| 12 | { |
| 13 | return c >= '0' && c <= '9'; |
| 14 | } |
| 15 | |
Ed Tanous | bb05f22 | 2022-01-24 18:56:47 -0800 | [diff] [blame] | 16 | enum class ModeType |
| 17 | { |
| 18 | STRING, |
| 19 | NUMBER |
| 20 | }; |
| 21 | |
Ed Tanous | a8544a5 | 2021-09-29 14:31:13 -0700 | [diff] [blame] | 22 | } // namespace details |
| 23 | |
| 24 | inline int alphanumComp(const std::string_view left, |
| 25 | const std::string_view right) |
| 26 | { |
Ed Tanous | a8544a5 | 2021-09-29 14:31:13 -0700 | [diff] [blame] | 27 | |
| 28 | std::string_view::const_iterator l = left.begin(); |
| 29 | std::string_view::const_iterator r = right.begin(); |
| 30 | |
Ed Tanous | bb05f22 | 2022-01-24 18:56:47 -0800 | [diff] [blame] | 31 | details::ModeType mode = details::ModeType::STRING; |
| 32 | |
Ed Tanous | a8544a5 | 2021-09-29 14:31:13 -0700 | [diff] [blame] | 33 | while (l != left.end() && r != right.end()) |
| 34 | { |
Ed Tanous | bb05f22 | 2022-01-24 18:56:47 -0800 | [diff] [blame] | 35 | if (mode == details::ModeType::STRING) |
Ed Tanous | a8544a5 | 2021-09-29 14:31:13 -0700 | [diff] [blame] | 36 | { |
Ed Tanous | bb05f22 | 2022-01-24 18:56:47 -0800 | [diff] [blame] | 37 | // check if this are digit characters |
| 38 | const bool lDigit = details::simpleIsDigit(*l); |
| 39 | const bool rDigit = details::simpleIsDigit(*r); |
| 40 | // if both characters are digits, we continue in NUMBER mode |
| 41 | if (lDigit && rDigit) |
Ed Tanous | a8544a5 | 2021-09-29 14:31:13 -0700 | [diff] [blame] | 42 | { |
Ed Tanous | bb05f22 | 2022-01-24 18:56:47 -0800 | [diff] [blame] | 43 | mode = details::ModeType::NUMBER; |
| 44 | continue; |
Ed Tanous | a8544a5 | 2021-09-29 14:31:13 -0700 | [diff] [blame] | 45 | } |
Ed Tanous | bb05f22 | 2022-01-24 18:56:47 -0800 | [diff] [blame] | 46 | // if only the left character is a digit, we have a result |
| 47 | if (lDigit) |
| 48 | { |
| 49 | return -1; |
| 50 | } // if only the right character is a digit, we have a result |
| 51 | if (rDigit) |
| 52 | { |
| 53 | return +1; |
| 54 | } |
| 55 | // compute the difference of both characters |
| 56 | const int diff = *l - *r; |
| 57 | // if they differ we have a result |
| 58 | if (diff != 0) |
| 59 | { |
| 60 | return diff; |
| 61 | } |
| 62 | // otherwise process the next characters |
| 63 | l++; |
| 64 | r++; |
Ed Tanous | a8544a5 | 2021-09-29 14:31:13 -0700 | [diff] [blame] | 65 | } |
| 66 | else // mode==NUMBER |
| 67 | { |
| 68 | // get the left number |
| 69 | int lInt = 0; |
Ed Tanous | bb05f22 | 2022-01-24 18:56:47 -0800 | [diff] [blame] | 70 | auto fc = std::from_chars(&(*l), &(*left.end()), lInt); |
| 71 | l += std::distance(l, fc.ptr); |
Ed Tanous | a8544a5 | 2021-09-29 14:31:13 -0700 | [diff] [blame] | 72 | |
| 73 | // get the right number |
| 74 | int rInt = 0; |
Ed Tanous | bb05f22 | 2022-01-24 18:56:47 -0800 | [diff] [blame] | 75 | fc = std::from_chars(&(*r), &(*right.end()), rInt); |
| 76 | r += std::distance(r, fc.ptr); |
Ed Tanous | a8544a5 | 2021-09-29 14:31:13 -0700 | [diff] [blame] | 77 | |
| 78 | // if the difference is not equal to zero, we have a comparison |
| 79 | // result |
| 80 | const int diff = lInt - rInt; |
| 81 | if (diff != 0) |
| 82 | { |
| 83 | return diff; |
| 84 | } |
| 85 | |
| 86 | // otherwise we process the next substring in STRING mode |
Ed Tanous | bb05f22 | 2022-01-24 18:56:47 -0800 | [diff] [blame] | 87 | mode = details::ModeType::STRING; |
Ed Tanous | a8544a5 | 2021-09-29 14:31:13 -0700 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | if (r == right.end() && l == left.end()) |
| 91 | { |
| 92 | return 0; |
| 93 | } |
| 94 | if (r == right.end()) |
| 95 | { |
| 96 | return 1; |
| 97 | } |
| 98 | return -1; |
| 99 | } |
| 100 | |
| 101 | // A generic template type compatible with std::less that can be used on generic |
| 102 | // containers (set, map, ect) |
| 103 | template <class Type> |
| 104 | struct AlphanumLess |
| 105 | { |
| 106 | bool operator()(const Type& left, const Type& right) const |
| 107 | { |
| 108 | return alphanumComp(left, right) < 0; |
| 109 | } |
| 110 | }; |