Coverage Report

Created: 2025-08-25 06:25

/src/jsoncons/include/jsoncons/detail/span.hpp
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2013-2025 Daniel Parker
2
// Distributed under the Boost license, Version 1.0.
3
// (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
4
5
// See https://github.com/danielaparker/jsoncons for latest version
6
7
#ifndef JSONCONS_DETAIL_SPAN_HPP
8
#define JSONCONS_DETAIL_SPAN_HPP
9
10
#include <array>
11
#include <cstddef>
12
#include <iterator>
13
#include <limits>
14
#include <memory> // std::addressof
15
#include <type_traits> // std::enable_if, std::true_type, std::false_type
16
17
#include <jsoncons/utility/more_type_traits.hpp>
18
19
namespace jsoncons {
20
namespace detail {
21
22
    constexpr std::size_t dynamic_extent = (std::numeric_limits<std::size_t>::max)();
23
24
    template< typename T, std::size_t Extent = dynamic_extent>
25
    class span;
26
27
    template <typename T>
28
    struct is_span : std::false_type{};
29
30
    template< typename T>
31
    struct is_span<span<T>> : std::true_type{};
32
33
    template<
34
        typename T,
35
        std::size_t Extent
36
    > class span
37
    {
38
    public:
39
        using element_type = T;
40
        using value_type = typename std::remove_const<T>::type;
41
        using size_type = std::size_t;
42
        using difference_type = std::ptrdiff_t;
43
        using pointer = T*;
44
        using const_pointer = const T*;
45
        using reference = T&;
46
        using const_reference = const T&;
47
        using iterator = pointer;
48
        using const_iterator = const_pointer;
49
        using reverse_iterator = std::reverse_iterator<iterator>;
50
        using const_reverse_iterator = std::reverse_iterator<const_iterator>;
51
    private:
52
        pointer data_{nullptr};
53
        size_type size_{0};
54
    public:
55
        static constexpr std::size_t extent = Extent;
56
57
        constexpr span() noexcept
58
        {
59
        }
60
        constexpr span(pointer data, size_type size)
61
22.6k
            : data_(data), size_(size)
62
22.6k
        {
63
22.6k
        }
64
65
        template <typename C>
66
        constexpr span(C& c,
67
                       typename std::enable_if<!is_span<C>::value && !ext_traits::is_std_array<C>::value && ext_traits::is_compatible_element<C,element_type>::value && ext_traits::has_data_and_size<C>::value>::type* = 0)
68
            : data_(c.data()), size_(c.size())
69
        {
70
        }
71
72
        template <std::size_t N>
73
        span(element_type (&arr)[N]) noexcept
74
            : data_(std::addressof(arr[0])), size_(N)
75
        {
76
        }
77
78
        template <std::size_t N>
79
        span(std::array<value_type, N>& arr,
80
             typename std::enable_if<(extent == dynamic_extent || extent == N)>::type* = 0) noexcept
81
            : data_(arr.data()), size_(arr.size())
82
        {
83
        }
84
85
        template <std::size_t N>
86
        span(const std::array<value_type, N>& arr,
87
             typename std::enable_if<(extent == dynamic_extent || extent == N)>::type* = 0) noexcept
88
            : data_(arr.data()), size_(arr.size())
89
        {
90
        }
91
92
        template <typename C>
93
        constexpr span(const C& c,
94
                       typename std::enable_if<!is_span<C>::value && !ext_traits::is_std_array<C>::value && ext_traits::is_compatible_element<C,element_type>::value && ext_traits::has_data_and_size<C>::value>::type* = 0)
95
            : data_(c.data()), size_(c.size())
96
        {
97
        }
98
99
        template <typename U, std::size_t N>
100
        constexpr span(const span<U, N>& s,
101
                       typename std::enable_if<(N == dynamic_extent || N == extent) && std::is_convertible<U(*)[], element_type(*)[]>::value>::type* = 0) noexcept
102
            : data_(s.data()), size_(s.size())
103
        {
104
        }
105
106
        constexpr span(const span& other) = default;
107
108
        span& operator=( const span& other ) = default;
109
110
        constexpr pointer data() const noexcept
111
22.6k
        {
112
22.6k
            return data_;
113
22.6k
        }
jsoncons::detail::span<unsigned char const, 18446744073709551615ul>::data() const
Line
Count
Source
111
22.6k
        {
112
22.6k
            return data_;
113
22.6k
        }
Unexecuted instantiation: jsoncons::detail::span<unsigned short const, 18446744073709551615ul>::data() const
Unexecuted instantiation: jsoncons::detail::span<unsigned int const, 18446744073709551615ul>::data() const
Unexecuted instantiation: jsoncons::detail::span<unsigned long const, 18446744073709551615ul>::data() const
Unexecuted instantiation: jsoncons::detail::span<signed char const, 18446744073709551615ul>::data() const
Unexecuted instantiation: jsoncons::detail::span<short const, 18446744073709551615ul>::data() const
Unexecuted instantiation: jsoncons::detail::span<int const, 18446744073709551615ul>::data() const
Unexecuted instantiation: jsoncons::detail::span<long const, 18446744073709551615ul>::data() const
Unexecuted instantiation: jsoncons::detail::span<float const, 18446744073709551615ul>::data() const
Unexecuted instantiation: jsoncons::detail::span<double const, 18446744073709551615ul>::data() const
114
115
        constexpr size_type size() const noexcept
116
22.6k
        {
117
22.6k
            return size_;
118
22.6k
        }
jsoncons::detail::span<unsigned char const, 18446744073709551615ul>::size() const
Line
Count
Source
116
22.6k
        {
117
22.6k
            return size_;
118
22.6k
        }
Unexecuted instantiation: jsoncons::detail::span<unsigned short const, 18446744073709551615ul>::size() const
Unexecuted instantiation: jsoncons::detail::span<unsigned int const, 18446744073709551615ul>::size() const
Unexecuted instantiation: jsoncons::detail::span<unsigned long const, 18446744073709551615ul>::size() const
Unexecuted instantiation: jsoncons::detail::span<signed char const, 18446744073709551615ul>::size() const
Unexecuted instantiation: jsoncons::detail::span<short const, 18446744073709551615ul>::size() const
Unexecuted instantiation: jsoncons::detail::span<int const, 18446744073709551615ul>::size() const
Unexecuted instantiation: jsoncons::detail::span<long const, 18446744073709551615ul>::size() const
Unexecuted instantiation: jsoncons::detail::span<float const, 18446744073709551615ul>::size() const
Unexecuted instantiation: jsoncons::detail::span<double const, 18446744073709551615ul>::size() const
119
120
         constexpr bool empty() const noexcept 
121
        { 
122
            return size_ == 0; 
123
        }
124
125
         constexpr reference operator[](size_type index) const
126
0
         {
127
0
             return data_[index];
128
0
         }
Unexecuted instantiation: jsoncons::detail::span<unsigned char const, 18446744073709551615ul>::operator[](unsigned long) const
Unexecuted instantiation: jsoncons::detail::span<unsigned short const, 18446744073709551615ul>::operator[](unsigned long) const
Unexecuted instantiation: jsoncons::detail::span<unsigned int const, 18446744073709551615ul>::operator[](unsigned long) const
Unexecuted instantiation: jsoncons::detail::span<unsigned long const, 18446744073709551615ul>::operator[](unsigned long) const
Unexecuted instantiation: jsoncons::detail::span<signed char const, 18446744073709551615ul>::operator[](unsigned long) const
Unexecuted instantiation: jsoncons::detail::span<short const, 18446744073709551615ul>::operator[](unsigned long) const
Unexecuted instantiation: jsoncons::detail::span<int const, 18446744073709551615ul>::operator[](unsigned long) const
Unexecuted instantiation: jsoncons::detail::span<long const, 18446744073709551615ul>::operator[](unsigned long) const
Unexecuted instantiation: jsoncons::detail::span<float const, 18446744073709551615ul>::operator[](unsigned long) const
Unexecuted instantiation: jsoncons::detail::span<double const, 18446744073709551615ul>::operator[](unsigned long) const
129
130
        // iterator support 
131
        const_iterator begin() const noexcept
132
0
        {
133
0
            return data_;
134
0
        }
Unexecuted instantiation: jsoncons::detail::span<unsigned char const, 18446744073709551615ul>::begin() const
Unexecuted instantiation: jsoncons::detail::span<unsigned short const, 18446744073709551615ul>::begin() const
Unexecuted instantiation: jsoncons::detail::span<unsigned int const, 18446744073709551615ul>::begin() const
Unexecuted instantiation: jsoncons::detail::span<unsigned long const, 18446744073709551615ul>::begin() const
Unexecuted instantiation: jsoncons::detail::span<signed char const, 18446744073709551615ul>::begin() const
Unexecuted instantiation: jsoncons::detail::span<short const, 18446744073709551615ul>::begin() const
Unexecuted instantiation: jsoncons::detail::span<int const, 18446744073709551615ul>::begin() const
Unexecuted instantiation: jsoncons::detail::span<long const, 18446744073709551615ul>::begin() const
Unexecuted instantiation: jsoncons::detail::span<float const, 18446744073709551615ul>::begin() const
Unexecuted instantiation: jsoncons::detail::span<double const, 18446744073709551615ul>::begin() const
135
        const_iterator end() const noexcept
136
0
        {
137
0
            return data_ + size_;
138
0
        }
Unexecuted instantiation: jsoncons::detail::span<unsigned char const, 18446744073709551615ul>::end() const
Unexecuted instantiation: jsoncons::detail::span<unsigned short const, 18446744073709551615ul>::end() const
Unexecuted instantiation: jsoncons::detail::span<unsigned int const, 18446744073709551615ul>::end() const
Unexecuted instantiation: jsoncons::detail::span<unsigned long const, 18446744073709551615ul>::end() const
Unexecuted instantiation: jsoncons::detail::span<signed char const, 18446744073709551615ul>::end() const
Unexecuted instantiation: jsoncons::detail::span<short const, 18446744073709551615ul>::end() const
Unexecuted instantiation: jsoncons::detail::span<int const, 18446744073709551615ul>::end() const
Unexecuted instantiation: jsoncons::detail::span<long const, 18446744073709551615ul>::end() const
Unexecuted instantiation: jsoncons::detail::span<float const, 18446744073709551615ul>::end() const
Unexecuted instantiation: jsoncons::detail::span<double const, 18446744073709551615ul>::end() const
139
        const_iterator cbegin() const noexcept 
140
        { 
141
            return data_; 
142
        }
143
        const_iterator cend() const noexcept 
144
        { 
145
            return data_ + size_; 
146
        }
147
        const_reverse_iterator rbegin() const noexcept 
148
        { 
149
            return const_reverse_iterator(end()); 
150
        }
151
        const_reverse_iterator rend() const noexcept 
152
        { 
153
            return const_reverse_iterator(begin()); 
154
        }
155
        const_reverse_iterator crbegin() const noexcept 
156
        { 
157
            return const_reverse_iterator(end()); 
158
        }
159
        const_reverse_iterator crend() const noexcept 
160
        { 
161
            return const_reverse_iterator(begin()); 
162
        }
163
164
        span<element_type, dynamic_extent>
165
        first(std::size_t count) const
166
        {
167
            JSONCONS_ASSERT(count <= size());
168
169
            return span< element_type, dynamic_extent >( data(), count );
170
        }
171
172
        span<element_type, dynamic_extent>
173
        last(std::size_t count) const
174
        {
175
            JSONCONS_ASSERT(count <= size());
176
177
            return span<element_type, dynamic_extent>(data() + ( size() - count ), count);
178
        }
179
180
        span<element_type, dynamic_extent>
181
        subspan(std::size_t offset, std::size_t count = dynamic_extent) const
182
        {
183
            //JSONCONS_ASSERT((offset <= size() && (count == dynamic_extent || (offset + count <= size()))));
184
185
            return span<element_type, dynamic_extent>(
186
                data() + offset, count == dynamic_extent ? size() - offset : count );
187
        }
188
    };
189
190
} // namespace detail
191
} // namespace jsoncons
192
193
#endif // JSONCONS_DETAIL_SPAN_HPP