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