Coverage Report

Created: 2025-09-04 07:34

/src/solidity/test/evmc/bytes.hpp
Line
Count
Source (jump to first uncovered line)
1
// EVMC: Ethereum Client-VM Connector API.
2
// Copyright 2024 The EVMC Authors.
3
// Licensed under the Apache License, Version 2.0.
4
#pragma once
5
6
#include <algorithm>
7
#include <cstring>
8
#include <string>
9
#include <string_view>
10
11
namespace evmc
12
{
13
/// The char traits for byte-like types.
14
///
15
/// See: https://en.cppreference.com/w/cpp/string/char_traits.
16
template <typename T>
17
struct byte_traits : std::char_traits<char>
18
{
19
    static_assert(sizeof(T) == 1, "type must be a byte");
20
21
    using char_type = T;  ///< The byte type.
22
23
    /// Assigns c2 to c1.
24
31.7M
    static constexpr void assign(char_type& c1, const char_type& c2) { c1 = c2; }
25
26
    /// Assigns value to each byte in [ptr, ptr+count).
27
    static constexpr char_type* assign(char_type* ptr, std::size_t count, char_type value)
28
    {
29
        std::fill_n(ptr, count, value);
30
        return ptr;
31
    }
32
33
    /// Returns true if bytes are equal.
34
    static constexpr bool eq(char_type a, char_type b) { return a == b; }
35
36
    /// Returns true if byte a is less than byte b.
37
0
    static constexpr bool lt(char_type a, char_type b) { return a < b; }
38
39
    /// Copies count bytes from src to dest. Performs correctly even if ranges overlap.
40
    static constexpr char_type* move(char_type* dest, const char_type* src, std::size_t count)
41
    {
42
        if (dest < src)
43
            std::copy_n(src, count, dest);
44
        else if (src < dest)
45
            std::copy_backward(src, src + count, dest + count);
46
        return dest;
47
    }
48
49
    /// Copies count bytes from src to dest. The ranges must not overlap.
50
    static constexpr char_type* copy(char_type* dest, const char_type* src, std::size_t count)
51
85.1k
    {
52
85.1k
        std::copy_n(src, count, dest);
53
85.1k
        return dest;
54
85.1k
    }
55
56
    /// Compares lexicographically the bytes in two ranges of equal length.
57
    static constexpr int compare(const char_type* a, const char_type* b, std::size_t count)
58
0
    {
59
0
        for (; count != 0; --count, ++a, ++b)
60
0
        {
61
0
            if (lt(*a, *b))
62
0
                return -1;
63
0
            if (lt(*b, *a))
64
0
                return 1;
65
0
        }
66
0
        return 0;
67
0
    }
68
69
    /// Returns the length of a null-terminated byte string.
70
    // TODO: Not constexpr
71
    static std::size_t length(const char_type* s)
72
    {
73
        return std::strlen(reinterpret_cast<const char*>(s));
74
    }
75
76
    /// Finds the value in the range of bytes and returns the pointer to the first occurrence
77
    /// or nullptr if not found.
78
    static constexpr const char_type* find(const char_type* s,
79
                                           std::size_t count,
80
                                           const char_type& value)
81
    {
82
        const auto end = s + count;
83
        const auto p = std::find(s, end, value);
84
        return p != end ? p : nullptr;
85
    }
86
};
87
88
/// String of unsigned chars representing bytes.
89
using bytes = std::basic_string<unsigned char, byte_traits<unsigned char>>;
90
91
/// String view of unsigned chars representing bytes.
92
using bytes_view = std::basic_string_view<unsigned char, byte_traits<unsigned char>>;
93
}  // namespace evmc