Exemple #1
0
        private static ITestEntry CreateTestEntry(string value, RegexFactory regexFactory, StringMatchMode mode)
        {
            if (value.Length > 1)
            {
                if (value[0] == '"' && value[value.Length - 1] == '"')
                {
                    return(new StringTestEntry(value.Substring(1, value.Length - 2), mode));
                }

                if (value[0] == '\'' && value[value.Length - 1] == '\'')
                {
                    return(new StringTestEntry(value.Substring(1, value.Length - 2), StringMatchMode.CompleteMatch));
                }

                if (value[0] == '`' && value[value.Length - 1] == '`')
                {
                    try {
                        var regex = new Regex(value.Substring(1, value.Length - 2), RegexOptions.Compiled | RegexOptions.IgnoreCase);
                        return(new RegexTestEntry(regex));
                    } catch (Exception) {
                        return(new ConstTestEntry(false));
                    }
                }
            }

            if (RegexFromQuery.IsQuery(value))
            {
                return(new RegexTestEntry(regexFactory(value, mode)));
            }

            return(new StringTestEntry(value, mode));
        }
        private static ITestEntry CreateTestEntry(string value, bool wholeMatch, bool strictMode)
        {
            if (value.Length > 1)
            {
                if (value[0] == '"' && value[value.Length - 1] == '"')
                {
                    return(new StringTestEntry(value.Substring(1, value.Length - 2), wholeMatch, true));
                }

                if (value[0] == '`' && value[value.Length - 1] == '`')
                {
                    try {
                        var regex = new Regex(value.Substring(1, value.Length - 2), RegexOptions.Compiled | RegexOptions.IgnoreCase);
                        return(new RegexTestEntry(regex));
                    } catch (Exception) {
                        return(new ConstTestEntry(false));
                    }
                }
            }

            if (value.Contains("*") || value.Contains("?"))
            {
                return(new RegexTestEntry(RegexFromQuery.Create(value, wholeMatch, strictMode)));
            }

            return(new StringTestEntry(value, wholeMatch, strictMode));
        }
Exemple #3
0
        public IEnumerable <string> GetKeys(string glob)
        {
            if (_originFilename == null)
            {
                return(new string[0]);
            }

            if (_dataList == null)
            {
                var directory = _originFilename;
                var dataList  = new List <string>();
                if (Directory.Exists(directory))
                {
                    dataList.AddRange(Directory.GetFiles(directory, "*", SearchOption.AllDirectories));
                    for (var i = 0; i < dataList.Count; i++)
                    {
                        dataList[i] = FileUtils.GetRelativePath(dataList[i], directory).Replace('\\', '/');
                    }

                    var fromArchive = GetDataArchive()?.Entries.Select(x => x.FullName);
                    if (fromArchive != null)
                    {
                        dataList.AddRange(fromArchive);
                    }
                }

                _dataList = dataList.ToArray();
            }

            var regex = RegexFromQuery.Create(glob, StringMatchMode.CompleteMatch);

            return(_dataList.Where(x => regex.IsMatch(x)));
        }
Exemple #4
0
        private static PaintableItem FindQuery([NotNull] Kn5 kn5, Func <string, PaintableItem> prepare, params string[] query)
        {
            var texture = query.Select(x => RegexFromQuery.IsQuery(x)
                    ? kn5.Textures.Keys.FirstOrDefault(RegexFromQuery.Create(x, StringMatchMode.CompleteMatch, false).IsMatch)
                    : kn5.Textures.Keys.Contains(x) ? x : null).FirstOrDefault(x => x != null);

            return(texture != null?prepare(texture) : null);
        }
            private IEnumerable <string> GetFiles(string mask)
            {
                var location = _current.Location;

                if (_subFiles == null)
                {
                    _subFiles = Directory.GetFiles(location, "*", SearchOption.AllDirectories)
                                .Select(x => FileUtils.GetRelativePath(x, location)).ToArray();
                }

                var f = RegexFromQuery.Create(mask.Replace('/', '\\'), true, true);

                return(_subFiles.Where(x => f.IsMatch(x)).Select(x => Path.Combine(location, x)));
            }
        private static ITestEntry CreateTestEntry(string value, [NotNull] FilterParams filterParams)
        {
            var customEntry = filterParams.CustomTestEntryFactory?.Invoke(value);

            if (customEntry != null)
            {
                return(customEntry);
            }

            if (value.Length > 1)
            {
                if (value[0] == '"' && value[value.Length - 1] == '"')
                {
                    return(new StringTestEntry(value.Substring(1, value.Length - 2), filterParams.StringMatchMode, filterParams.CaseInvariant));
                }

                if (value[0] == '\'' && value[value.Length - 1] == '\'')
                {
                    return(new StringTestEntry(value.Substring(1, value.Length - 2), StringMatchMode.CompleteMatch, filterParams.CaseInvariant));
                }

                if (value[0] == '`' && value[value.Length - 1] == '`')
                {
                    try {
                        var regex = new Regex(value.Substring(1, value.Length - 2),
                                              filterParams.CaseInvariant ? RegexOptions.Compiled | RegexOptions.IgnoreCase : RegexOptions.Compiled);
                        return(new RegexTestEntry(regex));
                    } catch (Exception) {
                        return(new ConstTestEntry(false));
                    }
                }
            }

            if (RegexFromQuery.IsQuery(value))
            {
                return(new RegexTestEntry(filterParams.RegexFactory(value, filterParams.StringMatchMode)));
            }

            return(new StringTestEntry(value, filterParams.StringMatchMode, filterParams.CaseInvariant));
        }
        // More information, tests and usage examples:
        // https://gist.github.com/gro-ove/a9e946ce271f89fe5b5f537c1b50c0c8?ts=4
        // TODO: Move tests somewhere nearby
        private IEnumerable <string> GetFiles(string query, string relativeQueryOrigin, bool optionalExtension)
        {
            var normalized = FileUtils.NormalizePath(Path.IsPathRooted(query) ? query : Path.Combine(relativeQueryOrigin, query));
            var pieces     = normalized.Split('\\').Select(x => new { Value = x, IsQuery = RegexFromQuery.IsQuery(x) }).ToArray();

            return(pieces.All(x => !x.IsQuery) || pieces.Length < 2
                    ? File.Exists(normalized)
                            ? new[] { normalized }
                            : optionalExtension
                                    ? Directory.GetFiles(Path.GetDirectoryName(normalized) ?? @".", Path.GetFileName(normalized) + ".*")
                                    : new string[0]
                    : pieces.Skip(1).Aggregate(
                       pieces[0].IsQuery
                                    ? DriveInfo.GetDrives().Select(x => x.RootDirectory.FullName).ToArray()
                                    : new[] { pieces[0].Value + Path.DirectorySeparatorChar },
                       (loc, piece) => {
                var isLast = piece == pieces[pieces.Length - 1];
                return (piece.IsQuery
                                        ? loc.SelectMany(x => isLast ? Directory.GetFiles(x, piece.Value) : Directory.GetDirectories(x, piece.Value))
                                        : loc.Select(x => Path.Combine(x, piece.Value)).Where(x => isLast ? File.Exists(x) : Directory.Exists(x))).ToArray();
            }));
        }