public static ServiceResult Validate(string textToParse, out NumericRange range)
{
range = NumericRange.Empty;
if (String.IsNullOrEmpty(textToParse))
{
return ServiceResult.Good;
}
// check for multidimensional ranges.
int index = textToParse.IndexOf(',');
if (index >= 0)
{
int start = 0;
List<NumericRange> subranges = new List<NumericRange>();
for (int ii = 0; ii < textToParse.Length; ii++)
{
char ch = textToParse[ii];
if (ch == ',' || ii == textToParse.Length-1)
{
NumericRange subrange = new NumericRange();
string subtext = (ch == ',') ? textToParse.Substring(start, ii - start) : textToParse.Substring(start);
ServiceResult result = Validate(subtext, out subrange);
if (ServiceResult.IsBad(result))
{
return result;
}
subranges.Add(subrange);
start = ii+1;
}
}
// must have at least two entries.
if (subranges.Count < 2)
{
return StatusCodes.BadIndexRangeInvalid;
}
range.m_begin = subranges[0].Begin;
range.m_end = subranges[0].End;
range.m_subranges = subranges.ToArray();
return ServiceResult.Good;
}
try
{
index = textToParse.IndexOf(':');
if (index != -1)
{
range.Begin = Convert.ToInt32(textToParse.Substring(0, index), CultureInfo.InvariantCulture);
range.End = Convert.ToInt32(textToParse.Substring(index + 1), CultureInfo.InvariantCulture);
if (range.End < 0)
{
return ServiceResult.Create(
StatusCodes.BadIndexRangeInvalid,
"NumericRange does not have a valid end index ({0}).",
range.End);
}
if (range.Begin >= range.End)
{
return ServiceResult.Create(
StatusCodes.BadIndexRangeInvalid,
"NumericRange does not have a start index that is less than the end index ({0}).",
range);
}
}
else
{
range.Begin = Convert.ToInt32(textToParse, CultureInfo.InvariantCulture);
range.End = -1;
}
if (range.Begin < 0)
{
return ServiceResult.Create(
StatusCodes.BadIndexRangeInvalid,
"NumericRange does not have a valid start index ({0}).",
range.Begin);
}
}
catch (Exception e)
{
return ServiceResult.Create(
e,
StatusCodes.BadIndexRangeInvalid,
"NumericRange cannot be parsed ({0}).",
textToParse);
}
return ServiceResult.Good;
}