简体   繁体   English

为什么每个Char静态“Is ...”都有一个字符串重载,例如IsWhiteSpace(string,Int32)?

[英]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: 三件事让我印象深刻:

  1. Why does it bother to do the limit check only on the upper bound? 为什么只在上限进行限制检查呢? Throwing an ArgumentOutOfRangeException , while index below 0 would give string's standard IndexOutOfRangeException 抛出ArgumentOutOfRangeException ,而索引低于0将给出字符串的标准IndexOutOfRangeException
  2. The precense of 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,但仍然不清楚它在这里做了什么以及它是否与上限检查相关联。
  3. TargetedPatchingOptOutAttribute is not present on other Is...(char) methods. 其他Is...(char)方法中不存在TargetedPatchingOptOutAttribute Example IsLetter , IsNumber etc. 示例IsLetterIsNumber

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.

相关问题 将4个字符串转换为int32 - Convert a 4 char string into int32 是否可以查找给定类型的所有名称别名(例如,“ int”与“ Int32”)? - Is it possible to look up all name alias of a given type (e.g. “int” versus “Int32”)? 为什么(string)int32总是抛出:无法将类型'int'转换为'string' - Why does (string)int32 always throw: Cannot convert type 'int' to 'string' 当值相同但类型不同时比较两个变量(例如Int16,Int32,Int64) - Comparing two variables when the value is the same but the type is different (e.g. Int16, Int32, Int64) C# - 是否可以从字符串中解析Type - 例如(伪代码)类型t = Type.Parse(“Int32”); - C# - Is it possible to parse a Type from a string - e.g (Pseudo code) Type t = Type.Parse(“Int32”); LINQ to Entities无法识别方法'Int32 Parse(System.String)'为什么? - LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' why? 为什么将字符串转换为Int32在网站中随机失败 - Why Convert String to Int32 fails randomly in Website 为什么System.String.EndsWith()有一个char重载而System.String.StartsWith()没有? - Why does System.String.EndsWith() have a a char overload and System.String.StartsWith() does not? string.Compare(String, Int32, String, Int32, Int32) 在 .NET 5.0 中未正确比较泰国文化 - string.Compare(String, Int32, String, Int32, Int32) is not compared correctly for Thai Culture in .NET 5.0 无法将字符串转换为int32 LINQ - Cannot convert string to int32 LINQ
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM