Example #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));
        }
Example #2
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);
        }
Example #3
0
        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();
            }));
        }