Skip to content

Commit 88b51e6

Browse files
committed
C#: Re-factor logic for fetching info from group and remove a redundant continue statement.
1 parent 881d863 commit 88b51e6

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

csharp/extractor/Semmle.Extraction.CSharp.Standalone/BuildAnalysis.cs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,23 @@ private void Restore(IEnumerable<string> targets, string? pathToNugetConfig = nu
323323
}
324324
}
325325

326+
private static string GetGroup(ReadOnlySpan<char> input, ValueMatch valueMatch, string groupPrefix)
327+
{
328+
var match = input.Slice(valueMatch.Index, valueMatch.Length);
329+
var includeIndex = match.IndexOf(groupPrefix, StringComparison.InvariantCultureIgnoreCase);
330+
if (includeIndex == -1)
331+
{
332+
return string.Empty;
333+
}
334+
335+
match = match.Slice(includeIndex + groupPrefix.Length + 1);
336+
337+
var quoteIndex1 = match.IndexOf("\"");
338+
var quoteIndex2 = match.Slice(quoteIndex1 + 1).IndexOf("\"");
339+
340+
return match.Slice(quoteIndex1 + 1, quoteIndex2).ToString().ToLowerInvariant();
341+
}
342+
326343
private void DownloadMissingPackages(IEnumerable<string> restoreTargets)
327344
{
328345
var alreadyDownloadedPackages = Directory.GetDirectories(packageDirectory.DirInfo.FullName).Select(d => Path.GetFileName(d).ToLowerInvariant()).ToHashSet();
@@ -356,20 +373,8 @@ private void DownloadMissingPackages(IEnumerable<string> restoreTargets)
356373
foreach (var valueMatch in PackageReference().EnumerateMatches(line))
357374
{
358375
// We can't get the group from the ValueMatch, so doing it manually:
359-
var match = line.Slice(valueMatch.Index, valueMatch.Length);
360-
var includeIndex = match.IndexOf("Include", StringComparison.InvariantCultureIgnoreCase);
361-
if (includeIndex == -1)
362-
{
363-
continue;
364-
}
365-
366-
match = match.Slice(includeIndex + "Include".Length + 1);
367-
368-
var quoteIndex1 = match.IndexOf("\"");
369-
var quoteIndex2 = match.Slice(quoteIndex1 + 1).IndexOf("\"");
370-
371-
var packageName = match.Slice(quoteIndex1 + 1, quoteIndex2).ToString().ToLowerInvariant();
372-
if (!alreadyDownloadedPackages.Contains(packageName))
376+
var packageName = GetGroup(line, valueMatch, "Include");
377+
if (!string.IsNullOrEmpty(packageName) && !alreadyDownloadedPackages.Contains(packageName))
373378
{
374379
notYetDownloadedPackages.Add(packageName);
375380
}
@@ -379,7 +384,6 @@ private void DownloadMissingPackages(IEnumerable<string> restoreTargets)
379384
catch (Exception ex)
380385
{
381386
progressMonitor.FailedToReadFile(file, ex);
382-
continue;
383387
}
384388
}
385389

0 commit comments

Comments
 (0)