/// <summary>
/// Does a split. In the right-to-left case we reorder the
/// array to be forwards.
/// </summary>
internal static string[] Split(Regex regex, string input, int count, int startat)
{
if (count < 0)
{
throw new ArgumentOutOfRangeException(nameof(count), SR.CountTooSmall);
}
if (startat < 0 || startat > input.Length)
{
throw new ArgumentOutOfRangeException(nameof(startat), SR.BeginIndexNotNegative);
}
string[] result;
if (count == 1)
{
result = new string[1];
result[0] = input;
return(result);
}
count -= 1;
Match match = regex.Match(input, startat);
if (!match.Success)
{
result = new string[1];
result[0] = input;
return(result);
}
else
{
List <string> al = new List <string>();
if (!regex.RightToLeft)
{
int prevat = 0;
for (; ;)
{
al.Add(input.Substring(prevat, match.Index - prevat));
prevat = match.Index + match.Length;
// add all matched capture groups to the list.
for (int i = 1; i < match.Groups.Count; i++)
{
if (match.IsMatched(i))
{
al.Add(match.Groups[i].ToString());
}
}
if (--count == 0)
{
break;
}
match = match.NextMatch();
if (!match.Success)
{
break;
}
}
al.Add(input.Substring(prevat, input.Length - prevat));
}
else
{
int prevat = input.Length;
for (; ;)
{
al.Add(input.Substring(match.Index + match.Length, prevat - match.Index - match.Length));
prevat = match.Index;
// add all matched capture groups to the list.
for (int i = 1; i < match.Groups.Count; i++)
{
if (match.IsMatched(i))
{
al.Add(match.Groups[i].ToString());
}
}
if (--count == 0)
{
break;
}
match = match.NextMatch();
if (!match.Success)
{
break;
}
}
al.Add(input.Substring(0, prevat));
al.Reverse(0, al.Count);
}
return(al.ToArray());
}
}