Stateful conversion¶
examples/stateful.cpp uses getDecoder and getEncoder directly.
These objects are useful when you are not using a full polycpp stream but still
receive or emit data in chunks.
Use this shape for:
a network protocol parser that receives partial frames
a file reader that processes fixed-size blocks
a decompressor that emits arbitrary byte chunks
a parser that needs precise control over when conversion state is flushed
#include <iostream>
#include <string>
#include <polycpp/iconv_lite/iconv_lite.hpp>
int main() {
namespace iconv = polycpp::iconv_lite;
auto decoder = iconv::getDecoder("gbk");
std::string text;
text += decoder.write(iconv::Buffer::from({0x61, 0x81}));
text += decoder.write(iconv::Buffer::from({0x40, 0x62}));
text += decoder.end();
std::cout << text << "\n";
auto encoder = iconv::getEncoder("base64");
auto binary = iconv::Buffer::concat({
encoder.write("aGV"),
encoder.write("sbG8="),
encoder.end(),
});
std::cout << binary.toString("utf8") << "\n";
return 0;
}
Expected output:
a丂b
hello
The first GBK byte chunk ends with an incomplete lead byte. The decoder keeps it until the next chunk arrives, then emits the complete character. The base64 encoder follows iconv-lite’s convention for that label: it consumes base64 text, emits bytes, and buffers input until a complete four-character group is available.