Code Coverage Statistics for Source File
Newtonsoft.Json\Utilities\MiscellaneousUtils.cs
Symbol Coverage: 89.83% (53 of 59)
Branch Coverage: 93.10% (27 of 29)
Cyclomatic Complexity Avg: 2.20 Max:4
Code Lines: 53
Symbol Coverage Trend
View:
L | V | Source |
---|---|---|
1 |
using System; |
|
2 |
using System.Collections; |
|
3 |
using System.Collections.Generic; |
|
4 |
using System.ComponentModel; |
|
5 |
using System.Reflection; |
|
6 |
using System.Text; |
|
7 |
using System.Globalization; |
|
8 |
||
9 |
namespace Newtonsoft.Json.Utilities |
|
10 |
{ |
|
11 |
internal delegate T Creator<T>(); |
|
12 |
||
13 |
internal static class MiscellaneousUtils |
|
14 |
{ |
|
15 |
public static ArgumentOutOfRangeException CreateArgumentOutOfRangeException(string paramName, object actualValue, string message) |
|
16 |
{ |
|
17 |
0 |
string newMessage = message + Environment.NewLine + @"Actual value was {0}.".FormatWith(CultureInfo.InvariantCulture, actualValue);
|
18 |
||
19 |
0 |
return new ArgumentOutOfRangeException(paramName, newMessage);
|
20 |
0 |
}
|
21 |
||
22 |
public static bool TryAction<T>(Creator<T> creator, out T output) |
|
23 |
{ |
|
24 |
40239 |
ValidationUtils.ArgumentNotNull(creator, "creator");
|
25 |
||
26 |
try |
|
27 |
{ |
|
28 |
40239 |
output = creator();
|
29 |
40233 |
return true;
|
30 |
} |
|
31 |
6 |
catch
|
32 |
{ |
|
33 |
6 |
output = default(T);
|
34 |
6 |
return false;
|
35 |
} |
|
36 |
40239 |
}
|
37 |
||
38 |
public static string ToString(object value) |
|
39 |
{ |
|
40 |
6 |
if (value == null)
|
41 |
0 |
return "{null}";
|
42 |
||
43 |
6 |
return (value is string) ? @"""" + value.ToString() + @"""" : value.ToString();
|
44 |
6 |
}
|
45 |
||
46 |
public static byte[] HexToBytes(string hex) |
|
47 |
{ |
|
48 |
33 |
string fixedHex = hex.Replace("-", string.Empty);
|
49 |
||
50 |
// array to put the result in |
|
51 |
33 |
byte[] bytes = new byte[fixedHex.Length / 2];
|
52 |
// variable to determine shift of high/low nibble |
|
53 |
33 |
int shift = 4;
|
54 |
// offset of the current byte in the array |
|
55 |
33 |
int offset = 0;
|
56 |
// loop the characters in the string |
|
57 |
33 |
foreach (char c in fixedHex)
|
58 |
{ |
|
59 |
// get character code in range 0-9, 17-22 |
|
60 |
// the % 32 handles lower case characters |
|
61 |
10628 |
int b = (c - '0') % 32;
|
62 |
// correction for a-f |
|
63 |
1460 |
if (b > 9) b -= 7;
|
64 |
// store nibble (4 bits) in byte array |
|
65 |
10628 |
bytes[offset] |= (byte)(b << shift);
|
66 |
// toggle the shift variable between 0 and 4 |
|
67 |
10628 |
shift ^= 4;
|
68 |
// move to next byte |
|
69 |
5314 |
if (shift != 0) offset++;
|
70 |
} |
|
71 |
33 |
return bytes;
|
72 |
33 |
}
|
73 |
||
74 |
public static string BytesToHex(byte[] bytes) |
|
75 |
{ |
|
76 |
11 |
return BytesToHex(bytes, false);
|
77 |
11 |
}
|
78 |
||
79 |
public static string BytesToHex(byte[] bytes, bool removeDashes) |
|
80 |
{ |
|
81 |
11 |
string hex = BitConverter.ToString(bytes);
|
82 |
11 |
if (removeDashes)
|
83 |
0 |
hex = hex.Replace("-", "");
|
84 |
||
85 |
11 |
return hex;
|
86 |
11 |
}
|
87 |
||
88 |
public static bool ByteArrayCompare(byte[] a1, byte[] a2) |
|
89 |
{ |
|
90 |
4 |
if (a1.Length != a2.Length)
|
91 |
1 |
return false;
|
92 |
||
93 |
3 |
for (int i = 0; i < a1.Length; i++) |
94 |
{ |
|
95 |
14 |
if (a1[i] != a2[i])
|
96 |
0 |
return false;
|
97 |
} |
|
98 |
||
99 |
3 |
return true;
|
100 |
4 |
}
|
101 |
||
102 |
public static string GetPrefix(string qualifiedName) |
|
103 |
{ |
|
104 |
string prefix; |
|
105 |
string localName; |
|
106 |
250 |
GetQualifiedNameParts(qualifiedName, out prefix, out localName);
|
107 |
||
108 |
250 |
return prefix;
|
109 |
250 |
}
|
110 |
||
111 |
public static string GetLocalName(string qualifiedName) |
|
112 |
{ |
|
113 |
string prefix; |
|
114 |
string localName; |
|
115 |
20 |
GetQualifiedNameParts(qualifiedName, out prefix, out localName);
|
116 |
||
117 |
20 |
return localName;
|
118 |
20 |
}
|
119 |
||
120 |
public static void GetQualifiedNameParts(string qualifiedName, out string prefix, out string localName) |
|
121 |
{ |
|
122 |
270 |
int colonPosition = qualifiedName.IndexOf(':');
|
123 |
||
124 |
270 |
if ((colonPosition == -1 || colonPosition == 0) || (qualifiedName.Length - 1) == colonPosition)
|
125 |
{ |
|
126 |
212 |
prefix = null;
|
127 |
212 |
localName = qualifiedName;
|
128 |
} |
|
129 |
else |
|
130 |
{ |
|
131 |
58 |
prefix = qualifiedName.Substring(0, colonPosition);
|
132 |
58 |
localName = qualifiedName.Substring(colonPosition + 1);
|
133 |
} |
|
134 |
270 |
}
|
135 |
} |
|
136 |
} |