Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 90c0aa9

Browse files
committed
Use regex for matching line
1 parent 5019370 commit 90c0aa9

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

src/GitHub.App/Services/GitHubContextService.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public class GitHubContextService
3535
static readonly Regex windowTitlePathRegex = new Regex($"{path} at {branch} · {owner}/{repo}( · GitHub)? - ", RegexOptions.Compiled);
3636
static readonly Regex windowTitleBranchesRegex = new Regex($"Branches · {owner}/{repo}( · GitHub)? - ", RegexOptions.Compiled);
3737

38+
static readonly Regex urlLineRegex = new Regex($"#L(?<line>[0-9]+)$", RegexOptions.Compiled);
39+
3840
public GitHubContext FindContextFromUrl(string url)
3941
{
4042
var uri = new UriString(url);
@@ -158,20 +160,16 @@ public GitHubContext FindContextFromWindowTitle(string windowTitle)
158160

159161
static int? FindLine(UriString gitHubUrl)
160162
{
161-
var prefix = "#L";
162163
var url = gitHubUrl.ToString();
163-
var index = url.LastIndexOf(prefix);
164-
if (index == -1)
165-
{
166-
return null;
167-
}
168164

169-
if (!int.TryParse(url.Substring(index + prefix.Length), out int lineNumber))
165+
var match = urlLineRegex.Match(url);
166+
if (match.Success)
170167
{
171-
return null;
168+
int.TryParse(match.Groups["line"].Value, out int line);
169+
return line;
172170
}
173171

174-
return lineNumber; // 1 based
172+
return null;
175173
}
176174

177175
string FindPath(UriString uri)

0 commit comments

Comments
 (0)