Exemple #1
0
 public override TemplateSegmentMatchResult GetMatchResult(DynamicTemplateValues currentValues, string matchText, int start, int length, ImmutableArray <Group> innerGroups)
 {
     return(new TemplateSegmentMatchResult(
                isInexact: Text.Length != length || string.Compare(Text, 0, matchText, start, length, StringComparison.OrdinalIgnoreCase) != 0,
                errorMessages: ImmutableArray <string> .Empty,
                updateMessages: ImmutableArray <string> .Empty));
 }
Exemple #2
0
        public override TemplateSegmentMatchResult GetMatchResult(DynamicTemplateValues currentValues, string matchText, int start, int length, ImmutableArray <Group> innerGroups)
        {
            var matchStartYear   = int.Parse(innerGroups[0].Value);
            var matchCurrentYear = innerGroups.Length == 2 ? int.Parse(innerGroups[1].Value) : matchStartYear;

            var errorMessages = ImmutableArray.CreateBuilder <string>();

            if (matchCurrentYear < matchStartYear)
            {
                errorMessages.Add($"The end year ({matchCurrentYear}) must be greater than or equal to the start year ({matchStartYear}).");
            }

            if (currentValues.CurrentYear < matchCurrentYear)
            {
                errorMessages.Add($"The year {matchCurrentYear} is invalid. The current year is {currentValues.CurrentYear}.");
            }

            var exactText = new StringBuilder();

            BuildText(exactText, matchStartYear, matchCurrentYear);

            return(new MatchResult(
                       isInexact: exactText.Length != length || string.Compare(exactText.ToString(), 0, matchText, start, length, StringComparison.OrdinalIgnoreCase) != 0,
                       errorMessages: errorMessages.ToImmutable(),
                       updateMessages: matchCurrentYear < currentValues.CurrentYear ? ImmutableArray.Create($"The current year is {currentValues.CurrentYear}.") : ImmutableArray <string> .Empty,
                       startYear: matchStartYear));
        }
Exemple #3
0
 public void Evaluate(DynamicTemplateValues currentValues, StringBuilder textBuilder)
 {
     foreach (var segment in Segments)
     {
         segment.AppendToTextEvaluation(currentValues, textBuilder);
     }
 }
Exemple #4
0
        public bool TryMatch(string text, DynamicTemplateValues currentValues, out MatchResult result)
        {
            if (!TryMatch(text, currentValues, out var start, out var length, out var segmentResults))
            {
                result = default;
                return(false);
            }

            var isInexact      = false;
            var errorMessages  = ImmutableArray.CreateBuilder <string>();
            var updateMessages = ImmutableArray.CreateBuilder <string>();

            foreach (var segmentResult in segmentResults)
            {
                isInexact |= segmentResult.IsInexact;
                errorMessages.AddRange(segmentResult.ErrorMessages);
                updateMessages.AddRange(segmentResult.UpdateMessages);
            }

            result = new MatchResult(
                start,
                length,
                isInexact,
                errorMessages.ToImmutable(),
                updateMessages.ToImmutable());

            return(true);
        }
        public override TemplateSegmentMatchResult GetMatchResult(DynamicTemplateValues currentValues, string matchText, int start, int length, ImmutableArray <Group> innerGroups)
        {
            var year = int.Parse(matchText.Substring(start, length));

            return(new TemplateSegmentMatchResult(
                       isInexact: false,
                       errorMessages: currentValues.CurrentYear < year ? ImmutableArray.Create($"The year {year} is invalid. The current year is {currentValues.CurrentYear}.") : ImmutableArray <string> .Empty,
                       updateMessages: year < currentValues.CurrentYear ? ImmutableArray.Create($"The current year is {currentValues.CurrentYear}.") : ImmutableArray <string> .Empty));
        }
        public override void AppendToTextEvaluation(DynamicTemplateValues currentValues, StringBuilder textBuilder, TemplateSegmentMatchResult previousMatchResult = null)
        {
            if (currentValues.CurrentYear < 1000 || currentValues.CurrentYear > 9999)
            {
                throw new ArgumentOutOfRangeException(nameof(currentValues), currentValues.CurrentYear, "Current year must be between 1000 and 9999, inclusive.");
            }

            textBuilder.Append(currentValues.CurrentYear);
        }
        public override TemplateSegmentMatchResult GetMatchResult(DynamicTemplateValues currentValues, string matchText, int start, int length, ImmutableArray <Group> innerGroups)
        {
            var trimmedMatch = matchText.Substring(start, length).Trim();

            return(new MatchResult(
                       isInexact: trimmedMatch.Length != length,
                       errorMessages: trimmedMatch == string.Empty || trimmedMatch == Placeholder ? ImmutableArray.Create("Name must not be blank.") : ImmutableArray <string> .Empty,
                       updateMessages: ImmutableArray <string> .Empty,
                       trimmedName: trimmedMatch));
        }
Exemple #8
0
 public void Evaluate(DynamicTemplateValues currentValues, StringBuilder textBuilder, string previousText, out int previousMatchStart, out int previousMatchLength)
 {
     if (previousText != null && TryMatch(previousText, currentValues, out previousMatchStart, out previousMatchLength, out var segmentResults))
     {
         for (var i = 0; i < Segments.Length; i++)
         {
             Segments[i].AppendToTextEvaluation(currentValues, textBuilder, segmentResults[i]);
         }
     }
     else
     {
         previousMatchStart  = -1;
         previousMatchLength = -1;
         Evaluate(currentValues, textBuilder);
     }
 }
Exemple #9
0
        private bool TryMatch(string text, DynamicTemplateValues currentValues, out int start, out int length, out ImmutableArray <TemplateSegmentMatchResult> segmentResults)
        {
            var match = regex.Value.Match(text);

            if (!match.Success)
            {
                start          = default;
                length         = default;
                segmentResults = default;
                return(false);
            }

            var segmentResultsBuilder = ImmutableArray.CreateBuilder <TemplateSegmentMatchResult>(Segments.Length);

            for (var i = 0; i < Segments.Length; i++)
            {
                var segmentGroup = match.Groups["HeaderTemplate_segment" + i];

                var innerGroups = ImmutableArray.CreateBuilder <Group>();
                foreach (Group group in match.Groups)
                {
                    if (group.Success &&
                        group != segmentGroup &&
                        group.Index >= segmentGroup.Index &&
                        group.Index + group.Length <= segmentGroup.Index + segmentGroup.Length)
                    {
                        innerGroups.Add(group);
                    }
                }

                segmentResultsBuilder.Add(Segments[i].GetMatchResult(
                                              currentValues,
                                              text,
                                              segmentGroup.Index,
                                              segmentGroup.Length,
                                              innerGroups.ToImmutable()));
            }

            start          = match.Index;
            length         = match.Length;
            segmentResults = segmentResultsBuilder.ToImmutable();
            return(true);
        }
Exemple #10
0
 public abstract void AppendToTextEvaluation(DynamicTemplateValues currentValues, StringBuilder textBuilder, TemplateSegmentMatchResult previousMatchResult = null);
Exemple #11
0
 public abstract TemplateSegmentMatchResult GetMatchResult(DynamicTemplateValues currentValues, string matchText, int start, int length, ImmutableArray <Group> innerGroups);
 public override void AppendToTextEvaluation(DynamicTemplateValues currentValues, StringBuilder textBuilder, TemplateSegmentMatchResult previousMatchResult = null)
 {
     textBuilder.Append((previousMatchResult as MatchResult)?.TrimmedName ?? DefaultName);
 }
Exemple #13
0
 public override void AppendToTextEvaluation(DynamicTemplateValues currentValues, StringBuilder textBuilder, TemplateSegmentMatchResult previousMatchResult = null)
 {
     textBuilder.Append(Text);
 }
Exemple #14
0
 public override void AppendToTextEvaluation(DynamicTemplateValues currentValues, StringBuilder textBuilder, TemplateSegmentMatchResult previousMatchResult = null)
 {
     BuildText(textBuilder, previousMatchResult is MatchResult r ? r.StartYear : currentValues.CurrentYear, currentValues.CurrentYear);
 }