Skip to content

Commit

Permalink
Add support for decoding strings of char8_t or std::byte
Browse files Browse the repository at this point in the history
  • Loading branch information
jimporter committed Aug 11, 2024
1 parent c56dd55 commit 8dbc9e6
Show file tree
Hide file tree
Showing 4 changed files with 562 additions and 205 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
- `bencode::data` (and `bencode::basic_data`, etc) now support `operator []` and
`at` member functions to get list/dictionary elements
- Decoding functions now accept a pointer plus length as input
- Add support for decoding data from other character types (`char8_t` or
`std::byte`), preserving the underlying character type
- Improve performance of `encode`; encoding is now up to 2x as fast, depending
on the data being encoded

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

#### Other Character Types

In addition to working with bencoded data as `char`s, you can use other
underlying character types: `char8_t` or `std::byte`. For the former, the
default bencoded string type is naturally `std::u8string` (or
`std::u8string_view` for data views). For the latter, the string type is
`std::vector<std::byte>` (or `std::span<std::byte>`). The bencode data types for
these are prefixed with `u8` and `b`, respectively:

```c++
std::vector<std::byte> buf = get_buffer();
bencode::bdata = bencode::decode(buf);
auto value = std::get<bencode::bstring>(data);
```

#### Errors

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

0 comments on commit 8dbc9e6

Please sign in to comment.