2

当我在 .NET 源代码中寻找以下源代码时,我没有找到源代码PadHelper是里面的方法PadLeftPadRight.

我的搜索有问题吗?

[System.Security.SecuritySafeCritical]  // auto-generated
[ResourceExposure(ResourceScope.None)]
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private extern String PadHelper(int totalWidth, char paddingChar, bool isRightPadded);
4

1 回答 1

5

这似乎不是一个有据可查的事情。我也不知道更多细节以及它是如何工作的,但是看看CodeProject上的这个线程。该方法似乎位于 comstring.cpp 中。

不幸的是,已在此线程中链接的帖子不再可用。这可能是一个有趣的事情。

编辑:在 github 上找到完整的源代码。

/*==================================PadHelper===================================
**Action:
**Returns:
**Arguments:
**Exceptions:
==============================================================================*/
FCIMPL4(Object*, COMString::PadHelper, StringObject* thisRefUNSAFE, INT32 totalWidth, CLR_CHAR paddingChar, CLR_BOOL isRightPadded)
{
    CONTRACTL {
        DISABLED(GC_TRIGGERS);
        THROWS;
        MODE_COOPERATIVE;
        SO_TOLERANT;        
    } CONTRACTL_END;

    STRINGREF refRetVal = NULL;
    STRINGREF thisRef = (STRINGREF) thisRefUNSAFE;
    HELPER_METHOD_FRAME_BEGIN_RET_ATTRIB_1(Frame::FRAME_ATTR_RETURNOBJ, thisRef);
    //-[autocvtpro]-------------------------------------------------------

    WCHAR *thisChars, *padChars;
    INT32 thisLength;


    if (thisRef==NULL) {
        COMPlusThrow(kNullReferenceException, L"NullReference_This");
    }

    RefInterpretGetStringValuesDangerousForGC(thisRef, &thisChars, &thisLength);

    //Don't let them pass in a negative totalWidth
    if (totalWidth<0) {
        COMPlusThrowArgumentOutOfRange(L"totalWidth", L"ArgumentOutOfRange_NeedNonNegNum");
    }

    //If the string is longer than the length which they requested, give them
    //back the old string.
    if (totalWidth<thisLength) {
        refRetVal = thisRef;
        goto lExit;
    }

    if (isRightPadded) {
        refRetVal = NewString(&(thisRef), 0, thisLength, totalWidth);
        padChars = refRetVal->GetBuffer();
        for (int i=thisLength; i<totalWidth; i++) {
            padChars[i] = paddingChar;
        }
        refRetVal->SetStringLength(totalWidth);
        _ASSERTE(padChars[totalWidth] == 0);
    } else {
        refRetVal = NewString(totalWidth);
        INT32 startingPos = totalWidth-thisLength;
        padChars = refRetVal->GetBuffer();
        // Reget thisChars, since if NewString triggers GC, thisChars may become trash.
        RefInterpretGetStringValuesDangerousForGC(thisRef, &thisChars, &thisLength);

            memcpyNoGCRefs(padChars+startingPos, thisChars, thisLength * sizeof(WCHAR));

        for (int i=0; i<startingPos; i++) {
            padChars[i] = paddingChar;
        }
    }

lExit: ;
    //-[autocvtepi]-------------------------------------------------------
    HELPER_METHOD_FRAME_END();
    return OBJECTREFToObject(refRetVal);
}
FCIMPLEND
于 2019-04-09T21:27:09.120 回答