Author Topic: getSubStr while excluding hex color codes  (Read 2342 times)

I want players to be able to set tags limited to four characters, not including hex color codes such as <color:FFFFFF>.
I use this currently:
Code: [Select]
  %prefix = getSubStr(%prefix , 0 , 4);
   %suffix = getSubStr(%suffix , 0 , 4);
The cmd is pretty much /tags [prefix] [suffix]
So attempting colored tags justs look like: <col
« Last Edit: February 24, 2015, 11:06:24 PM by Tezuni 2.0 »

Check it for hex codes, if it has one (or more) pull it/them out of the variable and store it in (a) different variable(s)?

Check it for hex codes, if it has one (or more) pull it/them out of the variable and store it in (a) different variable(s)?
how?

Here's a list of general string functions, this should help you out:
Code: [Select]
   General string manipulation functions.
   @{ */
   virtual int strcmp(string one, string two) {}
   virtual int stricmp(string one, string two) {}
   virtual int strlen(string one, string two) {}
   /*!  Returns the start of the sub string two in one or -1 if not found. */
   virtual int strstr(string one, string two) {}
   /*!  Find needle in hay, starting offset bytes in. */
   virtual int strpos(string hay, string needle, int offset=0) {}
   /*!  Find needle in hay, starting offset bytes in. (Case insensitive) */
   virtual int stripos(string hay, string needle, int offset=0) {}
   virtual string ltrim(string value) {}
   virtual string rtrim(string value) {}
   virtual string trim(string) {}
   /*!  Remove all the characters in chars from value. */
   virtual string stripChars(string value, string chars) {}
   /*!  Convert string to lower case. */
   virtual string strlwr(string) {}
   /*!  Convert string to upper case. */
   virtual string strupr(string) {}
   virtual string strchr(string,char) {}
   virtual string strreplace(string source, string from, string to) {}
   /*!  Returns the substring of str, starting at start, and continuing to either the end of the string, or numChars characters, whichever comes first. */
   virtual string getSubStr(string str, int start, int numChars) {}
   /*!  Returns number of occurences of char in string */
   virtual int getCharCount(string, char) {}
   /// @}

You can use stripMLControlChars (correct me if that name is wrong) to remove all TML. I think, to effectively remove all of them in cases where someone nests tags to try and circumvent stripping, some kind of while loop is necessary, but I don't know how precise you need to be, as obviously in this case that would simply cause the command to fail intended function. Basically, you can just check the length after stripping the TML. This does get tricky when you're trying to cut down the string, however.
« Last Edit: February 24, 2015, 11:38:57 PM by otto-san »

You can use stripMLControlChars (correct me if that name is wrong) to remove all TML. I think, to effectively remove all of them in cases where someone nests tags to try and circumvent stripping, some kind of while loop is necessary
stripMLControlChars is fixed from text inside text

<color:<color:000000>ffffff> would only use the function once rather than more.

if(strLen(collapseEscape(stripMLControlChars(%tag))) > 4)
    return messageClient(%client, '', "\c6You can only have four characters in your name, excluding color tags and \\c colors.");


That'll stop the function from continuing if the tag has more than 4 characters (excluding color and other ML tags) and send the client a message saying you can only have four.

Here's another useful function you can apply to the tag, too. It'll remove all ML tags besides color tags, that way you don't get huge text or changed fonts or anything.

%tag = strReplace(stripMLControlChars(strReplace(%tag, "<color:", "[color:")), "[color:", "<color:");

stripMLControlChars is fixed from text inside text

<color:<color:000000>ffffff> would only use the function once rather than more.
Yeah but this is for a character limit check, so someone putting that in would only be hurting themselves.

If you really needed to get rid of nested color tags, you might be able to get away with while(%text !$= (%text = stripMLControlChars(%text))){} which will run it over and over again until it stays the same. I only tested this with a single console command and it's kinda abusing torque a bit, but it did clear out a nested color tag so it might be useful in other situations.

stripMLControlChars is fixed from text inside text

<color:<color:000000>ffffff> would only use the function once rather than more.
No loop is necessary, coding help has been over this before I'm sure.

echo(stripMLcontrolchars("x<color:<color:f0f0f0>0f0f0f>y"));
xy

But you see this is actually undesirable behaviour, since xy is clearly less than 10 characters, while if you actually talk it into chat, you can clearly see

it's clearly more than 10 characters.
« Last Edit: February 27, 2015, 10:44:59 AM by Ipquarx »

No loop is necessary, coding help has been over this before I'm sure.

echo(stripMLcontrolchars("x<color:<color:f0f0f0>0f0f0f>y"));
xy
Derp. Tested the loop, didn't test the function itself.

I want players to be able to set tags limited to four characters, not including hex color codes such as <color:FFFFFF>.
I use this currently:
Code: [Select]
  %prefix = getSubStr(%prefix , 0 , 4);
   %suffix = getSubStr(%suffix , 0 , 4);
The cmd is pretty much /tags [prefix] [suffix]
So attempting colored tags justs look like: <col

There is already one made. Its GetHex

There is already one made. Its GetHex

Huh? GetHex isn't a default function; I've never heard of it before.

if(strLen(collapseEscape(stripMLControlChars(%tag))) > 4)
    return messageClient(%client, '', "\c6You can only have four characters in your name, excluding color tags and \\c colors.");


That'll stop the function from continuing if the tag has more than 4 characters (excluding color and other ML tags) and send the client a message saying you can only have four.

Here's another useful function you can apply to the tag, too. It'll remove all ML tags besides color tags, that way you don't get huge text or changed fonts or anything.

%tag = strReplace(stripMLControlChars(strReplace(%tag, "<color:", "[color:")), "[color:", "<color:");
I'm a little late in implementing this, but all of that is working perfectly for me in the server right now.  Thank you for being a charitable coding wizard.

And thank you to everyone else for their input and contributions here as well.