在C#中实现子字符串以及它使用哪种算法?

Mar*_*vds 3 c# java algorithm performance substring

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

像C#这样的问题使用蛮力的方式,或者像一个好孩子一样实施Boyer-Moore的算法.我需要这个源代码,我已经找到了它用于Java(谁在indexOf()方法中使用暴力实现为那些想知道的人).

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

你的帮助将非常有用!

Fra*_*ank 6

Microsoft已发布完整的框架源代码,包括注释.您将在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; 
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,他们正在使用wstrcpy,这可能和它一样快.