【问题标题】:Last rune of golang unicode/norm iterator not being read未读取 golang unicode/norm 迭代器的最后一个符文
【发布时间】:2015-09-22 23:51:26
【问题描述】:

我正在使用golang.org/x/text/unicode/norm 包来迭代[]byte 中的符文。我选择了这种方法,因为我需要检查每个符文并维护有关符文序列的信息。最后一次调用iter.Next() 不会读取最后一个符文。它在最后一个符文上读取 0 个字节。

代码如下:

package main

import (
  "fmt"
  "unicode/utf8"

  "golang.org/x/text/unicode/norm"
)

func main() {
  var (
    n   int
    r   rune
    it  norm.Iter
    out []byte
  )
  in := []byte(`test`)
  fmt.Printf("%s\n", in)
  fmt.Println(in)
  it.Init(norm.NFD, in)
  for !it.Done() {
    ruf := it.Next()
    r, n = utf8.DecodeRune(ruf)
    fmt.Printf("bytes read: %d. val: %q\n", n, r)
    buf := make([]byte, utf8.RuneLen(r))
    utf8.EncodeRune(buf, r)
    out = norm.NFC.Append(out, buf...)
  }
  fmt.Printf("%s\n", out)
  fmt.Println(out)
}

这会产生以下输出:

test
[116 101 115 116]
bytes read: 1. val: 't'
bytes read: 1. val: 'e'
bytes read: 1. val: 's'
bytes read: 0. val: '�'
tes�
[116 101 115 239 191 189]

【问题讨论】:

    标签: unicode go normalization unicode-normalization


    【解决方案1】:

    这可能是golang.org/x/text/unicode/norm 及其Init() 函数中的错误。

    在我看到的包的测试和示例中都使用了InitString。因此,作为一种解决方法,如果您更改:

     it.Init(norm.NFD, in)
    

    到:

     it.InitString(norm.NFD, `test`)
    

    一切都会按预期进行。

    我建议打开一个错误报告,但请注意,由于它位于“/x”目录中,因此 go 开发人员认为该包是实验性的。

    (顺便说一句,我使用go debugger 来帮助我追踪正在发生的事情,但我应该说它的使用是我希望看到的那种调试器。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-30
      • 2012-11-09
      • 2013-02-20
      • 2013-12-04
      • 1970-01-01
      • 2016-04-10
      • 2011-02-10
      • 2018-12-23
      相关资源
      最近更新 更多