Skip to content

Instantly share code, notes, and snippets.

@fstamour
Last active December 16, 2015 11:28
Show Gist options
  • Save fstamour/5427061 to your computer and use it in GitHub Desktop.
Save fstamour/5427061 to your computer and use it in GitHub Desktop.
A simple helper function to handle escapes in parsing, you may choose the kind of error handling you want (by return value or by exception) and it is really easily converted to C.
//#include <stdexcept>
/// @warning The parameter is also the return value.
/// @return true if the escape sequence was valid, false otherwise
bool escapeChar( char& c ) {
switch( c ) {
case 't':
c = '\t';
break;
case 'n':
c = '\n';
break;
case '\n':
return true;
case ' ':
case '\\':
case '"':
break;
default:
//throw std::runtime_error("Undefined escape sequence.");
return false;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment