Skip to content

Commit

Permalink
Color::Parser to represent a token range
Browse files Browse the repository at this point in the history
  • Loading branch information
mbasaglia committed Aug 29, 2015
1 parent 4623c2f commit 295e77a
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 95 deletions.
39 changes: 20 additions & 19 deletions daemon/src/common/Color.cpp
Expand Up @@ -94,9 +94,9 @@ const Color& Color::Indexed( int index )
int StrlenNocolor( const char *string )
{
int len = 0;
for ( TokenIterator i ( string ); *i; ++i )
for ( const auto& token : Parser( string ) )
{
if ( i->Type() == Token::CHARACTER || i->Type() == Token::ESCAPE )
if ( token.Type() == Token::CHARACTER || token.Type() == Token::ESCAPE )
{
len++;
}
Expand All @@ -109,13 +109,13 @@ char *StripColors( char *string )
{
std::string output;

for ( TokenIterator i ( string ); *i; ++i )
for ( const auto& token : Parser( string ) )
{
if ( i->Type() == Token::CHARACTER )
if ( token.Type() == Token::CHARACTER )
{
output.append( i->Begin(), i->Size() );
output.append( token.Begin(), token.Size() );
}
else if ( i->Type() == Token::ESCAPE )
else if ( token.Type() == Token::ESCAPE )
{
output.push_back(Constants::ESCAPE);
}
Expand All @@ -130,24 +130,25 @@ void StripColors( const char *in, char *out, int len )
{
--len;
bool decolor = true;
for ( TokenIterator i ( in ); *i; ++i )

for ( const auto& token : Parser( in ) )
{
if ( !decolor || i->Type() == Token::CHARACTER )
if ( !decolor || token.Type() == Token::CHARACTER )
{
if ( len < i->Size() )
if ( len < token.Size() )
{
break;
}
if ( *i->Begin() == Constants::DECOLOR_OFF || *i->Begin() == Constants::DECOLOR_ON )
if ( *token.Begin() == Constants::DECOLOR_OFF || *token.Begin() == Constants::DECOLOR_ON )
{
decolor = ( *i->Begin() == Constants::DECOLOR_ON );
decolor = ( *token.Begin() == Constants::DECOLOR_ON );
continue;
}
strncpy( out, i->Begin(), i->Size() );
out += i->Size();
len -= i->Size();
strncpy( out, token.Begin(), token.Size() );
out += token.Size();
len -= token.Size();
}
else if ( i->Type() == Token::ESCAPE )
else if ( token.Type() == Token::ESCAPE )
{
if ( len < 1 )
{
Expand All @@ -164,13 +165,13 @@ std::string StripColors( const std::string& input )
{
std::string output;

for ( TokenIterator i ( input.c_str() ); *i; ++i )
for ( const auto& token : Parser( input.c_str() ) )
{
if ( i->Type() == Token::CHARACTER )
if ( token.Type() == Token::CHARACTER )
{
output.append( i->Begin(), i->Size() );
output.append( token.Begin(), token.Size() );
}
else if ( i->Type() == Token::ESCAPE )
else if ( token.Type() == Token::ESCAPE )
{
output.push_back(Constants::ESCAPE);
}
Expand Down
36 changes: 36 additions & 0 deletions daemon/src/common/Color.h
Expand Up @@ -627,10 +627,46 @@ template<class CharT, class TokenAdvanceT = TokenAdvanceOne>
value_type token;
};

/**
* Generic class to parse C-style strings into tokens
*
* CharT is the type for the input
* TokenAdvanceT is the advancement policy used to define characters
*/
template<class CharT, class TokenAdvanceT = TokenAdvanceOne>
class BasicParser
{
public:
using value_type = BasicToken<CharT>;
using reference = const value_type&;
using pointer = const value_type*;
using iterator = BasicTokenIterator<CharT, TokenAdvanceOne>;

explicit BasicParser(const CharT* input, const Color& default_color = White)
: input(input), default_color(default_color)
{}

iterator begin() const
{
return iterator(input);
}

iterator end() const
{
return iterator();
}

private:
const CharT* input;
Color default_color;
};

// Default token type for Utf-8 C-strings
using Token = BasicToken<char>;
// Default token iterator for Utf-8 C-strings
using TokenIterator = BasicTokenIterator<char, TokenAdvanceUtf8>;
// Default parser for Utf-8 C-strings
using Parser = BasicParser<char, TokenAdvanceUtf8>;

// Returns the number of characters in a string discarding color codes
// UTF-8 sequences are counted as a single character
Expand Down
40 changes: 20 additions & 20 deletions daemon/src/engine/client/cl_console.cpp
Expand Up @@ -337,16 +337,16 @@ bool Con_CheckResize()
// Count the number of visible characters, adding a new line when too long
const char* begin = line.c_str();
int len = 0;
for ( Color::TokenIterator i ( line.c_str() ); *i; ++i )
for ( const auto& token : Color::Parser( line.c_str() ) )
{
if ( i->Type() == Color::Token::CHARACTER || i->Type() == Color::Token::ESCAPE )
if ( token.Type() == Color::Token::CHARACTER || token.Type() == Color::Token::ESCAPE )
{
len++;
}
if ( len > consoleState.textWidthInChars )
{
consoleState.lines.emplace_back( begin, i->Begin() );
begin = i->Begin();
consoleState.lines.emplace_back( begin, token.Begin() );
begin = token.Begin();
len = 0;
}
}
Expand Down Expand Up @@ -490,26 +490,26 @@ bool CL_InternalConsolePrint( const char *text )

Con_Linefeed();

for ( Color::TokenIterator it ( text ); *it; ++it )
for ( const auto& token : Color::Parser( text ) )
{
if ( it->Type() == Color::Token::COLOR || it->Type() == Color::Token::DEFAULT_COLOR )
if ( token.Type() == Color::Token::COLOR || token.Type() == Color::Token::DEFAULT_COLOR )
{
consoleState.lines.back().append( it->Begin(), it->Size() );
consoleState.lines.back().append( token.Begin(), token.Size() );
continue;
}

if ( !wordLen )
{
// count word length
for ( Color::TokenIterator i = it; *i; ++i )
for ( const auto& wordtoken : Color::Parser( token.Begin() ) )
{
if ( i->Type() == Color::Token::ESCAPE )
if ( token.Type() == Color::Token::ESCAPE )
{
wordLen++;
}
else if ( i->Type() == Color::Token::CHARACTER )
else if ( token.Type() == Color::Token::CHARACTER )
{
if ( std::isspace( *i->Begin() ) )
if ( std::isspace( *token.Begin() ) )
{
break;
}
Expand All @@ -524,14 +524,14 @@ bool CL_InternalConsolePrint( const char *text )
}
}

switch ( *it->Begin() )
switch ( *token.Begin() )
{
case '\n':
Con_Linefeed();
break;

default: // display character and advance
consoleState.lines.back().append( it->Begin(), it->Size() );
consoleState.lines.back().append( token.Begin(), token.Size() );
if ( wordLen > 0 )
{
--wordLen;
Expand Down Expand Up @@ -878,25 +878,25 @@ void Con_DrawConsoleContent()

re.SetColor( console_color_alpha );

for ( Color::TokenIterator it ( consoleState.lines[row].c_str() ); *it; ++it )
for ( const auto& token : Color::Parser( consoleState.lines[row].c_str() ) )
{
if ( it->Type() == Color::Token::COLOR )
if ( token.Type() == Color::Token::COLOR )
{
Color::Color color = it->Color();
Color::Color color = token.Color();
color.SetAlpha( console_color_alpha.Alpha() );
re.SetColor( color );
}
else if ( it->Type() == Color::Token::DEFAULT_COLOR )
else if ( token.Type() == Color::Token::DEFAULT_COLOR )
{
re.SetColor( console_color_alpha );
}
else if ( it->Type() == Color::Token::CHARACTER )
else if ( token.Type() == Color::Token::CHARACTER )
{
int ch = Q_UTF8_CodePoint( it->Begin() );
int ch = Q_UTF8_CodePoint( token.Begin() );
SCR_DrawConsoleFontUnichar( currentWidthLocation, floor( lineDrawPosition + 0.5 ), ch );
currentWidthLocation += SCR_ConsoleFontUnicharWidth( ch );
}
else if ( it->Type() == Color::Token::ESCAPE )
else if ( token.Type() == Color::Token::ESCAPE )
{
int ch = Color::Constants::ESCAPE;
SCR_DrawConsoleFontUnichar( currentWidthLocation, floor( lineDrawPosition + 0.5 ), ch );
Expand Down
14 changes: 7 additions & 7 deletions daemon/src/engine/client/cl_scrn.cpp
Expand Up @@ -291,35 +291,35 @@ void SCR_DrawSmallStringExt( int x, int y, const char *string,
xx = x;
re.SetColor( setColor );

for ( Color::TokenIterator i( string ); *i; ++i )
for ( const auto& token : Color::Parser( string ) )
{
if ( i->Type() == Color::Token::COLOR || i->Type() == Color::Token::DEFAULT_COLOR )
if ( token.Type() == Color::Token::COLOR || token.Type() == Color::Token::DEFAULT_COLOR )
{
if ( !forceColor )
{
Color::Color color;
if ( i->Type() == Color::Token::DEFAULT_COLOR )
if ( token.Type() == Color::Token::DEFAULT_COLOR )
{
color = setColor;
}
else
{
color = i->Color();
color = token.Color();
color.SetAlpha( setColor.Alpha() );
}
re.SetColor( color );
}

if ( noColorEscape )
{
for ( const char *c = i->Begin(); c != i->End(); c++ )
for ( const char *c = token.Begin(); c != token.End(); c++ )
{
SCR_DrawConsoleFontUnichar( xx, y, *c );
xx += SCR_ConsoleFontUnicharWidth( *c );
}
}
}
else if ( i->Type() == Color::Token::ESCAPE )
else if ( token.Type() == Color::Token::ESCAPE )
{
SCR_DrawConsoleFontUnichar( xx, y, Color::Constants::ESCAPE );
xx += SCR_ConsoleFontUnicharWidth( Color::Constants::ESCAPE );
Expand All @@ -332,7 +332,7 @@ void SCR_DrawSmallStringExt( int x, int y, const char *string,
}
else
{
int ch = Q_UTF8_CodePoint( i->Begin() );
int ch = Q_UTF8_CodePoint( token.Begin() );
SCR_DrawConsoleFontUnichar( xx, y, ch );
xx += SCR_ConsoleFontUnicharWidth( ch );
}
Expand Down
22 changes: 11 additions & 11 deletions daemon/src/engine/sys/con_curses.cpp
Expand Up @@ -116,25 +116,25 @@ static void CON_ColorPrint( WINDOW *win, const char *msg, bool stripcodes )
Con_ClearColor( win );

std::string buffer;
for ( Color::TokenIterator i ( msg ); *i; ++i )
for ( const auto& token : Color::Parser( msg ) )
{

if ( i->Type() == Color::Token::COLOR )
if ( token.Type() == Color::Token::COLOR )
{
if ( !buffer.empty() )
{
waddstr( win, buffer.c_str() );
buffer.clear();
}

CON_SetColor( win, i->Color() );
CON_SetColor( win, token.Color() );

if ( !stripcodes )
{
buffer.append( i->Begin(), i->Size() );
buffer.append( token.Begin(), token.Size() );
}
}
else if ( i->Type() == Color::Token::DEFAULT_COLOR )
else if ( token.Type() == Color::Token::DEFAULT_COLOR )
{
if ( !buffer.empty() )
{
Expand All @@ -146,23 +146,23 @@ static void CON_ColorPrint( WINDOW *win, const char *msg, bool stripcodes )

if ( !stripcodes )
{
buffer.append( i->Begin(), i->Size() );
buffer.append( token.Begin(), token.Size() );
}
}
else if ( i->Type() == Color::Token::ESCAPE )
else if ( token.Type() == Color::Token::ESCAPE )
{
if ( !stripcodes )
{
buffer.append( i->Begin(), i->Size() );
buffer.append( token.Begin(), token.Size() );
}
else
{
buffer += Color::Constants::ESCAPE;
}
}
else if ( i->Type() == Color::Token::CHARACTER )
else if ( token.Type() == Color::Token::CHARACTER )
{
if ( *i->Begin() == '\n' )
if ( *token.Begin() == '\n' )
{
waddstr( win, buffer.c_str() );
buffer.clear();
Expand All @@ -171,7 +171,7 @@ static void CON_ColorPrint( WINDOW *win, const char *msg, bool stripcodes )
}
else
{
buffer.append( i->Begin(), i->Size() );
buffer.append( token.Begin(), token.Size() );
}
}
}
Expand Down

0 comments on commit 295e77a

Please sign in to comment.