Count occurrence of a set of characters and subtract spaces from the string

亡梦爱人 提交于 2019-12-24 04:18:15

问题


See the first image below for the problem. You can see that the first line of text correctly aligns, but the second does not.

A few months ago I wrote some code that centers the text in a string in order to process it nicely onto a console for a gameserver modification. This code works really nicely, but has two major problems:

  • The gameserver crashes if we go over the 112 character limit of the actual console (this problem doesn't occur in a normal console message)

  • Use of colours in the text causes the string to shift to the right by two characters for each colour code

I am looking to solve the second problem (although solving the first would be a massive benefit), but do not have a clue how to do this. I cannot simply take the colour coding out since this is needed to colour the text.

This code might be simple if I didn't have 9 colour types to choose from:

*$1= White
*$2= Dark Blue
*$3= Green
*$4= Red
*$5= Yellow
*$6= Light Blue
*$7= Purple
*$8= Orange
*$9= Grey

I don't want to simply look for the '$' either, since this is used as a normal character in some cases.

Is ignoring the $1-$9 when inserting the spaces possible? If so, how would I approach this?

The Code

int CScriptBind_GameRules::CentreTextForConsole(IFunctionHandler *pH, const char *msg)
{
    if (msg)
    {
        const int linelength=200;
        char newmsg[linelength+1];
        for(int i=0;i<linelength;i++)
            newmsg[i]=0;
        for(int i=0;i<linelength;i++)
            newmsg[i]=' ';
        newmsg[linelength]=0;
        int msglen=strlen(msg);
        int startpos=4+linelength/2-msglen/2;
        for(int i=msglen-1;i>=0;i--)
            newmsg[startpos+i]=msg[i];
        msg=0; //Null the message.
        return pH->EndFunction(newmsg); 
}
else
{
    return pH->EndFunction();
}

Example

I can't get to my development PC right now as I'm as work- I'll post a screenshot in a few hours when I get back if this issue hasn't been resolved.

Caller from Lua:

Core.CenteredConsole:All(g_gameRules.game:CentreTextForConsole("$1Crysis$4Wars Infinity iX 5.0"));

This shows what it should look like. The message only works properly if it is less than 80 characters long.


回答1:


Your program crashes because of the defined length of your arrays. Resize newmsg appropriately

Also check that startpos + i >= 0 && < length of array ...


As for your second question:

As you said:

Use of colours in the text causes the string to shift to the right by two characters for each colour code

Seems that colour codes are being removed before printed and your problems arises because you are taking into account a length of a message which includes color codes...

If so, how would I approach this?`

Maybe adding 2 less spaces for each color code found on the msg for example.



来源:https://stackoverflow.com/questions/20097537/count-occurrence-of-a-set-of-characters-and-subtract-spaces-from-the-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!