How can I ignore certain strings in my string centring function?

眉间皱痕 提交于 2020-01-22 00:20:14

问题


N.B: Directly connected to a problem I had a few years ago, but I'd like to resolve the first issue there which wasn't otherwise part of the question, so please don't flag it as a duplicate of my earlier question.

I have a string centring function that centres the given string according to the given width (which is 113 characters):

std::string center(std::string input, int width = 113) { 
    return std::string((width - input.length()) / 2, ' ') + input;
}

I am using a game SDK in order to create a gameserver modification, and this game SDK supports coloured strings in the game's command console, which are denoted using a dollar sign and a number from 0-9 (i.e, $1) and are not printed in the console itself.

The string centring function above treats these markers as part of the total string, so I want to add the total amount of characters these markers take up to the width so that the string is actually centred.

I have tried modifying the function:

std::string centre(std::string input, int width = 113) { 
    std::ostringstream pStream;
    for(std::string::size_type i = 0; i < input.size(); ++i) {
        if (i+1 > input.length()) break;
        pStream << input[i] << input[i+1];
        CryLogAlways(pStream.str().c_str());
        if (pStream.str() == "$1" || pStream.str() == "$2" || pStream.str() == "$3" || pStream.str() == "$4" || pStream.str() == "$5" || pStream.str() == "$6" || pStream.str() == "$7" || pStream.str() == "$8" || pStream.str() == "$9" || pStream.str() == "$0")
            width = width+2;
        pStream.clear();
    }
    return std::string((width - input.length()) / 2, ' ') + input;
}

The goal of the above function is to iterate through the string, add the current character and the next to an ostringstream, and evaluate the ostringstream.

This didn't exactly do as I wanted:

<16:58:57> 8I
<16:58:57> 8IIn
<16:58:57> 8IInnc
<16:58:57> 8IInncco
<16:58:57> 8IInnccoom
<16:58:57> 8IInnccoommi
<16:58:57> 8IInnccoommiin
<16:58:57> 8IInnccoommiinng
<16:58:57> 8IInnccoommiinngg 
<16:58:57> 8IInnccoommiinngg  C
<16:58:57> 8IInnccoommiinngg  CCo
<16:58:57> 8IInnccoommiinngg  CCoon
<16:58:57> 8IInnccoommiinngg  CCoonnn
<16:58:57> 8IInnccoommiinngg  CCoonnnne

(snippet from server log)

Here's a brief summary of the issue:

I think I might be missing how iteration works; what am I missing, and how can I make this function work in the way I want it to?


回答1:


So, what you are really trying to do is count the instances of $N in your string, where N is a decimal digit. To do this, just look in the string for instances of $ using std::string::find, and then check the next character to see if it is a digit.

std::string::size_type pos = 0;
while ((pos = input.find('$', pos)) != std::string::npos) {
    if (pos + 1 == input.size()) {
        break;  //  The last character of the string is a '$'
    }
    if (std::isdigit(input[pos + 1])) {
        width += 2;
    }
    ++pos;  //  Start next search from the next char
}

In order to use std::isdigit, you need to first:

#include <cctype>


来源:https://stackoverflow.com/questions/38171813/how-can-i-ignore-certain-strings-in-my-string-centring-function

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