本文整理汇总了C#中UriFormat类的典型用法代码示例。如果您正苦于以下问题:C# UriFormat类的具体用法?C# UriFormat怎么用?C# UriFormat使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
UriFormat类属于命名空间,在下文中一共展示了UriFormat类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConstructorsShouldSetExpectedProperties
public void ConstructorsShouldSetExpectedProperties( UriComponents components, UriFormat format, bool ignoreCase )
{
// arrange
var target = new UriComparer( components, format, ignoreCase );
// act
// assert
Assert.Equal( components, target.UriComponents );
Assert.Equal( format, target.UriFormat );
Assert.Equal( ignoreCase, target.IgnoreCase );
}
示例2: GetComponents
// protected methods
protected internal virtual string GetComponents (Uri uri, UriComponents components, UriFormat format)
{
if ((format < UriFormat.UriEscaped) || (format > UriFormat.SafeUnescaped))
throw new ArgumentOutOfRangeException ("format");
if ((components & UriComponents.SerializationInfoString) != 0) {
if (components != UriComponents.SerializationInfoString)
throw new ArgumentOutOfRangeException ("components", "UriComponents.SerializationInfoString must not be combined with other UriComponents.");
if (!uri.IsAbsoluteUri)
return UriHelper.FormatRelative (uri.OriginalString, "", format);
components |= UriComponents.AbsoluteUri;
}
return GetComponentsHelper (uri, components, format);
}
示例3: GetComponentsHelper
internal string GetComponentsHelper(Uri uri, UriComponents components, UriFormat format)
{
UriElements elements = UriParseComponents.ParseComponents(uri.OriginalString.Trim(), UriKind.Absolute);
string scheme = scheme_name;
int dp = default_port;
if ((scheme == null) || (scheme == "*"))
{
scheme = elements.scheme;
dp = Uri.GetDefaultPort(scheme);
}
else if (String.Compare(scheme.ToLower(), elements.scheme.ToLower()) != 0)
{
throw new SystemException("URI Parser: scheme mismatch: " + scheme + " vs. " + elements.scheme);
}
var formatFlags = UriHelper.FormatFlags.None;
if (UriHelper.HasCharactersToNormalize(uri.OriginalString))
formatFlags |= UriHelper.FormatFlags.HasUriCharactersToNormalize;
if (uri.UserEscaped)
formatFlags |= UriHelper.FormatFlags.UserEscaped;
if (!StringUtilities.IsNullOrEmpty(elements.host))
formatFlags |= UriHelper.FormatFlags.HasHost;
// it's easier to answer some case directly (as the output isn't identical
// when mixed with others components, e.g. leading slash, # ...)
switch (components)
{
case UriComponents.Scheme:
return scheme;
case UriComponents.UserInfo:
return elements.user ?? "";
case UriComponents.Host:
return elements.host;
case UriComponents.Port:
{
int p = elements.port;
if (p >= 0 && p != dp)
return p.ToString();
return String.Empty;
}
case UriComponents.Path:
var path = elements.path;
if (scheme != Uri.UriSchemeMailto && scheme != Uri.UriSchemeNews)
path = IgnoreFirstCharIf(elements.path, '/');
return UriHelper.FormatAbsolute(path, scheme, UriComponents.Path, format, formatFlags);
case UriComponents.Query:
return UriHelper.FormatAbsolute(elements.query, scheme, UriComponents.Query, format, formatFlags);
case UriComponents.Fragment:
return UriHelper.FormatAbsolute(elements.fragment, scheme, UriComponents.Fragment, format, formatFlags);
case UriComponents.StrongPort:
{
return elements.port >= 0
? elements.port.ToString()
: dp.ToString();
}
case UriComponents.SerializationInfoString:
components = UriComponents.AbsoluteUri;
break;
}
// now we deal with multiple flags...
StringBuilder sb = new StringBuilder();
if ((components & UriComponents.Scheme) != 0)
{
sb.Append(scheme);
sb.Append(elements.delimiter);
}
if ((components & UriComponents.UserInfo) != 0)
{
string userinfo = elements.user;
if (userinfo != null)
{
sb.Append(elements.user);
sb.Append('@');
}
}
if ((components & UriComponents.Host) != 0)
sb.Append(elements.host);
// for StrongPort always show port - even if -1
// otherwise only display if ut's not the default port
if ((components & UriComponents.StrongPort) != 0)
{
sb.Append(":");
if (elements.port >= 0)
{
sb.Append(elements.port);
}
else
{
sb.Append(dp);
}
//.........这里部分代码省略.........
示例4: GetComponents
public string GetComponents (UriComponents components, UriFormat format)
{
return Parser.GetComponents (this, components, format);
}
示例5: Compare
//
//
// This is for languages that do not support == != operators overloading
//
// Note that Uri.Equals will get an optimized path but is limited to true/fasle result only
//
public static int Compare(Uri uri1, Uri uri2, UriComponents partsToCompare, UriFormat compareFormat,
StringComparison comparisonType)
{
if ((object) uri1 == null)
{
if (uri2 == null)
return 0; // Equal
return -1; // null < non-null
}
if ((object) uri2 == null)
return 1; // non-null > null
// a relative uri is always less than an absolute one
if (!uri1.IsAbsoluteUri || !uri2.IsAbsoluteUri)
return uri1.IsAbsoluteUri? 1: uri2.IsAbsoluteUri? -1: string.Compare(uri1.OriginalString,
uri2.OriginalString, comparisonType);
return string.Compare(
uri1.GetParts(partsToCompare, compareFormat),
uri2.GetParts(partsToCompare, compareFormat),
comparisonType
);
}
示例6: GetComponentsHelper
//
// UriParser helpers methods
//
internal string GetComponentsHelper(UriComponents uriComponents, UriFormat uriFormat)
{
if (uriComponents == UriComponents.Scheme)
return m_Syntax.SchemeName;
// A serialzation info is "almost" the same as AbsoluteUri except for IPv6 + ScopeID hostname case
if ((uriComponents & UriComponents.SerializationInfoString) != 0)
uriComponents |= UriComponents.AbsoluteUri;
//This will get all the offsets, HostString will be created below if needed
EnsureParseRemaining();
if ((uriComponents & UriComponents.NormalizedHost) != 0)
{
// Down the path we rely on Host to be ON for NormalizedHost
uriComponents |= UriComponents.Host;
}
//Check to see if we need the host/authotity string
if ((uriComponents & UriComponents.Host) != 0)
EnsureHostString(true);
//This, single Port request is always processed here
if (uriComponents == UriComponents.Port || uriComponents == UriComponents.StrongPort)
{
if (((m_Flags & Flags.NotDefaultPort) != 0) || (uriComponents == UriComponents.StrongPort
&& m_Syntax.DefaultPort != UriParser.NoDefaultPort))
{
// recreate string from the port value
return m_Info.Offset.PortValue.ToString(CultureInfo.InvariantCulture);
}
return string.Empty;
}
if ((uriComponents & UriComponents.StrongPort) != 0)
{
// Down the path we rely on Port to be ON for StrongPort
uriComponents |= UriComponents.Port;
}
//This request sometime is faster to process here
if (uriComponents == UriComponents.Host && (uriFormat == UriFormat.UriEscaped
|| (( m_Flags & (Flags.HostNotCanonical | Flags.E_HostNotCanonical)) == 0)))
{
EnsureHostString(false);
return m_Info.Host;
}
switch (uriFormat)
{
case UriFormat.UriEscaped:
return GetEscapedParts(uriComponents);
case V1ToStringUnescape:
case UriFormat.SafeUnescaped:
case UriFormat.Unescaped:
return GetUnescapedParts(uriComponents, uriFormat);
default:
throw new ArgumentOutOfRangeException("uriFormat");
}
}
示例7: GetComponents
public string GetComponents (UriComponents components, UriFormat format)
{
if ((components & UriComponents.SerializationInfoString) == 0)
EnsureAbsoluteUri ();
return Parser.GetComponents (this, components, format);
}
示例8: ReCreateParts
private string ReCreateParts(UriComponents parts, ushort nonCanonical, UriFormat formatAs)
{
ushort num3;
this.EnsureHostString(false);
string input = ((parts & UriComponents.Host) == 0) ? string.Empty : this.m_Info.Host;
int destinationIndex = (this.m_Info.Offset.End - this.m_Info.Offset.User) * ((formatAs == UriFormat.UriEscaped) ? 12 : 1);
char[] destination = new char[(((input.Length + destinationIndex) + this.m_Syntax.SchemeName.Length) + 3) + 1];
destinationIndex = 0;
if ((parts & UriComponents.Scheme) != 0)
{
this.m_Syntax.SchemeName.CopyTo(0, destination, destinationIndex, this.m_Syntax.SchemeName.Length);
destinationIndex += this.m_Syntax.SchemeName.Length;
if (parts != UriComponents.Scheme)
{
destination[destinationIndex++] = ':';
if (this.InFact(Flags.AuthorityFound))
{
destination[destinationIndex++] = '/';
destination[destinationIndex++] = '/';
}
}
}
if (((parts & UriComponents.UserInfo) != 0) && this.InFact(Flags.HasUserInfo))
{
if ((nonCanonical & 2) == 0)
{
UnescapeString(this.m_String, this.m_Info.Offset.User, this.m_Info.Offset.Host, destination, ref destinationIndex, 0xffff, 0xffff, 0xffff, UnescapeMode.CopyOnly, this.m_Syntax, false, false);
}
else
{
switch (formatAs)
{
case UriFormat.UriEscaped:
if (!this.NotAny(Flags.HostNotParsed | Flags.UserEscaped))
{
this.InFact(Flags.E_UserNotCanonical);
this.m_String.CopyTo(this.m_Info.Offset.User, destination, destinationIndex, this.m_Info.Offset.Host - this.m_Info.Offset.User);
destinationIndex += this.m_Info.Offset.Host - this.m_Info.Offset.User;
break;
}
destination = EscapeString(this.m_String, this.m_Info.Offset.User, this.m_Info.Offset.Host, destination, ref destinationIndex, true, '?', '#', '%');
break;
case UriFormat.Unescaped:
destination = UnescapeString(this.m_String, this.m_Info.Offset.User, this.m_Info.Offset.Host, destination, ref destinationIndex, 0xffff, 0xffff, 0xffff, UnescapeMode.UnescapeAll | UnescapeMode.Unescape, this.m_Syntax, false, false);
break;
case UriFormat.SafeUnescaped:
destination = UnescapeString(this.m_String, this.m_Info.Offset.User, this.m_Info.Offset.Host - 1, destination, ref destinationIndex, '@', '/', '\\', this.InFact(Flags.HostNotParsed | Flags.UserEscaped) ? UnescapeMode.Unescape : UnescapeMode.EscapeUnescape, this.m_Syntax, false, false);
destination[destinationIndex++] = '@';
break;
default:
destination = UnescapeString(this.m_String, this.m_Info.Offset.User, this.m_Info.Offset.Host, destination, ref destinationIndex, 0xffff, 0xffff, 0xffff, UnescapeMode.CopyOnly, this.m_Syntax, false, false);
break;
}
}
if (parts == UriComponents.UserInfo)
{
destinationIndex--;
}
}
if (((parts & UriComponents.Host) != 0) && (input.Length != 0))
{
UnescapeMode copyOnly;
if (((formatAs != UriFormat.UriEscaped) && (this.HostType == Flags.BasicHostType)) && ((nonCanonical & 4) != 0))
{
copyOnly = (formatAs == UriFormat.Unescaped) ? (UnescapeMode.UnescapeAll | UnescapeMode.Unescape) : (this.InFact(Flags.HostNotParsed | Flags.UserEscaped) ? UnescapeMode.Unescape : UnescapeMode.EscapeUnescape);
}
else
{
copyOnly = UnescapeMode.CopyOnly;
}
destination = UnescapeString(input, 0, input.Length, destination, ref destinationIndex, '/', '?', '#', copyOnly, this.m_Syntax, false, false);
if ((((parts & UriComponents.SerializationInfoString) != 0) && (this.HostType == (Flags.HostNotParsed | Flags.IPv6HostType))) && (this.m_Info.ScopeId != null))
{
this.m_Info.ScopeId.CopyTo(0, destination, destinationIndex - 1, this.m_Info.ScopeId.Length);
destinationIndex += this.m_Info.ScopeId.Length;
destination[destinationIndex - 1] = ']';
}
}
if ((parts & UriComponents.Port) != 0)
{
if ((nonCanonical & 8) == 0)
{
if (this.InFact(Flags.HostNotParsed | Flags.NotDefaultPort))
{
ushort path = this.m_Info.Offset.Path;
while (this.m_String[path = (ushort) (path - 1)] != ':')
{
}
this.m_String.CopyTo(path, destination, destinationIndex, this.m_Info.Offset.Path - path);
destinationIndex += this.m_Info.Offset.Path - path;
}
else if (((parts & UriComponents.StrongPort) != 0) && (this.m_Syntax.DefaultPort != -1))
{
destination[destinationIndex++] = ':';
input = this.m_Info.Offset.PortValue.ToString(CultureInfo.InvariantCulture);
input.CopyTo(0, destination, destinationIndex, input.Length);
destinationIndex += input.Length;
//.........这里部分代码省略.........
示例9: NeedToUnescape
private static bool NeedToUnescape (char c, UriSchemes scheme, UriComponents component, UriKind uriKind,
UriFormat uriFormat, FormatFlags formatFlags)
{
if ((formatFlags & FormatFlags.IPv6Host) != 0)
return false;
if (uriFormat == UriFormat.Unescaped)
return true;
UriSchemes sDecoders = UriSchemes.NetPipe | UriSchemes.NetTcp;
if (!IriParsing)
sDecoders |= UriSchemes.Http | UriSchemes.Https;
if (c == '/' || c == '\\') {
if (!IriParsing && uriKind == UriKind.Absolute && uriFormat != UriFormat.UriEscaped &&
uriFormat != UriFormat.SafeUnescaped)
return true;
if (SchemeContains (scheme, UriSchemes.File)) {
return component != UriComponents.Fragment &&
(component != UriComponents.Query || !IriParsing);
}
return component != UriComponents.Query && component != UriComponents.Fragment &&
SchemeContains (scheme, sDecoders);
}
if (c == '?') {
//Avoid creating new query
if (SupportsQuery (scheme) && component == UriComponents.Path)
return false;
if (!IriParsing && uriFormat == ToStringUnescape) {
if (SupportsQuery (scheme))
return component == UriComponents.Query || component == UriComponents.Fragment;
return component == UriComponents.Fragment;
}
return false;
}
if (c == '#')
return false;
if (uriFormat == ToStringUnescape && !IriParsing) {
if (uriKind == UriKind.Relative)
return false;
switch (c) {
case '$':
case '&':
case '+':
case ',':
case ';':
case '=':
case '@':
return true;
}
if (c < 0x20 || c == 0x7f)
return true;
}
if (uriFormat == UriFormat.SafeUnescaped || uriFormat == ToStringUnescape) {
switch (c) {
case '-':
case '.':
case '_':
case '~':
return true;
case ' ':
case '!':
case '"':
case '\'':
case '(':
case ')':
case '*':
case '<':
case '>':
case '^':
case '`':
case '{':
case '}':
case '|':
return uriKind != UriKind.Relative ||
(IriParsing && (formatFlags & FormatFlags.HasUriCharactersToNormalize) != 0);
case ':':
case '[':
case ']':
return uriKind != UriKind.Relative;
}
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9'))
return true;
if (c > 0x7f)
return true;
//.........这里部分代码省略.........
示例10: FormatChar
private static string FormatChar (char c, char surrogate, string cStr, UriSchemes scheme, UriKind uriKind,
UriComponents component, UriFormat uriFormat, FormatFlags formatFlags)
{
var isEscaped = cStr.Length != 1;
var userEscaped = (formatFlags & FormatFlags.UserEscaped) != 0;
if (!isEscaped && !userEscaped && NeedToEscape (c, scheme, component, uriKind, uriFormat, formatFlags))
return HexEscapeMultiByte (c);
if (isEscaped && (
(userEscaped && c < 0xFF) ||
!NeedToUnescape (c, scheme, component, uriKind, uriFormat, formatFlags))) {
if (IriParsing &&
(c == '<' || c == '>' || c == '^' || c == '{' || c == '|' || c == '}' || c > 0x7F) &&
(formatFlags & FormatFlags.HasUriCharactersToNormalize) != 0)
return cStr.ToUpperInvariant (); //Upper case escape
return cStr; //Keep original case
}
if ((formatFlags & FormatFlags.NoSlashReplace) == 0 &&
c == '\\' && component == UriComponents.Path) {
if (!IriParsing && uriFormat != UriFormat.UriEscaped &&
SchemeContains (scheme, UriSchemes.Http | UriSchemes.Https))
return "/";
if (SchemeContains (scheme, UriSchemes.Http | UriSchemes.Https | UriSchemes.Ftp | UriSchemes.CustomWithHost))
return (isEscaped && uriFormat != UriFormat.UriEscaped) ? "\\" : "/";
if (SchemeContains (scheme, UriSchemes.NetPipe | UriSchemes.NetTcp | UriSchemes.File))
return "/";
if (SchemeContains (scheme, UriSchemes.Custom) &&
(formatFlags & FormatFlags.HasWindowsPath) == 0)
return "/";
}
var ret = c.ToString (CultureInfo.InvariantCulture);
if (surrogate != char.MinValue)
ret += surrogate.ToString (CultureInfo.InvariantCulture);
return ret;
}
示例11: FormatString
private static string FormatString (string str, UriSchemes scheme, UriKind uriKind,
UriComponents component, UriFormat uriFormat, FormatFlags formatFlags)
{
var s = new StringBuilder ();
int len = str.Length;
for (int i = 0; i < len; i++) {
char c = str [i];
if (c == '%') {
int iStart = i;
char surrogate;
bool invalidUnescape;
char x = Uri.HexUnescapeMultiByte (str, ref i, out surrogate, out invalidUnescape);
if (invalidUnescape
) {
s.Append (c);
i = iStart;
continue;
}
string cStr = str.Substring (iStart, i-iStart);
s.Append (FormatChar (x, surrogate, cStr, scheme, uriKind, component, uriFormat, formatFlags));
i--;
} else
s.Append (FormatChar (c, char.MinValue, "" + c, scheme, uriKind, component, uriFormat, formatFlags));
}
return s.ToString ();
}
示例12: Format
private static string Format (string str, string schemeName, UriKind uriKind,
UriComponents component, UriFormat uriFormat, FormatFlags formatFlags)
{
if (string.IsNullOrEmpty (str))
return "";
if (UriHelper.HasCharactersToNormalize (str))
formatFlags |= UriHelper.FormatFlags.HasComponentCharactersToNormalize | FormatFlags.HasUriCharactersToNormalize;
if (component == UriComponents.Fragment && UriHelper.HasPercentage (str))
formatFlags |= UriHelper.FormatFlags.HasFragmentPercentage;
if (component == UriComponents.Host &&
str.Length > 1 && str [0] == '[' && str [str.Length - 1] == ']')
formatFlags |= UriHelper.FormatFlags.IPv6Host;
if (component == UriComponents.Path &&
str.Length >= 2 && str [1] != ':' &&
('a' <= str [0] && str [0] <= 'z') || ('A' <= str [0] && str [0] <= 'Z'))
formatFlags |= UriHelper.FormatFlags.HasWindowsPath;
UriSchemes scheme = GetScheme (schemeName);
if (scheme == UriSchemes.Custom && (formatFlags & FormatFlags.HasHost) != 0)
scheme = UriSchemes.CustomWithHost;
var reduceAfter = UriSchemes.Http | UriSchemes.Https | UriSchemes.File | UriSchemes.NetPipe | UriSchemes.NetTcp;
if (IriParsing) {
reduceAfter |= UriSchemes.Ftp;
} else if (component == UriComponents.Path &&
(formatFlags & FormatFlags.NoSlashReplace) == 0) {
if (scheme == UriSchemes.Ftp)
str = Reduce (str.Replace ('\\', '/'), !IriParsing);
if (scheme == UriSchemes.CustomWithHost)
str = Reduce (str.Replace ('\\', '/'), false);
}
str = FormatString (str, scheme, uriKind, component, uriFormat, formatFlags);
if (component == UriComponents.Path &&
(formatFlags & FormatFlags.NoReduce) == 0) {
if (SchemeContains (scheme, reduceAfter))
str = Reduce (str, !IriParsing);
if (IriParsing && scheme == UriSchemes.CustomWithHost)
str = Reduce (str, false);
}
return str;
}
示例13: FormatRelative
internal static string FormatRelative (string str, string schemeName, UriFormat uriFormat)
{
return Format (str, schemeName, UriKind.Relative, UriComponents.Path, uriFormat, FormatFlags.None);
}
示例14: FormatAbsolute
internal static string FormatAbsolute (string str, string schemeName,
UriComponents component, UriFormat uriFormat, FormatFlags formatFlags = FormatFlags.None)
{
return Format (str, schemeName, UriKind.Absolute, component, uriFormat, formatFlags);
}
示例15: GetRelativeSerializationString
private string GetRelativeSerializationString(UriFormat format)
{
if (format == UriFormat.UriEscaped)
{
if (this.m_String.Length == 0)
{
return string.Empty;
}
int destPos = 0;
char[] chArray = EscapeString(this.m_String, 0, this.m_String.Length, null, ref destPos, true, 0xffff, 0xffff, '%');
if (chArray == null)
{
return this.m_String;
}
return new string(chArray, 0, destPos);
}
if (format == UriFormat.Unescaped)
{
return UnescapeDataString(this.m_String);
}
if (format != UriFormat.SafeUnescaped)
{
throw new ArgumentOutOfRangeException("format");
}
if (this.m_String.Length == 0)
{
return string.Empty;
}
char[] dest = new char[this.m_String.Length];
int length = 0;
return new string(UnescapeString(this.m_String, 0, this.m_String.Length, dest, ref length, 0xffff, 0xffff, 0xffff, UnescapeMode.EscapeUnescape, null, false, true), 0, length);
}