Skip to content

Commit

Permalink
Raise TCellChar size to 12
Browse files Browse the repository at this point in the history
After some testing I have found Wikipedia articles where 8 bytes were not enough to fit all the diacritics in one cell. So I raised it to 12 bytes, where at least 2 combining characters can fit in the worst case. This should be enough for most real-world, natural language use cases. I don't care about zalgo.

12 bytes and alignas(4) is still a sweet spot for performance where most operations (including comparison) can be carried out in registers. It also preserves sizeof(TScreenCell) == 16, although that struct is likely to become larger in the future if true color support is added.

See #26.
  • Loading branch information
magiblot committed Oct 31, 2020
1 parent 4bd684d commit 18b485c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
40 changes: 36 additions & 4 deletions include/tvision/scrncell.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,44 @@ struct TScreenCellA : trivially_convertible<uint16_t>

};

struct TCellChar : trivially_convertible<uint64_t>
struct alignas(4) TCellChar
{

uint8_t bytes[8];
uint8_t bytes[12];

using trivially_convertible::trivially_convertible;
TCellChar() = default;

TCellChar(uint64_t ch)
{
*this = ch;
}

TCellChar& operator=(uint64_t ch)
{
memset(this, 0, sizeof(*this));
memcpy(bytes, &ch, std::min(sizeof(bytes), sizeof(ch)));
return *this;
}

bool operator==(TCellChar other) const
{
return memcmp(bytes, other.bytes, sizeof(bytes)) == 0;
}

bool operator!=(TCellChar other) const
{
return !(*this == other);
}

uint8_t& operator[](size_t i)
{
return bytes[i];
}

const uint8_t& operator[](size_t i) const
{
return bytes[i];
}

void append(TStringView text)
{
Expand All @@ -204,7 +236,7 @@ struct TCellChar : trivially_convertible<uint64_t>

static constexpr void check_assumptions()
{
check_trivial<TCellChar>();
static_assert(std::is_trivial<TCellChar>());
}

};
Expand Down
12 changes: 7 additions & 5 deletions source/platform/buffdisp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,13 @@ void BufferedDisplay::onScreenResize()
void BufferedDisplay::ensurePrintable(BufferCell &cell) const
{
auto &ch = cell.Char;
if (ch == '\0')
ch = ' ';
else if (ch < ' ' || (0x7F <= ch && ch < 0x100)) {
// Translate from codepage as fallback.
ch = CpTranslator::toUtf8Int(ch);
if (!ch[1]) /* size 1 */ {
auto &c = ch[0];
if (c == '\0')
c = ' ';
else if (c < ' ' || 0x7F <= c)
// Translate from codepage as fallback.
ch = CpTranslator::toUtf8Int(c);
} else if (ch == TScreenCell::wideCharTrail) {
ch = widePlaceholder;
cell.extraWidth = 0;
Expand Down

0 comments on commit 18b485c

Please sign in to comment.