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