Compare() public static method

public static Compare ( Uri uri1, Uri uri2, UriComponents partsToCompare, UriFormat compareFormat, System.StringComparison comparisonType ) : int
uri1 Uri
uri2 Uri
partsToCompare UriComponents
compareFormat UriFormat
comparisonType System.StringComparison
return int
Ejemplo n.º 1
0
        /// <summary>Determines whether <paramref name="baseUri" /> is a base URI for <paramref name="relativeUri" />.</summary>
        /// <returns>true if <paramref name="baseUri" /> is a base URI for <paramref name="relativeUri" />; otherwise, false.</returns>
        /// <param name="baseUri">The base URI.</param>
        /// <param name="relativeUri">The URI to test.</param>
        protected internal virtual bool IsBaseOf(Uri baseUri, Uri relativeUri)
        {
            if (Uri.Compare(baseUri, relativeUri, UriComponents.Scheme | UriComponents.UserInfo | UriComponents.Host | UriComponents.Port, UriFormat.Unescaped, StringComparison.InvariantCultureIgnoreCase) != 0)
            {
                return(false);
            }
            string localPath = baseUri.LocalPath;
            int    length    = localPath.LastIndexOf('/') + 1;

            return(string.Compare(localPath, 0, relativeUri.LocalPath, 0, length, StringComparison.InvariantCultureIgnoreCase) == 0);
        }
Ejemplo n.º 2
0
        protected internal virtual bool IsBaseOf(Uri baseUri, Uri relativeUri)
        {
            // compare, not case sensitive, the scheme, host and port (+ user informations)
            if (Uri.Compare(baseUri, relativeUri, UriComponents.SchemeAndServer | UriComponents.UserInfo, UriFormat.Unescaped, StringComparison.InvariantCultureIgnoreCase) != 0)
            {
                return(false);
            }

            string base_string = baseUri.LocalPath;
            int    last_slash  = base_string.LastIndexOf('/') + 1;          // keep the slash

            return(String.Compare(base_string, 0, relativeUri.LocalPath, 0, last_slash, StringComparison.InvariantCultureIgnoreCase) == 0);
        }
Ejemplo n.º 3
0
        public UriTemplateMatch Match(Uri baseAddress, Uri candidate)
        {
            CheckBaseAddress(baseAddress);
            if (candidate == null)
            {
                throw new ArgumentNullException("candidate");
            }

            var us = baseAddress.LocalPath;

            if (us [us.Length - 1] != '/')
            {
                baseAddress = new Uri(baseAddress.GetComponents(UriComponents.SchemeAndServer | UriComponents.Path, UriFormat.Unescaped) + '/' + baseAddress.Query, baseAddress.IsAbsoluteUri ? UriKind.Absolute : UriKind.RelativeOrAbsolute);
            }
            if (IgnoreTrailingSlash)
            {
                us = candidate.LocalPath;
                if (us.Length > 0 && us [us.Length - 1] != '/')
                {
                    candidate = new Uri(candidate.GetComponents(UriComponents.SchemeAndServer | UriComponents.Path, UriFormat.Unescaped) + '/' + candidate.Query, candidate.IsAbsoluteUri ? UriKind.Absolute : UriKind.RelativeOrAbsolute);
                }
            }

            if (Uri.Compare(baseAddress, candidate, UriComponents.StrongAuthority, UriFormat.SafeUnescaped, StringComparison.Ordinal) != 0)
            {
                return(null);
            }

            int i = 0, c = 0;
            UriTemplateMatch m = new UriTemplateMatch();

            m.BaseUri    = baseAddress;
            m.Template   = this;
            m.RequestUri = candidate;
            var vc             = m.BoundVariables;

            string cp = Uri.UnescapeDataString(baseAddress.MakeRelativeUri(candidate).ToString());

            if (IgnoreTrailingSlash && cp [cp.Length - 1] == '/')
            {
                cp = cp.Substring(0, cp.Length - 1);
            }

            int tEndCp = cp.IndexOf('?');

            if (tEndCp >= 0)
            {
                cp = cp.Substring(0, tEndCp);
            }

            if (template.Length > 0 && template [0] == '/')
            {
                i++;
            }
            if (cp.Length > 0 && cp [0] == '/')
            {
                c++;
            }

            foreach (string name in path)
            {
                int n = StringIndexOf(template, '{' + name + '}', i);
                if (String.CompareOrdinal(cp, c, template, i, n - i) != 0)
                {
                    return(null);                    // doesn't match before current template part.
                }
                c += n - i;
                i  = n + 2 + name.Length;
                int ce = cp.IndexOf('/', c);
                if (ce < 0)
                {
                    ce = cp.Length;
                }
                string value = cp.Substring(c, ce - c);
                if (value.Length == 0)
                {
                    return(null);                    // empty => mismatch
                }
                vc [name] = value;
                m.RelativePathSegments.Add(value);
                c += value.Length;
            }
            int tEnd = template.IndexOf('?');

            if (tEnd < 0)
            {
                tEnd = template.Length;
            }
            bool wild = (template [tEnd - 1] == '*');

            if (wild)
            {
                tEnd--;
            }
            if (!wild && (cp.Length - c) != (tEnd - i) ||
                String.CompareOrdinal(cp, c, template, i, tEnd - i) != 0)
            {
                return(null);                // suffix doesn't match
            }
            if (wild)
            {
                c += tEnd - i;
                foreach (var pe in cp.Substring(c).Split(slashSep, StringSplitOptions.RemoveEmptyEntries))
                {
                    m.WildcardPathSegments.Add(pe);
                }
            }
            if (candidate.Query.Length == 0)
            {
                return(m);
            }


            string [] parameters = Uri.UnescapeDataString(candidate.Query.Substring(1)).Split('&');                // chop first '?'
            foreach (string parameter in parameters)
            {
                string [] pair = parameter.Split('=');
                m.QueryParameters.Add(pair [0], pair [1]);
                if (!query_params.ContainsKey(pair [0]))
                {
                    continue;
                }
                string templateName = query_params [pair [0]];
                vc.Add(templateName, pair [1]);
            }

            return(m);
        }