From f9116ac9567bfc15d8fc07413b50f40df3193d7c Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Mon, 18 Sep 2017 14:22:19 -0400 Subject: Remove now dead ParseNumbers code in runtime --- src/vm/comutilnative.cpp | 645 ----------------------------------------------- 1 file changed, 645 deletions(-) (limited to 'src/vm/comutilnative.cpp') diff --git a/src/vm/comutilnative.cpp b/src/vm/comutilnative.cpp index 766336ee7f..5c5a1e9eaa 100644 --- a/src/vm/comutilnative.cpp +++ b/src/vm/comutilnative.cpp @@ -44,20 +44,6 @@ #define STACK_OVERFLOW_MESSAGE W("StackOverflowException") -//These are defined in System.ParseNumbers and should be kept in sync. -#define PARSE_TREATASUNSIGNED 0x200 -#define PARSE_TREATASI1 0x400 -#define PARSE_TREATASI2 0x800 -#define PARSE_ISTIGHT 0x1000 -#define PARSE_NOSPACE 0x2000 - - -// -// -// PARSENUMBERS (and helper functions) -// -// - /*===================================IsDigit==================================== **Returns a bool indicating whether the character passed in represents a ** **digit. @@ -119,638 +105,7 @@ INT32 wtoi(__in_ecount(length) WCHAR* wstr, DWORD length) return result; } -INT32 ParseNumbers::GrabInts(const INT32 radix, __in_ecount(length) WCHAR *buffer, const int length, int *i, BOOL isUnsigned) -{ - CONTRACTL - { - THROWS; - GC_NOTRIGGER; - MODE_COOPERATIVE; - PRECONDITION(CheckPointer(buffer)); - PRECONDITION(CheckPointer(i)); - PRECONDITION(*i >= 0); - PRECONDITION(length >= 0); - PRECONDITION( radix==2 || radix==8 || radix==10 || radix==16 ); - } - CONTRACTL_END; - - UINT32 result=0; - int value; - UINT32 maxVal; - - // Allow all non-decimal numbers to set the sign bit. - if (radix==10 && !isUnsigned) { - maxVal = (0x7FFFFFFF / 10); - - //Read all of the digits and convert to a number - while (*i maxVal || ((INT32)result)<0) - COMPlusThrow(kOverflowException, W("Overflow_Int32")); - result = result*radix + value; - (*i)++; - } - if ((INT32)result<0 && result!=0x80000000) - COMPlusThrow(kOverflowException, W("Overflow_Int32")); - - } - else { - maxVal = ((UINT32) -1) / radix; - - //Read all of the digits and convert to a number - while (*i maxVal) - COMPlusThrow(kOverflowException, W("Overflow_UInt32")); - // the above check won't cover 4294967296 to 4294967299 - UINT32 temp = result*radix + value; - if( temp < result) { // this means overflow as well - COMPlusThrow(kOverflowException, W("Overflow_UInt32")); - } - - result = temp; - (*i)++; - } - } - return(INT32) result; -} - -INT64 ParseNumbers::GrabLongs(const INT32 radix, __in_ecount(length) WCHAR *buffer, const int length, int *i, BOOL isUnsigned) -{ - CONTRACTL - { - THROWS; - GC_NOTRIGGER; - MODE_COOPERATIVE; - PRECONDITION(CheckPointer(buffer)); - PRECONDITION(CheckPointer(i)); - PRECONDITION(*i >= 0); - PRECONDITION(length >= 0); - } - CONTRACTL_END; - - UINT64 result=0; - int value; - UINT64 maxVal; - - // Allow all non-decimal numbers to set the sign bit. - if (radix==10 && !isUnsigned) { - maxVal = (UI64(0x7FFFFFFFFFFFFFFF) / 10); - - //Read all of the digits and convert to a number - while (*i maxVal || ((INT64)result)<0) - COMPlusThrow(kOverflowException, W("Overflow_Int64")); - result = result*radix + value; - (*i)++; - } - if ((INT64)result<0 && result!=UI64(0x8000000000000000)) - COMPlusThrow(kOverflowException, W("Overflow_Int64")); - - } - else { - maxVal = ((UINT64) -1L) / radix; - - //Read all of the digits and convert to a number - while (*i maxVal) - COMPlusThrow(kOverflowException, W("Overflow_UInt64")); - - UINT64 temp = result*radix + value; - if( temp < result) { // this means overflow as well - COMPlusThrow(kOverflowException, W("Overflow_UInt64")); - } - result = temp; - - (*i)++; - } - } - return(INT64) result; -} - -void EatWhiteSpace(__in_ecount(length) WCHAR *buffer, int length, int *i) -{ - CONTRACTL - { - NOTHROW; - GC_NOTRIGGER; - MODE_ANY; - PRECONDITION(CheckPointer(buffer)); - PRECONDITION(CheckPointer(i)); - PRECONDITION(length >= 0); - } - CONTRACTL_END; - - for (; *iMaxRadix) - COMPlusThrowArgumentException(W("radix"), W("Arg_InvalidBase")); - - //If the number is negative, make it positive and remember the sign. - if (n<0) { - isNegative=true; - - // For base 10, write out -num, but other bases write out the - // 2's complement bit pattern - if (10==radix) - l = (UINT64)(-n); - else - l = (UINT64)n; - } - else { - l=(UINT64)n; - } - - if (flags&PrintAsI1) - l = l&0xFF; - else if (flags&PrintAsI2) - l = l&0xFFFF; - else if (flags&PrintAsI4) - l=l&0xFFFFFFFF; - - //Special case the 0. - if (0==l) { - buffer[0]='0'; - index=1; - } - else { - //Pull apart the number and put the digits (in reverse order) into the buffer. - for (index=0; l>0; l=l/radix, index++) { - if ((charVal=(int)(l%radix))<10) - buffer[index] = (WCHAR)(charVal + '0'); - else - buffer[index] = (WCHAR)(charVal + 'a' - 10); - } - } - - //If they want the base, append that to the string (in reverse order) - if (radix!=10 && ((flags&PrintBase)!=0)) { - if (16==radix) { - buffer[index++]='x'; - buffer[index++]='0'; - } - else if (8==radix) { - buffer[index++]='0'; - } - else if ((flags&PrintRadixBase)!=0) { - buffer[index++]='#'; - buffer[index++]=((radix%10)+'0'); - buffer[index++]=((static_cast(radix)/10)+'0'); - } - } - - if (10==radix) { - //If it was negative, append the sign. - if (isNegative) { - buffer[index++]='-'; - } - - //else if they requested, add the '+'; - else if ((flags&PrintSign)!=0) { - buffer[index++]='+'; - } - - //If they requested a leading space, put it on. - else if ((flags&PrefixSpace)!=0) { - buffer[index++]=' '; - } - } - - //Figure out the size of our string. - if (width<=index) - buffLength=index; - else - buffLength=width; - - STRINGREF Local = StringObject::NewString(buffLength); - WCHAR *LocalBuffer = Local->GetBuffer(); - - //Put the characters into the String in reverse order - //Fill the remaining space -- if there is any -- - //with the correct padding character. - if ((flags&LeftAlign)!=0) { - for (i=0; i=0; j--) { - LocalBuffer[j]=paddingChar; - } - } - - *((STRINGREF *)&rv)=Local; - - HELPER_METHOD_FRAME_END(); - - return rv; -} -FCIMPLEND - - -FCIMPL5(LPVOID, ParseNumbers::IntToString, INT32 n, INT32 radix, INT32 width, CLR_CHAR paddingChar, INT32 flags) -{ - FCALL_CONTRACT; - - LPVOID rv = NULL; - - HELPER_METHOD_FRAME_BEGIN_RET_0(); - - bool isNegative = false; - int index=0; - int charVal; - int buffLength; - int i; - UINT32 l; - WCHAR buffer[66]; //Longest possible string length for an integer in binary notation with prefix - - if (radixMaxRadix) - COMPlusThrowArgumentException(W("radix"), W("Arg_InvalidBase")); - - //If the number is negative, make it positive and remember the sign. - //If the number is MIN_VALUE, this will still be negative, so we'll have to - //special case this later. - if (n<0) { - isNegative=true; - // For base 10, write out -num, but other bases write out the - // 2's complement bit pattern - if (10==radix) - l = (UINT32)(-n); - else - l = (UINT32)n; - } - else { - l=(UINT32)n; - } - - //The conversion to a UINT will sign extend the number. In order to ensure - //that we only get as many bits as we expect, we chop the number. - if (flags&PrintAsI1) { - l = l&0xFF; - } - else if (flags&PrintAsI2) { - l = l&0xFFFF; - } - else if (flags&PrintAsI4) { - l=l&0xFFFFFFFF; - } - - //Special case the 0. - if (0==l) { - buffer[0]='0'; - index=1; - } - else { - do { - charVal = l%radix; - l=l/radix; - if (charVal<10) { - buffer[index++] = (WCHAR)(charVal + '0'); - } - else { - buffer[index++] = (WCHAR)(charVal + 'a' - 10); - } - } - while (l!=0); - } - - //If they want the base, append that to the string (in reverse order) - if (radix!=10 && ((flags&PrintBase)!=0)) { - if (16==radix) { - buffer[index++]='x'; - buffer[index++]='0'; - } - else if (8==radix) { - buffer[index++]='0'; - } - } - - if (10==radix) { - //If it was negative, append the sign. - if (isNegative) { - buffer[index++]='-'; - } - - //else if they requested, add the '+'; - else if ((flags&PrintSign)!=0) { - buffer[index++]='+'; - } - - //If they requested a leading space, put it on. - else if ((flags&PrefixSpace)!=0) { - buffer[index++]=' '; - } - } - - //Figure out the size of our string. - if (width<=index) { - buffLength=index; - } - else { - buffLength=width; - } - - STRINGREF Local = StringObject::NewString(buffLength); - WCHAR *LocalBuffer = Local->GetBuffer(); - - //Put the characters into the String in reverse order - //Fill the remaining space -- if there is any -- - //with the correct padding character. - if ((flags&LeftAlign)!=0) { - for (i=0; i=0; j--) { - LocalBuffer[j]=paddingChar; - } - } - - *((STRINGREF *)&rv)=Local; - - HELPER_METHOD_FRAME_END(); - - return rv; -} -FCIMPLEND - - -/*===================================FixRadix=================================== -**It's possible that we parsed the radix in a base other than 10 by accident. -**This method will take that number, verify that it only contained valid base 10 -**digits, and then do the conversion to base 10. If it contained invalid digits, -**they tried to pass us a radix such as 1A, so we throw a FormatException. -** -**Args: oldVal: The value that we had actually parsed in some arbitrary base. -** oldBase: The base in which we actually did the parsing. -** -**Returns: oldVal as if it had been parsed as a base-10 number. -**Exceptions: FormatException if either of the digits in the radix aren't -** valid base-10 numbers. -==============================================================================*/ -int FixRadix(int oldVal, int oldBase) -{ - CONTRACTL - { - THROWS; - GC_NOTRIGGER; - MODE_COOPERATIVE; - } - CONTRACTL_END; - - int firstDigit = (oldVal/oldBase); - int secondDigit = (oldVal%oldBase); - - if ((firstDigit>=10) || (secondDigit>=10)) - COMPlusThrow(kFormatException, W("Format_BadBase")); - - return(firstDigit*10)+secondDigit; -} - -/*=================================StringToLong================================= -**Action: -**Returns: -**Exceptions: -==============================================================================*/ -FCIMPL4(INT64, ParseNumbers::StringToLong, StringObject * s, INT32 radix, INT32 flags, INT32 *currPos) -{ - FCALL_CONTRACT; - - INT64 result = 0; - - HELPER_METHOD_FRAME_BEGIN_RET_1(s); - - int sign = 1; - WCHAR *input; - int length; - int i; - int grabNumbersStart=0; - INT32 r; - - _ASSERTE((flags & PARSE_TREATASI1) == 0 && (flags & PARSE_TREATASI2) == 0); - - if (s) { - i = currPos ? *currPos : 0; - - //Do some radix checking. - //A radix of -1 says to use whatever base is spec'd on the number. - //Parse in Base10 until we figure out what the base actually is. - r = (-1==radix)?10:radix; - - if (r!=2 && r!=10 && r!=8 && r!=16) - COMPlusThrow(kArgumentException, W("Arg_InvalidBase")); - - s->RefInterpretGetStringValuesDangerousForGC(&input, &length); - - if (i<0 || i>=length) - COMPlusThrowArgumentOutOfRange(W("startIndex"), W("ArgumentOutOfRange_Index")); - - //Get rid of the whitespace and then check that we've still got some digits to parse. - if (!(flags & PARSE_ISTIGHT) && !(flags & PARSE_NOSPACE)) { - EatWhiteSpace(input,length,&i); - if (i==length) - COMPlusThrow(kFormatException, W("Format_EmptyInputString")); - } - - //Check for a sign - if (input[i]=='-') { - if (r != 10) - COMPlusThrow(kArgumentException, W("Arg_CannotHaveNegativeValue")); - - if (flags & PARSE_TREATASUNSIGNED) - COMPlusThrow(kOverflowException, W("Overflow_NegativeUnsigned")); - - sign = -1; - i++; - } - else if (input[i]=='+') { - i++; - } - - if ((radix==-1 || radix==16) && (i+1RefInterpretGetStringValuesDangerousForGC(&input, &length); - - if (i<0 || i>=length) - COMPlusThrowArgumentOutOfRange(W("startIndex"), W("ArgumentOutOfRange_Index")); - - //Get rid of the whitespace and then check that we've still got some digits to parse. - if (!(flags & PARSE_ISTIGHT) && !(flags & PARSE_NOSPACE)) { - EatWhiteSpace(input,length,&i); - if (i==length) - COMPlusThrow(kFormatException, W("Format_EmptyInputString")); - } - - //Check for a sign - if (input[i]=='-') { - if (r != 10) - COMPlusThrow(kArgumentException, W("Arg_CannotHaveNegativeValue")); - - if (flags & PARSE_TREATASUNSIGNED) - COMPlusThrow(kOverflowException, W("Overflow_NegativeUnsigned")); - - sign = -1; - i++; - } - else if (input[i]=='+') { - i++; - } - - //Consume the 0x if we're in an unknown base or in base-16. - if ((radix==-1||radix==16) && (i+1 0xFF) - COMPlusThrow(kOverflowException, W("Overflow_SByte")); - - // result looks positive when parsed as an I4 - _ASSERTE(sign==1 || r==10); - } - else if (flags & PARSE_TREATASI2) { - if ((UINT32)result > 0xFFFF) - COMPlusThrow(kOverflowException, W("Overflow_Int16")); - - // result looks positive when parsed as an I4 - _ASSERTE(sign==1 || r==10); - } - else if ((UINT32) result==0x80000000U && sign==1 && r==10 && !(flags & PARSE_TREATASUNSIGNED)) { - COMPlusThrow(kOverflowException, W("Overflow_Int32")); - } - - if (r == 10) - result *= sign; - } - else { - result = 0; - } - - HELPER_METHOD_FRAME_END(); - - return result; -} -FCIMPLEND // // -- cgit v1.2.3