Skip to content

Commit 20efb19

Browse files
committed
Use TextSpan
1 parent 3eb2946 commit 20efb19

3 files changed

Lines changed: 18 additions & 21 deletions

File tree

AspNetCoreAnalyzers.Tests/Helpers/UrlTemplateTests.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,15 @@ public async Task<IActionResult> GetOrder([FromRoute]int id)
9797
Assert.AreEqual("id", parameter.Name.Text);
9898
}
9999

100-
[TestCase("orders/{id:alpha}", new[] { "orders", "{id:alpha}" })]
101-
[TestCase("orders/{id:alpha:minlength(1)}", new[] { "orders", "{id:alpha:minlength(1)}" })]
102-
[TestCase("orders/{id:minlength(1)}", new[] { "orders", "{id:minlength(1)}" })]
103-
[TestCase("orders/{id:maxlength(1)}", new[] { "orders", "{id:maxlength(1)}" })]
104-
[TestCase("orders/{id:length(1)}", new[] { "orders", "{id:length(1)}" })]
105-
[TestCase("orders/{id:length(1,2)}", new[] { "orders", "{id:length(1,2)}" })]
100+
[TestCase("orders/{id?}", new[] { "orders", "{id?}" })]
101+
[TestCase("orders/{id:alpha}", new[] { "orders", "{id:alpha}" })]
102+
[TestCase("orders/{id:alpha:minlength(1)}", new[] { "orders", "{id:alpha:minlength(1)}" })]
103+
[TestCase("orders/{id:minlength(1)}", new[] { "orders", "{id:minlength(1)}" })]
104+
[TestCase("orders/{id:maxlength(1)}", new[] { "orders", "{id:maxlength(1)}" })]
105+
[TestCase("orders/{id:length(1)}", new[] { "orders", "{id:length(1)}" })]
106+
[TestCase("orders/{id:length(1,2)}", new[] { "orders", "{id:length(1,2)}" })]
106107
[TestCase("orders/{id:regex(^\\\\d{{3}}-\\\\d{{2}}-\\\\d{{4}}$)}", new[] { "orders", "{id:regex(^\\d{{3}}-\\d{{2}}-\\d{{4}}$)}" })]
107-
[TestCase("orders/{id:regex(a/b)}", new[] { "orders", "{id:regex(a/b)}" })]
108+
[TestCase("orders/{id:regex(a/b)}", new[] { "orders", "{id:regex(a/b)}" })]
108109
////[TestCase("orders/{id:regex(a[)}/]b)}", new[] { "orders", "{id:regex(a[)}/]b)}" })]
109110
public void TryParseWhenStringParameter(string text, string[] expected)
110111
{

AspNetCoreAnalyzers/Helpers/TextAndLocation.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,15 @@ public struct TextAndLocation
1111
public TextAndLocation(LiteralExpressionSyntax literal, int start, int end)
1212
{
1313
this.literal = literal;
14-
this.Start = start;
15-
this.End = end;
14+
this.Span = new TextSpan(start, end - start);
1615
this.Text = literal.Token.ValueText.Substring(start, end - start);
1716
}
1817

19-
public int Start { get; }
20-
21-
public int End { get; }
18+
public TextSpan Span { get; }
2219

2320
public string Text { get; }
2421

25-
public Location Location => Location.Create(this.literal.SyntaxTree, TextSpan.FromBounds(this.literal.SpanStart + this.Start, this.literal.SpanStart + this.End));
22+
public Location Location => Location.Create(this.literal.SyntaxTree, this.Span);
2623

2724
public static bool operator ==(TextAndLocation left, TextAndLocation right)
2825
{
@@ -36,7 +33,7 @@ public TextAndLocation(LiteralExpressionSyntax literal, int start, int end)
3633

3734
public bool Equals(TextAndLocation other)
3835
{
39-
return this.literal.Equals(other.literal) && this.Start == other.Start && this.End == other.End;
36+
return this.literal.Equals(other.literal) && this.Span == other.Span;
4037
}
4138

4239
public override bool Equals(object obj)
@@ -50,15 +47,14 @@ public override int GetHashCode()
5047
unchecked
5148
{
5249
var hashCode = this.literal.GetHashCode();
53-
hashCode = (hashCode * 397) ^ this.Start;
54-
hashCode = (hashCode * 397) ^ this.End;
50+
hashCode = (hashCode * 397) ^ this.Span.GetHashCode();
5551
return hashCode;
5652
}
5753
}
5854

5955
internal TextAndLocation Substring(int index, int length)
6056
{
61-
return new TextAndLocation(this.literal, this.Start + index, this.Start + index + length);
57+
return new TextAndLocation(this.literal, this.Span.Start + index, this.Span.Start + index + length);
6258
}
6359
}
6460
}

AspNetCoreAnalyzers/Helpers/UrlTemplate.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ public static bool TryParse(LiteralExpressionSyntax literal, out UrlTemplate tem
3737
{
3838
var text = literal.Token.ValueText;
3939
var builder = ImmutableArray.CreateBuilder<PathSegment>();
40-
var start = 0;
41-
while (TryParse(literal, text, start, out var component))
40+
var pos = 0;
41+
while (TryParse(literal, text, pos, out var component))
4242
{
4343
builder.Add(component);
44-
start = component.Text.End;
44+
pos = component.Text.Span.End;
4545
}
4646

47-
if (start == text.Length)
47+
if (pos == text.Length)
4848
{
4949
template = new UrlTemplate(literal, builder.Count == builder.Capacity ? builder.MoveToImmutable() : builder.ToImmutable());
5050
return true;

0 commit comments

Comments
 (0)