matheusgomes28/base64pp

add std::string / std::string_view overload to base64pp::encode()

Closed this issue · 5 comments

As base64 is often used to encode text data, it's likely that the input data is present in a std::string.
Encoding data from a std::string with base64pp currently looks like this:

std::string str = "Hello World";
std::string encoded = base64pp::encode({reinterpret_cast<std::uint8_t const *>(str.data()), str.size()});

With a proper overload, it could be simplified like that:

std::string str = "Hello World";
std::string encoded = base64pp::encode(str);

Happy to add this overload. Perhaps we could also benefit from adding vector overloads too.

The following compiles fine with current base64pp:

std::vector<std::uint8_t> test;
base64pp::encode(test);

It looks like many std:: containers can be implicitly converted to std::span:

https://stackoverflow.com/questions/64900594/how-can-stdvector-be-converted-to-stdspan

I guess that this doesn't apply to std::string, because it uses char instead of uint8_t

Thanks for the suggestions @ceggers-arri , I've implemented the overloads in #107 . As you said, other stl containers do have implicit conversion to std::span so it's not worth adding more overloads aside from the std::string{_view} ones.

Feel free to contribute or post more usage suggestions here!

Would like to add an unit test?

Closing this as implemented with #107