Validating labels¶
examples/labels.cpp shows how to accept an encoding label from metadata,
validate it, inspect how it resolves, and then use it for conversion.
Use this pattern when the label comes from:
a configuration file
a database column
a content-type charset parameter
an import job setting
user-provided metadata
#include <iostream>
#include <string>
#include <polycpp/iconv_lite/iconv_lite.hpp>
int main() {
namespace iconv = polycpp::iconv_lite;
const std::string label_from_config = "ISO_8859-5:1988";
if (!iconv::encodingExists(label_from_config)) {
std::cerr << "unsupported encoding: " << label_from_config << "\n";
return 1;
}
auto info = iconv::inspectEncoding(label_from_config);
std::cout << info.canonical << "\n";
std::cout << info.converter << "\n";
auto bytes = iconv::encode("Привет", label_from_config);
std::cout << bytes.toString("hex") << "\n";
std::cout << iconv::decode(bytes, "iso-8859-5") << "\n";
return 0;
}
Expected output:
iso88595
iso88595
bfe0d8d2d5e2
Привет
encodingExists is the cheap guard for untrusted labels. inspectEncoding
is for diagnostics, logs, and tests where you want to see the canonical label
and resolved converter.