Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use memcmp for TextAttribute & TextColor comparison #10566

Merged
5 commits merged into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/buffer/out/TextAttribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>);

BYTE TextAttribute::s_legacyDefaultForeground = 7;
BYTE TextAttribute::s_legacyDefaultBackground = 0;
Expand Down
31 changes: 10 additions & 21 deletions src/buffer/out/TextAttribute.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,15 @@ class TextAttribute final

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;
Expand Down Expand Up @@ -178,7 +181,7 @@ class TextAttribute final
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;
Expand All @@ -195,20 +198,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( \
Expand Down
11 changes: 11 additions & 0 deletions src/buffer/out/TextColor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ constexpr std::array<BYTE, 256> Index256ToIndex16 = {

// clang-format on

// We should only need 4B for TextColor. Any more than that is just waste.
static_assert(sizeof(TextColor) <= 4 * sizeof(BYTE), "We should only need 4B for an entire TextColor. Any more than that is just waste");

// Assert that the use of memcmp() for comparisons is safe.
static_assert(std::has_unique_object_representations_v<TextColor>);

bool TextColor::CanBeBrightened() const noexcept
{
return IsIndex16() || IsDefault();
Expand Down Expand Up @@ -106,6 +112,8 @@ void TextColor::SetIndex(const BYTE index, const bool isIndex256) noexcept
{
_meta = isIndex256 ? ColorType::IsIndex256 : ColorType::IsIndex16;
_index = index;
_green = 0;
_blue = 0;
skyline75489 marked this conversation as resolved.
Show resolved Hide resolved
}

// Method Description:
Expand All @@ -118,6 +126,9 @@ void TextColor::SetIndex(const BYTE index, const bool isIndex256) noexcept
void TextColor::SetDefault() noexcept
{
_meta = ColorType::IsDefault;
_red = 0;
skyline75489 marked this conversation as resolved.
Show resolved Hide resolved
_green = 0;
_blue = 0;
}

// Method Description:
Expand Down
26 changes: 9 additions & 17 deletions src/buffer/out/TextColor.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,15 @@ struct TextColor
{
}

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;
Expand Down Expand Up @@ -115,19 +122,6 @@ struct TextColor
#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
Expand Down Expand Up @@ -157,5 +151,3 @@ namespace WEX
}
}
#endif

static_assert(sizeof(TextColor) <= 4 * sizeof(BYTE), "We should only need 4B for an entire TextColor. Any more than that is just waste");
2 changes: 1 addition & 1 deletion src/inc/conattrs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to make std::has_unique_object_representations_v<TextAttribute> happy.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better to keep this as BYTE & add a uint8_t zeropad to TextAttribute?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good idea! If @skyline75489 wants to he could still change it. 🙂
Personally I'd be fine merging it either way though.

{
Normal = 0x00,
Bold = 0x01,
Expand Down