diff options
author | Jay Berkenbilt <ejb@ql.org> | 2019-01-04 02:03:30 +0100 |
---|---|---|
committer | Jay Berkenbilt <ejb@ql.org> | 2019-01-04 05:18:13 +0100 |
commit | 02281632ccbba3ef00a6968bfd697f4be836d0dd (patch) | |
tree | bac13076f812b968257140d9123f664905a5da8e /libqpdf | |
parent | b55567a0fa6708500cd0905f7a26a28d70979001 (diff) | |
download | qpdf-02281632ccbba3ef00a6968bfd697f4be836d0dd.tar.zst |
Add QUtil::utf8_to_ascii
Diffstat (limited to 'libqpdf')
-rw-r--r-- | libqpdf/QUtil.cc | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/libqpdf/QUtil.cc b/libqpdf/QUtil.cc index 44ffec7f..7c2d9bc9 100644 --- a/libqpdf/QUtil.cc +++ b/libqpdf/QUtil.cc @@ -892,3 +892,26 @@ QUtil::parse_numrange(char const* range, int max) } return result; } + +std::string +QUtil::utf8_to_ascii(std::string const& utf8, char unknown_char) +{ + std::string ascii_value; + for (size_t i = 0; i < utf8.length(); ++i) + { + unsigned char ch = static_cast<unsigned char>(utf8.at(i)); + if (ch < 128) + { + ascii_value.append(1, ch); + } + else if ((ch & 0xc0) == 0x80) + { + // Ignore subsequent byte of UTF-8 encoded character + } + else + { + ascii_value.append(1, unknown_char); + } + } + return ascii_value; +} |