Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 6d94a62

Browse files
Changing string parsing to regexes
1 parent 57cc7c3 commit 6d94a62

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

src/GitHub.Api/OutputProcessors/LfsVersionOutputProcessor.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
using System;
2+
using System.Text.RegularExpressions;
23

34
namespace GitHub.Unity
45
{
56
class LfsVersionOutputProcessor : BaseOutputProcessor<Version>
67
{
8+
public static Regex GitLfsVersionRegex = new Regex(@"git-lfs/([\d]+)\.([\d]+)\.([\d]+)");
9+
710
public override void LineReceived(string line)
811
{
912
if (String.IsNullOrEmpty(line))
1013
return;
1114

12-
var gitVersion = "git-lfs/";
13-
if (line.StartsWith(gitVersion))
14-
{
15-
line = line.Substring(gitVersion.Length, line.IndexOf(" ", StringComparison.InvariantCultureIgnoreCase) - gitVersion.Length);
16-
var strings = line.Split(new[] { "." }, StringSplitOptions.None);
15+
var match = GitLfsVersionRegex.Match(line);
1716

18-
RaiseOnEntry(new Version(Int32.Parse(strings[0]), Int32.Parse(strings[1]), Int32.Parse(strings[2])));
17+
if (match.Groups.Count > 0)
18+
{
19+
var major = Int32.Parse(match.Groups[1].Value);
20+
var minor = Int32.Parse(match.Groups[2].Value);
21+
var build = Int32.Parse(match.Groups[3].Value);
22+
var version = new Version(major, minor, build);
23+
RaiseOnEntry(version);
1924
}
2025
}
2126
}

src/GitHub.Api/OutputProcessors/VersionOutputProcessor.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text.RegularExpressions;
35

46
namespace GitHub.Unity
57
{
68
class VersionOutputProcessor : BaseOutputProcessor<Version>
79
{
10+
public static Regex GitVersionRegex = new Regex(@"git version ([\d]+)\.([\d]+)\.([\d]+)");
11+
812
public override void LineReceived(string line)
913
{
1014
if (String.IsNullOrEmpty(line))
1115
return;
1216

13-
var gitVersion = "git version ";
14-
if (line.StartsWith(gitVersion))
15-
{
16-
line = line.Substring(gitVersion.Length);
17-
var strings = line.Split(new[] { "." }, StringSplitOptions.None);
17+
var match = GitVersionRegex.Match(line);
1818

19-
RaiseOnEntry(new Version(Int32.Parse(strings[0]), Int32.Parse(strings[1]), Int32.Parse(strings[2])));
19+
if (match.Groups.Count > 0)
20+
{
21+
var major = Int32.Parse(match.Groups[1].Value);
22+
var minor = Int32.Parse(match.Groups[2].Value);
23+
var build = Int32.Parse(match.Groups[3].Value);
24+
var version = new Version(major, minor, build);
25+
RaiseOnEntry(version);
2026
}
2127
}
2228
}

0 commit comments

Comments
 (0)