Skip to content

Commit

Permalink
Remove templates from color-parsing classes
Browse files Browse the repository at this point in the history
  • Loading branch information
mbasaglia committed Aug 29, 2015
1 parent 295e77a commit d157b57
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 159 deletions.
96 changes: 86 additions & 10 deletions daemon/src/common/Color.cpp
Expand Up @@ -83,14 +83,6 @@ static Color g_color_table[ 32 ] =
{ 1.00f, 1.00f, 0.50f, 1.00f }, // O 31
};

template<>
const Color& Color::Indexed( int index )
{
if ( index < 0 )
index *= -1;
return g_color_table[index%32];
}

int StrlenNocolor( const char *string )
{
int len = 0;
Expand Down Expand Up @@ -182,8 +174,14 @@ std::string StripColors( const std::string& input )

namespace detail {

template<class Int>
constexpr bool Has8Bits( Int v )
const Color& Indexed( int index )
{
if ( index < 0 )
index *= -1;
return g_color_table[index%32];
}

constexpr bool Has8Bits( uint8_t v )
{
return ( v / 0x11 * 0x11 ) != v;
}
Expand Down Expand Up @@ -227,4 +225,82 @@ const char* CString ( const Color32Bit& color )

} // namespace detail



/*
* Converts a hexadecimal character to the value of the digit it represents.
* Pre: ishex(ch)
*/
static constexpr int gethex( char ch )
{
return ch > '9' ?
( ch >= 'a' ? ch - 'a' + 10 : ch - 'A' + 10 )
: ch - '0'
;
}

// Whether a character is a hexadecimal digit
static constexpr bool ishex( char ch )
{
return ( ch >= '0' && ch <= '9' ) ||
( ch >= 'A' && ch <= 'F' ) ||
( ch >= 'a' && ch <= 'f' );
}

TokenIterator::value_type TokenIterator::NextToken(const char* input)
{
if ( !input || *input == '\0' )
{
return value_type();
}

if ( input[0] == Constants::ESCAPE )
{
if ( input[1] == Constants::ESCAPE )
{
return value_type( input, input+2, value_type::ESCAPE );
}
else if ( input[1] == Constants::NULL_COLOR )
{
return value_type( input, input+2, value_type::DEFAULT_COLOR );
}
else if ( std::toupper( input[1] ) >= '0' && std::toupper( input[1] ) < 'P' )
{
return value_type( input, input+2, detail::Indexed( input[1] - '0' ) );
}
else if ( std::tolower( input[1] ) == 'x' && ishex( input[2] ) && ishex( input[3] ) && ishex( input[4] ) )
{
return value_type( input, input+5, Color(
gethex( input[2] ) / 15.f,
gethex( input[3] ) / 15.f,
gethex( input[4] ) / 15.f,
1
) );
}
else if ( input[1] == '#' )
{
bool long_hex = true;
for ( int i = 0; i < 6; i++ )
{
if ( !ishex( input[i+2] ) )
{
long_hex = false;
break;
}
}
if ( long_hex )
{
return value_type( input, input+8, Color(
( (gethex( input[2] ) << 4) | gethex( input[3] ) ) / 255.f,
( (gethex( input[4] ) << 4) | gethex( input[5] ) ) / 255.f,
( (gethex( input[6] ) << 4) | gethex( input[7] ) ) / 255.f,
1
) );
}
}
}

return value_type( input, input + Q_UTF8_Width( input ), value_type::CHARACTER );
}

} // namespace Color

0 comments on commit d157b57

Please sign in to comment.