Ejemplo n.º 1
0
        public static IMatchTypeStrategy StrategyFor(MatcherType t)
        {
            if (GeneratorRules.TryGetValue(t, out var r))
            {
                return(r);
            }

            return(new BasicMatchTypeStrategy());
        }
Ejemplo n.º 2
0
 public SimpleValue(object example, MatcherType match)
 {
     Example     = example;
     MatcherList = new MatcherList {
         Matchers = new List <Matcher> {
             new Matcher {
                 MatcherType = match
             }
         }
     };
 }
Ejemplo n.º 3
0
 private static bool canBeLevel(MatcherType type)
 {
     if (type == MatcherType.DateTime || type == MatcherType.Number)
     {
         return(false);
     }
     else
     {
         return(true);
     }
 }
Ejemplo n.º 4
0
        private static ISpecializedMatcherVM getInstance(MatcherType type)
        {
            switch (type)
            {
            case MatcherType.DateTime:
                return(new DateTimeFormatMatcherVM());

            case MatcherType.Number:
                return(new NumberMatcherVM());

            case MatcherType.Text:
                return(new TextMatcherVM());

            case MatcherType.Regex:
                return(new RegexMatcherVM());

            default:
                throw new System.InvalidOperationException("Invalid MatcherType!");
            }
        }
Ejemplo n.º 5
0
        private void OnSelectedMatcherChanged(MatcherType newValue)
        {
            if (MatcherVM != null)
            {
                MatcherVM.remove();
                if (MatcherVM.hasBackingMatcher())
                {
                    matcherVMRemoved = MatcherVM;
                }
            }
            MatcherVM    = getInstance(newValue);
            HasAffixes   = MatcherVM.hasAffixes();
            HasSeparator = MatcherVM.hasSeparator();

            CanBeLevel = canBeLevel(newValue);
            if (!CanBeLevel)
            {
                IsLevel = false;
            }
        }
Ejemplo n.º 6
0
        public static IAnyOfMatcher <TEntity> CreateMatcher <TContext, TEntity>(MatcherType type, params int[] indices)
            where TEntity : class, IEntity, IGenericEntity
            where TContext : IContext
        {
            Matcher <TEntity> matcher = default;

            switch (type)
            {
            case MatcherType.None:
            case MatcherType.All:
                matcher = (Matcher <TEntity>)Entitas.Matcher <TEntity> .AllOf(indices);

                matcher.componentNames = ContextHelper <TContext> .ContextInfo.componentNames;
                break;

            case MatcherType.Any:
                matcher = (Matcher <TEntity>)Entitas.Matcher <TEntity> .AnyOf(indices);

                matcher.componentNames = ContextHelper <TContext> .ContextInfo.componentNames;
                break;
            }
            return(matcher);
        }
Ejemplo n.º 7
0
        public WildcardMatcher(string pattern, bool ignoreCase = false)
        {
            m_pattern          = pattern;
            m_tokens           = null;
            m_stringComparison = ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;

            if (pattern == null || pattern.Length == 0)
            {
                m_matchType = MatcherType.Nothing;
                return;
            }

            if (pattern.Equals("*", StringComparison.Ordinal))
            {
                m_matchType = MatcherType.Everything;
                return;
            }

            // find all tokens
            int numStars    = 0;
            int curTokStart = 0;
            int curTokLen   = 0;

            while (curTokStart + curTokLen < pattern.Length)
            {
                var examineIndex = curTokStart + curTokLen;
                var c            = pattern[examineIndex];
                switch (c)
                {
                case '?':
                case '*':
                    if (m_tokens == null)
                    {
                        m_tokens = new FastList <Token>(8);
                    }

                    // end current token
                    if (curTokLen > 0)
                    {
                        ref var token = ref m_tokens.Add();
                        token.Start  = curTokStart;
                        token.Length = curTokLen;
                        token.Type   = TokenType.Exact;
                    }

                    Token qtok;
                    qtok.Start  = examineIndex;
                    qtok.Length = 1;

                    if (c == '?')
                    {
                        qtok.Type = TokenType.QuestionMark;
                        m_tokens.Add(qtok);
                    }
                    else
                    {
                        numStars++;
                        qtok.Type = TokenType.Star;

                        // remove consecutive stars
                        var cnt = m_tokens.Count;
                        if (cnt == 0 || m_tokens[cnt - 1].Type != TokenType.Star)
                        {
                            m_tokens.Add(qtok);                                     // else drop it
                        }
                    }
                    curTokStart = examineIndex + 1;
                    curTokLen   = 0;
                    break;
Ejemplo n.º 8
0
        public static MultiValueDictionary <string, FileInfo> matchByHashOrContent(List <FileInfo> candidates, MatcherType type, CancellationToken cancel)
        {
            if (!(type == MatcherType.LengthHash || type == MatcherType.LengthHashContent))
            {
                throw new ArgumentException("Can only match by lengthHash or lengthHashContent!");
            }

            // group by length
            var lengthToFiles = new MultiValueDictionary <long, FileInfo>();

            foreach (FileInfo file in candidates)
            {
                lengthToFiles.Add(file.Length, file);
            }

            // filter out one element length groups, regroup remaining by hash
            var hashToFiles = new MultiValueDictionary <string, FileInfo>();

            using (var md5 = MD5.Create())
            {
                foreach (KeyValuePair <long, HashSet <FileInfo> > entry in lengthToFiles)
                {
                    if (entry.Value.Count > 1)
                    {
                        foreach (FileInfo file in entry.Value)
                        {
                            using (var stream = File.OpenRead(file.FullName))
                            {
                                byte[] hashBytes = md5.ComputeHash(stream);
                                string hash      = md5ToString(hashBytes);

                                if (type == MatcherType.LengthHash)
                                {
                                    // in case of LengthHash, we make hash key nice for display
                                    hashToFiles.Add("Hash: " + hash, file);
                                }
                                else
                                {
                                    // in case of LengthHashContent, we don't need to becaue they are intermediate
                                    hashToFiles.Add(hash, file);
                                }
                            }
                            if (cancel.IsCancellationRequested)
                            {
                                return(null);
                            }
                        }
                    }
                }
            }
            if (type == MatcherType.LengthHash)
            {
                return(ComparisonUtils.removeOneElementGroups(hashToFiles));
            }

            // filter out one element hash groups, regroup remaining by content
            var idToFiles = new MultiValueDictionary <string, FileInfo>();

            // we use this map to be able to only compare with content groups from the same hash groups
            // (we only need to compare all files with each other inside a hash group, so we store the hierarchy)
            var hashToIds = new MultiValueDictionary <string, string>();

            int matchCount = 0;

            foreach (KeyValuePair <string, HashSet <FileInfo> > entry in hashToFiles)
            {
                if (entry.Value.Count > 1)
                {
                    foreach (FileInfo fileA in entry.Value)
                    {
                        bool foundAGroup = false;

                        if (hashToIds.ContainsKey(entry.Key))
                        {
                            foreach (string id in hashToIds[entry.Key])
                            {
                                foreach (FileInfo fileB in idToFiles[id])
                                {
                                    ContentComparisonResult comparisonResult = HashContentComparison.compareFilesByContent(fileA, fileB, cancel);
                                    if (cancel.IsCancellationRequested)
                                    {
                                        return(null);
                                    }
                                    if (comparisonResult.result)
                                    {
                                        // even though we are iterating result, we know that we are only adding an item to the hashset inside value, not a key or value directly
                                        // thats why we should be able to modify result regardles
                                        idToFiles.Add(id, fileA);
                                        foundAGroup = true;
                                    }
                                    break;
                                }
                                if (foundAGroup)
                                {
                                    break;
                                }
                            }
                        }

                        // only if we didnt find an existing group, we happily create our own group, so that later files from the same hash can join our group
                        if (!foundAGroup)
                        {
                            string key = "Id: " + matchCount++;
                            idToFiles.Add(key, fileA);
                            hashToIds.Add(entry.Key, key);
                        }
                    }
                    if (cancel.IsCancellationRequested)
                    {
                        return(null);
                    }
                }
            }

            // let's just hope there are no bugs in here, not time for testing colliding hashes and dictionary juggling
            return(ComparisonUtils.removeOneElementGroups(idToFiles));
        }
Ejemplo n.º 9
0
 public static bool CanAddTiles(this MatcherType m)
 {
     return(m == MatcherType.Basic);
 }
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            MatcherType type = (MatcherType)value;

            return(type.ToString());
        }
Ejemplo n.º 11
0
 public static string ToSerialString(this MatcherType value) => value switch
 {