Skip to content

Commit 047b3a5

Browse files
committed
Add support for decoding strings of char8_t or std::byte
1 parent 8e48528 commit 047b3a5

File tree

4 files changed

+576
-217
lines changed

4 files changed

+576
-217
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
- `bencode::data` (and `bencode::basic_data`, etc) now support `operator []` and
77
`at` member functions to get list/dictionary elements
88
- Decoding functions now accept a pointer plus length as input
9+
- Add support for decoding data from other character types (`char8_t` or
10+
`std::byte`), preserving the underlying character type
911
- Improve performance of `encode`; encoding is now up to 2x as fast, depending
1012
on the data being encoded
1113

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,30 @@ pointer's value in-place.
107107
If the buffer holding the bencoded data is stable (i.e. won't change or be
108108
destroyed until you're done working with the parsed representation), you can
109109
decode the data as a *view* on the buffer to save memory. This results in all
110-
parsed strings being nothing more than pointers pointing to slices of your
111-
buffer. Simply add `_view` to the functions/types to take advantage of this:
110+
parsed strings being `std::string_view`s pointing to slices of your buffer.
111+
Simply add `_view` to the functions/types to take advantage of this:
112112
113113
```c++
114114
std::string buf = "3:foo";
115115
bencode::data_view data = bencode::decode_view(buf); // or `decode_view_some`
116116
auto value = std::get<bencode::string_view>(data);
117117
```
118118

119+
#### Other Character Types
120+
121+
In addition to working with bencoded data as `char`s, you can use other
122+
underlying character types: `char8_t` or `std::byte`. For the former, the
123+
default bencoded string type is naturally `std::u8string` (or
124+
`std::u8string_view` for data views). For the latter, the string type is
125+
`std::vector<std::byte>` (or `std::span<std::byte>`). The bencode data types for
126+
these are prefixed with `u8` and `b`, respectively:
127+
128+
```c++
129+
std::vector<std::byte> buf = get_buffer();
130+
bencode::bdata = bencode::decode(buf);
131+
auto value = std::get<bencode::bstring>(data);
132+
```
133+
119134
#### Errors
120135

121136
If there's an error trying to decode some bencode data, a `decode_error` will be

0 commit comments

Comments
 (0)