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 
25 namespace 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(std::string_view ll, 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* r = rr.data();
59  while(l != ll.end() && r != rr.end() && *l && *r)
60  {
61  if(mode == STRING)
62  {
63  char l_char{}, r_char{};
64  while((l != ll.end() && r != rr.end()) && (l_char = *l) && (r_char = *r))
65  {
66  // check if this are digit characters
67  const bool l_digit = alphanum_isdigit(l_char),
68  r_digit = alphanum_isdigit(r_char);
69  // if both characters are digits, we continue in NUMBER mode
70  if(l_digit && r_digit)
71  {
72  mode = NUMBER;
73  break;
74  }
75  // if only the left character is a digit, we have a result
76  if(l_digit)
77  return -1;
78  // if only the right character is a digit, we have a result
79  if(r_digit)
80  return +1;
81  // compute the difference of both characters
82  const int diff = l_char - r_char;
83  // if they differ we have a result
84  if(diff != 0)
85  return diff;
86  // otherwise process the next characters
87  ++l;
88  ++r;
89  }
90  }
91  else // mode==NUMBER
92  {
93  // get the left number
94  unsigned long l_int = 0;
95  while(l != ll.end() && *l && alphanum_isdigit(*l))
96  {
97  // TODO: this can overflow
98  l_int = l_int * 10 + *l - '0';
99  ++l;
100  }
101 
102  // get the right number
103  unsigned long r_int = 0;
104  while(r != rr.end() && *r && alphanum_isdigit(*r))
105  {
106  // TODO: this can overflow
107  r_int = r_int * 10 + *r - '0';
108  ++r;
109  }
110 
111  // if the difference is not equal to zero, we have a comparison result
112  const long diff = l_int - r_int;
113  if(diff != 0)
114  return diff;
115 
116  // otherwise we process the next substring in STRING mode
117  mode = STRING;
118  }
119  }
120 
121  if(*r)
122  return -1;
123  if(*l)
124  return +1;
125  return 0;
126  }
127 
128  constexpr bool operator()(std::string_view l, std::string_view r) const noexcept
129  {
130  return impl(l.data(), r.data()) < 0;
131  }
132 };
133 }
Definition: Alphanum.hpp:28
static constexpr int impl(std::string_view ll, std::string_view rr) noexcept
Definition: Alphanum.hpp:48