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.
String function that returns the number of visible characters.
Moderator: GZDoom Developers
-
- Posts: 2383
- Joined: Thu Feb 11, 2016 9:59 am
-
- Posts: 1337
- Joined: Tue Jul 15, 2003 4:18 pm
Re: String function that returns the number of visible characters.
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;
}
}
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"
-
- Posts: 2954
- 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.
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.
-
- Lead GZDoom+Raze Developer
- Posts: 49183
- Joined: Sat Jul 19, 2003 10:19 am
- Location: Germany
Re: String function that returns the number of visible characters.
GZDoom does not handle combining characters. They are too much of a hassle for a simple output system as we have.
-
- Lead GZDoom+Raze Developer
- Posts: 49183
- Joined: Sat Jul 19, 2003 10:19 am
- Location: Germany
Re: String function that returns the number of visible characters.
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.
viewtopic.php?p=1231446#p1231446
As things stand I see no need to act.
-
- Posts: 2383
- Joined: Thu Feb 11, 2016 9:59 am
Re: String function that returns the number of visible characters.
I see. In that case I can try what AFADoomer suggested. Thanks. I suppose this could be closed.
-
- Posts: 1337
- Joined: Tue Jul 15, 2003 4:18 pm
Re: String function that returns the number of visible characters.
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.
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.
-
- Lead GZDoom+Raze Developer
- Posts: 49183
- Joined: Sat Jul 19, 2003 10:19 am
- Location: Germany
Re: String function that returns the number of visible characters.
Of course they don't. They are string functions operating on the naked data.
-
- Posts: 13793
- Joined: Tue Jan 13, 2004 1:31 pm
- Preferred Pronouns: She/Her
Re: String function that returns the number of visible characters.
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 ...)
-
- Lead GZDoom+Raze Developer
- Posts: 49183
- Joined: Sat Jul 19, 2003 10:19 am
- Location: Germany
Re: String function that returns the number of visible characters.
It probably should be redone natively. There's surely legitimate reasons to retrieve such a stripped string on occasion.
-
- Posts: 2383
- Joined: Thu Feb 11, 2016 9:59 am
Re: String function that returns the number of visible characters.
Yes please. This would be perfect for my use case and addresses the initial suggestion.