stripColors & whatever - need comments & functions

This is the place where you can find everything related to the dedicated server, control scripts and community tools.

Moderators: Pit Crew, TM-Patrol

nocturne
solid chaser
solid chaser
Posts: 1390
Joined: 08 Jun 2007 18:48
Owned TM-games: all
Contact:

Re: stripColors & whatever - need comments & functions

Post by nocturne » 09 Oct 2010 10:39

Definitely some good recent work... I've noticed quite a few more accurate color code renderings since I instituted Slig's most recent changes in a few of my scripts.

And, agree with Xymph... I have players that somehow think a color code like $fof is real... and unfortunately TMF can somehow still recognize it as something workable beyond my reasoning, thus screwing up the display in every other script such as stats and RCP. I say let them suffer from their own stupidity... turn invalid codes into $g by default!
Chief Janitorial Supervisor of the Let'sRock! TM community : www.LetsRock-TM.com

Xymph
Pit Crew
Pit Crew
Posts: 5714
Joined: 19 Aug 2007 12:58
Owned TM-games: TMN, TMU, TMF, TM²
Contact:

Re: stripColors & whatever - need comments & functions

Post by Xymph » 09 Oct 2010 15:34

Xymph wrote:... except in one case:
"$[test" is stripped to "test" whereas the game renders it as "[test". Similar for "$]test", but all other non-alphanumerics after that $ are stripped along with the $. I tried a few approaches, but haven't found a way yet to match that effect.
It took a lot of fiddling and testing, but this is solved now. The trick was a look-ahead assertion for the brackets. I've also updated the comments related to Slig's changes. Here is the updated über-überfunc: :D

Code: Select all

/**
 * Summary: Strips all display formatting from an input string, suitable for display
 *          within the game ('$$' escape pairs are preserved) and for logging
 * Params : $input - The input string to strip formatting from
 *          $for_tm - Optional flag to double up '$' into '$$' (default, for TM) or not (for logs, etc)
 * Returns: The content portions of $input without formatting
 * Authors: Bilge/Assembler Maniac/Xymph/Slig
 */
function stripFormatting($input, $for_tm = true) {
    return
        //Replace all occurrences of a null character back with a pair of dollar
        //signs for displaying in TM, or a single dollar for log messages etc.
        str_replace("\0", ($for_tm ? '$$' : '$'),
            //Replace links (introduced in TMU)
            preg_replace(
                '/
                #Strip TMF H, L & P links by stripping everything between each square
                #bracket pair until another $H, $L or $P sequence (or EoS) is found;
                #this allows a $H to close a $L and vice versa, as does the game
                \\$[hlp](.*?)(?:\\[.*?\\](.*?))*(?:\\$[hlp]|$)
                /ixu',
                //Keep the first and third capturing groups if present
                '$1$2',
                //Replace various patterns beginning with an unescaped dollar
                preg_replace(
                    '/
                    #Match a single dollar sign and any of the following:
                    \\$
                    (?:
                        #Strip color codes by matching any hexadecimal character and
                        #any other two characters following it (except $)
                        [0-9a-f][^$][^$]
                        #Strip any incomplete color codes by matching any hexadecimal
                        #character followed by another character (except $)
                        |[0-9a-f][^$]
                        #Strip any single style code (including an invisible UTF8 char)
                        #that is not an H, L or P link or a bracket ($[ and $])
                        |[^][hlp]
                        #Strip the dollar sign if it is followed by [ or ], but do not
                        #strip the brackets themselves
                        |(?=[][])
                        #Strip the dollar sign if it is at the end of the string
                        |$
                    )
                    #Ignore alphabet case, ignore whitespace in pattern & use UTF-8 mode
                    /ixu',
                    //Replace any matches with nothing (i.e. strip matches)
                    '',
                    //Replace all occurrences of dollar sign pairs with a null character
                    str_replace('$$', "\0", $input)
                )
            )
        )
    ;
}
Developer of XASECO for TMF/TMN ESWC & XASECO2 for TM²: see XAseco.org
Find your way around the Mania community from the TMN ESWC hub, TMF hub, TM² hub, and SM hub

User avatar
Slig
Pit Crew
Pit Crew
Posts: 2124
Joined: 05 Sep 2005 17:51
Owned TM-games: ALL
Location: TraxicoLand (Fr)
Contact:

Re: stripColors & whatever - need comments & functions

Post by Slig » 09 Oct 2010 18:20

Xymph wrote:Here is the updated über-überfunc: :D
Looks nice ! :thumbsup:

Post Reply