【问题标题】:Implementation of substring in C# and which algorithm does it use?C#中子字符串的实现以及它使用哪种算法?
【发布时间】:2015-01-07 15:55:41
【问题描述】:

我有一个问题,我正在研究一些编程语言。 该研究是关于 C# 和 Java 中子字符串函数的效率。

诸如 C# 之类的问题是使用蛮力的方式,还是他们像一个好孩子一样实现了 Boyer-Moore 的算法。 我需要它的源代码,我已经为 Java 找到了它(谁在 indexOf() 方法中为那些想知道的人使用了蛮力实现)。

有谁知道如何在 C# 中检索这些方法的源代码。 我的笔记本电脑上安装了视觉工作室,但我找不到任何源代码......

非常感谢您的帮助!

【问题讨论】:

    标签: java c# performance algorithm substring


    【解决方案1】:

    Microsoft 已发布完整的框架源代码,包括 cmets。你会发现实际的实现over here on referencesource。对于SubString,它归结为一些非托管代码:

        [System.Security.SecurityCritical]  // auto-generated
        unsafe string InternalSubString(int startIndex, int length) {
            Contract.Assert( startIndex >= 0 && startIndex <= this.Length, "StartIndex is out of range!");
            Contract.Assert( length >= 0 && startIndex <= this.Length - length, "length is out of range!");            
    
            String result = FastAllocateString(length);
    
            fixed(char* dest = &result.m_firstChar)
                fixed(char* src = &this.m_firstChar) {
                    wstrcpy(dest, src + startIndex, length);
                }
    
            return result; 
    

    如您所见,他们正在使用 wstrcpy,这可能是最快的。

    【讨论】:

    猜你喜欢
    • 2015-08-05
    • 2018-04-02
    • 2018-09-17
    • 2014-03-02
    • 2016-09-26
    • 1970-01-01
    • 2020-04-30
    • 1970-01-01
    • 2018-06-22
    相关资源
    最近更新 更多