🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Custom Escape Sequences?

Started by
4 comments, last by brandon698sherrick 3 weeks ago

I was writing a custom print function to support capturing variables via a format string that looks like so:

print("text {var_name} other text {another_var} ... ");

it works and users can implement the IDebug interface for thier class that exposed a ToString() function to support this printing. only issue i have now is making it possible to do something like "\{raw_text}" so that any section preceeded by a ‘\’ is printed as is. but “\{” deletes the character and i end up with “raw_text}”. is it possible to tell as to ignore unknown escape sequences?

Advertisement

It is currently not possible. However you can escape the \ character with \\.

If that doesn't suit you, you can take a look at the logic for string parsing and customize it to suit your need. You'll find this logic in asCCompiler::ProcessStringConstant.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Thank you it worked, for anyone else curious in ascompiler.cpp in the asCompiler::ProcessStringConstant, where you find a warning for TXT_INVALID_ESCAPE_SEQUENCE i replaced the else block with

else
{
	// Invalid escape sequence
	char array[2] = {'\\', cstr[n]};
	str.Concatenate(array, 2);
	Warning(TXT_INVALID_ESCAPE_SEQUENCE, node);
	continue;
}

essential what it does it on an invalid escape sequence, it just pushes \{the character} as raw text to the output string. Maybe this could be an option to the script engine? or perhaps the default behaviour because the compiler issues a warning that can be handled.

I agree. This is certainly something I can add an engine property to control.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

MindOfTony said:

I was writing a custom print function to support capturing variables via a format string that looks like so:

print("text {var_name} other text {another_var} ... ");

it works and users can implement the IDebug interface for thier class that exposed a ToString() function to support this printing. only issue i have now is making it possible to do something like "\{raw_text}" so that any section preceeded by a ‘\’ is printed as is. but “\{” deletes the character and i end up with “raw_text}”. is it possible to tell as to ignore unknown escape sequences? Kynect Gov

Loop through each character in the format string. If the current character is a backslash (\), check the next character. If the next character is a known escape sequence (e.g., \n for newline, \t for tab), handle it accordingly and update the output string. If the next character is not a known escape sequence, append both the backslash and the next character to the output string, essentially printing them literally. If the current character is not a backslash, simply append it to the output string.

Advertisement