This commit is contained in:
Chester Liu 2021-11-23 21:27:53 +01:00 committed by GitHub
commit fbeb84a1f9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 37 deletions

View file

@ -11,6 +11,8 @@ static_assert(sizeof(TextAttribute) == 14);
static_assert(alignof(TextAttribute) == 2);
// Ensure that we can memcpy() and memmove() the struct for performance.
static_assert(std::is_trivially_copyable_v<TextAttribute>);
// Assert that the use of memcmp() for comparisons is safe.
static_assert(std::has_unique_object_representations_v<TextAttribute>);
namespace
{

View file

@ -83,12 +83,15 @@ public:
void Invert() noexcept;
friend constexpr bool operator==(const TextAttribute& a, const TextAttribute& b) noexcept;
friend constexpr bool operator!=(const TextAttribute& a, const TextAttribute& b) noexcept;
friend constexpr bool operator==(const TextAttribute& attr, const WORD& legacyAttr) noexcept;
friend constexpr bool operator!=(const TextAttribute& attr, const WORD& legacyAttr) noexcept;
friend constexpr bool operator==(const WORD& legacyAttr, const TextAttribute& attr) noexcept;
friend constexpr bool operator!=(const WORD& legacyAttr, const TextAttribute& attr) noexcept;
inline bool operator==(const TextAttribute& other) const noexcept
{
return memcmp(this, &other, sizeof(TextAttribute)) == 0;
}
inline bool operator!=(const TextAttribute& other) const noexcept
{
return memcmp(this, &other, sizeof(TextAttribute)) != 0;
}
bool IsLegacy() const noexcept;
bool IsBold() const noexcept;
@ -174,7 +177,7 @@ private:
uint16_t _hyperlinkId; // sizeof: 2, alignof: 2
TextColor _foreground; // sizeof: 4, alignof: 1
TextColor _background; // sizeof: 4, alignof: 1
ExtendedAttributes _extendedAttrs; // sizeof: 1, alignof: 1
ExtendedAttributes _extendedAttrs; // sizeof: 2, alignof: 2
#ifdef UNIT_TESTING
friend class TextBufferTests;
@ -191,20 +194,6 @@ enum class TextAttributeBehavior
StoredOnly, // only use the contained text attribute and skip the insertion of anything else
};
constexpr bool operator==(const TextAttribute& a, const TextAttribute& b) noexcept
{
return a._wAttrLegacy == b._wAttrLegacy &&
a._foreground == b._foreground &&
a._background == b._background &&
a._extendedAttrs == b._extendedAttrs &&
a._hyperlinkId == b._hyperlinkId;
}
constexpr bool operator!=(const TextAttribute& a, const TextAttribute& b) noexcept
{
return !(a == b);
}
#ifdef UNIT_TESTING
#define LOG_ATTR(attr) (Log::Comment(NoThrowString().Format( \

View file

@ -54,6 +54,8 @@ constexpr std::array<BYTE, 256> Index256ToIndex16 = {
// We should only need 4B for TextColor. Any more than that is just waste.
static_assert(sizeof(TextColor) == 4);
// Assert that the use of memcmp() for comparisons is safe.
static_assert(std::has_unique_object_representations_v<TextColor>);
bool TextColor::CanBeBrightened() const noexcept
{
@ -111,6 +113,8 @@ void TextColor::SetIndex(const BYTE index, const bool isIndex256) noexcept
{
_meta = isIndex256 ? ColorType::IsIndex256 : ColorType::IsIndex16;
_index = index;
_green = 0;
_blue = 0;
}
// Method Description:
@ -123,6 +127,9 @@ void TextColor::SetIndex(const BYTE index, const bool isIndex256) noexcept
void TextColor::SetDefault() noexcept
{
_meta = ColorType::IsDefault;
_red = 0;
_green = 0;
_blue = 0;
}
// Method Description:

View file

@ -94,8 +94,15 @@ public:
{
}
friend constexpr bool operator==(const TextColor& a, const TextColor& b) noexcept;
friend constexpr bool operator!=(const TextColor& a, const TextColor& b) noexcept;
bool operator==(const TextColor& other) const noexcept
{
return memcmp(this, &other, sizeof(TextColor)) == 0;
}
bool operator!=(const TextColor& other) const noexcept
{
return memcmp(this, &other, sizeof(TextColor)) != 0;
}
bool CanBeBrightened() const noexcept;
bool IsLegacy() const noexcept;
@ -144,19 +151,6 @@ private:
#endif
};
bool constexpr operator==(const TextColor& a, const TextColor& b) noexcept
{
return a._meta == b._meta &&
a._red == b._red &&
a._green == b._green &&
a._blue == b._blue;
}
bool constexpr operator!=(const TextColor& a, const TextColor& b) noexcept
{
return !(a == b);
}
#ifdef UNIT_TESTING
namespace WEX

View file

@ -8,7 +8,7 @@ Licensed under the MIT license.
#define BG_ATTRS (BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY)
#define META_ATTRS (COMMON_LVB_LEADING_BYTE | COMMON_LVB_TRAILING_BYTE | COMMON_LVB_GRID_HORIZONTAL | COMMON_LVB_GRID_LVERTICAL | COMMON_LVB_GRID_RVERTICAL | COMMON_LVB_REVERSE_VIDEO | COMMON_LVB_UNDERSCORE)
enum class ExtendedAttributes : BYTE
enum class ExtendedAttributes : uint16_t
{
Normal = 0x00,
Bold = 0x01,