Page 1 of 1

String function that returns the number of visible characters.

Posted: Fri Nov 04, 2022 10:00 am
by Accensus
I mean something that DOESN'T count the color escapes. Both Length and CodePointCount are completely useless whenever I need to work with a colored string, which is 99% of the time. I tried using FIlter() out of frustration but that didn't do much either.

Hell, anything that allows me to get rid of the color escapes would be appreciated, I don't care what it is. Colored strings are a nightmare to work with right now. I'd even accept regex to allow me to just purge the color escapes from the string in order to make Length() work correctly.

Re: String function that returns the number of visible characters.

Posted: Fri Nov 04, 2022 6:09 pm
by AFADoomer

Code: Select all

class ZScriptTools
{
	// Strip color codes out of a string
	static String StripColorCodes(String input)
	{
		int place = 0;
		int len = input.length();
		String output;

		while (place < len)
		{
			if (!(input.Mid(place, 1) == String.Format("%c", 0x1C)))
			{
				output = output .. input.Mid(place, 1);
				place++;
			}
			else if (input.Mid(place + 1, 1) == "[")
			{
				place += 2;
				while (place < len - 1 && !(input.Mid(place, 1) == "]")) { place++; }
				if (input.Mid(place, 1) == "]") { place++; }
			}
			else
			{
				if (place + 1 < len - 1) { place += 2; }
				else break;
			}
		}

		return output;
	}

	// Strips all control codes out of a string
	static String StripControlCodes(String input)
	{
		String output = "";
		input = ZScriptTools.StripColorCodes(input); // Special handling to also remove the color index or string name

		int i = 0;
		int c = -1;

		while (c != 0)
		{
			[c, i] = input.GetNextCodePoint(i);
			if (
				(c > 0x001F && c < 0x007F) || // Skip C0 characters (ASCII Control Codes)
				(c > 0x007F && c < 0x0080) || // Skip Delete (ASCII Delete)
				c > 0x009F // Skip C1 characters (UNICODE-specific control codes)
			) { output.AppendCharacter(c); }
		}

		return output;
	}
}
Try this.

String nocolor = ZScriptTools.StripColorCodes("\cATest\n\c[Red]String");
will return "Test\nString"

String stripped = ZScriptTools.StripControlCodes("\cATest\n\c[Red]String");
will return "TestString"

Re: String function that returns the number of visible characters.

Posted: Fri Nov 04, 2022 8:09 pm
by Chris
Note that "visible characters" becomes murky when dealing with Unicode, as there are plenty of characters that are used to alter other characters (adding diacritics, change rendering direction, etc). With UTF-8, some characters may span 2, 3, or 4 bytes. Simply counting the number of bytes and ignoring the color codes won't result in the number of printed characters.

Re: String function that returns the number of visible characters.

Posted: Sat Nov 05, 2022 12:46 am
by Graf Zahl
GZDoom does not handle combining characters. They are too much of a hassle for a simple output system as we have.

Re: String function that returns the number of visible characters.

Posted: Sat Nov 05, 2022 3:29 am
by Graf Zahl
I think this entire request stems from the same misunderstanting as this one:
viewtopic.php?p=1231446#p1231446

As things stand I see no need to act.

Re: String function that returns the number of visible characters.

Posted: Sat Nov 05, 2022 6:46 am
by Accensus
I see. In that case I can try what AFADoomer suggested. Thanks. I suppose this could be closed.

Re: String function that returns the number of visible characters.

Posted: Sat Nov 05, 2022 9:33 am
by AFADoomer
Oops, didn't realize that this was in feature suggestions when I replied...

That said - if you are doing any kind of manual string manipulation, the Left, Mid, etc. functions do *not* ignore color codes or other control codes, which is why I wrote functions above.

Re: String function that returns the number of visible characters.

Posted: Sat Nov 05, 2022 10:23 am
by Graf Zahl
Of course they don't. They are string functions operating on the naked data.

Re: String function that returns the number of visible characters.

Posted: Sat Nov 05, 2022 11:11 am
by Rachael
Accensus wrote: Sat Nov 05, 2022 6:46 am I see. In that case I can try what AFADoomer suggested. Thanks. I suppose this could be closed.
If there is absolutely no desire to include a native C++ version of AFADoomer's code, I will do that. But there may be some performance benefits by redoing this in native C++ code, if only for the idea that this function might be called every tic or frame, depending on where the function that is placing the text to the screen is located. (To be proper, ideally the output of this function should be cached to a local variable, anyway, but not everyone will do that ...)

Re: String function that returns the number of visible characters.

Posted: Sat Nov 05, 2022 11:37 am
by Graf Zahl
It probably should be redone natively. There's surely legitimate reasons to retrieve such a stripped string on occasion.

Re: String function that returns the number of visible characters.

Posted: Sat Nov 05, 2022 12:51 pm
by Accensus
Yes please. This would be perfect for my use case and addresses the initial suggestion.