繁体   English   中英

std::codecvt do_out 在 1:N 转换后跳过字符

[英]std::codecvt do_out skipping characters after 1:N conversions

但是,我尝试编写一个自动缩进器 - 它在向流中添加新字符时跳过字符。 我试过调试它并验证 from_next 和 to_next 以及 from 和 to 工作正常。

当然,我错过了规范中的某些内容,但这是我的代码,也许您能帮帮我:

  virtual result_t do_out(state_type& state, const intern_type* from, const intern_type* from_end, const intern_type*& from_next,
     extern_type* to, extern_type* to_end, extern_type*& to_next) const override
  {
    auto result = std::codecvt_base::noconv;

    while (from < from_end && to < to_end)
    {
      if (getState(state).missingWhitespaces > 0u && *from != '\n')
      {
        while (getState(state).missingWhitespaces > 0u && to < to_end)
        {
          *to = ' ';
          to++;
          getState(state).missingWhitespaces--;
        }
        
        if (to < to_end)
        {
          result = std::codecvt_base::partial;
        }
        else
        {
          result = std::codecvt_base::partial;
          break;
        }
      }
      else
      {
        *to = *from;
         
        if (*from == '\n')
        {
          getState(state).missingWhitespaces = tabSize * indentLevel;
        }
        
        to++;
        from++;
      }
    }

    from_next = from;
    to_next = to;
   
    return result;
  };

状态对象也正常工作。 问题只发生在函数调用之间。

编辑:将if (to < to_end)后的结果更改为std::codecvt_base::ok也不能解决问题。

经过更多挖掘,我找到了解决问题的方法。 我从这个网站得到了std::codecvt的详细解释: http std::codecvt

原来,我忘了覆盖这两个方法:

virtual int do_length(state_type& state, const extern_type *from, const extern_type *end, size_t max) const;
确定并返回n ,其中n是源范围[from,end)extern_type的元素数,这些元素可以转换为intern_type max或更少字符,就像通过调用in(state, from, from_end, from_next, to, to_end, to_next)其中to_end == to + max

将 state 的值设置为对应于from + n开始的序列的移位状态。

必须在以下前提条件下调用函数do_length

state要么初始化为序列的开头,要么等于序列上一次转换的结果。

from <= end定义明确且为真。

请注意,此函数的行为与 C 标准库函数mbsrtowcs() 请参阅 mbsrtowcs.cpp 示例程序以了解使用 codecvt 方面的此函数的实现。

virtual int do_max_length() const throw();

返回do_length()可以为其前三个参数的任何有效组合返回的最大值,第四个参数max设置为 1。

我以这种方式实现它们并且它起作用了:

  virtual int do_length(state_type& state, const extern_type* from, const extern_type* end, size_t max) const override
  { 
    auto numberOfCharsAbleToCopy = max;
    
    numberOfCharsAbleToCopy -= std::min(static_cast<unsigned int>(numberOfCharsAbleToCopy), getState(state).missingWhitespaces);
    
    bool newLineToAppend = false;
    for (auto c = from + getState(state).missingWhitespaces; c < end && numberOfCharsAbleToCopy > 0u; c++)
    {
      if (*c == '\n' && !newLineToAppend)
      {
        newLineToAppend = true;
      }
      else if (*c != '\n' && newLineToAppend)
      {
        numberOfCharsAbleToCopy -= std::min(tabSize * indentLevel, numberOfCharsAbleToCopy);
        
        if (numberOfCharsAbleToCopy == 0u)
        {
          break;
        }
        
        newLineToAppend = false;
      }
    }
    
    return numberOfCharsAbleToCopy;
  }
  
  virtual int do_max_length() const throw() override
  {
    return tabSize * indentLevel;
  }
  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM