blob: 5cb6e562838bb667d3283d3e8368e87ae0e89e2b [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 std::string_view::const_iterator l = left.begin();
28 std::string_view::const_iterator r = right.begin();
29
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
62 l++;
63 r++;
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);
70 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);
75 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
101// containers (set, map, ect)
102template <class Type>
103struct AlphanumLess
104{
105 bool operator()(const Type& left, const Type& right) const
106 {
107 return alphanumComp(left, right) < 0;
108 }
109};