public static string ReplaceEmoteKeywords( [NotNull] string message ) {
if ( message == null )
throw new ArgumentNullException( "message" );
int startIndex = message.IndexOf( '{' );
if ( startIndex == -1 ) {
return message; // break out early if there are no opening braces
}
StringBuilder output = new StringBuilder( message.Length );
int lastAppendedIndex = 0;
while ( startIndex != -1 ) {
int endIndex = message.IndexOf( '}', startIndex + 1 );
if ( endIndex == -1 ) {
break; // abort if there are no more closing braces
}
// see if emote was escaped (if odd number of backslashes precede it)
bool escaped = false;
for ( int i = startIndex - 1; i >= 0 && message[i] == '\\'; i-- ) {
escaped = !escaped;
}
// extract the keyword
string keyword = message.Substring( startIndex + 1, endIndex - startIndex - 1 );
char substitute;
if ( EmoteKeywords.TryGetValue( keyword.ToLowerInvariant(), out substitute ) ) {
if ( escaped ) {
// it was escaped; remove escaping character
startIndex++;
output.Append( message, lastAppendedIndex, startIndex - lastAppendedIndex - 2 );
lastAppendedIndex = startIndex - 1;
} else {
// it was not escaped; insert substitute character
output.Append( message, lastAppendedIndex, startIndex - lastAppendedIndex );
output.Append( substitute );
startIndex = endIndex + 1;
lastAppendedIndex = startIndex;
}
} else {
startIndex++; // unrecognized macro, keep going
}
startIndex = message.IndexOf( '{', startIndex );
}
// append the leftovers
output.Append( message, lastAppendedIndex, message.Length - lastAppendedIndex );
return output.ToString();
}