/
ascii_numeric_string.hpp
76 lines (66 loc) · 2.04 KB
/
ascii_numeric_string.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright (c) 2019 Dr. Colin Hirsch and Daniel Frey
// Please see LICENSE for license or visit https://github.com/taocpp/PEGTL/
#ifndef TAO_PEGTL_CONTRIB_ASCII_NUMERIC_HPP
#define TAO_PEGTL_CONTRIB_ASCII_NUMERIC_HPP
#include <algorithm>
#include <cctype>
#include <cstdint>
#include <cstdlib>
#include <string>
#include "../analysis/generic.hpp"
#include "../config.hpp"
namespace TAO_PEGTL_NAMESPACE
{
namespace internal
{
constexpr std::size_t digits( const std::uint64_t N ) noexcept
{
return ( N < 10 ) ? 1 : ( 1 + digits( N / 10 ) );
}
} // namespace internal
inline namespace ascii
{
template< std::uint64_t Maximum >
struct numeric_string
{
using analyze_t = analysis::generic< analysis::rule_type::any >;
template< typename Input >
[[nodiscard]] static bool match( Input& in ) noexcept( noexcept( in.size( 0 ) ) )
{
if( in.empty() ) {
return false;
}
const auto c = in.peek_char( 0 );
if( !std::isdigit( c ) ) {
return false;
}
if( c == '0' ) {
in.bump_in_this_line( 1 );
return true;
}
constexpr auto d = internal::digits( Maximum );
const auto s = ( std::min )( d, in.size( d ) );
std::size_t p = 1;
while( ( p < s ) && std::isdigit( in.peek_char( p ) ) ) {
++p;
}
if( p == s ) {
const std::string v( in.current(), p );
errno = 0;
const auto r = std::strtoull( v.c_str(), nullptr, 10 );
if( ( errno == ERANGE ) || !( r <= Maximum ) ) {
if( p == 1 ) {
return false;
}
else {
--p;
}
}
}
in.bump_in_this_line( p );
return true;
}
};
} // namespace ascii
} // namespace TAO_PEGTL_NAMESPACE
#endif