bytes and sstring are distinct types, since their internal buffers are of different length, but bytes_view is an alias of sstring_view, which makes it possible of objects of different types to leak across the abstraction boundary. Fix this by making bytes a basic_sstring<int8_t, ...> instead of using char. int8_t is a 'signed char', which is a distinct type from char, so now bytes_view is a distinct type from sstring_view. uint8_t would have been an even better choice, but that diverges from Origin and would have required an audit.
43 lines
981 B
C++
43 lines
981 B
C++
/*
|
|
* Copyright (C) 2015 Cloudius Systems, Ltd.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include "core/sstring.hh"
|
|
#include <experimental/optional>
|
|
#include <iosfwd>
|
|
#include <functional>
|
|
|
|
using bytes = basic_sstring<int8_t, uint32_t, 31>;
|
|
using bytes_view = std::experimental::basic_string_view<int8_t>;
|
|
using bytes_opt = std::experimental::optional<bytes>;
|
|
using sstring_view = std::experimental::string_view;
|
|
|
|
namespace std {
|
|
|
|
template <>
|
|
struct hash<bytes_view> {
|
|
size_t operator()(bytes_view v) const {
|
|
return hash<sstring_view>()({reinterpret_cast<const char*>(v.begin()), v.size()});
|
|
}
|
|
};
|
|
|
|
}
|
|
|
|
bytes from_hex(sstring_view s);
|
|
sstring to_hex(bytes_view b);
|
|
sstring to_hex(const bytes& b);
|
|
sstring to_hex(const bytes_opt& b);
|
|
|
|
std::ostream& operator<<(std::ostream& os, const bytes& b);
|
|
std::ostream& operator<<(std::ostream& os, const bytes_opt& b);
|
|
|
|
namespace std {
|
|
|
|
// Must be in std:: namespace, or ADL fails
|
|
std::ostream& operator<<(std::ostream& os, const bytes_view& b);
|
|
|
|
}
|
|
|