示例#1
0
        private static bool ParseUPathInternal(
            string path,
            UPathParseMode parseMode,
            bool throwFormatException,
            out char?drive,
            out string uniformPath)
        {
            path  = path.Trim();
            drive = null;

            if ((path.Length >= 2) && (path[1] == ':'))
            {
                drive = char.ToUpperInvariant(path[0]);

                if (!ValidDriveChars.Contains(drive.Value))
                {
                    if (throwFormatException)
                    {
                        throw new UPathFormatException($"Invalid drive char: {drive}");
                    }

                    uniformPath = null;
                    return(false);
                }

                path = path.Substring(2, path.Length - 2);
            }

            // TODO: Parse and validate.
            uniformPath = path.Replace("\\", "/");
            return(true);
        }
示例#2
0
        public UPath(string path, UPathParseMode parseMode = UPathParseMode.AllowIncorrectFormat)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            ParseUPathInternal(path, parseMode, true, out _drive, out _uniformPath);
        }
示例#3
0
        public static bool TryParse(string path, UPathParseMode parseMode, out UPath uPath)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            if (ParseUPathInternal(path, parseMode, false, out char?drive, out string uniformPath))
            {
                uPath = new UPath(uniformPath, drive);
                return(true);
            }

            uPath = null;
            return(false);
        }
示例#4
0
        public void ParsePath(string path, UPathParseMode parseMode, string expectedPath, string expectedException)
        {
            if (expectedPath != null)
            {
                var p = new UPath(path, parseMode);
                p.ToString().Should().Be(expectedPath);

                bool result = UPath.TryParse(path, parseMode, out p);
                result.Should().BeTrue();
                p.ToString().Should().Be(expectedPath);
            }
            else
            {
                Action a = () =>
                {
                    // ReSharper disable once ObjectCreationAsStatement
                    new UPath(path, parseMode);
                };
                a.ShouldThrow <UPathFormatException>().Where(x => x.Message.StartsWith(expectedException));

                bool result = UPath.TryParse(path, parseMode, out var p);
                result.Should().BeFalse();
            }
        }