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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
|
// Copyright (c) 2019-2020 Dr. Colin Hirsch and Daniel Frey
// Please see LICENSE for license or visit https://github.com/taocpp/PEGTL/
#ifndef TAO_JSON_PEGTL_CONTRIB_INTEGER_HPP
#define TAO_JSON_PEGTL_CONTRIB_INTEGER_HPP
#include <cstdint>
#include <cstdlib>
#include <limits>
#include <string_view>
#include <type_traits>
#include "../ascii.hpp"
#include "../parse.hpp"
#include "../parse_error.hpp"
#include "../rules.hpp"
#include "analyze_traits.hpp"
namespace TAO_JSON_PEGTL_NAMESPACE
{
struct unsigned_rule_old
: plus< digit >
{
// Pre-3.0 version of this rule.
};
struct unsigned_rule_new
: if_then_else< one< '0' >, not_at< digit >, plus< digit > >
{
// New version that does not allow leading zeros.
};
struct signed_rule_old
: seq< opt< one< '-', '+' > >, plus< digit > >
{
// Pre-3.0 version of this rule.
};
struct signed_rule_new
: seq< opt< one< '-', '+' > >, if_then_else< one< '0' >, not_at< digit >, plus< digit > > >
{
// New version that does not allow leading zeros.
};
struct signed_rule_bis
: seq< opt< one< '-' > >, if_then_else< one< '0' >, not_at< digit >, plus< digit > > >
{};
struct signed_rule_ter
: seq< one< '-', '+' >, if_then_else< one< '0' >, not_at< digit >, plus< digit > > >
{};
namespace internal
{
[[nodiscard]] constexpr bool is_digit( const char c ) noexcept
{
// We don't use std::isdigit() because it might
// return true for other values on MS platforms.
return ( '0' <= c ) && ( c <= '9' );
}
template< typename Integer, Integer Maximum = ( std::numeric_limits< Integer >::max )() >
[[nodiscard]] constexpr bool accumulate_digit( Integer& result, const char digit ) noexcept
{
// Assumes that digit is a digit as per is_digit(); returns false on overflow.
static_assert( std::is_integral_v< Integer > );
constexpr Integer cutoff = Maximum / 10;
constexpr Integer cutlim = Maximum % 10;
const Integer c = digit - '0';
if( ( result > cutoff ) || ( ( result == cutoff ) && ( c > cutlim ) ) ) {
return false;
}
result *= 10;
result += c;
return true;
}
template< typename Integer, Integer Maximum = ( std::numeric_limits< Integer >::max )() >
[[nodiscard]] constexpr bool accumulate_digits( Integer& result, const std::string_view input ) noexcept
{
// Assumes input is a non-empty sequence of digits; returns false on overflow.
for( char c : input ) {
if( !accumulate_digit< Integer, Maximum >( result, c ) ) {
return false;
}
}
return true;
}
template< typename Integer, Integer Maximum = ( std::numeric_limits< Integer >::max )() >
[[nodiscard]] constexpr bool convert_positive( Integer& result, const std::string_view input ) noexcept
{
// Assumes result == 0 and that input is a non-empty sequence of digits; returns false on overflow.
static_assert( std::is_integral_v< Integer > );
return accumulate_digits< Integer, Maximum >( result, input );
}
template< typename Signed >
[[nodiscard]] constexpr bool convert_negative( Signed& result, const std::string_view input ) noexcept
{
// Assumes result == 0 and that input is a non-empty sequence of digits; returns false on overflow.
static_assert( std::is_signed_v< Signed > );
using Unsigned = std::make_unsigned_t< Signed >;
constexpr Unsigned maximum = static_cast< Unsigned >( ( std::numeric_limits< Signed >::max )() ) + 1;
Unsigned temporary = 0;
if( accumulate_digits< Unsigned, maximum >( temporary, input ) ) {
result = static_cast< Signed >( ~temporary ) + 1;
return true;
}
return false;
}
template< typename Unsigned, Unsigned Maximum = ( std::numeric_limits< Unsigned >::max )() >
[[nodiscard]] constexpr bool convert_unsigned( Unsigned& result, const std::string_view input ) noexcept
{
// Assumes result == 0 and that input is a non-empty sequence of digits; returns false on overflow.
static_assert( std::is_unsigned_v< Unsigned > );
return accumulate_digits< Unsigned, Maximum >( result, input );
}
template< typename Signed >
[[nodiscard]] constexpr bool convert_signed( Signed& result, const std::string_view input ) noexcept
{
// Assumes result == 0 and that input is an optional sign followed by a non-empty sequence of digits; returns false on overflow.
static_assert( std::is_signed_v< Signed > );
if( input[ 0 ] == '-' ) {
return convert_negative< Signed >( result, std::string_view( input.data() + 1, input.size() - 1 ) );
}
const auto offset = unsigned( input[ 0 ] == '+' );
return convert_positive< Signed >( result, std::string_view( input.data() + offset, input.size() - offset ) );
}
template< typename ParseInput >
[[nodiscard]] bool match_unsigned( ParseInput& in ) noexcept( noexcept( in.empty() ) )
{
if( !in.empty() ) {
const char c = in.peek_char();
if( is_digit( c ) ) {
in.bump_in_this_line();
if( c == '0' ) {
return in.empty() || ( !is_digit( in.peek_char() ) );
}
while( ( !in.empty() ) && is_digit( in.peek_char() ) ) {
in.bump_in_this_line();
}
return true;
}
}
return false;
}
template< typename ParseInput,
typename Unsigned,
Unsigned Maximum = ( std::numeric_limits< Unsigned >::max )() >
[[nodiscard]] bool match_and_convert_unsigned_with_maximum( ParseInput& in, Unsigned& st )
{
// Assumes st == 0.
if( !in.empty() ) {
char c = in.peek_char();
if( is_digit( c ) ) {
if( c == '0' ) {
in.bump_in_this_line();
return in.empty() || ( !is_digit( in.peek_char() ) );
}
do {
if( !accumulate_digit< Unsigned, Maximum >( st, c ) ) {
throw TAO_JSON_PEGTL_NAMESPACE::parse_error( "integer overflow", in ); // Consistent with "as if" an action was doing the conversion.
}
in.bump_in_this_line();
} while( ( !in.empty() ) && is_digit( c = in.peek_char() ) );
return true;
}
}
return false;
}
} // namespace internal
struct unsigned_action
{
// Assumes that 'in' contains a non-empty sequence of ASCII digits.
template< typename ActionInput, typename Unsigned >
static void apply( const ActionInput& in, Unsigned& st )
{
// This function "only" offers basic exception safety.
st = 0;
if( !internal::convert_unsigned( st, in.string_view() ) ) {
throw parse_error( "unsigned integer overflow", in );
}
}
};
struct unsigned_rule
{
using rule_t = unsigned_rule;
using subs_t = empty_list;
template< typename ParseInput >
[[nodiscard]] static bool match( ParseInput& in ) noexcept( noexcept( in.empty() ) )
{
return internal::match_unsigned( in ); // Does not check for any overflow.
}
};
struct unsigned_rule_with_action
{
using rule_t = unsigned_rule_with_action;
using subs_t = empty_list;
template< apply_mode A,
rewind_mode M,
template< typename... >
class Action,
template< typename... >
class Control,
typename ParseInput,
typename... States >
[[nodiscard]] static auto match( ParseInput& in, States&&... /*unused*/ ) noexcept( noexcept( in.empty() ) ) -> std::enable_if_t< A == apply_mode::nothing, bool >
{
return internal::match_unsigned( in ); // Does not check for any overflow.
}
template< apply_mode A,
rewind_mode M,
template< typename... >
class Action,
template< typename... >
class Control,
typename ParseInput,
typename Unsigned >
[[nodiscard]] static auto match( ParseInput& in, Unsigned& st ) -> std::enable_if_t< ( A == apply_mode::action ) && std::is_unsigned_v< Unsigned >, bool >
{
// This function "only" offers basic exception safety.
st = 0;
return internal::match_and_convert_unsigned_with_maximum( in, st ); // Throws on overflow.
}
};
template< typename Unsigned, Unsigned Maximum >
struct maximum_action
{
// Assumes that 'in' contains a non-empty sequence of ASCII digits.
static_assert( std::is_unsigned_v< Unsigned > );
template< typename ActionInput, typename Unsigned2 >
static void apply( const ActionInput& in, Unsigned2& st )
{
// This function "only" offers basic exception safety.
st = 0;
if( !internal::convert_unsigned< Unsigned, Maximum >( st, in.string_view() ) ) {
throw parse_error( "unsigned integer overflow", in );
}
}
};
template< typename Unsigned, Unsigned Maximum = ( std::numeric_limits< Unsigned >::max )() >
struct maximum_rule
{
using rule_t = maximum_rule;
using subs_t = empty_list;
static_assert( std::is_unsigned_v< Unsigned > );
template< typename ParseInput >
[[nodiscard]] static bool match( ParseInput& in )
{
Unsigned st = 0;
return internal::match_and_convert_unsigned_with_maximum< ParseInput, Unsigned, Maximum >( in, st ); // Throws on overflow.
}
};
template< typename Unsigned, Unsigned Maximum = ( std::numeric_limits< Unsigned >::max )() >
struct maximum_rule_with_action
{
using rule_t = maximum_rule_with_action;
using subs_t = empty_list;
static_assert( std::is_unsigned_v< Unsigned > );
template< apply_mode A,
rewind_mode M,
template< typename... >
class Action,
template< typename... >
class Control,
typename ParseInput,
typename... States >
[[nodiscard]] static auto match( ParseInput& in, States&&... /*unused*/ ) -> std::enable_if_t< A == apply_mode::nothing, bool >
{
Unsigned st = 0;
return internal::match_and_convert_unsigned_with_maximum< ParseInput, Unsigned, Maximum >( in, st ); // Throws on overflow.
}
template< apply_mode A,
rewind_mode M,
template< typename... >
class Action,
template< typename... >
class Control,
typename ParseInput,
typename Unsigned2 >
[[nodiscard]] static auto match( ParseInput& in, Unsigned2& st ) -> std::enable_if_t< ( A == apply_mode::action ) && std::is_same_v< Unsigned, Unsigned2 >, bool >
{
// This function "only" offers basic exception safety.
st = 0;
return internal::match_and_convert_unsigned_with_maximum< ParseInput, Unsigned, Maximum >( in, st ); // Throws on overflow.
}
};
struct signed_action
{
// Assumes that 'in' contains a non-empty sequence of ASCII digits,
// with optional leading sign; with sign, in.size() must be >= 2.
template< typename ActionInput, typename Signed >
static void apply( const ActionInput& in, Signed& st )
{
// This function "only" offers basic exception safety.
st = 0;
if( !internal::convert_signed( st, in.string_view() ) ) {
throw parse_error( "signed integer overflow", in );
}
}
};
struct signed_rule
{
using rule_t = signed_rule;
using subs_t = empty_list;
template< typename ParseInput >
[[nodiscard]] static bool match( ParseInput& in ) noexcept( noexcept( in.empty() ) )
{
return TAO_JSON_PEGTL_NAMESPACE::parse< signed_rule_new >( in ); // Does not check for any overflow.
}
};
namespace internal
{
template< typename Rule >
struct signed_action_action
: nothing< Rule >
{};
template<>
struct signed_action_action< signed_rule_new >
: signed_action
{};
} // namespace internal
struct signed_rule_with_action
{
using rule_t = signed_rule_with_action;
using subs_t = empty_list;
template< apply_mode A,
rewind_mode M,
template< typename... >
class Action,
template< typename... >
class Control,
typename ParseInput,
typename... States >
[[nodiscard]] static auto match( ParseInput& in, States&&... /*unused*/ ) noexcept( noexcept( in.empty() ) ) -> std::enable_if_t< A == apply_mode::nothing, bool >
{
return TAO_JSON_PEGTL_NAMESPACE::parse< signed_rule_new >( in ); // Does not check for any overflow.
}
template< apply_mode A,
rewind_mode M,
template< typename... >
class Action,
template< typename... >
class Control,
typename ParseInput,
typename Signed >
[[nodiscard]] static auto match( ParseInput& in, Signed& st ) -> std::enable_if_t< ( A == apply_mode::action ) && std::is_signed_v< Signed >, bool >
{
return TAO_JSON_PEGTL_NAMESPACE::parse< signed_rule_new, internal::signed_action_action >( in, st ); // Throws on overflow.
}
};
template< typename Name >
struct analyze_traits< Name, unsigned_rule >
: analyze_any_traits<>
{};
template< typename Name >
struct analyze_traits< Name, unsigned_rule_with_action >
: analyze_any_traits<>
{};
template< typename Name, typename Integer, Integer Maximum >
struct analyze_traits< Name, maximum_rule< Integer, Maximum > >
: analyze_any_traits<>
{};
template< typename Name, typename Integer, Integer Maximum >
struct analyze_traits< Name, maximum_rule_with_action< Integer, Maximum > >
: analyze_any_traits<>
{};
template< typename Name >
struct analyze_traits< Name, signed_rule >
: analyze_any_traits<>
{};
template< typename Name >
struct analyze_traits< Name, signed_rule_with_action >
: analyze_any_traits<>
{};
} // namespace TAO_JSON_PEGTL_NAMESPACE
#endif
|