blob: 7ab326d1a185429c87d4c5a960e761b1c00c890b [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{
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};