/
Regex.Split.cs
150 lines (126 loc) · 5.62 KB
/
Regex.Split.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
namespace System.Text.RegularExpressions
{
public partial class Regex
{
/// <summary>
/// Splits the <paramref name="input "/>string at the position defined
/// by <paramref name="pattern"/>.
/// </summary>
public static string[] Split(string input, [StringSyntax(StringSyntaxAttribute.Regex)] string pattern) =>
RegexCache.GetOrAdd(pattern).Split(input);
/// <summary>
/// Splits the <paramref name="input "/>string at the position defined by <paramref name="pattern"/>.
/// </summary>
public static string[] Split(string input, [StringSyntax(StringSyntaxAttribute.Regex, nameof(options))] string pattern, RegexOptions options) =>
RegexCache.GetOrAdd(pattern, options, s_defaultMatchTimeout).Split(input);
public static string[] Split(string input, [StringSyntax(StringSyntaxAttribute.Regex, nameof(options))] string pattern, RegexOptions options, TimeSpan matchTimeout) =>
RegexCache.GetOrAdd(pattern, options, matchTimeout).Split(input);
/// <summary>
/// Splits the <paramref name="input"/> string at the position defined by a
/// previous pattern.
/// </summary>
public string[] Split(string input)
{
if (input is null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.input);
}
return Split(this, input, 0, RightToLeft ? input.Length : 0);
}
/// <summary>
/// Splits the <paramref name="input"/> string at the position defined by a
/// previous pattern.
/// </summary>
public string[] Split(string input, int count)
{
if (input is null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.input);
}
return Split(this, input, count, RightToLeft ? input.Length : 0);
}
/// <summary>
/// Splits the <paramref name="input"/> string at the position defined by a previous pattern.
/// </summary>
public string[] Split(string input, int count, int startat)
{
if (input is null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.input);
}
return Split(this, input, count, startat);
}
/// <summary>
/// Does a split. In the right-to-left case we reorder the
/// array to be forwards.
/// </summary>
private static string[] Split(Regex regex, string input, int count, int startat)
{
if (count < 0)
{
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.CountTooSmall);
}
if ((uint)startat > (uint)input.Length)
{
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startat, ExceptionResource.BeginIndexNotNegative);
}
if (count == 1)
{
return [input];
}
count--;
var state = (results: new List<string>(), prevat: 0, input, count);
if (!regex.RightToLeft)
{
regex.RunAllMatchesWithCallback(input, startat, ref state, static (ref (List<string> results, int prevat, string input, int count) state, Match match) =>
{
state.results.Add(state.input.Substring(state.prevat, match.Index - state.prevat));
state.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))
{
state.results.Add(match.Groups[i].Value);
}
}
return --state.count != 0;
}, RegexRunnerMode.FullMatchRequired, reuseMatchObject: true);
if (state.results.Count == 0)
{
return [input];
}
state.results.Add(input.Substring(state.prevat));
}
else
{
state.prevat = input.Length;
regex.RunAllMatchesWithCallback(input, startat, ref state, static (ref (List<string> results, int prevat, string input, int count) state, Match match) =>
{
state.results.Add(state.input.Substring(match.Index + match.Length, state.prevat - match.Index - match.Length));
state.prevat = match.Index;
// add all matched capture groups to the list.
for (int i = 1; i < match.Groups.Count; i++)
{
if (match.IsMatched(i))
{
state.results.Add(match.Groups[i].Value);
}
}
return --state.count != 0;
}, RegexRunnerMode.FullMatchRequired, reuseMatchObject: true);
if (state.results.Count == 0)
{
return [input];
}
state.results.Add(input.Substring(0, state.prevat));
state.results.Reverse(0, state.results.Count);
}
return state.results.ToArray();
}
}
}