unicode - 确定 Go 中的空格

标签 unicode go whitespace

来自documentation of Go's unicode package :

func IsSpace

func IsSpace(r rune) bool

IsSpace reports whether the rune is a space character as defined by Unicode's White Space property; in the Latin-1 space this is

'\t', '\n', '\v', '\f', '\r', ' ', U+0085 (NEL), U+00A0 (NBSP).

Other definitions of spacing characters are set by category Z and property Pattern_White_Space.

我的问题是:“其他定义”由 Z 类别和 Pattern_White_Space 设置是什么意思?这是否意味着调用 unicode.IsSpace()、检查字符是否在 Z 类别中以及检查字符是否在 Pattern_White_Space 中都会产生不同的结果结果? 如果是这样,有什么区别?为什么会有差异?

最佳答案

IsSpace 函数将首先检查您的 rune 是否在 Latin1 字符空间中。如果是,它将使用您列出的空格字符来确定空格。

如果不是,则调用 isExcludingLatin ( http://golang.org/src/unicode/letter.go?h=isExcludingLatin#L170 ),如下所示:

   170  func isExcludingLatin(rangeTab *RangeTable, r rune) bool {
   171      r16 := rangeTab.R16
   172      if off := rangeTab.LatinOffset; len(r16) > off && r <= rune(r16[len(r16)-1].Hi) {
   173          return is16(r16[off:], uint16(r))
   174      }
   175      r32 := rangeTab.R32
   176      if len(r32) > 0 && r >= rune(r32[0].Lo) {
   177          return is32(r32, uint32(r))
   178      }
   179      return false
   180  }

传入的*RangeTableWhite_Space,它看起来是在这里定义的:

http://golang.org/src/unicode/tables.go?h=White_Space#L6069

  6069  var _White_Space = &RangeTable{
  6070      R16: []Range16{
  6071          {0x0009, 0x000d, 1},
  6072          {0x0020, 0x0020, 1},
  6073          {0x0085, 0x0085, 1},
  6074          {0x00a0, 0x00a0, 1},
  6075          {0x1680, 0x1680, 1},
  6076          {0x2000, 0x200a, 1},
  6077          {0x2028, 0x2029, 1},
  6078          {0x202f, 0x202f, 1},
  6079          {0x205f, 0x205f, 1},
  6080          {0x3000, 0x3000, 1},
  6081      },
  6082      LatinOffset: 4,
  6083  }

要回答您的主要问题,IsSpace 检查不限于 Latin-1。

编辑
为澄清起见,如果您正在测试的字符不在 Latin-1 字符集中,则使用范围表查找。表中的 Range16 值表示 16 位数字的范围 {Low, Hi, Stride}。 isExcludingLatin 将使用该范围表子部分 (R16) 调用 is16 并确定提供的 rune 是否落入在 LatinOffset 索引之后的任何范围内(在本例中为 4)。

所以,这就是检查这些范围:

 {0x1680, 0x1680, 1},
 {0x2000, 0x200a, 1},
 {0x2028, 0x2029, 1},
 {0x202f, 0x202f, 1},
 {0x205f, 0x205f, 1},
 {0x3000, 0x3000, 1},

Unicode 代码点用于:

http://www.fileformat.info/info/unicode/char/1680/index.htm http://www.fileformat.info/info/unicode/char/2000/index.htm http://www.fileformat.info/info/unicode/char/2001/index.htm http://www.fileformat.info/info/unicode/char/2002/index.htm http://www.fileformat.info/info/unicode/char/2003/index.htm http://www.fileformat.info/info/unicode/char/2004/index.htm http://www.fileformat.info/info/unicode/char/2005/index.htm http://www.fileformat.info/info/unicode/char/2006/index.htm http://www.fileformat.info/info/unicode/char/2007/index.htm http://www.fileformat.info/info/unicode/char/2008/index.htm http://www.fileformat.info/info/unicode/char/2009/index.htm http://www.fileformat.info/info/unicode/char/200a/index.htm http://www.fileformat.info/info/unicode/char/2028/index.htm http://www.fileformat.info/info/unicode/char/2029/index.htm http://www.fileformat.info/info/unicode/char/202f/index.htm http://www.fileformat.info/info/unicode/char/205f/index.htm http://www.fileformat.info/info/unicode/char/3000/index.htm

以上所有都被认为是“空白”

关于unicode - 确定 Go 中的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29038314/

相关文章:

c++ - 静态库 LNK2019 中的 _tWinMain

python - Tornado set_secure_cookie unicode 错误

json - 我如何在 Go Lang 中使用多个元素对 JSON 进行编码

JavaScript 会忽略多余的空格吗?

html - 无法解释的空格?

c++ - 如何更改控制台字体?

java - NFC 规范化语义在 Java 6 和 7 之间是否发生了变化?

php - 如何用 Golang 解码 PHP 序列化的 Redis 响应

go - 包装 io.PipeReader 来存储进度

c - 已经使用另一个定界符时在 C 中定界空白(一次)的简单方法