c++ - 计数但小错误

标签 c++ token stringtokenizer

我正在做单独的一个字符串,当看到分隔符时我确实添加了一个案例,它对最后一个分隔符工作正常。例如我的字符串是 "symbol control_line : std_logic:= '0' ; --example comment" 当看到第一个定界符时输出是正确的:但是当它看第二个时 := 它失败了。我不知道为什么会这样?代码应该适用于两个定界器,为什么只找出第一个,但第二个却失败了?

这个 prepareNextToken 函数是计算出第二个 Token 的 tokenLength 是多少。我可以使用这个函数来获取当前 token 。

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

我的问题是为什么当“:”出现在它自己的行中,但为什么“:=”出现时它以 std_logic 结尾?

最佳答案

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

s=name.substr(offset,tokenLength);

关于c++ - 计数但小错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33332448/

相关文章:

c++ - 要加倍或 float 的字符串会截去小数点

c++ - 将硅网络服务器与 libmicrohttpd 后端链接

c++ - 将 C++ 方法参数转换为模板参数失败并出现编译错误

php - 简单的类似 token 的身份验证

c# - 如何在 C# 中从 USB token 读取证书

java - 如何从 Lucene TokenStream 中获取 Token?

c++ - key = pair 的多重映射

java - 如何使用java中的字符串分词器从字符串中逐个获取字符

java - 在 Java 中使用 StringTokenizer 分割路径 平台无关

java - 用于创建输入文件标记的扫描器类