Rhino.ScriptRuntime.IndexFromString C# (CSharp) Method

IndexFromString() public static method

Return -1L if str is not an index, or the index value as lower 32 bits of the result.
Return -1L if str is not an index, or the index value as lower 32 bits of the result. Note that the result needs to be cast to an int in order to produce the actual index, which may be negative.
public static IndexFromString ( string str ) : long
str string
return long
		public static long IndexFromString(string str)
		{
			// The length of the decimal string representation of
			//  Integer.MAX_VALUE, 2147483647
			int MAX_VALUE_LENGTH = 10;
			int len = str.Length;
			if (len > 0)
			{
				int i = 0;
				bool negate = false;
				int c = str[0];
				if (c == '-')
				{
					if (len > 1)
					{
						c = str[1];
						if (c == '0')
						{
							return -1L;
						}
						// "-0" is not an index
						i = 1;
						negate = true;
					}
				}
				c -= '0';
				if (0 <= c && c <= 9 && len <= (negate ? MAX_VALUE_LENGTH + 1 : MAX_VALUE_LENGTH))
				{
					// Use negative numbers to accumulate index to handle
					// Integer.MIN_VALUE that is greater by 1 in absolute value
					// then Integer.MAX_VALUE
					int index = -c;
					int oldIndex = 0;
					i++;
					if (index != 0)
					{
						// Note that 00, 01, 000 etc. are not indexes
						while (i != len && 0 <= (c = str[i] - '0') && c <= 9)
						{
							oldIndex = index;
							index = 10 * index - c;
							i++;
						}
					}
					// Make sure all characters were consumed and that it couldn't
					// have overflowed.
					if (i == len && (oldIndex > (int.MinValue / 10) || (oldIndex == (int.MinValue / 10) && c <= (negate ? -(int.MinValue % 10) : (int.MaxValue % 10)))))
					{
						return unchecked((long)(0xFFFFFFFFL)) & (negate ? index : -index);
					}
				}
			}
			return -1L;
		}
ScriptRuntime