gpt4 book ai didi

unicode - 确定 Go 中的空格

转载 作者:IT王子 更新时间:2023-10-29 01:42:47 24 4
gpt4 key购买 nike

来自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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com