I need a function that would give me the escaped special character from the letter character. It would apply only when it makes sense: 't', 'n', 'r', 'b', 'f', 'v'.
Right now, here is my implementation:
typedef struct SpecialCharacter
{
SpecialCharacter( char letter, char special )
{
m_letter = letter;
m_special = special;
}
char m_letter;
char m_special;
} SpecialCharacter;
typedef std::vector<SpecialCharacter> SpecialCharacters;
static const SpecialCharacters& GetSpecialCharacters()
{
static SpecialCharacters characters;
if ( characters.empty() )
{
characters.push_back( SpecialCharacter( 'b', '\b' ) );
characters.push_back( SpecialCharacter( 'f', '\f' ) );
characters.push_back( SpecialCharacter( 'n', '\n' ) );
characters.push_back( SpecialCharacter( 'r', '\r' ) );
characters.push_back( SpecialCharacter( 't', '\t' ) );
characters.push_back( SpecialCharacter( 'v', '\v' ) );
}
return characters;
}
static bool getEscaped( char letter, char& escaped )
{
for ( SpecialCharacters::const_iterator iter = GetSpecialCharacters().begin();
iter != GetSpecialCharacters().end();
++iter )
{
if ( letter == iter->m_letter)
{
escaped = iter->m_special;
return true;
}
}
return false;
}
I don't like the way I'm adding elements to the characters vector:
characters.push_back( SpecialCharacter( 'b', '\b' ) );
Would there be a way to do:
characters.push_back( SpecialCharacter( 'b' ) );
and then have something like:
SpecialCharacter( char letter )
{
m_letter = letter;
m_special = '\' + letter; // does not work, of course!
}
I'd like a way to merge '\' and 'n' into '\n', or get '\n' from 'n' by any different way...
Edit: case statement is what I had originally before I moved the code to a vector of struct (because I need a function to do the way around). So that would not be a good solution to my problem. I'd like to transform 'n' to '\n' without any kind of "lookup table"