DAW JSON Link
Loading...
Searching...
No Matches
daw_count_digits.h
Go to the documentation of this file.
1// Copyright (c) Darrell Wright
2//
3// Distributed under the Boost Software License, Version 1.0. (See accompanying
4// file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
5//
6// Official repository: https://github.com/beached/daw_json_link
7//
8
9#pragma once
10
11#include "version.h"
12
13#include <daw/daw_attributes.h>
14#include <daw/daw_cxmath.h>
15#include <daw/daw_simple_array.h>
16#include <daw/daw_uint_buffer.h>
17
18#include <cstdint>
19
20namespace daw::json {
21 inline namespace DAW_JSON_VER {
22 namespace json_details {
23 inline constexpr auto is_digit =
24 []( char c ) DAW_JSON_CPP23_STATIC_CALL_OP -> daw::UInt8 {
25 return static_cast<unsigned>( static_cast<unsigned char>( c ) ) -
26 static_cast<unsigned>(
27 static_cast<unsigned char>( '0' ) ) <
28 10U
29 ? daw::UInt8{ 0 }
30 : daw::UInt8{ 0xFFU };
31 };
32
33 template<typename Predicate>
34 DAW_ATTRIB_FLATINLINE DAW_ATTRIB_NONNULL( ) constexpr std::int32_t
35 count_4digits( daw::not_null<char const *> first, Predicate pred ) {
36 daw::simple_array<daw::UInt8, 4> const buff{ pred( first[3] ),
37 pred( first[2] ),
38 pred( first[2] ),
39 pred( first[1] ) };
40 auto const v = DAW_BIT_CAST( std::uint32_t, buff );
41 if( v != 0 ) {
42 auto result = daw::cxmath::count_leading_zeroes( v );
43 result /= 8;
44 return static_cast<std::int32_t>( result );
45 }
46 return -1;
47 }
48
49 template<typename Predicate>
50 DAW_ATTRIB_FLATINLINE DAW_ATTRIB_NONNULL( ) constexpr std::int32_t
51 count_8digits( daw::not_null<char const *> first, Predicate pred ) {
52 daw::simple_array<daw::UInt8, 8> const buff{ pred( first[7] ),
53 pred( first[6] ),
54 pred( first[5] ),
55 pred( first[4] ),
56 pred( first[3] ),
57 pred( first[2] ),
58 pred( first[1] ),
59 pred( first[0] ) };
60
61 auto const v = DAW_BIT_CAST( std::uint64_t, buff );
62 if( v != 0 ) {
63 auto result = daw::cxmath::count_leading_zeroes( v );
64 result /= 8;
65 return static_cast<std::int32_t>( result );
66 }
67 return -1;
68 }
69
70 DAW_ATTRIB_FLATTEN
71 constexpr daw::not_null<char const *>
72 count_digits( daw::not_null<char const *> first,
73 daw::not_null<char const *> last ) {
74 while( DAW_LIKELY( last - first >= 8 ) ) {
75 auto const v = count_8digits( first, is_digit );
76 if( v >= 0 ) {
77 return first + v;
78 }
79 first += 8;
80 }
81 while( last - first >= 4 ) {
82 auto const v = count_4digits( first, is_digit );
83 if( v >= 0 ) {
84 return first + v;
85 }
86 first += 4;
87 }
88
89 while( first != last ) {
90 if( static_cast<unsigned>( *first ) -
91 static_cast<unsigned>( static_cast<unsigned char>( '0' ) ) >=
92 10U ) {
93 return first;
94 }
95 ++first;
96 }
97 return first;
98 }
99 } // namespace json_details
100 } // namespace DAW_JSON_VER
101} // namespace daw::json
#define DAW_JSON_CPP23_STATIC_CALL_OP
Customization point traits.
#define DAW_JSON_VER
The version string used in namespace definitions. Must be a valid namespace name.
Definition version.h:20