Bug 1577700 - Add a function for obtaining a Span of char from a Span of uint8_t. r=froydnj

Differential Revision: https://phabricator.services.mozilla.com/D44124

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Henri Sivonen 2019-08-30 13:16:19 +00:00
Родитель 17bddfd388
Коммит 95fecf7af4
2 изменённых файлов: 31 добавлений и 0 удалений

Просмотреть файл

@ -808,6 +808,20 @@ AsWritableBytes(Span<ElementType, Extent> s) {
return {reinterpret_cast<uint8_t*>(s.data()), s.size_bytes()};
}
/**
* View a span of uint8_t as a span of char.
*/
inline Span<const char> AsChars(Span<const uint8_t> s) {
return {reinterpret_cast<const char*>(s.data()), s.size()};
}
/**
* View a writable span of uint8_t as a span of char.
*/
inline Span<char> AsWritableChars(Span<uint8_t> s) {
return {reinterpret_cast<char*>(s.data()), s.size()};
}
//
// MakeSpan() - Utility functions for creating Spans
//

Просмотреть файл

@ -1965,6 +1965,23 @@ SPAN_TEST(as_writable_bytes) {
}
}
SPAN_TEST(as_chars) {
const uint8_t a[] = {1, 2, 3, 4};
Span<const uint8_t> u = MakeSpan(a);
Span<const char> c = AsChars(u);
ASSERT_EQ(static_cast<const void*>(u.data()),
static_cast<const void*>(c.data()));
ASSERT_EQ(u.size(), c.size());
}
SPAN_TEST(as_writable_chars) {
uint8_t a[] = {1, 2, 3, 4};
Span<uint8_t> u = MakeSpan(a);
Span<char> c = AsWritableChars(u);
ASSERT_EQ(static_cast<void*>(u.data()), static_cast<void*>(c.data()));
ASSERT_EQ(u.size(), c.size());
}
SPAN_TEST(fixed_size_conversions) {
int arr[] = {1, 2, 3, 4};