数,但小虫子

时间:2015-10-25 17:05:16

标签: c++ token stringtokenizer

我正在做一个单独的字符串,我在看到删除器时添加一个案例,它适用于最后一个删除器。例如,我的字符串是"symbol control_line : std_logic:= '0' ; --example comment"当看到第一个去除者时输出是正确的:但当它看第二个时:=它失败了。我不知道为什么会这样?代码应该适用于两个删除器如何才能找出第一个,但第二个失败了?

这个prepareNextToken函数弄清楚第二个令牌的tokenLength是什么。我可以使用此函数来获取当前令牌。

void Tokenizer::prepareNextToken()
{
        string real=*str;
        if(offset==real.size())
            complete=true;
        else
        {
            if(ifcomment==false)
            {
                size_t length=0;
                size_t index=offset;
                size_t smallest=find_first_delimilater(vhdl_char);
                while(index<real.size() )
                {
                    length++;
                    if(index==smallest && real[index+1]==' ')
                    {
                        cout<<real[smallest]<<" ";
                       break;
                    }
                    else if(index==smallest && real[index+1]!=' ')
                    {
                        length++;
                       break;
                    }
                    else if(index==real.find(' ',offset))
                    {
                        break;
                    }
                    else if(index==real.find("--",offset))
                    {
                        length++;
                       break;
                    }
                    index++;
                }
                tokenLength=length;
            }
            else if(ifcomment==true)
                tokenLength=real.size()-offset;
        }
        //cout<<tokenLength<<endl;
}

我的输出是

    signal            --which is correct
    control_line      --the current offset
    :                 --which is right because I reach the first case in my    
                      --prepareNextToken and ":" is first delimilator
    std_logic:=       --that is the wrong output because it should be std_logic
                      -- and in a separate line comes out ";=" which is another 
                      --delimilator, and is a multiple delimilator no empty case 
                      -- so that means I go to the second cases 
   --                 -- which is also right which go to fourth case
   sample comment    -- which is right

我的问题是为什么当&#34;:&#34;它以自己的方式出现,但为什么&#34;:=&#34;出来它以std_logic结束?

1 个答案:

答案 0 :(得分:1)

substr的第二个参数是要提取的字符数而不是结束位置(请参阅http://www.cplusplus.com/reference/string/string/substr/)。 所以你的提取线应该是:

s=name.substr(offset,tokenLength);