You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+97-81Lines changed: 97 additions & 81 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,73 +1,96 @@
1
-
# byte_span
1
+
# range3::byte_span
2
2
3
-
`byte_span` is a lightweight view class for efficient handling of byte data. Based on the concept of `std::span`, it provides specialized functionality for byte data manipulation.
3
+
byte_span is a non-owning view class for byte sequences, similar to std::span.
4
+
It provides unified handling of byte-like types (`std::byte`, `char`, `unsigned char`)
5
+
and allows viewing any trivially copyable data as a byte sequence.
4
6
5
7
## Features
6
8
7
-
- Non-owning view type
9
+
- Non-owning view type with zero overhead
8
10
- Unified handling of byte-like types (`std::byte`, `char`, `unsigned char`)
9
-
- View trivially copyable data types as byte sequences
11
+
- View any trivially copyable type as byte sequences
12
+
- Compatible with any contiguous range (vectors, arrays, C-style arrays)
10
13
- Type-safe design using C++20 concepts
11
-
- Zero-overhead abstraction
12
14
13
15
## Basic Usage
14
16
15
-
### 1. Creating from Byte Arrays
17
+
### Creating from Byte-like Types
16
18
17
-
The most basic usage is creating a `byte_span`from a byte array:
19
+
The most common use case is creating a byte view from containers of byte-like types:
18
20
19
21
```cpp
20
-
std::byte data[4] = {
21
-
std::byte{0x00}, std::byte{0x01},
22
-
std::byte{0x02}, std::byte{0x03}
22
+
std::vector<char> chars = {'a', 'b', 'c'};
23
+
byte_span view1{chars}; // Deduced as byte_span<std::byte>
24
+
25
+
unsigned char raw[4] = {0x00, 0x01, 0x02, 0x03};
26
+
byte_span view2{raw}; // Deduced as byte_span<std::byte, 4>
27
+
28
+
std::array<std::byte, 3> bytes = {
29
+
std::byte{1}, std::byte{2}, std::byte{3}
23
30
};
24
-
byte_span view{data}; // Deduced as byte_span<std::byte, 4>
31
+
byte_span view3{bytes}; // Deduced as byte_span<std::byte, 3>
25
32
```
26
33
27
-
### 2. Creating from std::array
28
-
29
-
Easy creation from `std::array`:
34
+
### Creating from Any Trivially Copyable Type
35
+
View any trivially copyable data as bytes:
30
36
31
37
```cpp
32
-
std::array<std::byte, 4> arr = {
33
-
std::byte{0x00}, std::byte{0x01},
34
-
std::byte{0x02}, std::byte{0x03}
35
-
};
36
-
byte_span view{arr}; // Deduced as byte_span<std::byte, 4>
38
+
// From vector of integers
39
+
std::vector<int> ints = {1, 2, 3};
40
+
byte_span view1{ints}; // Deduced as byte_span<std::byte>
41
+
assert(view1.size() == sizeof(int) * 3);
42
+
43
+
// From custom struct
44
+
struct Point {
45
+
float x, y;
46
+
};
47
+
Point p{1.0f, 2.0f};
48
+
byte_span view2{&p, 1}; // Deduced as byte_span<std::byte>
0 commit comments