blob: 02018b1e14646b448b0d99efc4e2889ea2bf6436 [file] [log] [blame]
Ed Tanousa8544a52021-09-29 14:31:13 -07001#pragma once
2
Ed Tanousbb05f222022-01-24 18:56:47 -08003#include <charconv>
Ed Tanousa8544a52021-09-29 14:31:13 -07004#include <string_view>
5
6namespace 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.
11inline bool simpleIsDigit(const char c)
12{
13 return c >= '0' && c <= '9';
14}
15
Ed Tanousbb05f222022-01-24 18:56:47 -080016enum class ModeType
17{
18 STRING,
19 NUMBER
20};
21
Ed Tanousa8544a52021-09-29 14:31:13 -070022} // namespace details
23
24inline int alphanumComp(const std::string_view left,
25 const std::string_view right)
26{
Ed Tanousa8544a52021-09-29 14:31:13 -070027
28 std::string_view::const_iterator l = left.begin();
29 std::string_view::const_iterator r = right.begin();
30
Ed Tanousbb05f222022-01-24 18:56:47 -080031 details::ModeType mode = details::ModeType::STRING;
32
Ed Tanousa8544a52021-09-29 14:31:13 -070033 while (l != left.end() && r != right.end())
34 {
Ed Tanousbb05f222022-01-24 18:56:47 -080035 if (mode == details::ModeType::STRING)
Ed Tanousa8544a52021-09-29 14:31:13 -070036 {
Ed Tanousbb05f222022-01-24 18:56:47 -080037 // 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 Tanousa8544a52021-09-29 14:31:13 -070042 {
Ed Tanousbb05f222022-01-24 18:56:47 -080043 mode = details::ModeType::NUMBER;
44 continue;
Ed Tanousa8544a52021-09-29 14:31:13 -070045 }
Ed Tanousbb05f222022-01-24 18:56:47 -080046 // 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 Tanousa8544a52021-09-29 14:31:13 -070065 }
66 else // mode==NUMBER
67 {
68 // get the left number
69 int lInt = 0;
Ed Tanousbb05f222022-01-24 18:56:47 -080070 auto fc = std::from_chars(&(*l), &(*left.end()), lInt);
71 l += std::distance(l, fc.ptr);
Ed Tanousa8544a52021-09-29 14:31:13 -070072
73 // get the right number
74 int rInt = 0;
Ed Tanousbb05f222022-01-24 18:56:47 -080075 fc = std::from_chars(&(*r), &(*right.end()), rInt);
76 r += std::distance(r, fc.ptr);
Ed Tanousa8544a52021-09-29 14:31:13 -070077
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 Tanousbb05f222022-01-24 18:56:47 -080087 mode = details::ModeType::STRING;
Ed Tanousa8544a52021-09-29 14:31:13 -070088 }
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)
103template <class Type>
104struct AlphanumLess
105{
106 bool operator()(const Type& left, const Type& right) const
107 {
108 return alphanumComp(left, right) < 0;
109 }
110};