Skip to content
Permalink
Browse files

Color::Parser to represent a token range

  • Loading branch information...
mbasaglia committed Aug 29, 2015
1 parent 4623c2f commit 295e77aa5007206f714b67816e22451d5dbf4a7b
@@ -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++;
}
@@ -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);
}
@@ -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 )
{
@@ -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);
}
@@ -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
@@ -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;
}
}
@@ -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;
}
@@ -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;
@@ -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 );
@@ -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 );
@@ -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 );
}
@@ -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() )
{
@@ -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();
@@ -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() );
}
}
}

0 comments on commit 295e77a

Please sign in to comment.
You can’t perform that action at this time.