C++ API may a bit slower than C API. But it is more convenient to use.
This struct contains version information.
int major: Major version.int minor: Minor version.int patch: Patch version.
It's a variable for version information of current library.
std::cout << "cppp-reiconv version: " << reiconv::version.major << "."
<< reiconv::version.minor << "." << reiconv::version.patch << std::endl;Encoding class wraps encoding lookup functions.
It can convert to a int value, equals to the internal encoding id.
Construct an encoding object from a encoding name.
public:
_CPPP_API Encoding(const char* const name);
_CPPP_API Encoding(const std::string& name);name: The encoding name.//suffix is not allowed.
Encoding: The encoding object.
std::invalid_argument: If the encoding name is not valid.
Construct an encoding object from a codepage.
public:
_CPPP_API Encoding(const int codepage);codepage: The codepage. This cannot open a encoding that do not have a codepage.
Encoding: The encoding object.
std::invalid_argument: If the codepage is invalid.
public:
constexpr Encoding(const Encodings index);index: The encoding id. Available encoding ids are defined in enum classreiconv::Encodings.
Encoding: The encoding object.
noexcept, but we will not check if the encoding id is valid. So please pass a valid
encoding id from reiconv::Encodings. Or you will get undefined behavior.
public:
operator int() const noexcept;int: The encoding id.
public:
bool operator==(const Encoding other) const noexcept;other: The other encoding object.
bool: True if the two encoding objects are equal. If equal, they have the same encoding id.
reiconv::Encoding gb18030(reiconv::Encodings::GB18030); // Open from encoding id.
reiconv::Encoding cp936(936); // Open from codepage.
reiconv::Encoding cp936_2("cp936"); // Open from encoding name.
reiconv::Encoding utf8("UTF-8");
std::cout << "(int)gb18030: " << (int)gb18030 << std::endl;
std::cout << "cp936 == cp936_2: " << (cp936 == cp936_2) << std::endl; // True
std::cout << "cp936 == utf8: " << (cp936 == utf8) << std::endl; // Falseenum class ConvertFlag
{
NO_FLAGS = 0,
DISCARD_ILSEQ = 1,
};This is a bitmask for conversion flags.
NO_FLAGS: No flags. Pass it by default.DISCARD_ILSEQ: Discard invalid sequence. It's same as iconv's//IGNOREflag.
Use | to combine flags. Use it in reiconv::convert function.
extern _CPPP_API std::string convert(Encoding from, Encoding to, const std::string_view input, enum ConvertFlag flag = ConvertFlag::NO_FLAGS);Convert a string from one encoding to another. It will automatically allocate memory for the output string.
from: The source encoding.to: The target encoding.input: The input string.flag: The conversion flags. Default isConvertFlag::NO_FLAGS.
std::string: The converted string.
std::system_errorIf the conversion failed. Use it because it's has errno set.
const std::string_view src = "\xb8\xfc\xcf\xb2\xe1\xba\xc9\xbd\xc7\xa7\xc0\xef\xd1\xa9\xa3\xac\xc8\xfd\xbe\xfc\xb9\xfd\xba\xf3\xbe\xa1\xbf\xaa\xd1\xd5\xa3\xa1";
std::string result = reiconv::convert(reiconv::Encodings::GB18030, "UTF-8", src);
std::cout << result << std::endl;extern _CPPP_API std::string_view locale_charset();Determine the current locale's character encoding. But not same as GNU LIBICONV's
implementation, we will not canonicalize the encoding name. But nearly the most of
it's result can be used in reiconv::Encoding(). Except for some VERY RARE
encodings or some OSX specific encodings in old OSX versions. These encodings are
not supported in cppp-reiconv and GNU LIBICONV.
The current locale's character encoding.
std::setlocale(LC_ALL, "");
std::string_view charset = reiconv::locale_charset();
std::cout <<"Your locale charset is: " << charset << std::endl;