EVMC
bytes.hpp
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
11namespace evmc
12{
16template <typename T>
17struct byte_traits : std::char_traits<char>
18{
19 static_assert(sizeof(T) == 1, "type must be a byte");
20
21 using char_type = T;
22
24 static constexpr void assign(char_type& c1, const char_type& c2) { c1 = c2; }
25
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
34 static constexpr bool eq(char_type a, char_type b) { return a == b; }
35
37 static constexpr bool lt(char_type a, char_type b) { return a < b; }
38
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
50 static constexpr char_type* copy(char_type* dest, const char_type* src, std::size_t count)
51 {
52 std::copy_n(src, count, dest);
53 return dest;
54 }
55
57 static constexpr int compare(const char_type* a, const char_type* b, std::size_t count)
58 {
59 for (; count != 0; --count, ++a, ++b)
60 {
61 if (lt(*a, *b))
62 return -1;
63 if (lt(*b, *a))
64 return 1;
65 }
66 return 0;
67 }
68
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
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
89using bytes = std::basic_string<unsigned char, byte_traits<unsigned char>>;
90
92using bytes_view = std::basic_string_view<unsigned char, byte_traits<unsigned char>>;
93} // namespace evmc
EVMC C++ API - wrappers and bindings for C++.
Definition: bytes.hpp:12
std::basic_string_view< unsigned char, byte_traits< unsigned char > > bytes_view
String view of unsigned chars representing bytes.
Definition: bytes.hpp:92
std::basic_string< unsigned char, byte_traits< unsigned char > > bytes
String of unsigned chars representing bytes.
Definition: bytes.hpp:89
The char traits for byte-like types.
Definition: bytes.hpp:18
static constexpr void assign(char_type &c1, const char_type &c2)
Assigns c2 to c1.
Definition: bytes.hpp:24
static constexpr char_type * copy(char_type *dest, const char_type *src, std::size_t count)
Copies count bytes from src to dest. The ranges must not overlap.
Definition: bytes.hpp:50
static std::size_t length(const char_type *s)
Returns the length of a null-terminated byte string.
Definition: bytes.hpp:71
static constexpr bool eq(char_type a, char_type b)
Returns true if bytes are equal.
Definition: bytes.hpp:34
static constexpr char_type * move(char_type *dest, const char_type *src, std::size_t count)
Copies count bytes from src to dest. Performs correctly even if ranges overlap.
Definition: bytes.hpp:40
static constexpr int compare(const char_type *a, const char_type *b, std::size_t count)
Compares lexicographically the bytes in two ranges of equal length.
Definition: bytes.hpp:57
static constexpr const char_type * find(const char_type *s, std::size_t count, const char_type &value)
Finds the value in the range of bytes and returns the pointer to the first occurrence or nullptr if n...
Definition: bytes.hpp:78
static constexpr bool lt(char_type a, char_type b)
Returns true if byte a is less than byte b.
Definition: bytes.hpp:37
T char_type
The byte type.
Definition: bytes.hpp:21
static constexpr char_type * assign(char_type *ptr, std::size_t count, char_type value)
Assigns value to each byte in [ptr, ptr+count).
Definition: bytes.hpp:27