-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSmallUri.cs
155 lines (128 loc) · 4.35 KB
/
SmallUri.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
namespace Standard
{
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Text;
[DebuggerDisplay("SmallUri: { GetUri() }")]
internal struct SmallUri : IEquatable<SmallUri>
{
private static readonly UTF8Encoding s_Encoder = new UTF8Encoding(false /* do not emit BOM */, true /* throw on error */);
private readonly byte[] _utf8String;
private readonly bool _isHttp;
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
public SmallUri(Uri value)
{
_isHttp = false;
_utf8String = null;
if (value == null)
{
return;
}
if (!value.IsAbsoluteUri)
{
throw new ArgumentException("The parameter is not a valid absolute uri", "value");
}
string strValue = value.OriginalString;
if (strValue.StartsWith("http://", StringComparison.OrdinalIgnoreCase))
{
_isHttp = true;
strValue = strValue.Substring(7);
}
_utf8String = s_Encoder.GetBytes(strValue);
Assert.IsNotNull(_utf8String);
}
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
public SmallUri(string value)
{
_isHttp = false;
_utf8String = null;
if (string.IsNullOrEmpty(value))
{
return;
}
if (!Uri.IsWellFormedUriString(value, UriKind.Absolute))
{
throw new ArgumentException("The parameter is not a valid uri", "value");
}
if (value.StartsWith("http://", StringComparison.OrdinalIgnoreCase))
{
_isHttp = true;
value = value.Substring(7);
}
_utf8String = s_Encoder.GetBytes(value);
Assert.IsNotNull(_utf8String);
}
#region Object Overrides
[SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "GetString")]
public override string ToString()
{
Assert.Fail();
throw new NotSupportedException("This exception exists to prevent accidental performance penalties. Call GetString() instead.");
}
public override int GetHashCode()
{
// Intentionally hashes similarly to the expanded strings.
return GetString().GetHashCode();
}
public override bool Equals(object obj)
{
try
{
return Equals((SmallUri)obj);
}
catch (InvalidCastException)
{
return false;
}
}
#endregion
#region IEquatable<SmallUri> Members
public bool Equals(SmallUri other)
{
if (_utf8String == null)
{
return other._utf8String == null;
}
if (other._utf8String == null)
{
return false;
}
if (_isHttp != other._isHttp)
{
return false;
}
if (_utf8String.Length != other._utf8String.Length)
{
return false;
}
return Utility.MemCmp(_utf8String, other._utf8String, _utf8String.Length);
}
#endregion
public string GetString()
{
if (_utf8String == null)
{
return "";
}
return GetUri().ToString();
}
public Uri GetUri()
{
if (_utf8String == null)
{
return null;
}
return new Uri((_isHttp ? "http://" : "") + s_Encoder.GetString(_utf8String), UriKind.Absolute);
}
public static bool operator ==(SmallUri left, SmallUri right)
{
return left.Equals(right);
}
public static bool operator !=(SmallUri left, SmallUri right)
{
return !left.Equals(right);
}
}
}