2 using System.Collections;
3 using System.Collections.Generic;
4 using System.ComponentModel;
5 using System.Reflection;
7 using System.Globalization;
9 namespace Newtonsoft.Json.Utilities
11 internal delegate T Creator<T>();
13 internal static class MiscellaneousUtils
15 public static bool ValueEquals(object objA, object objB)
17 if (objA == null && objB == null)
19 if (objA != null && objB == null)
21 if (objA == null && objB != null)
24 // comparing an Int32 and Int64 both of the same value returns false
25 // make types the same then compare
26 if (objA.GetType() != objB.GetType())
28 if (ConvertUtils.IsInteger(objA) && ConvertUtils.IsInteger(objB))
29 return Convert.ToDecimal(objA, CultureInfo.CurrentCulture).Equals(Convert.ToDecimal(objB, CultureInfo.CurrentCulture));
30 else if ((objA is double || objA is float || objA is decimal) && (objB is double || objB is float || objB is decimal))
31 return MathUtils.ApproxEquals(Convert.ToDouble(objA, CultureInfo.CurrentCulture), Convert.ToDouble(objB, CultureInfo.CurrentCulture));
36 return objA.Equals(objB);
39 public static ArgumentOutOfRangeException CreateArgumentOutOfRangeException(string paramName, object actualValue, string message)
41 string newMessage = message + Environment.NewLine + @"Actual value was {0}.".FormatWith(CultureInfo.InvariantCulture, actualValue);
43 return new ArgumentOutOfRangeException(paramName, newMessage);
46 public static bool TryAction<T>(Creator<T> creator, out T output)
48 ValidationUtils.ArgumentNotNull(creator, "creator");
62 public static string ToString(object value)
67 return (value is string) ? @"""" + value.ToString() + @"""" : value.ToString();
70 public static byte[] HexToBytes(string hex)
72 string fixedHex = hex.Replace("-", string.Empty);
74 // array to put the result in
75 byte[] bytes = new byte[fixedHex.Length / 2];
76 // variable to determine shift of high/low nibble
78 // offset of the current byte in the array
80 // loop the characters in the string
81 foreach (char c in fixedHex)
83 // get character code in range 0-9, 17-22
84 // the % 32 handles lower case characters
85 int b = (c - '0') % 32;
88 // store nibble (4 bits) in byte array
89 bytes[offset] |= (byte)(b << shift);
90 // toggle the shift variable between 0 and 4
93 if (shift != 0) offset++;
98 public static string BytesToHex(byte[] bytes)
100 return BytesToHex(bytes, false);
103 public static string BytesToHex(byte[] bytes, bool removeDashes)
105 string hex = BitConverter.ToString(bytes);
107 hex = hex.Replace("-", "");
112 public static int ByteArrayCompare(byte[] a1, byte[] a2)
114 int lengthCompare = a1.Length.CompareTo(a2.Length);
115 if (lengthCompare != 0)
116 return lengthCompare;
118 for (int i = 0; i < a1.Length; i++)
120 int valueCompare = a1[i].CompareTo(a2[i]);
121 if (valueCompare != 0)
128 public static string GetPrefix(string qualifiedName)
132 GetQualifiedNameParts(qualifiedName, out prefix, out localName);
137 public static string GetLocalName(string qualifiedName)
141 GetQualifiedNameParts(qualifiedName, out prefix, out localName);
146 public static void GetQualifiedNameParts(string qualifiedName, out string prefix, out string localName)
148 int colonPosition = qualifiedName.IndexOf(':');
150 if ((colonPosition == -1 || colonPosition == 0) || (qualifiedName.Length - 1) == colonPosition)
153 localName = qualifiedName;
157 prefix = qualifiedName.Substring(0, colonPosition);
158 localName = qualifiedName.Substring(colonPosition + 1);