示例#1
0
            private static Words GetLongestBackwardSubsequence(int length, StringBreaks breaks, string baseName)
            {
                var breakCount = breaks.GetCount();
                var start      = breakCount - length;

                return(GetWords(start, breakCount, breaks, baseName));
            }
示例#2
0
 private static PatternMatchKind GetCamelCaseKind(CamelCaseResult result, StringBreaks candidateHumps)
 {
     var toEnd = result.MatchCount == candidateHumps.GetCount();
     if (result.FromStart)
     {
         if (result.Contiguous)
         {
             // We contiguously matched humps from the start of this candidate.  If we 
             // matched all the humps, then this was an exact match, otherwise it was a 
             // contiguous prefix match
             return toEnd
                 ? PatternMatchKind.CamelCaseExact
                 : PatternMatchKind.CamelCasePrefix;
         }
         else
         {
             return PatternMatchKind.CamelCaseNonContiguousPrefix;
         }
     }
     else
     {
         // We didn't match from the start.  Distinguish between a match whose humps are all
         // contiguous, and one that isn't.
         return result.Contiguous
             ? PatternMatchKind.CamelCaseSubstring
             : PatternMatchKind.CamelCaseNonContiguousSubstring;
     }
 }
 public AllLowerCamelCaseMatcher(bool includeMatchedSpans, string candidate, StringBreaks candidateHumps, TextChunk patternChunk)
 {
     _includeMatchedSpans = includeMatchedSpans;
     _candidate           = candidate;
     _candidateHumps      = candidateHumps;
     _patternChunk        = patternChunk;
     _patternText         = _patternChunk.Text;
 }
示例#4
0
 public TextChunk(string text, bool allowFuzzingMatching)
 {
     this.Text              = text;
     this.CharacterSpans    = StringBreaker.BreakIntoCharacterParts(text);
     this.SimilarityChecker = allowFuzzingMatching
         ? WordSimilarityChecker.Allocate(text, substringsAreSimilar: false)
         : null;
 }
示例#5
0
        private PatternMatchKind?TryAllLowerCamelCaseMatch(
            string candidate,
            StringBreaks candidateParts,
            TextChunk patternChunk,
            out ImmutableArray <TextSpan> matchedSpans)
        {
            var matcher = new AllLowerCamelCaseMatcher(_includeMatchedSpans, candidate, candidateParts, patternChunk);

            return(matcher.TryMatch(out matchedSpans));
        }
示例#6
0
        private int?TryAllLowerCamelCaseMatch(
            string candidate,
            bool includeMatchedSpans,
            StringBreaks candidateParts,
            TextChunk patternChunk,
            out List <TextSpan> matchedSpans)
        {
            var matcher = new AllLowerCamelCaseMatcher(candidate, includeMatchedSpans, candidateParts, patternChunk);

            return(matcher.TryMatch(out matchedSpans));
        }
示例#7
0
        private static IList<string> PartListToSubstrings(string identifier, StringBreaks parts)
        {
            var result = new List<string>();
            for (int i = 0, n = parts.GetCount(); i < n; i++)
            {
                var span = parts[i];
                result.Add(identifier.Substring(span.Start, span.Length));
            }

            return result;
        }
            private static Words GetWords(int start, int end, StringBreaks breaks, string baseName)
            {
                var result = ImmutableArray.Create <string>();

                for (; start < end; start++)
                {
                    var @break = breaks[start];
                    result = result.Add(baseName.Substring(@break.Start, @break.Length));
                }

                return(result);
            }
示例#9
0
        private List <string> CreateWords(StringBreaks wordBreaks, string name)
        {
            var result = new List <string>(wordBreaks.GetCount());

            for (int i = 0, n = wordBreaks.GetCount(); i < n; i++)
            {
                var br = wordBreaks[i];
                result.Add(name.Substring(br.Start, br.Length));
            }

            return(result);
        }
示例#10
0
        private static IList <string> PartListToSubstrings(string identifier, StringBreaks parts)
        {
            var result = new List <string>();

            for (var i = 0; i < parts.Count; i++)
            {
                var span = parts[i];
                result.Add(identifier.Substring(span.Start, span.Length));
            }

            return(result);
        }
        private List <string> CreateWords(StringBreaks wordBreaks, string name)
        {
            var result = new List <string>(wordBreaks.Count);

            for (var i = 0; i < wordBreaks.Count; i++)
            {
                var br = wordBreaks[i];
                result.Add(name.Substring(br.Start, br.Length));
            }

            return(result);
        }
示例#12
0
        private PatternMatchKind?TryUpperCaseCamelCaseMatch(
            string candidate,
            StringBreaks candidateHumps,
            TextChunk patternChunk,
            CompareOptions compareOption,
            out ImmutableArray <TextSpan> matchedSpans)
        {
            var patternHumps = patternChunk.CharacterSpans;

            // Note: we may have more pattern parts than candidate parts.  This is because multiple
            // pattern parts may match a candidate part.  For example "SiUI" against "SimpleUI".
            // We'll have 3 pattern parts Si/U/I against two candidate parts Simple/UI.  However, U
            // and I will both match in UI.

            int  currentCandidateHump = 0;
            int  currentPatternHump   = 0;
            int? firstMatch           = null;
            bool?contiguous           = null;

            var patternHumpCount   = patternHumps.GetCount();
            var candidateHumpCount = candidateHumps.GetCount();

            var matchSpans = ArrayBuilder <TextSpan> .GetInstance();

            while (true)
            {
                // Let's consider our termination cases
                if (currentPatternHump == patternHumpCount)
                {
                    Contract.Requires(firstMatch.HasValue);
                    Contract.Requires(contiguous.HasValue);

                    var matchCount = matchSpans.Count;
                    matchedSpans = _includeMatchedSpans
                        ? new NormalizedTextSpanCollection(matchSpans).ToImmutableArray()
                        : ImmutableArray <TextSpan> .Empty;
                    matchSpans.Free();

                    var camelCaseResult = new CamelCaseResult(firstMatch == 0, contiguous.Value, matchCount, null);
                    return(GetCamelCaseKind(camelCaseResult, candidateHumps));
                }
                else if (currentCandidateHump == candidateHumpCount)
                {
                    // No match, since we still have more of the pattern to hit
                    matchedSpans = ImmutableArray <TextSpan> .Empty;
                    matchSpans.Free();
                    return(null);
                }

                var  candidateHump            = candidateHumps[currentCandidateHump];
                bool gotOneMatchThisCandidate = false;

                // Consider the case of matching SiUI against SimpleUIElement. The candidate parts
                // will be Simple/UI/Element, and the pattern parts will be Si/U/I.  We'll match 'Si'
                // against 'Simple' first.  Then we'll match 'U' against 'UI'. However, we want to
                // still keep matching pattern parts against that candidate part.
                for (; currentPatternHump < patternHumpCount; currentPatternHump++)
                {
                    var patternChunkCharacterSpan = patternHumps[currentPatternHump];

                    if (gotOneMatchThisCandidate)
                    {
                        // We've already gotten one pattern part match in this candidate.  We will
                        // only continue trying to consume pattern parts if the last part and this
                        // part are both upper case.
                        if (!char.IsUpper(patternChunk.Text[patternHumps[currentPatternHump - 1].Start]) ||
                            !char.IsUpper(patternChunk.Text[patternHumps[currentPatternHump].Start]))
                        {
                            break;
                        }
                    }

                    if (!PartStartsWith(candidate, candidateHump, patternChunk.Text, patternChunkCharacterSpan, compareOption))
                    {
                        break;
                    }

                    matchSpans.Add(new TextSpan(candidateHump.Start, patternChunkCharacterSpan.Length));
                    gotOneMatchThisCandidate = true;

                    firstMatch = firstMatch ?? currentCandidateHump;

                    // If we were contiguous, then keep that value.  If we weren't, then keep that
                    // value.  If we don't know, then set the value to 'true' as an initial match is
                    // obviously contiguous.
                    contiguous = contiguous ?? true;

                    candidateHump = new TextSpan(candidateHump.Start + patternChunkCharacterSpan.Length, candidateHump.Length - patternChunkCharacterSpan.Length);
                }

                // Check if we matched anything at all.  If we didn't, then we need to unset the
                // contiguous bit if we currently had it set.
                // If we haven't set the bit yet, then that means we haven't matched anything so
                // far, and we don't want to change that.
                if (!gotOneMatchThisCandidate && contiguous.HasValue)
                {
                    contiguous = false;
                }

                // Move onto the next candidate.
                currentCandidateHump++;
            }
        }
            private static Words GetLongestForwardSubsequence(int length, StringBreaks breaks, string baseName)
            {
                var end = length;

                return(GetWords(0, end, breaks, baseName));
            }
            private static ImmutableArray <IEnumerable <string> > GetInterleavedPatterns(StringBreaks breaks, string baseName)
            {
                var result = ArrayBuilder <IEnumerable <string> > .GetInstance();

                result.Add(GetWords(0, breaks.Count, breaks, baseName));

                for (int length = breaks.Count - 1; length > 0; length--)
                {
                    // going forward
                    result.Add(GetLongestForwardSubsequence(length, breaks, baseName));

                    // going backward
                    result.Add(GetLongestBackwardSubsequence(length, breaks, baseName));
                }
                return(result.ToImmutable());
            }
示例#15
0
        private int?TryCamelCaseMatch(
            string candidate,
            bool includeMatchedSpans,
            StringBreaks candidateParts,
            TextChunk chunk,
            CompareOptions compareOption,
            out List <TextSpan> matchedSpans)
        {
            matchedSpans = null;
            var chunkCharacterSpans = chunk.CharacterSpans;

            // Note: we may have more pattern parts than candidate parts.  This is because multiple
            // pattern parts may match a candidate part.  For example "SiUI" against "SimpleUI".
            // We'll have 3 pattern parts Si/U/I against two candidate parts Simple/UI.  However, U
            // and I will both match in UI.

            int  currentCandidate = 0;
            int  currentChunkSpan = 0;
            int? firstMatch       = null;
            bool?contiguous       = null;

            while (true)
            {
                // Let's consider our termination cases
                if (currentChunkSpan == chunkCharacterSpans.Count)
                {
                    Contract.Requires(firstMatch.HasValue);
                    Contract.Requires(contiguous.HasValue);

                    // We did match! We shall assign a weight to this
                    int weight = 0;

                    // Was this contiguous?
                    if (contiguous.Value)
                    {
                        weight += 1;
                    }

                    // Did we start at the beginning of the candidate?
                    if (firstMatch.Value == 0)
                    {
                        weight += 2;
                    }

                    return(weight);
                }
                else if (currentCandidate == candidateParts.Count)
                {
                    // No match, since we still have more of the pattern to hit
                    matchedSpans = null;
                    return(null);
                }

                var  candidatePart            = candidateParts[currentCandidate];
                bool gotOneMatchThisCandidate = false;

                // Consider the case of matching SiUI against SimpleUIElement. The candidate parts
                // will be Simple/UI/Element, and the pattern parts will be Si/U/I.  We'll match 'Si'
                // against 'Simple' first.  Then we'll match 'U' against 'UI'. However, we want to
                // still keep matching pattern parts against that candidate part.
                for (; currentChunkSpan < chunkCharacterSpans.Count; currentChunkSpan++)
                {
                    var chunkCharacterSpan = chunkCharacterSpans[currentChunkSpan];

                    if (gotOneMatchThisCandidate)
                    {
                        // We've already gotten one pattern part match in this candidate.  We will
                        // only continue trying to consume pattern parts if the last part and this
                        // part are both upper case.
                        if (!char.IsUpper(chunk.Text[chunkCharacterSpans[currentChunkSpan - 1].Start]) ||
                            !char.IsUpper(chunk.Text[chunkCharacterSpans[currentChunkSpan].Start]))
                        {
                            break;
                        }
                    }

                    if (!PartStartsWith(candidate, candidatePart, chunk.Text, chunkCharacterSpan, compareOption))
                    {
                        break;
                    }

                    if (includeMatchedSpans)
                    {
                        matchedSpans = matchedSpans ?? new List <TextSpan>();
                        matchedSpans.Add(new TextSpan(candidatePart.Start, chunkCharacterSpan.Length));
                    }

                    gotOneMatchThisCandidate = true;

                    firstMatch = firstMatch ?? currentCandidate;

                    // If we were contiguous, then keep that value.  If we weren't, then keep that
                    // value.  If we don't know, then set the value to 'true' as an initial match is
                    // obviously contiguous.
                    contiguous = contiguous ?? true;

                    candidatePart = new TextSpan(candidatePart.Start + chunkCharacterSpan.Length, candidatePart.Length - chunkCharacterSpan.Length);
                }

                // Check if we matched anything at all.  If we didn't, then we need to unset the
                // contiguous bit if we currently had it set.
                // If we haven't set the bit yet, then that means we haven't matched anything so
                // far, and we don't want to change that.
                if (!gotOneMatchThisCandidate && contiguous.HasValue)
                {
                    contiguous = false;
                }

                // Move onto the next candidate.
                currentCandidate++;
            }
        }