blob: f4dfbe03c7c22b29117c9e9f66b34b0b0ee03d98 [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
25inline int alphanumComp(const std::string_view left,
26 const std::string_view right)
27{
Ed Tanousa8544a52021-09-29 14:31:13 -070028
29 std::string_view::const_iterator l = left.begin();
30 std::string_view::const_iterator r = right.begin();
31
Ed Tanousbb05f222022-01-24 18:56:47 -080032 details::ModeType mode = details::ModeType::STRING;
33
Ed Tanousa8544a52021-09-29 14:31:13 -070034 while (l != left.end() && r != right.end())
35 {
Ed Tanousbb05f222022-01-24 18:56:47 -080036 if (mode == details::ModeType::STRING)
Ed Tanousa8544a52021-09-29 14:31:13 -070037 {
Ed Tanousbb05f222022-01-24 18:56:47 -080038 // 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 Tanousa8544a52021-09-29 14:31:13 -070043 {
Ed Tanousbb05f222022-01-24 18:56:47 -080044 mode = details::ModeType::NUMBER;
45 continue;
Ed Tanousa8544a52021-09-29 14:31:13 -070046 }
Ed Tanousbb05f222022-01-24 18:56:47 -080047 // 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
64 l++;
65 r++;
Ed Tanousa8544a52021-09-29 14:31:13 -070066 }
67 else // mode==NUMBER
68 {
69 // get the left number
70 int lInt = 0;
Ed Tanousbb05f222022-01-24 18:56:47 -080071 auto fc = std::from_chars(&(*l), &(*left.end()), lInt);
72 l += std::distance(l, fc.ptr);
Ed Tanousa8544a52021-09-29 14:31:13 -070073
74 // get the right number
75 int rInt = 0;
Ed Tanousbb05f222022-01-24 18:56:47 -080076 fc = std::from_chars(&(*r), &(*right.end()), rInt);
77 r += std::distance(r, fc.ptr);
Ed Tanousa8544a52021-09-29 14:31:13 -070078
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 Tanousbb05f222022-01-24 18:56:47 -080088 mode = details::ModeType::STRING;
Ed Tanousa8544a52021-09-29 14:31:13 -070089 }
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
103// containers (set, map, ect)
104template <class Type>
105struct AlphanumLess
106{
107 bool operator()(const Type& left, const Type& right) const
108 {
109 return alphanumComp(left, right) < 0;
110 }
111};