From aa4b98af7c40ead3eeb5c8548c2dd6263f2c387b Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 24 Sep 2015 23:00:47 +0100 Subject: [PATCH] Add utf8::read_codepoint that both gets the codepoint and advance iterator --- src/highlighters.cc | 13 ++++++------- src/utf8.hh | 15 +++++++++++---- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/highlighters.cc b/src/highlighters.cc index 052ff657..9c818a89 100644 --- a/src/highlighters.cc +++ b/src/highlighters.cc @@ -861,20 +861,19 @@ void expand_unprintable(const Context& context, HighlightFlags flags, DisplayBuf for (auto it = buffer.iterator_at(atom_it->begin()), end = buffer.iterator_at(atom_it->end()); it < end;) { - Codepoint cp = utf8::codepoint(it, end); - auto next = utf8::next(it, end); + auto coord = it.coord(); + Codepoint cp = utf8::read_codepoint(it, end); if (cp != '\n' and not iswprint(cp)) { - if (it.coord() != atom_it->begin()) - atom_it = ++line.split(atom_it, it.coord()); - if (next.coord() < atom_it->end()) - atom_it = line.split(atom_it, next.coord()); + if (coord != atom_it->begin()) + atom_it = ++line.split(atom_it, coord); + if (it.coord() < atom_it->end()) + atom_it = line.split(atom_it, it.coord()); atom_it->replace(format("U+{}", hex(cp))); atom_it->face = { Color::Red, Color::Black }; break; } - it = next; } } } diff --git a/src/utf8.hh b/src/utf8.hh index 545255fd..09d66b99 100644 --- a/src/utf8.hh +++ b/src/utf8.hh @@ -110,7 +110,7 @@ struct Pass // is pointed by it template -Codepoint codepoint(Iterator it, const Iterator& end) +Codepoint read_codepoint(Iterator& it, const Iterator& end) { if (it == end) return InvalidPolicy{}(-1); @@ -124,14 +124,14 @@ Codepoint codepoint(Iterator it, const Iterator& end) return InvalidPolicy{}(byte); if ((byte & 0xE0) == 0xC0) // 110xxxxx - return ((byte & 0x1F) << 6) | (*it & 0x3F); + return ((byte & 0x1F) << 6) | (*it++ & 0x3F); if ((byte & 0xF0) == 0xE0) // 1110xxxx { Codepoint cp = ((byte & 0x0F) << 12) | ((*it++ & 0x3F) << 6); if (it == end) return InvalidPolicy{}(cp); - return cp | (*it & 0x3F); + return cp | (*it++ & 0x3F); } if ((byte & 0xF8) == 0xF0) // 11110xxx @@ -142,11 +142,18 @@ Codepoint codepoint(Iterator it, const Iterator& end) cp |= (*it++ & 0x3F) << 6; if (it == end) return InvalidPolicy{}(cp); - return cp | (*it & 0x3F); + return cp | (*it++ & 0x3F); } return InvalidPolicy{}(byte); } +template +Codepoint codepoint(Iterator it, const Iterator& end) +{ + return read_codepoint(it, end); +} + template ByteCount codepoint_size(char byte) {