[英]Why does every Char static “Is…” have a string overload, e.g. IsWhiteSpace(string, Int32)?
http://msdn.microsoft.com/en-us/library/1x308yk8.aspx http://msdn.microsoft.com/en-us/library/1x308yk8.aspx
This allows me to do this: 这允许我这样做:
var str = "string ";
Char.IsWhiteSpace(str, 6);
Rather than: 而不是:
Char.IsWhiteSpace(str[6]);
Seems unusual, so I looked at the reflection: 看起来很不寻常,所以我看了一下反思:
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
public static bool IsWhiteSpace(char c)
{
if (char.IsLatin1(c))
{
return char.IsWhiteSpaceLatin1(c);
}
return CharUnicodeInfo.IsWhiteSpace(c);
}
[SecuritySafeCritical]
public static bool IsWhiteSpace(string s, int index)
{
if (s == null)
{
throw new ArgumentNullException("s");
}
if (index >= s.Length)
{
throw new ArgumentOutOfRangeException("index");
}
if (char.IsLatin1(s[index]))
{
return char.IsWhiteSpaceLatin1(s[index]);
}
return CharUnicodeInfo.IsWhiteSpace(s, index);
}
Three things struck me: 三件事让我印象深刻:
ArgumentOutOfRangeException
, while index below 0 would give string's standard IndexOutOfRangeException
ArgumentOutOfRangeException
,而索引低于0将给出字符串的标准IndexOutOfRangeException
SecuritySafeCriticalAttribute
which I've read the general blerb about, but still unclear what it is doing here and if it is linked to the upper bound check. SecuritySafeCriticalAttribute
的前提,我已经阅读了一般的blerb,但仍然不清楚它在这里做了什么以及它是否与上限检查相关联。 TargetedPatchingOptOutAttribute
is not present on other Is...(char)
methods. Is...(char)
方法中不存在TargetedPatchingOptOutAttribute
。 Example IsLetter
, IsNumber
etc. IsLetter
, IsNumber
等 Because not every character fits in a C#
char. 因为不是每个角色都适合
C#
char。 For instance, "𠀀"
takes 2 C# chars
, and you couldn't get any information about that character with just a char
overload. 例如,
"𠀀"
需要2个C# chars
,并且您只能通过char
重载获得有关该字符的任何信息。 With String
and an index, the methods can see if the character at index i
is a High Surrogate char
, and then read the Low Surrogate char
at next index, add them up according to the algorithm , and retrieve info about the code point U+20000
. 使用
String
和索引,方法可以查看索引i
处的字符是否为High Surrogate char
,然后在下一个索引处读取Low Surrogate char
, 根据算法将其添加 ,并检索有关代码点U+20000
This is how UTF-16 can encode 1 million different code points, it's a variable-width encoding. 这就是UTF-16可以编码100万个不同代码点的方式,它是一种可变宽度编码。 It takes 2-4 bytes to encode a character, or 1-2 C# chars.
编码字符需要2-4个字节,或1-2 C#字符。
Why does it bother to do the limit check only on the upper bound?
为什么只在上限进行限制检查呢?
It doesn't. 它没有。 It performs an unsigned comparison, so every negative number will compare larger than the length and cause the appropriate exception to be thrown.
它执行无符号比较,因此每个负数将比长度大,并导致抛出相应的异常。 This happens to not get decompiled accurately.
这恰好不能准确地反编译。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.