String function that returns the number of visible characters.

Remember, just because you request it, that doesn't mean you'll get it.

Moderator: GZDoom Developers

Accensus
Posts: 2377
Joined: Thu Feb 11, 2016 9:59 am

String function that returns the number of visible characters.

Post 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.
User avatar
AFADoomer
Posts: 1272
Joined: Tue Jul 15, 2003 4:18 pm

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

Post 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"
User avatar
Chris
Posts: 2863
Joined: Thu Jul 17, 2003 12:07 am
Graphics Processor: ATI/AMD with Vulkan/Metal Support

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

Post 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.
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 48325
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

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

Post by Graf Zahl »

GZDoom does not handle combining characters. They are too much of a hassle for a simple output system as we have.
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 48325
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

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

Post 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.
Accensus
Posts: 2377
Joined: Thu Feb 11, 2016 9:59 am

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

Post by Accensus »

I see. In that case I can try what AFADoomer suggested. Thanks. I suppose this could be closed.
User avatar
AFADoomer
Posts: 1272
Joined: Tue Jul 15, 2003 4:18 pm

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

Post 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.
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 48325
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

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

Post by Graf Zahl »

Of course they don't. They are string functions operating on the naked data.
User avatar
Rachael
Admin
Posts: 13084
Joined: Tue Jan 13, 2004 1:31 pm
Preferred Pronouns: She/Her

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

Post 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 ...)
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 48325
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

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

Post by Graf Zahl »

It probably should be redone natively. There's surely legitimate reasons to retrieve such a stripped string on occasion.
Accensus
Posts: 2377
Joined: Thu Feb 11, 2016 9:59 am

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

Post by Accensus »

Yes please. This would be perfect for my use case and addresses the initial suggestion.

Return to “Feature Suggestions [GZDoom]”