Skip to content

Commit 1471af0

Browse files
committed
StringLiteral
1 parent fbe341e commit 1471af0

6 files changed

Lines changed: 99 additions & 35 deletions

File tree

AspNetCoreAnalyzers.Tests/Documentation/Tests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ namespace AspNetCoreAnalyzers.Tests.Documentation
1212
using System.Text;
1313
using Gu.Roslyn.AnalyzerExtensions;
1414
using Gu.Roslyn.Asserts;
15-
using Gu.Roslyn.CodeFixExtensions;
1615
using Microsoft.CodeAnalysis;
1716
using Microsoft.CodeAnalysis.Diagnostics;
1817
using NUnit.Framework;

AspNetCoreAnalyzers/Analyzers/AttributeAnalyzer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ private static bool HasWrongRegexSyntax(PathSegment segment, out Location locati
367367
{
368368
escaped.Add(text[i]);
369369
if (text[i] == '\\' &&
370-
!segment.Span.IsVerbatim)
370+
!segment.Span.Literal.IsVerbatim)
371371
{
372372
escaped.Add('\\');
373373
escaped.Add('\\');

AspNetCoreAnalyzers/Helpers/PathSegment.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
namespace AspNetCoreAnalyzers
22
{
33
using System.Diagnostics;
4-
using Microsoft.CodeAnalysis.CSharp.Syntax;
54

65
[DebuggerDisplay("{this.Span.Text}")]
76
public struct PathSegment
87
{
9-
public PathSegment(LiteralExpressionSyntax literal, int start, int end)
8+
public PathSegment(StringLiteral literal, int start, int end)
109
{
1110
this.Span = new Span(literal, start, end);
1211
this.Parameter = TemplateParameter.TryParse(this.Span, out var parameter)
@@ -18,10 +17,10 @@ public PathSegment(LiteralExpressionSyntax literal, int start, int end)
1817

1918
public TemplateParameter? Parameter { get; }
2019

21-
public static bool TryRead(LiteralExpressionSyntax literal, int start, out PathSegment segment)
20+
public static bool TryRead(StringLiteral literal, int start, out PathSegment segment)
2221
{
2322
// https://tools.ietf.org/html/rfc3986
24-
var text = literal.Token.ValueText;
23+
var text = literal.LiteralExpression.Token.ValueText;
2524
var pos = start;
2625
if (pos < text.Length - 1)
2726
{

AspNetCoreAnalyzers/Helpers/Span.cs

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,19 @@ namespace AspNetCoreAnalyzers
77

88
public struct Span : IEquatable<Span>
99
{
10-
public Span(LiteralExpressionSyntax literal, int start, int end)
10+
public Span(StringLiteral literal, int start, int end)
1111
{
1212
this.Literal = literal;
1313
this.TextSpan = new TextSpan(start, end - start);
14-
this.Text = literal.Token.ValueText.Substring(start, end - start);
14+
this.Text = literal.LiteralExpression.Token.ValueText.Substring(this.TextSpan.Start, this.TextSpan.Length);
1515
}
1616

17-
public LiteralExpressionSyntax Literal { get; }
17+
public StringLiteral Literal { get; }
1818

1919
public TextSpan TextSpan { get; }
2020

2121
public string Text { get; }
2222

23-
public bool IsVerbatim
24-
{
25-
get
26-
{
27-
foreach (var c in this.Literal.Token.Text)
28-
{
29-
switch (c)
30-
{
31-
case '"':
32-
return false;
33-
case '@':
34-
return true;
35-
}
36-
}
37-
38-
return false;
39-
}
40-
}
41-
4223
public static bool operator ==(Span left, Span right)
4324
{
4425
return left.Equals(right);
@@ -70,14 +51,11 @@ public override int GetHashCode()
7051
}
7152
}
7253

73-
public override string ToString()
74-
{
75-
return this.Literal.Token.ValueText.Substring(this.TextSpan.Start, this.TextSpan.Length);
76-
}
54+
public override string ToString() => this.Literal.LiteralExpression.Token.ValueText.Substring(this.TextSpan.Start, this.TextSpan.Length);
7755

78-
public Location GetLocation() => GetLocation(this.Literal, this.TextSpan);
56+
public Location GetLocation() => this.Literal.GetLocation(this.TextSpan);
7957

80-
public Location GetLocation(int start, int length) => GetLocation(this.Literal, new TextSpan(this.TextSpan.Start + start, length));
58+
public Location GetLocation(int start, int length) => this.Literal.GetLocation(new TextSpan(this.TextSpan.Start + start, length));
8159

8260
internal Span Slice(int start, int end)
8361
{
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
namespace AspNetCoreAnalyzers
2+
{
3+
using System;
4+
using Microsoft.CodeAnalysis;
5+
using Microsoft.CodeAnalysis.CSharp.Syntax;
6+
using Microsoft.CodeAnalysis.Text;
7+
8+
public struct StringLiteral
9+
{
10+
public StringLiteral(LiteralExpressionSyntax literalExpression)
11+
{
12+
this.LiteralExpression = literalExpression;
13+
}
14+
15+
public LiteralExpressionSyntax LiteralExpression { get; }
16+
17+
public ReadOnlySpan<char> Text => this.LiteralExpression.Token.Text.AsSpan();
18+
19+
public ReadOnlySpan<char> ValueText => this.LiteralExpression.Token.ValueText.AsSpan();
20+
21+
public bool IsVerbatim
22+
{
23+
get
24+
{
25+
foreach (var c in this.LiteralExpression.Token.Text)
26+
{
27+
switch (c)
28+
{
29+
case '"':
30+
return false;
31+
case '@':
32+
return true;
33+
}
34+
}
35+
36+
return false;
37+
}
38+
}
39+
40+
public Location GetLocation(TextSpan textSpan)
41+
{
42+
var text = this.LiteralExpression.Token.Text;
43+
var start = 0;
44+
var verbatim = false;
45+
while (start < 3)
46+
{
47+
if (text[start] == '"')
48+
{
49+
start++;
50+
break;
51+
}
52+
53+
if (text[start] == '@')
54+
{
55+
verbatim = true;
56+
}
57+
58+
start++;
59+
}
60+
61+
return Location.Create(this.LiteralExpression.SyntaxTree,
62+
verbatim
63+
? new TextSpan(
64+
this.LiteralExpression.SpanStart + start + textSpan.Start,
65+
textSpan.Length)
66+
: TextSpan.FromBounds(
67+
this.LiteralExpression.SpanStart + GetIndex(textSpan.Start),
68+
this.LiteralExpression.SpanStart + GetIndex(textSpan.End)));
69+
70+
int GetIndex(int pos)
71+
{
72+
var index = start;
73+
for (var i = start; i < pos + start; i++)
74+
{
75+
index++;
76+
if (text[i] == '\\' &&
77+
text[i + 1] == '\\')
78+
{
79+
i++;
80+
index += 2;
81+
}
82+
}
83+
84+
return index;
85+
}
86+
}
87+
}
88+
}

AspNetCoreAnalyzers/Helpers/UrlTemplate.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public static bool TryParse(LiteralExpressionSyntax literal, out UrlTemplate tem
3939
{
4040
var builder = ImmutableArray.CreateBuilder<PathSegment>();
4141
var pos = 0;
42-
while (PathSegment.TryRead(literal, pos, out var component))
42+
while (PathSegment.TryRead(new StringLiteral(literal), pos, out var component))
4343
{
4444
builder.Add(component);
4545
pos = component.Span.TextSpan.End;

0 commit comments

Comments
 (0)