dotnet-installer/src/Microsoft.DotNet.ProjectJsonMigration/Microsoft.DotNet.Internal.ProjectModel/FileSystemGlobbing/Internal/PathSegments/WildcardPathSegment.cs
2017-03-02 20:35:20 -08:00

74 lines
2.5 KiB
C#

// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
namespace Microsoft.DotNet.Internal.ProjectModel.FileSystemGlobbing.Internal.PathSegments
{
internal class WildcardPathSegment : IPathSegment
{
// It doesn't matter which StringComparison type is used in this MatchAll segment because
// all comparing are skipped since there is no content in the segment.
public static readonly WildcardPathSegment MatchAll = new WildcardPathSegment(
string.Empty, new List<string>(), string.Empty, StringComparison.OrdinalIgnoreCase);
private readonly StringComparison _comparisonType;
public WildcardPathSegment(string beginsWith, List<string> contains, string endsWith, StringComparison comparisonType)
{
BeginsWith = beginsWith;
Contains = contains;
EndsWith = endsWith;
_comparisonType = comparisonType;
}
public bool CanProduceStem { get { return true; } }
public string BeginsWith { get; }
public List<string> Contains { get; }
public string EndsWith { get; }
public bool Match(string value)
{
var wildcard = this;
if (value.Length < wildcard.BeginsWith.Length + wildcard.EndsWith.Length)
{
return false;
}
if (!value.StartsWith(wildcard.BeginsWith, _comparisonType))
{
return false;
}
if (!value.EndsWith(wildcard.EndsWith, _comparisonType))
{
return false;
}
var beginRemaining = wildcard.BeginsWith.Length;
var endRemaining = value.Length - wildcard.EndsWith.Length;
for (var containsIndex = 0; containsIndex != wildcard.Contains.Count; ++containsIndex)
{
var containsValue = wildcard.Contains[containsIndex];
var indexOf = value.IndexOf(
value: containsValue,
startIndex: beginRemaining,
count: endRemaining - beginRemaining,
comparisonType: _comparisonType);
if (indexOf == -1)
{
return false;
}
beginRemaining = indexOf + containsValue.Length;
}
return true;
}
}
}