blob: d248cbafdfc18a4156639b686b4a5a3604129d72 [file] [log] [blame]
Ed Tanousa8544a52021-09-29 14:31:13 -07001#pragma once
2
Ed Tanousbb05f222022-01-24 18:56:47 -08003#include <charconv>
Nan Zhoud5c80ad2022-07-11 01:16:31 +00004#include <iterator>
Ed Tanousa8544a52021-09-29 14:31:13 -07005#include <string_view>
6
7namespace 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.
12inline bool simpleIsDigit(const char c)
13{
14 return c >= '0' && c <= '9';
15}
16
Ed Tanousbb05f222022-01-24 18:56:47 -080017enum class ModeType
18{
19 STRING,
20 NUMBER
21};
22
Ed Tanousa8544a52021-09-29 14:31:13 -070023} // namespace details
24
Ed Tanous26ccae32023-02-16 10:28:44 -080025inline int alphanumComp(std::string_view left, std::string_view right)
Ed Tanousa8544a52021-09-29 14:31:13 -070026{
Patrick Williamsd13f4682023-05-10 17:04:19 -050027 std::string_view::const_iterator l = left.cbegin();
28 std::string_view::const_iterator r = right.cbegin();
Ed Tanousa8544a52021-09-29 14:31:13 -070029
Ed Tanousbb05f222022-01-24 18:56:47 -080030 details::ModeType mode = details::ModeType::STRING;
31
Ed Tanousa8544a52021-09-29 14:31:13 -070032 while (l != left.end() && r != right.end())
33 {
Ed Tanousbb05f222022-01-24 18:56:47 -080034 if (mode == details::ModeType::STRING)
Ed Tanousa8544a52021-09-29 14:31:13 -070035 {
Ed Tanousbb05f222022-01-24 18:56:47 -080036 // 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 Tanousa8544a52021-09-29 14:31:13 -070041 {
Ed Tanousbb05f222022-01-24 18:56:47 -080042 mode = details::ModeType::NUMBER;
43 continue;
Ed Tanousa8544a52021-09-29 14:31:13 -070044 }
Ed Tanousbb05f222022-01-24 18:56:47 -080045 // 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 Williamsd13f4682023-05-10 17:04:19 -050062 std::advance(l, 1);
63 std::advance(r, 1);
Ed Tanousa8544a52021-09-29 14:31:13 -070064 }
65 else // mode==NUMBER
66 {
67 // get the left number
68 int lInt = 0;
Ed Tanousbb05f222022-01-24 18:56:47 -080069 auto fc = std::from_chars(&(*l), &(*left.end()), lInt);
Patrick Williamsd13f4682023-05-10 17:04:19 -050070 std::advance(l, std::distance(l, fc.ptr));
Ed Tanousa8544a52021-09-29 14:31:13 -070071
72 // get the right number
73 int rInt = 0;
Ed Tanousbb05f222022-01-24 18:56:47 -080074 fc = std::from_chars(&(*r), &(*right.end()), rInt);
Patrick Williamsd13f4682023-05-10 17:04:19 -050075 std::advance(r, std::distance(r, fc.ptr));
Ed Tanousa8544a52021-09-29 14:31:13 -070076
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 Tanousbb05f222022-01-24 18:56:47 -080086 mode = details::ModeType::STRING;
Ed Tanousa8544a52021-09-29 14:31:13 -070087 }
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 Tanous8ece0e42024-01-02 13:16:50 -0800101// containers (set, map, etc)
Ed Tanousa8544a52021-09-29 14:31:13 -0700102template <class Type>
103struct AlphanumLess
104{
105 bool operator()(const Type& left, const Type& right) const
106 {
107 return alphanumComp(left, right) < 0;
108 }
109};