Skip to content

Commit

Permalink
Fixed incorrect processing of SGR 0 (reset)
Browse files Browse the repository at this point in the history
Fixed processing of "reset SGR":  \x1b[0m
Updated tests.
  • Loading branch information
pozemka committed Feb 16, 2020
1 parent 89480a0 commit 95b7990
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/ansi_esc2html.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,19 @@ ANSI_SGR2HTML::impl::SGRParts ANSI_SGR2HTML::impl::splitSGR(std::string_view dat

// ~30% faster
unsigned char v = 0;
bool has_digit = false;
for(size_t i = 0 ; i < data.size(); i++) {
char cc = data[i];
if(isdigit(cc)) {
v = static_cast<unsigned char>(v * 10 + (cc-'0')); //Part of SGR are 0..255 so unsigned char overflow happen only for incorrect data.
} else if(v != 0) {
has_digit = true;
} else if(has_digit) {
sgr_parts.push_back(v);
v = 0;
has_digit = false;
}
}
if(v != 0) {
if(has_digit) {
sgr_parts.push_back(v);
}
return sgr_parts;
Expand Down
7 changes: 5 additions & 2 deletions tests/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ TEST_CASE( "mixed", "[mixed]" ) {
R"(<font color="#2cb5e9"><span style="background-color:#39b54a">E<font color="#762671">xit</font></span></font>)"}},
{"bg+fg then fg",
{"\x1b[46;32mE\x1b[35mxit",
R"(<span style="background-color:#2cb5e9"><font color="#39b54a">E<font color="#762671">xit</font></font></span>)"}}
R"(<span style="background-color:#2cb5e9"><font color="#39b54a">E<font color="#762671">xit</font></font></span>)"}},
{"bold then reset then bold",
{"\x1b[1m bold \x1b[0m not bold \x1b[1m bold \x1b[0m",
R"(<b> bold </b> not bold <b> bold </b>)"}}
}));
GIVEN( section )
THEN( "output should be " << test_case.expected ) {
Expand Down Expand Up @@ -155,7 +158,7 @@ TEST_CASE( "basic bad inputs for simpleParse", "[basic_bad_simple]") {
R"(bbb)"}},
{"broken sgr then correct one",
{"\x1b[31\x1b[32m aaa",
R"(<font color="#39b54a"> aaa</font>)"}},
R"(<font color="#39b54a"> aaa</font>)"}}
}));
GIVEN( section )
THEN( "output should be " << test_case.expected ) {
Expand Down

0 comments on commit 95b7990

Please sign in to comment.