private static ImmutableArray <SyntaxToken> GetTokensFromText(
            ISyntaxFactsService syntaxFacts, SyntaxNode root, SourceText content, string text, Func <SyntaxToken, bool> candidate, CancellationToken cancellationToken)
        {
            var result = ImmutableArray.CreateBuilder <SyntaxToken>();

            var index = 0;

            while ((index = content.IndexOf(text, index, syntaxFacts.IsCaseSensitive)) >= 0)
            {
                cancellationToken.ThrowIfCancellationRequested();

                var nextIndex = index + text.Length;

                var token = root.FindToken(index, findInsideTrivia: true);
                var span  = token.Span;
                if (!token.IsMissing && span.Start == index && span.Length == text.Length && candidate(token))
                {
                    result.Add(token);
                }

                nextIndex = Math.Max(nextIndex, token.SpanStart);
                index     = nextIndex;
            }

            return(result.ToImmutable());
        }
Beispiel #2
0
        bool TestOutput(List <string> sentence, MakeSentenceOptions options)
        {
            if (string.IsNullOrEmpty(SourceText))
            {
                return(true);
            }

            var overlapMax = Math.Min(
                options.MaxOverlapWords,
                (int)Math.Round(options.MaxOverlapRatio * sentence.Count));

            var sequenceCount  = Math.Max(sentence.Count - overlapMax, 1);
            var sequenceLength = Math.Min(sentence.Count, overlapMax);

            for (var i = 0; i < sequenceCount; i++)
            {
                var sequence = string.Join(" ", sentence.GetRange(i, sequenceLength));
                if (SourceText.IndexOf(sequence, StringComparison.OrdinalIgnoreCase) != -1)
                {
                    return(false);
                }
            }

            return(true);
        }
Beispiel #3
0
            public PatternMatchResult(string pattern, string text, int maxMatchCount)
            {
                Pattern    = pattern ?? string.Empty;
                SourceText = text ?? string.Empty;

                if (Pattern.Length == 0)
                {
                    return;
                }
                if (SourceText.Length == 0)
                {
                    return;
                }

                int currPos = 0;

                do
                {
                    int matchPos = SourceText.IndexOf(Pattern, currPos, StringComparison.OrdinalIgnoreCase);

                    if (matchPos == -1)
                    {
                        break;
                    }

                    if (!HasMatches)
                    {
                        HasMatches   = true;
                        _matchStarts = new List <int>();
                    }

                    _matchStarts.Add(matchPos);

                    if (_matchStarts.Count >= maxMatchCount)
                    {
                        break;
                    }

                    currPos = matchPos + Pattern.Length;

                    if (currPos >= SourceText.Length - 1)
                    {
                        break;
                    }
                } while (true);
            }
Beispiel #4
0
        protected int GetNumber(char separator)
        {
            var pos = CurrentPosition;

            CurrentPosition = SourceText.IndexOf(separator, CurrentPosition);

            if (CurrentPosition < 0)
            {
                return(-1);
            }

            var result = 0;

            if (!int.TryParse(SourceText.Substring(pos, CurrentPosition - pos), out result))
            {
                return(-1);
            }

            ++CurrentPosition;

            return(result);
        }