确定 Go 中的空格

Mat*_*att 11 unicode whitespace go

来自Gounicode的文档:

函数 IsSpace

func IsSpace(r rune) bool

IsSpace 报告符文是否是由 Unicode 的 White Space 属性定义的空格字符;在 Latin-1 空间中,这是

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

间距字符的其他定义由类别 Z 和属性 Pattern_White_Space 设置。

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

sbe*_*rry 5

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

如果没有,则调用isExcludingLatinhttp://golang.org/src/unicode/letter.go?h=isExclusionLatin#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  }
Run Code Online (Sandbox Code Playgroud)

*RangeTable传入的外观是在White_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  }
Run Code Online (Sandbox Code Playgroud)

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

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

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

 {0x1680, 0x1680, 1},
 {0x2000, 0x200a, 1},
 {0x2028, 0x2029, 1},
 {0x202f, 0x202f, 1},
 {0x205f, 0x205f, 1},
 {0x3000, 0x3000, 1},
Run Code Online (Sandbox Code Playgroud)

有以下 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/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/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/2029/index.htm http://www.fileformat.info/info/unicode/char/2029/index.htm fileformat.info/info/unicode/char/202f/index.htm http://www.fileformat.info/info/unicode/char/205f/index.htm http://www.fileformat.info/info/unicode/字符/3000/index.htm

以上所有内容均被视为“空白”

  • 那么调用“unicode.IsSpace()”、检查字符是否在 Z 类别中以及检查字符是否在“Pattern_White_Space”中之间的白盒区别是什么?换句话说,哪些字符被其中一种方法视为空格,而另一种则不被视为空格? (2认同)