imgtool: Fix build by updating charconv.cpp to use string_view internally

This commit is contained in:
AJR 2020-12-15 18:34:12 -05:00
parent 68d9460a79
commit 06109d35b5
2 changed files with 19 additions and 42 deletions

View File

@ -8,34 +8,14 @@
***************************************************************************/ ***************************************************************************/
#include "corestr.h"
#include "charconv.h" #include "charconv.h"
#include "unicode.h"
#include "corestr.h"
#include "coretmpl.h" #include "coretmpl.h"
imgtool::simple_charconverter imgtool::charconverter_iso_8859_1(nullptr, nullptr); imgtool::simple_charconverter imgtool::charconverter_iso_8859_1(nullptr, nullptr);
//-------------------------------------------------
// from_utf8
//-------------------------------------------------
void imgtool::charconverter::from_utf8(std::ostream &dest, const std::string &src) const
{
from_utf8(dest, src.c_str(), src.size());
}
//-------------------------------------------------
// to_utf8
//-------------------------------------------------
void imgtool::charconverter::to_utf8(std::ostream &dest, const std::string &src) const
{
to_utf8(dest, src.c_str(), src.size());
}
//------------------------------------------------- //-------------------------------------------------
// simple_charconverter::simple_charconverter // simple_charconverter::simple_charconverter
//------------------------------------------------- //-------------------------------------------------
@ -63,23 +43,23 @@ imgtool::simple_charconverter::simple_charconverter(const char32_t lowpage[0x80]
// from_utf8 // from_utf8
//------------------------------------------------- //-------------------------------------------------
void imgtool::simple_charconverter::from_utf8(std::ostream &dest, const char *src, size_t src_length) const void imgtool::simple_charconverter::from_utf8(std::ostream &dest, std::string_view src) const
{ {
// normalize the incoming unicode // normalize the incoming unicode
std::string normalized_src = normalize_unicode(src, src_length, m_norm); std::string const normalized_src = normalize_unicode(src, m_norm);
auto iter = normalized_src.begin(); auto nsrc = std::string_view(normalized_src);
while (iter != normalized_src.end()) while (!nsrc.empty())
{ {
// get the next character // get the next character
char32_t ch; char32_t ch;
int rc = uchar_from_utf8(&ch, &*iter, normalized_src.end() - iter); int rc = uchar_from_utf8(&ch, nsrc);
if (rc < 0) if (rc < 0)
{ {
ch = 0xFFFD; ch = 0xFFFD;
rc = 1; rc = 1;
} }
iter += rc; nsrc.remove_prefix(rc);
// do the reverse lookup // do the reverse lookup
auto lookup = std::lower_bound(m_reverse_lookup.begin(), m_reverse_lookup.end(), ch, [](const std::pair<char32_t, char> &a, const char32_t &b) auto lookup = std::lower_bound(m_reverse_lookup.begin(), m_reverse_lookup.end(), ch, [](const std::pair<char32_t, char> &a, const char32_t &b)
@ -99,24 +79,24 @@ void imgtool::simple_charconverter::from_utf8(std::ostream &dest, const char *sr
// to_utf8 // to_utf8
//------------------------------------------------- //-------------------------------------------------
void imgtool::simple_charconverter::to_utf8(std::ostream &dest, const char *src, size_t src_length) const void imgtool::simple_charconverter::to_utf8(std::ostream &dest, std::string_view src) const
{ {
for (size_t i = 0; i < src_length; i++) for (uint8_t c : src)
{ {
// which page is this in? // which page is this in?
const char32_t *page = ((src[i] & 0x80) == 0) ? m_lowpage : m_highpage; const char32_t *page = ((c & 0x80) == 0) ? m_lowpage : m_highpage;
// is this page present? // is this page present?
if ((src[i] & 0x80) == 0) if ((c & 0x80) == 0)
{ {
// no - pass it on // no - pass it on
dest << src[i]; dest << c;
} }
else else
{ {
// yes - we need to do a lookup // yes - we need to do a lookup
size_t base = ((src[i] & 0x80) == 0) ? 0x00 : 0x80; size_t base = ((c & 0x80) == 0) ? 0x00 : 0x80;
char32_t ch = page[((unsigned char)(src[i])) - base]; char32_t ch = page[((unsigned char)(c)) - base];
if (ch == 0) if (ch == 0)
throw charconverter_exception(); throw charconverter_exception();

View File

@ -22,11 +22,8 @@ namespace imgtool
class charconverter class charconverter
{ {
public: public:
virtual void from_utf8(std::ostream &dest, const char *src, size_t src_length) const = 0; virtual void from_utf8(std::ostream &dest, std::string_view src) const = 0;
virtual void to_utf8(std::ostream &dest, const char *src, size_t src_length) const = 0; virtual void to_utf8(std::ostream &dest, std::string_view src) const = 0;
void from_utf8(std::ostream &dest, const std::string &src) const;
void to_utf8(std::ostream &dest, const std::string &src) const;
std::string from_utf8(const std::string &src) const std::string from_utf8(const std::string &src) const
{ {
@ -59,8 +56,8 @@ namespace imgtool
simple_charconverter(const char32_t lowpage[0x80], const char32_t highpage[0x80], unicode_normalization_form norm = unicode_normalization_form::C); simple_charconverter(const char32_t lowpage[0x80], const char32_t highpage[0x80], unicode_normalization_form norm = unicode_normalization_form::C);
virtual void from_utf8(std::ostream &dest, const char *src, size_t src_length) const override; virtual void from_utf8(std::ostream &dest, std::string_view src) const override;
virtual void to_utf8(std::ostream &dest, const char *src, size_t src_length) const override; virtual void to_utf8(std::ostream &dest, std::string_view src) const override;
private: private:
std::vector<std::pair<char32_t, char> > m_reverse_lookup; std::vector<std::pair<char32_t, char> > m_reverse_lookup;