Skip to content

Commit

Permalink
Reduce the complexity of FOptiAttr::prevent_no_color_video_attributes…
Browse files Browse the repository at this point in the history
…() and FTermDetection::secDA_Analysis()
  • Loading branch information
gansm committed Nov 8, 2023
1 parent 90c2ba7 commit 7d74973
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 151 deletions.
145 changes: 84 additions & 61 deletions final/output/tty/foptiattr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,50 @@ auto FOptiAttr::hasNoAttribute (const FChar& attr) -> bool
return ! hasAttribute(attr);
}

//----------------------------------------------------------------------
inline auto FOptiAttr::isItalicsUsed ( const FChar& term
, const FChar& next ) const -> bool
{
return ! term.attr.bit.italic && next.attr.bit.italic;
}

//----------------------------------------------------------------------
inline auto FOptiAttr::isCrossedOutUsed ( const FChar& term
, const FChar& next ) const -> bool
{
return ! term.attr.bit.crossed_out && next.attr.bit.crossed_out;
}

//----------------------------------------------------------------------
inline auto FOptiAttr::isDoubleUnderlineUsed ( const FChar& term
, const FChar& next ) const -> bool
{
return ! term.attr.bit.dbl_underline && next.attr.bit.dbl_underline;
}

//----------------------------------------------------------------------
inline auto FOptiAttr::isPCcharsetUsed ( const FChar& term
, const FChar& next ) const -> bool
{
return ! term.attr.bit.pc_charset && next.attr.bit.pc_charset;
}

//----------------------------------------------------------------------
inline auto FOptiAttr::isPCcharsetUsable ( FChar& term
, const FChar& next ) -> bool
{
if ( alt_equal_pc_charset
&& F_pc_charset.on.cap
&& next.attr.bit.alt_charset )
{
term.attr.bit.pc_charset = next.attr.bit.pc_charset;
changes.off.attr.bit.pc_charset = false;
return false;
}

return true;
}

//----------------------------------------------------------------------
inline auto FOptiAttr::hasColorChanged ( const FChar& term
, const FChar& next ) const -> bool
Expand All @@ -1021,63 +1065,21 @@ inline void FOptiAttr::resetColor (FChar& attr) const
inline void FOptiAttr::prevent_no_color_video_attributes ( FChar& attr
, bool next_has_color )
{
// Ignore attributes which can not combined with a color
// Ignore video attributes which can not combined with a color

if ( ! (hasColor(attr) || next_has_color) || F_color.attr_without_color <= 0 )
return;

static const auto& no_color_video_handlers = getNoColorVideoHandlerTable();
auto set_bits = uInt(F_color.attr_without_color);

while ( set_bits )
{
uInt bit = set_bits & (~set_bits + 1); // Get rightmost set bit
set_bits &= ~bit; // Clear rightmost set bit

switch ( bit )
{
case standout_mode:
attr.attr.bit.standout = false;
break;

case underline_mode:
attr.attr.bit.underline = false;
break;

case reverse_mode:
fake_reverse = true;
break;

case blink_mode:
attr.attr.bit.blink = false;
break;

case dim_mode:
attr.attr.bit.dim = false;
break;

case bold_mode:
attr.attr.bit.bold = false;
break;

case invisible_mode:
attr.attr.bit.invisible = false;
break;

case protected_mode:
attr.attr.bit.protect = false;
break;

case alt_charset_mode:
attr.attr.bit.alt_charset = false;
break;

case italic_mode:
attr.attr.bit.italic = false;
break;

default:
break; // Unknown attribute mode
}
if ( bit )
no_color_video_handlers[ffs(bit)](this, attr);
}
}

Expand Down Expand Up @@ -1107,8 +1109,6 @@ inline void FOptiAttr::deactivateAttributes (FChar& term, FChar& next)
//----------------------------------------------------------------------
inline void FOptiAttr::changeAttributeSGR (FChar& term, FChar& next)
{
bool pc_charset_usable{true};

if ( switchOn() || switchOff() )
setTermAttributes ( term
, { next.attr.bit.standout
Expand All @@ -1121,29 +1121,21 @@ inline void FOptiAttr::changeAttributeSGR (FChar& term, FChar& next)
, next.attr.bit.protect
, next.attr.bit.alt_charset } );

if ( alt_equal_pc_charset
&& F_pc_charset.on.cap
&& next.attr.bit.alt_charset )
{
term.attr.bit.pc_charset = next.attr.bit.pc_charset;
changes.off.attr.bit.pc_charset = false;
pc_charset_usable = false;
}
auto pc_charset_usable = isPCcharsetUsable(term, next);

if ( changes.off.attr.bit.pc_charset )
unsetTermPCcharset(term);

if ( ! term.attr.bit.italic && next.attr.bit.italic )
if ( isItalicsUsed(term, next) )
setTermItalic(term);

if ( ! term.attr.bit.crossed_out && next.attr.bit.crossed_out )
if ( isCrossedOutUsed(term, next) )
setTermCrossedOut(term);

if ( ! term.attr.bit.dbl_underline && next.attr.bit.dbl_underline )
if ( isDoubleUnderlineUsed(term, next) )
setTermDoubleUnderline(term);

if ( ! term.attr.bit.pc_charset && next.attr.bit.pc_charset
&& pc_charset_usable )
if ( isPCcharsetUsed(term, next) && pc_charset_usable )
setTermPCcharset(term);

if ( hasColorChanged(term, next) )
Expand Down Expand Up @@ -1344,6 +1336,37 @@ inline auto FOptiAttr::hasCharsetEquivalence() const -> bool
|| ( alt_off && pc_off && std::strcmp (alt_off, pc_off) == 0 );
}

//----------------------------------------------------------------------
auto FOptiAttr::getNoColorVideoHandlerTable() -> const NoColorVideoHandlerTable&
{
static const auto& no_color_video_handlers = std::make_unique<NoColorVideoHandlerTable>
(
NoColorVideoHandlerTable
{{
[] (FOptiAttr*, FChar&) {}, // No bit set (0)
[] (FOptiAttr*, FChar& fchar) { fchar.attr.bit.standout = false; }, // Standout mode (1)
[] (FOptiAttr*, FChar& fchar) { fchar.attr.bit.underline = false; }, // Underline mode (2)
[] (FOptiAttr* obj, FChar&) { obj->fake_reverse = true; }, // Reverse mode (4)
[] (FOptiAttr*, FChar& fchar) { fchar.attr.bit.blink = false; }, // Blink mode (8)
[] (FOptiAttr*, FChar& fchar) { fchar.attr.bit.dim = false; }, // Dim mode (16)
[] (FOptiAttr*, FChar& fchar) { fchar.attr.bit.bold = false; }, // Bold mode (32)
[] (FOptiAttr*, FChar& fchar) { fchar.attr.bit.invisible = false; }, // Invisible mode (64)
[] (FOptiAttr*, FChar& fchar) { fchar.attr.bit.protect = false; }, // Protected mode (128)
[] (FOptiAttr*, FChar& fchar) { fchar.attr.bit.alt_charset = false; }, // Alt_charset mode (256)
[] (FOptiAttr*, FChar&) {}, // Horizontal mode (512)
[] (FOptiAttr*, FChar&) {}, // Left mode (1024)
[] (FOptiAttr*, FChar&) {}, // Low mode (2048)
[] (FOptiAttr*, FChar&) {}, // Right mode (4096)
[] (FOptiAttr*, FChar&) {}, // Top mode (8192)
[] (FOptiAttr*, FChar&) {}, // Vertical mode (1638)
[] (FOptiAttr*, FChar& fchar) { fchar.attr.bit.italic = false; }, // Italic mode (32768)
[] (FOptiAttr*, FChar&) {} // No mode (65536)
}}
);

return *no_color_video_handlers;
}

//----------------------------------------------------------------------
auto FOptiAttr::getAttributeOnHandlers() -> const AttributeHandlers&
{
Expand Down
31 changes: 9 additions & 22 deletions final/output/tty/foptiattr.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ class FOptiAttr final
};

using AttributeHandlers = std::array<AttributeHandlerEntry, 13>;
using NoColorVideoHandler = std::function<void(FOptiAttr*, FChar&)>;
using NoColorVideoHandlerTable = std::array<NoColorVideoHandler, 18>;

// Enumerations
enum init_reset_tests
Expand All @@ -207,27 +209,6 @@ class FOptiAttr final
all_tests = 0x1f
};

enum attr_modes
{
standout_mode = 1,
underline_mode = 2,
reverse_mode = 4,
blink_mode = 8,
dim_mode = 16,
bold_mode = 32,
invisible_mode = 64,
protected_mode = 128,
alt_charset_mode = 256,
horizontal_mode = 512,
left_mode = 1024,
low_mode = 2048,
right_mode = 4096,
top_mode = 8192,
vertical_mode = 16384,
italic_mode = 32768,
no_mode = 65536
};

// Mutators
auto setTermBold (FChar&) -> bool;
auto unsetTermBold (FChar&) -> bool;
Expand Down Expand Up @@ -267,9 +248,14 @@ class FOptiAttr final
static auto hasColor (const FChar&) -> bool;
static auto hasAttribute (const FChar&) -> bool;
static auto hasNoAttribute (const FChar&) -> bool;
auto isItalicsUsed (const FChar&, const FChar&) const -> bool;
auto isCrossedOutUsed (const FChar&, const FChar&) const -> bool;
auto isDoubleUnderlineUsed (const FChar&, const FChar&) const -> bool;
auto isPCcharsetUsed (const FChar&, const FChar&) const -> bool;
auto isPCcharsetUsable (FChar&, const FChar&) -> bool;
auto hasColorChanged (const FChar&, const FChar&) const -> bool;

// Methods
auto hasColorChanged (const FChar&, const FChar&) const -> bool;
void resetColor (FChar&) const;
void prevent_no_color_video_attributes (FChar&, bool = false);
void deactivateAttributes (FChar&, FChar&);
Expand All @@ -282,6 +268,7 @@ class FOptiAttr final
void reset (FChar&) const;
auto caused_reset_attributes (const char[], uChar = all_tests) const -> bool;
auto hasCharsetEquivalence() const -> bool;
static auto getNoColorVideoHandlerTable() -> const NoColorVideoHandlerTable&;
static auto getAttributeOnHandlers() -> const AttributeHandlers&;
static auto getAttributeOffHandlers() -> const AttributeHandlers&;
static auto getByte0ReverseMask() -> uInt8;
Expand Down
Loading

0 comments on commit 7d74973

Please sign in to comment.