StringToInt() public static method

public static StringToInt ( string value, int fromBase, int flags ) : int
value string
fromBase int
flags int
return int
Beispiel #1
0
 public unsafe static int StringToInt(string s, int radix, int flags, ref int currPos)
 {
     fixed(int *ptr = &currPos)
     {
         return(ParseNumbers.StringToInt(s, radix, flags, ptr));
     }
 }
Beispiel #2
0
        private static int TryParse(string str, ref int parsePos, int requiredLength)
        {
            int num  = parsePos;
            int num2 = ParseNumbers.StringToInt(str, 0x10, 0x2000, ref parsePos);

            if ((parsePos - num) != requiredLength)
            {
                throw new FormatException(Environment.GetResourceString("Format_GuidInvalidChar"));
            }
            return(num2);
        }
Beispiel #3
0
        private static int TryParse(String str, ref int parsePos, int requiredLength)
        {
            int currStart = parsePos;
            // the exception message from ParseNumbers is better
            int retVal = ParseNumbers.StringToInt(str, 16, ParseNumbers.NoSpace, ref parsePos);

            //If we didn't parse enough characters, there's clearly an error.
            if (parsePos - currStart != requiredLength)
            {
                throw new FormatException(Environment.GetResourceString("Format_GuidInvalidChar"));
            }
            return(retVal);
        }
Beispiel #4
0
        private static bool StringToInt(String str, ref int parsePos, int requiredLength, int flags, out int result, ref GuidResult parseResult)
        {
            result = 0;

            int currStart = parsePos;

            try
            {
                result = ParseNumbers.StringToInt(str, 16, flags, ref parsePos);
            }
            catch (OverflowException ex)
            {
                if (parseResult._throwStyle == GuidParseThrowStyle.All)
                {
                    throw;
                }
                else if (parseResult._throwStyle == GuidParseThrowStyle.AllButOverflow)
                {
                    throw new FormatException(SR.Format_GuidUnrecognized, ex);
                }
                else
                {
                    parseResult.SetFailure(ex);
                    return(false);
                }
            }
            catch (Exception ex)
            {
                if (parseResult._throwStyle == GuidParseThrowStyle.None)
                {
                    parseResult.SetFailure(ex);
                    return(false);
                }
                else
                {
                    throw;
                }
            }

            //If we didn't parse enough characters, there's clearly an error.
            if (requiredLength != -1 && parsePos - currStart != requiredLength)
            {
                parseResult.SetFailure(ParseFailureKind.Format, nameof(SR.Format_GuidInvalidChar));
                return(false);
            }
            return(true);
        }
Beispiel #5
0
        private static int TryParse(String str, int [] parsePos, int requiredLength)
        {
            int currStart = parsePos[0];
            int retVal;

            try {
                retVal = ParseNumbers.StringToInt(str, 16, 0, parsePos);
            }
            catch (FormatException) {
                throw new FormatException("Format_GuidUnrecognized");
            }

            //If we didn't parse enough characters, there's clearly an error.
            if (parsePos[0] - currStart != requiredLength)
            {
                throw new FormatException("Format_GuidUnrecognized");
            }
            return(retVal);
        }
Beispiel #6
0
        private static unsafe bool StringToInt(string str, int *parsePos, int requiredLength, int flags, out int result, ref GuidResult parseResult)
        {
            result = 0;
            int num = (parsePos == null) ? 0 : parsePos[0];

            try
            {
                result = ParseNumbers.StringToInt(str, 0x10, flags, parsePos);
            }
            catch (OverflowException exception)
            {
                if (parseResult.throwStyle == GuidParseThrowStyle.All)
                {
                    throw;
                }
                if (parseResult.throwStyle == GuidParseThrowStyle.AllButOverflow)
                {
                    throw new FormatException(Environment.GetResourceString("Format_GuidUnrecognized"), exception);
                }
                parseResult.SetFailure(exception);
                return(false);
            }
            catch (Exception exception2)
            {
                if (parseResult.throwStyle != GuidParseThrowStyle.None)
                {
                    throw;
                }
                parseResult.SetFailure(exception2);
                return(false);
            }
            if (((requiredLength != -1) && (parsePos != null)) && ((parsePos[0] - num) != requiredLength))
            {
                parseResult.SetFailure(ParseFailureKind.Format, "Format_GuidInvalidChar");
                return(false);
            }
            return(true);
        }
Beispiel #7
0
        // Creates a new guid based on the value in the string.  The value is made up
        // of hex digits speared by the dash ("-"). The string may begin and end with
        // brackets ("{", "}").
        //
        // The string must be of the form dddddddd-dddd-dddd-dddd-dddddddddddd. where
        // d is a hex digit. (That is 8 hex digits, followed by 4, then 4, then 4,
        // then 12) such as: "CA761232-ED42-11CE-BACD-00AA0057B223"
        //
        public Guid(String g)
        {
            if (g == null)
            {
                throw new ArgumentNullException("g");
            }

            int  startPos = 0;
            int  temp;
            long templ;
            int  currentPos = 0;

            try
            {
                // Check if it's of the form dddddddd-dddd-dddd-dddd-dddddddddddd
                if (g.IndexOf('-', 0) >= 0)
                {
                    String guidString = g.Trim();  //Remove Whitespace

                    // check to see that it's the proper length
                    if (guidString[0] == '{')
                    {
                        if (guidString.Length != 38 || guidString[37] != '}')
                        {
                            throw new FormatException(Environment.GetResourceString("Format_GuidInvLen"));
                        }
                        startPos = 1;
                    }
                    else if (guidString[0] == '(')
                    {
                        if (guidString.Length != 38 || guidString[37] != ')')
                        {
                            throw new FormatException(Environment.GetResourceString("Format_GuidInvLen"));
                        }
                        startPos = 1;
                    }
                    else if (guidString.Length != 36)
                    {
                        throw new FormatException(Environment.GetResourceString("Format_GuidInvLen"));
                    }
                    if (guidString[8 + startPos] != '-' ||
                        guidString[13 + startPos] != '-' ||
                        guidString[18 + startPos] != '-' ||
                        guidString[23 + startPos] != '-')
                    {
                        throw new FormatException(Environment.GetResourceString("Format_GuidDashes"));
                    }

                    currentPos = startPos;
                    _a         = TryParse(guidString, ref currentPos, 8);
                    ++currentPos; //Increment past the '-';
                    _b = (short)TryParse(guidString, ref currentPos, 4);
                    ++currentPos; //Increment past the '-';
                    _c = (short)TryParse(guidString, ref currentPos, 4);
                    ++currentPos; //Increment past the '-';
                    temp = TryParse(guidString, ref currentPos, 4);
                    ++currentPos; //Increment past the '-';
                    startPos = currentPos;
                    templ    = ParseNumbers.StringToLong(guidString, 16, ParseNumbers.NoSpace, ref currentPos);
                    if (currentPos - startPos != 12)
                    {
                        throw new FormatException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Format_GuidInvLen")));
                    }
                    _d   = (byte)(temp >> 8);
                    _e   = (byte)(temp);
                    temp = (int)(templ >> 32);
                    _f   = (byte)(temp >> 8);
                    _g   = (byte)(temp);
                    temp = (int)(templ);
                    _h   = (byte)(temp >> 24);
                    _i   = (byte)(temp >> 16);
                    _j   = (byte)(temp >> 8);
                    _k   = (byte)(temp);
                }
                // Else check if it is of the form
                // {0xdddddddd,0xdddd,0xdddd,{0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd}}
                else if (g.IndexOf('{', 0) >= 0)
                {
                    int numStart = 0;
                    int numLen   = 0;

                    // Convert to lower case
                    //g = g.ToLower();

                    // Eat all of the whitespace
                    g = EatAllWhitespace(g);

                    // Check for leading '{'
                    if (g[0] != '{')
                    {
                        throw new FormatException(Environment.GetResourceString("Format_GuidBrace"));
                    }

                    // Check for '0x'
                    if (!IsHexPrefix(g, 1))
                    {
                        throw new FormatException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Format_GuidHexPrefix"), "{0xdddddddd, etc}"));
                    }

                    // Find the end of this hex number (since it is not fixed length)
                    numStart = 3;
                    numLen   = g.IndexOf(',', numStart) - numStart;
                    if (numLen <= 0)
                    {
                        throw new FormatException(Environment.GetResourceString("Format_GuidComma"));
                    }

                    // Read in the number
                    _a = (int)ParseNumbers.StringToInt(g.Substring(numStart, numLen),  // first DWORD
                                                       16,                             // hex
                                                       ParseNumbers.IsTight);          // tight parsing

                    // Check for '0x'
                    if (!IsHexPrefix(g, numStart + numLen + 1))
                    {
                        throw new FormatException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Format_GuidHexPrefix"), "{0xdddddddd, 0xdddd, etc}"));
                    }

                    // +3 to get by ',0x'
                    numStart = numStart + numLen + 3;
                    numLen   = g.IndexOf(',', numStart) - numStart;
                    if (numLen <= 0)
                    {
                        throw new FormatException(Environment.GetResourceString("Format_GuidComma"));
                    }

                    // Read in the number
                    _b = (short)ParseNumbers.StringToInt(
                        g.Substring(numStart, numLen),                                   // first DWORD
                        16,                                                              // hex
                        ParseNumbers.IsTight);                                           // tight parsing

                    // Check for '0x'
                    if (!IsHexPrefix(g, numStart + numLen + 1))
                    {
                        throw new FormatException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Format_GuidHexPrefix"), "{0xdddddddd, 0xdddd, 0xdddd, etc}"));
                    }

                    // +3 to get by ',0x'
                    numStart = numStart + numLen + 3;
                    numLen   = g.IndexOf(',', numStart) - numStart;
                    if (numLen <= 0)
                    {
                        throw new FormatException(Environment.GetResourceString("Format_GuidComma"));
                    }

                    // Read in the number
                    _c = (short)ParseNumbers.StringToInt(
                        g.Substring(numStart, numLen),                                   // first DWORD
                        16,                                                              // hex
                        ParseNumbers.IsTight);                                           // tight parsing

                    // Check for '{'
                    if (g.Length <= numStart + numLen + 1 || g[numStart + numLen + 1] != '{')
                    {
                        throw new FormatException(Environment.GetResourceString("Format_GuidBrace"));
                    }

                    // Prepare for loop
                    numLen++;
                    byte[] bytes = new byte[8];

                    for (int i = 0; i < 8; i++)
                    {
                        // Check for '0x'
                        if (!IsHexPrefix(g, numStart + numLen + 1))
                        {
                            throw new FormatException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Format_GuidHexPrefix"), "{... { ... 0xdd, ...}}"));
                        }

                        // +3 to get by ',0x' or '{0x' for first case
                        numStart = numStart + numLen + 3;

                        // Calculate number length
                        if (i < 7)  // first 7 cases
                        {
                            numLen = g.IndexOf(',', numStart) - numStart;
                            if (numLen <= 0)
                            {
                                throw new FormatException(Environment.GetResourceString("Format_GuidComma"));
                            }
                        }
                        else       // last case ends with '}', not ','
                        {
                            numLen = g.IndexOf('}', numStart) - numStart;
                            if (numLen <= 0)
                            {
                                throw new FormatException(Environment.GetResourceString("Format_GuidBraceAfterLastNumber"));
                            }
                        }

                        // Read in the number
                        uint number = (uint)Convert.ToInt32(g.Substring(numStart, numLen), 16);
                        // check for overflow
                        if (number > 255)
                        {
                            throw new FormatException(Environment.GetResourceString("Overflow_Byte"));
                        }
                        bytes[i] = (byte)number;
                    }

                    _d = bytes[0];
                    _e = bytes[1];
                    _f = bytes[2];
                    _g = bytes[3];
                    _h = bytes[4];
                    _i = bytes[5];
                    _j = bytes[6];
                    _k = bytes[7];

                    // Check for last '}'
                    if (numStart + numLen + 1 >= g.Length || g[numStart + numLen + 1] != '}')
                    {
                        throw new FormatException(Environment.GetResourceString("Format_GuidEndBrace"));
                    }

                    // Check if we have extra characters at the end
                    if (numStart + numLen + 1 != g.Length - 1)
                    {
                        throw new FormatException(Environment.GetResourceString("Format_ExtraJunkAtEnd"));
                    }

                    return;
                }
                else
                // Check if it's of the form dddddddddddddddddddddddddddddddd
                {
                    String guidString = g.Trim();  //Remove Whitespace

                    if (guidString.Length != 32)
                    {
                        throw new FormatException(Environment.GetResourceString("Format_GuidInvLen"));
                    }

                    for (int i = 0; i < guidString.Length; i++)
                    {
                        char ch = guidString[i];
                        if (ch >= '0' && ch <= '9')
                        {
                            continue;
                        }
                        else
                        {
                            char upperCaseCh = Char.ToUpper(ch, CultureInfo.InvariantCulture);
                            if (upperCaseCh >= 'A' && upperCaseCh <= 'F')
                            {
                                continue;
                            }
                        }

                        throw new FormatException(Environment.GetResourceString("Format_GuidInvalidChar"));
                    }

                    _a = (int)ParseNumbers.StringToInt(guidString.Substring(startPos, 8), // first DWORD
                                                       16,                                // hex
                                                       ParseNumbers.IsTight);             // tight parsing
                    startPos += 8;
                    _b        = (short)ParseNumbers.StringToInt(guidString.Substring(startPos, 4),
                                                                16,
                                                                ParseNumbers.IsTight);
                    startPos += 4;
                    _c        = (short)ParseNumbers.StringToInt(guidString.Substring(startPos, 4),
                                                                16,
                                                                ParseNumbers.IsTight);

                    startPos += 4;
                    temp      = (short)ParseNumbers.StringToInt(guidString.Substring(startPos, 4),
                                                                16,
                                                                ParseNumbers.IsTight);
                    startPos  += 4;
                    currentPos = startPos;
                    templ      = ParseNumbers.StringToLong(guidString, 16, startPos, ref currentPos);
                    if (currentPos - startPos != 12)
                    {
                        throw new FormatException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Format_GuidInvLen")));
                    }
                    _d   = (byte)(temp >> 8);
                    _e   = (byte)(temp);
                    temp = (int)(templ >> 32);
                    _f   = (byte)(temp >> 8);
                    _g   = (byte)(temp);
                    temp = (int)(templ);
                    _h   = (byte)(temp >> 24);
                    _i   = (byte)(temp >> 16);
                    _j   = (byte)(temp >> 8);
                    _k   = (byte)(temp);
                }
            }
            catch (IndexOutOfRangeException)
            {
                throw new FormatException(Environment.GetResourceString("Format_GuidUnrecognized"));
            }
        }
Beispiel #8
0
        // Creates a new guid based on the value in the string.  The value is made up
        // of hex digits speared by the dash ("-"). The string may begin and end with
        // brackets ("{", "}").
        //
        // The string must be of the form dddddddd-dddd-dddd-dddd-dddddddddddd. where
        // d is a hex digit. (That is 8 hex digits, followed by 4, then 4, then 4,
        // then 12) such as: "CA761232-ED42-11CE-BACD-00AA0057B223"
        //
        //| <include path='docs/doc[@for="Guid.Guid2"]/*' />
        public Guid(String g)
        {
            if (g == null)
            {
                throw new ArgumentNullException("g", "ArgumentNull_String");
            }

            int  startPos = 0;
            long _dTemp;
            int  _dStartTemp = 0;

            int[]   spArray = new int[1];
            byte [] dstartArray;
            byte [] dArray;

            try {
                // Check if it's of the form dddddddd-dddd-dddd-dddd-dddddddddddd
                if (g.IndexOf('-', 0) >= 0)
                {
                    String guidString = g.Trim();  //Remove Whitespace

                    // check to see that it's the proper length
                    if (guidString[0] == '{')
                    {
                        if (guidString.Length != 38 || guidString[37] != '}')
                        {
                            throw new FormatException("Format_GuidInvLen");
                        }
                        startPos = 1;
                    }
                    else if (guidString[0] == '(')
                    {
                        if (guidString.Length != 38 || guidString[37] != ')')
                        {
                            throw new FormatException("Format_GuidInvLen");
                        }
                        startPos = 1;
                    }
                    else if (guidString.Length != 36)
                    {
                        throw new FormatException("Format_GuidInvLen");
                    }
                    if (guidString[8 + startPos] != '-' ||
                        guidString[13 + startPos] != '-' ||
                        guidString[18 + startPos] != '-' ||
                        guidString[23 + startPos] != '-')
                    {
                        throw new FormatException("Format_GuidDashes");
                    }

                    spArray[0] = startPos;
                    _a         = TryParse(guidString, spArray, 8);
                    spArray[0]++; //Increment past the '-';
                    _b = (short)TryParse(guidString, spArray, 4);
                    spArray[0]++; //Increment past the '-';
                    _c = (short)TryParse(guidString, spArray, 4);
                    spArray[0]++; //Increment past the '-';
                    _dStartTemp = TryParse(guidString, spArray, 4);
                    spArray[0]++; //Increment past the '-';
                    startPos = spArray[0];
                    _dTemp   = ParseNumbers.StringToLong(guidString, 16, 0, spArray);
                    if (spArray[0] - startPos != 12)
                    {
                        throw new FormatException(String.Format("Format_GuidInvLen"));
                    }
                    dstartArray = BitConverter.GetBytes(_dStartTemp);
                    dArray      = BitConverter.GetBytes(_dTemp);

                    if (BitConverter.IsLittleEndian)
                    {
                        _d = dstartArray[1];
                        _e = dstartArray[0];
                        _f = dArray[5];
                        _g = dArray[4];
                        _h = dArray[3];
                        _i = dArray[2];
                        _j = dArray[1];
                        _k = dArray[0];
                    }
                    else
                    {
                        _d = dstartArray[0];
                        _e = dstartArray[1];
                        _f = dArray[0];
                        _g = dArray[1];
                        _h = dArray[2];
                        _i = dArray[3];
                        _j = dArray[4];
                        _k = dArray[5];
                    }
                }

                // Else check if it is of the form
                // {0xdddddddd,0xdddd,0xdddd,{0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd}}
                else if (g.IndexOf('{', 0) >= 0)
                {
                    int numStart = 0;
                    int numLen   = 0;

                    // Convert to lower case
                    //g = g.ToLower();

                    // Eat all of the whitespace
                    g = EatAllWhitespace(g);

                    // Check for leading '{'
                    if (g[0] != '{')
                    {
                        throw new FormatException("Format_GuidBrace");
                    }

                    // Check for '0x'
                    if (!IsHexPrefix(g, 1))
                    {
                        throw new FormatException(String.Format("Format_GuidHexPrefix", "{0xdddddddd, etc}"));
                    }

                    // Find the end of this hex number (since it is not fixed length)
                    numStart = 3;
                    numLen   = g.IndexOf(',', numStart) - numStart;
                    if (numLen <= 0)
                    {
                        throw new FormatException("Format_GuidComma");
                    }

                    // Read in the number
                    _a = (int)ParseNumbers.StringToInt(g.Substring(numStart, numLen),  // first DWORD
                                                       16,                             // hex
                                                       ParseNumbers.IsTight);          // tight parsing

                    // Check for '0x'
                    if (!IsHexPrefix(g, numStart + numLen + 1))
                    {
                        throw new FormatException(String.Format("Format_GuidHexPrefix", "{0xdddddddd, 0xdddd, etc}"));
                    }

                    // +3 to get by ',0x'
                    numStart = numStart + numLen + 3;
                    numLen   = g.IndexOf(',', numStart) - numStart;
                    if (numLen <= 0)
                    {
                        throw new FormatException("Format_GuidComma");
                    }

                    // Read in the number
                    _b = (short)ParseNumbers.StringToInt(
                        g.Substring(numStart, numLen),                                   // first DWORD
                        16,                                                              // hex
                        ParseNumbers.IsTight);                                           // tight parsing

                    // Check for '0x'
                    if (!IsHexPrefix(g, numStart + numLen + 1))
                    {
                        throw new FormatException(String.Format("Format_GuidHexPrefix", "{0xdddddddd, 0xdddd, 0xdddd, etc}"));
                    }

                    // +3 to get by ',0x'
                    numStart = numStart + numLen + 3;
                    numLen   = g.IndexOf(',', numStart) - numStart;
                    if (numLen <= 0)
                    {
                        throw new FormatException("Format_GuidComma");
                    }

                    // Read in the number
                    _c = (short)ParseNumbers.StringToInt(
                        g.Substring(numStart, numLen),                                   // first DWORD
                        16,                                                              // hex
                        ParseNumbers.IsTight);                                           // tight parsing

                    // Check for '{'
                    if (g.Length <= numStart + numLen + 1 || g[numStart + numLen + 1] != '{')
                    {
                        throw new FormatException("Format_GuidBrace");
                    }

                    // Prepare for loop
                    numLen++;
                    byte[] bytes = new byte[8];

                    for (int i = 0; i < 8; i++)
                    {
                        // Check for '0x'
                        if (!IsHexPrefix(g, numStart + numLen + 1))
                        {
                            throw new FormatException(String.Format("Format_GuidHexPrefix", "{... { ... 0xdd, ...}}"));
                        }

                        // +3 to get by ',0x' or '{0x' for first case
                        numStart = numStart + numLen + 3;

                        // Calculate number length
                        if (i < 7) // first 7 cases
                        {
                            numLen = g.IndexOf(',', numStart) - numStart;
                        }
                        else       // last case ends with '}', not ','
                        {
                            numLen = g.IndexOf('}', numStart) - numStart;
                        }
                        if (numLen <= 0)
                        {
                            throw new FormatException("Format_GuidComma");
                        }

                        // Read in the number
                        bytes[i] = (byte)ParseNumbers.StringToInt(g.Substring(numStart, numLen),
                                                                  16,
                                                                  ParseNumbers.IsTight);
                    }

                    _d = bytes[0];
                    _e = bytes[1];
                    _f = bytes[2];
                    _g = bytes[3];
                    _h = bytes[4];
                    _i = bytes[5];
                    _j = bytes[6];
                    _k = bytes[7];

                    // Check for last '}'
                    if (numStart + numLen + 1 >= g.Length || g[numStart + numLen + 1] != '}')
                    {
                        throw new FormatException("Format_GuidEndBrace");
                    }

                    return;
                }
                else
                // Check if it's of the form dddddddddddddddddddddddddddddddd
                {
                    String guidString = g.Trim();  //Remove Whitespace

                    if (guidString.Length != 32)
                    {
                        throw new FormatException("Format_GuidInvLen");
                    }

                    _a = (int)ParseNumbers.StringToInt(g.Substring(startPos, 8),  // first DWORD
                                                       16,                        // hex
                                                       ParseNumbers.IsTight);     // tight parsing
                    startPos += 8;
                    _b        = (short)ParseNumbers.StringToInt(g.Substring(startPos, 4),
                                                                16,
                                                                ParseNumbers.IsTight);
                    startPos += 4;
                    _c        = (short)ParseNumbers.StringToInt(g.Substring(startPos, 4),
                                                                16,
                                                                ParseNumbers.IsTight);

                    startPos   += 4;
                    _dStartTemp = (short)ParseNumbers.StringToInt(g.Substring(startPos, 4),
                                                                  16,
                                                                  ParseNumbers.IsTight);
                    startPos  += 4;
                    spArray[0] = startPos;
                    _dTemp     = ParseNumbers.StringToLong(guidString, 16, startPos, spArray);
                    if (spArray[0] - startPos != 12)
                    {
                        throw new FormatException(String.Format("Format_GuidInvLen"));
                    }
                    dstartArray = BitConverter.GetBytes(_dStartTemp);
                    dArray      = BitConverter.GetBytes(_dTemp);

                    if (BitConverter.IsLittleEndian)
                    {
                        _d = dstartArray[1];
                        _e = dstartArray[0];
                        _f = dArray[5];
                        _g = dArray[4];
                        _h = dArray[3];
                        _i = dArray[2];
                        _j = dArray[1];
                        _k = dArray[0];
                    }
                    else
                    {
                        _d = dstartArray[0];
                        _e = dstartArray[1];
                        _f = dArray[0];
                        _g = dArray[1];
                        _h = dArray[2];
                        _i = dArray[3];
                        _j = dArray[4];
                        _k = dArray[5];
                    }
                }
            }
            catch (IndexOutOfRangeException) {
                throw new FormatException("Format_GuidUnrecognized");
            }
        }
Beispiel #9
0
 public static int StringToInt(string s, int radix, int flags)
 {
     return(ParseNumbers.StringToInt(s, radix, flags, null));
 }
Beispiel #10
0
 public static unsafe int StringToInt(string s, int radix, int flags, ref int currPos)
 {
     fixed(int *currPos1 = &currPos)
     return(ParseNumbers.StringToInt(s, radix, flags, currPos1));
 }
Beispiel #11
0
        public Guid(string g)
        {
            if (g == null)
            {
                throw new ArgumentNullException("g");
            }
            int startIndex = 0;
            int parsePos   = 0;

            try
            {
                int  num2;
                long num3;
                if (g.IndexOf('-', 0) >= 0)
                {
                    string str = g.Trim();
                    if (str[0] == '{')
                    {
                        if ((str.Length != 0x26) || (str[0x25] != '}'))
                        {
                            throw new FormatException(Environment.GetResourceString("Format_GuidInvLen"));
                        }
                        startIndex = 1;
                    }
                    else if (str[0] == '(')
                    {
                        if ((str.Length != 0x26) || (str[0x25] != ')'))
                        {
                            throw new FormatException(Environment.GetResourceString("Format_GuidInvLen"));
                        }
                        startIndex = 1;
                    }
                    else if (str.Length != 0x24)
                    {
                        throw new FormatException(Environment.GetResourceString("Format_GuidInvLen"));
                    }
                    if (((str[8 + startIndex] != '-') || (str[13 + startIndex] != '-')) || ((str[0x12 + startIndex] != '-') || (str[0x17 + startIndex] != '-')))
                    {
                        throw new FormatException(Environment.GetResourceString("Format_GuidDashes"));
                    }
                    parsePos = startIndex;
                    this._a  = TryParse(str, ref parsePos, 8);
                    parsePos++;
                    this._b = (short)TryParse(str, ref parsePos, 4);
                    parsePos++;
                    this._c = (short)TryParse(str, ref parsePos, 4);
                    parsePos++;
                    num2 = TryParse(str, ref parsePos, 4);
                    parsePos++;
                    startIndex = parsePos;
                    num3       = ParseNumbers.StringToLong(str, 0x10, 0x2000, ref parsePos);
                    if ((parsePos - startIndex) != 12)
                    {
                        throw new FormatException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Format_GuidInvLen"), new object[0]));
                    }
                    this._d = (byte)(num2 >> 8);
                    this._e = (byte)num2;
                    num2    = (int)(num3 >> 0x20);
                    this._f = (byte)(num2 >> 8);
                    this._g = (byte)num2;
                    num2    = (int)num3;
                    this._h = (byte)(num2 >> 0x18);
                    this._i = (byte)(num2 >> 0x10);
                    this._j = (byte)(num2 >> 8);
                    this._k = (byte)num2;
                }
                else if (g.IndexOf('{', 0) >= 0)
                {
                    int num5   = 0;
                    int length = 0;
                    g = EatAllWhitespace(g);
                    if (g[0] != '{')
                    {
                        throw new FormatException(Environment.GetResourceString("Format_GuidBrace"));
                    }
                    if (!IsHexPrefix(g, 1))
                    {
                        throw new FormatException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Format_GuidHexPrefix"), new object[] { "{0xdddddddd, etc}" }));
                    }
                    num5   = 3;
                    length = g.IndexOf(',', num5) - num5;
                    if (length <= 0)
                    {
                        throw new FormatException(Environment.GetResourceString("Format_GuidComma"));
                    }
                    this._a = ParseNumbers.StringToInt(g.Substring(num5, length), 0x10, 0x1000);
                    if (!IsHexPrefix(g, (num5 + length) + 1))
                    {
                        throw new FormatException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Format_GuidHexPrefix"), new object[] { "{0xdddddddd, 0xdddd, etc}" }));
                    }
                    num5   = (num5 + length) + 3;
                    length = g.IndexOf(',', num5) - num5;
                    if (length <= 0)
                    {
                        throw new FormatException(Environment.GetResourceString("Format_GuidComma"));
                    }
                    this._b = (short)ParseNumbers.StringToInt(g.Substring(num5, length), 0x10, 0x1000);
                    if (!IsHexPrefix(g, (num5 + length) + 1))
                    {
                        throw new FormatException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Format_GuidHexPrefix"), new object[] { "{0xdddddddd, 0xdddd, 0xdddd, etc}" }));
                    }
                    num5   = (num5 + length) + 3;
                    length = g.IndexOf(',', num5) - num5;
                    if (length <= 0)
                    {
                        throw new FormatException(Environment.GetResourceString("Format_GuidComma"));
                    }
                    this._c = (short)ParseNumbers.StringToInt(g.Substring(num5, length), 0x10, 0x1000);
                    if ((g.Length <= ((num5 + length) + 1)) || (g[(num5 + length) + 1] != '{'))
                    {
                        throw new FormatException(Environment.GetResourceString("Format_GuidBrace"));
                    }
                    length++;
                    byte[] buffer = new byte[8];
                    for (int i = 0; i < 8; i++)
                    {
                        if (!IsHexPrefix(g, (num5 + length) + 1))
                        {
                            throw new FormatException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Format_GuidHexPrefix"), new object[] { "{... { ... 0xdd, ...}}" }));
                        }
                        num5 = (num5 + length) + 3;
                        if (i < 7)
                        {
                            length = g.IndexOf(',', num5) - num5;
                            if (length <= 0)
                            {
                                throw new FormatException(Environment.GetResourceString("Format_GuidComma"));
                            }
                        }
                        else
                        {
                            length = g.IndexOf('}', num5) - num5;
                            if (length <= 0)
                            {
                                throw new FormatException(Environment.GetResourceString("Format_GuidBraceAfterLastNumber"));
                            }
                        }
                        uint num8 = (uint)Convert.ToInt32(g.Substring(num5, length), 0x10);
                        if (num8 > 0xff)
                        {
                            throw new FormatException(Environment.GetResourceString("Overflow_Byte"));
                        }
                        buffer[i] = (byte)num8;
                    }
                    this._d = buffer[0];
                    this._e = buffer[1];
                    this._f = buffer[2];
                    this._g = buffer[3];
                    this._h = buffer[4];
                    this._i = buffer[5];
                    this._j = buffer[6];
                    this._k = buffer[7];
                    if ((((num5 + length) + 1) >= g.Length) || (g[(num5 + length) + 1] != '}'))
                    {
                        throw new FormatException(Environment.GetResourceString("Format_GuidEndBrace"));
                    }
                    if (((num5 + length) + 1) != (g.Length - 1))
                    {
                        throw new FormatException(Environment.GetResourceString("Format_ExtraJunkAtEnd"));
                    }
                }
                else
                {
                    string s = g.Trim();
                    if (s.Length != 0x20)
                    {
                        throw new FormatException(Environment.GetResourceString("Format_GuidInvLen"));
                    }
                    for (int j = 0; j < s.Length; j++)
                    {
                        char c = s[j];
                        if ((c < '0') || (c > '9'))
                        {
                            char ch2 = char.ToUpper(c, CultureInfo.InvariantCulture);
                            if ((ch2 < 'A') || (ch2 > 'F'))
                            {
                                throw new FormatException(Environment.GetResourceString("Format_GuidInvalidChar"));
                            }
                        }
                    }
                    this._a     = ParseNumbers.StringToInt(s.Substring(startIndex, 8), 0x10, 0x1000);
                    startIndex += 8;
                    this._b     = (short)ParseNumbers.StringToInt(s.Substring(startIndex, 4), 0x10, 0x1000);
                    startIndex += 4;
                    this._c     = (short)ParseNumbers.StringToInt(s.Substring(startIndex, 4), 0x10, 0x1000);
                    startIndex += 4;
                    num2        = (short)ParseNumbers.StringToInt(s.Substring(startIndex, 4), 0x10, 0x1000);
                    startIndex += 4;
                    parsePos    = startIndex;
                    num3        = ParseNumbers.StringToLong(s, 0x10, startIndex, ref parsePos);
                    if ((parsePos - startIndex) != 12)
                    {
                        throw new FormatException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Format_GuidInvLen"), new object[0]));
                    }
                    this._d = (byte)(num2 >> 8);
                    this._e = (byte)num2;
                    num2    = (int)(num3 >> 0x20);
                    this._f = (byte)(num2 >> 8);
                    this._g = (byte)num2;
                    num2    = (int)num3;
                    this._h = (byte)(num2 >> 0x18);
                    this._i = (byte)(num2 >> 0x10);
                    this._j = (byte)(num2 >> 8);
                    this._k = (byte)num2;
                }
            }
            catch (IndexOutOfRangeException)
            {
                throw new FormatException(Environment.GetResourceString("Format_GuidUnrecognized"));
            }
        }