コード例 #1
0
        public void MatcherTest()
        {
            var glob  = "C:/Projects/editorconfig-core-net/tests/filetree/top/of/path";
            var file  = "C:/Projects/editorconfig-core-net/tests/filetree/top/of/path";
            var m     = new EditorConfigMinimatcher(glob, _globOptions);
            var match = m.IsMatch(file);

            match.Should().BeTrue();
        }
        internal static bool MatchHeading(string heading, string path, NPath currentDirectory)
        {
            if (!IsSupportedHeading(heading))
            {
                var options = new EditorConfigMinimatcherOptions()
                {
                    //AllowWindowsPaths = true,
                    MatchBase = true,
                    Dot       = true,
                    NoExt     = true
                };
                var fixedGlob    = FixGlobForEditorConfigMinimatcher(heading, currentDirectory);
                var minimatcher  = new EditorConfigMinimatcher(fixedGlob, options);
                var fullFilename = currentDirectory.Combine(path);
                return(minimatcher.IsMatch(fullFilename.ToString(SlashMode.Forward)));
            }

            int  headingStart = 0;
            bool negate       = false;

            while (headingStart < heading.Length && heading[headingStart] == '!')
            {
                negate = !negate;
                headingStart++;
            }

            var firstSlashIndex = heading.IndexOf('/');

            //skip initial '/', which is redundant
            if (firstSlashIndex == headingStart)
            {
                headingStart++;
            }

            if (headingStart == heading.Length)
            {
                return(false);
            }

            if (firstSlashIndex == -1)
            {
                //If there is no '/' we allow a match on just the filename
                heading      = "**/" + heading.Substring(headingStart);
                headingStart = 0;
            }

            var isMatch = MatchHeading(heading, headingStart, path, 0);

            if (negate)
            {
                isMatch = !isMatch;
            }

            return(isMatch);
        }