2
// Copyright (c) 2007 James Newton-King
4
// Permission is hereby granted, free of charge, to any person
5
// obtaining a copy of this software and associated documentation
6
// files (the "Software"), to deal in the Software without
7
// restriction, including without limitation the rights to use,
8
// copy, modify, merge, publish, distribute, sublicense, and/or sell
9
// copies of the Software, and to permit persons to whom the
10
// Software is furnished to do so, subject to the following
13
// The above copyright notice and this permission notice shall be
14
// included in all copies or substantial portions of the Software.
16
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
// OTHER DEALINGS IN THE SOFTWARE.
27
using System.Collections;
28
using System.Collections.Generic;
29
using System.ComponentModel;
30
using System.Reflection;
32
using System.Globalization;
34
namespace Newtonsoft.Json.Utilities
36
internal delegate T Creator<T>();
38
internal static class MiscellaneousUtils
40
public static bool ValueEquals(object objA, object objB)
42
if (objA == null && objB == null)
44
if (objA != null && objB == null)
46
if (objA == null && objB != null)
49
// comparing an Int32 and Int64 both of the same value returns false
50
// make types the same then compare
51
if (objA.GetType() != objB.GetType())
53
if (ConvertUtils.IsInteger(objA) && ConvertUtils.IsInteger(objB))
54
return Convert.ToDecimal(objA, CultureInfo.CurrentCulture).Equals(Convert.ToDecimal(objB, CultureInfo.CurrentCulture));
55
else if ((objA is double || objA is float || objA is decimal) && (objB is double || objB is float || objB is decimal))
56
return MathUtils.ApproxEquals(Convert.ToDouble(objA, CultureInfo.CurrentCulture), Convert.ToDouble(objB, CultureInfo.CurrentCulture));
61
return objA.Equals(objB);
64
public static ArgumentOutOfRangeException CreateArgumentOutOfRangeException(string paramName, object actualValue, string message)
66
string newMessage = message + Environment.NewLine + @"Actual value was {0}.".FormatWith(CultureInfo.InvariantCulture, actualValue);
68
return new ArgumentOutOfRangeException(paramName, newMessage);
71
public static bool TryAction<T>(Creator<T> creator, out T output)
73
ValidationUtils.ArgumentNotNull(creator, "creator");
87
public static string ToString(object value)
92
return (value is string) ? @"""" + value.ToString() + @"""" : value.ToString();
95
public static byte[] HexToBytes(string hex)
97
string fixedHex = hex.Replace("-", string.Empty);
99
// array to put the result in
100
byte[] bytes = new byte[fixedHex.Length / 2];
101
// variable to determine shift of high/low nibble
103
// offset of the current byte in the array
105
// loop the characters in the string
106
foreach (char c in fixedHex)
108
// get character code in range 0-9, 17-22
109
// the % 32 handles lower case characters
110
int b = (c - '0') % 32;
111
// correction for a-f
113
// store nibble (4 bits) in byte array
114
bytes[offset] |= (byte)(b << shift);
115
// toggle the shift variable between 0 and 4
118
if (shift != 0) offset++;
123
public static string BytesToHex(byte[] bytes)
125
return BytesToHex(bytes, false);
128
public static string BytesToHex(byte[] bytes, bool removeDashes)
130
string hex = BitConverter.ToString(bytes);
132
hex = hex.Replace("-", "");
137
public static int ByteArrayCompare(byte[] a1, byte[] a2)
139
int lengthCompare = a1.Length.CompareTo(a2.Length);
140
if (lengthCompare != 0)
141
return lengthCompare;
143
for (int i = 0; i < a1.Length; i++)
145
int valueCompare = a1[i].CompareTo(a2[i]);
146
if (valueCompare != 0)
153
public static string GetPrefix(string qualifiedName)
157
GetQualifiedNameParts(qualifiedName, out prefix, out localName);
162
public static string GetLocalName(string qualifiedName)
166
GetQualifiedNameParts(qualifiedName, out prefix, out localName);
171
public static void GetQualifiedNameParts(string qualifiedName, out string prefix, out string localName)
173
int colonPosition = qualifiedName.IndexOf(':');
175
if ((colonPosition == -1 || colonPosition == 0) || (qualifiedName.Length - 1) == colonPosition)
178
localName = qualifiedName;
182
prefix = qualifiedName.Substring(0, colonPosition);
183
localName = qualifiedName.Substring(colonPosition + 1);
b'\\ No newline at end of file'