Loading...
Searching...
No Matches
Alphanum.hpp
1#pragma once
2/*
3 Released under the MIT License - https://opensource.org/licenses/MIT
4
5 Permission is hereby granted, free of charge, to any person obtaining
6 a copy of this software and associated documentation files (the "Software"),
7 to deal in the Software without restriction, including without limitation
8 the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 and/or sell copies of the Software, and to permit persons to whom the
10 Software is furnished to do so, subject to the following conditions:
11
12 The above copyright notice and this permission notice shall be included
13 in all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
19 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 USE OR OTHER DEALINGS IN THE SOFTWARE.
22*/
23#include <string_view>
24
25namespace doj
26{
28{
29 static constexpr bool alphanum_isdigit(const char c) noexcept
30 {
31 return c >= '0' && c <= '9';
32 }
48 static constexpr int impl(const std::string_view ll, const std::string_view rr) noexcept
49 {
50 enum mode_t
51 {
52 STRING,
53 NUMBER
54 } mode
55 = STRING;
56
57 const char* l = ll.data();
58 const char* ll_end = ll.data() + ll.size();
59 const char* r = rr.data();
60 const char* rr_end = rr.data() + rr.size();
61 while(l != ll_end && r != rr_end && *l && *r)
62 {
63 if(mode == STRING)
64 {
65 char l_char{}, r_char{};
66 while((l != ll_end && r != rr_end) && (l_char = *l) && (r_char = *r))
67 {
68 // check if this are digit characters
69 const bool l_digit = alphanum_isdigit(l_char),
70 r_digit = alphanum_isdigit(r_char);
71 // if both characters are digits, we continue in NUMBER mode
72 if(l_digit && r_digit)
73 {
74 mode = NUMBER;
75 break;
76 }
77 // if only the left character is a digit, we have a result
78 if(l_digit)
79 return -1;
80 // if only the right character is a digit, we have a result
81 if(r_digit)
82 return +1;
83 // compute the difference of both characters
84 const int diff = l_char - r_char;
85 // if they differ we have a result
86 if(diff != 0)
87 return diff;
88 // otherwise process the next characters
89 ++l;
90 ++r;
91 }
92 }
93 else // mode==NUMBER
94 {
95 // get the left number
96 unsigned long l_int = 0;
97 while(l != ll_end && *l && alphanum_isdigit(*l))
98 {
99 // TODO: this can overflow
100 l_int = l_int * 10 + *l - '0';
101 ++l;
102 }
103
104 // get the right number
105 unsigned long r_int = 0;
106 while(r != rr_end && *r && alphanum_isdigit(*r))
107 {
108 // TODO: this can overflow
109 r_int = r_int * 10 + *r - '0';
110 ++r;
111 }
112
113 // if the difference is not equal to zero, we have a comparison result
114 const long diff = l_int - r_int;
115 if(diff != 0)
116 return diff;
117
118 // otherwise we process the next substring in STRING mode
119 mode = STRING;
120 }
121 }
122
123 if(*r)
124 return -1;
125 if(*l)
126 return +1;
127 return 0;
128 }
129
130 constexpr bool operator()(std::string_view l, std::string_view r) const noexcept
131 {
132 return impl(l.data(), r.data()) < 0;
133 }
134};
135}
Definition Alphanum.hpp:28
static constexpr int impl(const std::string_view ll, const std::string_view rr) noexcept
Definition Alphanum.hpp:48