All files
[pithos-ms-client] / trunk / Libraries / Json40r2 / Source / Src / Newtonsoft.Json / Utilities / MiscellaneousUtils.cs
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 bool ValueEquals(object objA, object objB)
16     {
17       if (objA == null && objB == null)
18         return true;
19       if (objA != null && objB == null)
20         return false;
21       if (objA == null && objB != null)
22         return false;
23
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())
27       {
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));
32         else
33           return false;
34       }
35
36       return objA.Equals(objB);
37     }
38
39     public static ArgumentOutOfRangeException CreateArgumentOutOfRangeException(string paramName, object actualValue, string message)
40     {
41       string newMessage = message + Environment.NewLine + @"Actual value was {0}.".FormatWith(CultureInfo.InvariantCulture, actualValue);
42
43       return new ArgumentOutOfRangeException(paramName, newMessage);
44     }
45
46     public static bool TryAction<T>(Creator<T> creator, out T output)
47     {
48       ValidationUtils.ArgumentNotNull(creator, "creator");
49
50       try
51       {
52         output = creator();
53         return true;
54       }
55       catch
56       {
57         output = default(T);
58         return false;
59       }
60     }
61
62     public static string ToString(object value)
63     {
64       if (value == null)
65         return "{null}";
66
67       return (value is string) ? @"""" + value.ToString() + @"""" : value.ToString();
68     }
69
70     public static byte[] HexToBytes(string hex)
71     {
72       string fixedHex = hex.Replace("-", string.Empty);
73
74       // array to put the result in
75       byte[] bytes = new byte[fixedHex.Length / 2];
76       // variable to determine shift of high/low nibble
77       int shift = 4;
78       // offset of the current byte in the array
79       int offset = 0;
80       // loop the characters in the string
81       foreach (char c in fixedHex)
82       {
83         // get character code in range 0-9, 17-22
84         // the % 32 handles lower case characters
85         int b = (c - '0') % 32;
86         // correction for a-f
87         if (b > 9) b -= 7;
88         // store nibble (4 bits) in byte array
89         bytes[offset] |= (byte)(b << shift);
90         // toggle the shift variable between 0 and 4
91         shift ^= 4;
92         // move to next byte
93         if (shift != 0) offset++;
94       }
95       return bytes;
96     }
97
98     public static string BytesToHex(byte[] bytes)
99     {
100       return BytesToHex(bytes, false);
101     }
102
103     public static string BytesToHex(byte[] bytes, bool removeDashes)
104     {
105       string hex = BitConverter.ToString(bytes);
106       if (removeDashes)
107         hex = hex.Replace("-", "");
108
109       return hex;
110     }
111
112     public static int ByteArrayCompare(byte[] a1, byte[] a2)
113     {
114       int lengthCompare = a1.Length.CompareTo(a2.Length);
115       if (lengthCompare != 0)
116         return lengthCompare;
117
118       for (int i = 0; i < a1.Length; i++)
119       {
120         int valueCompare = a1[i].CompareTo(a2[i]);
121         if (valueCompare != 0)
122           return valueCompare;
123       }
124
125       return 0;
126     }
127
128     public static string GetPrefix(string qualifiedName)
129     {
130       string prefix;
131       string localName;
132       GetQualifiedNameParts(qualifiedName, out prefix, out localName);
133
134       return prefix;
135     }
136
137     public static string GetLocalName(string qualifiedName)
138     {
139       string prefix;
140       string localName;
141       GetQualifiedNameParts(qualifiedName, out prefix, out localName);
142
143       return localName;
144     }
145
146     public static void GetQualifiedNameParts(string qualifiedName, out string prefix, out string localName)
147     {
148       int colonPosition = qualifiedName.IndexOf(':');
149
150       if ((colonPosition == -1 || colonPosition == 0) || (qualifiedName.Length - 1) == colonPosition)
151       {
152         prefix = null;
153         localName = qualifiedName;
154       }
155       else
156       {
157         prefix = qualifiedName.Substring(0, colonPosition);
158         localName = qualifiedName.Substring(colonPosition + 1);
159       }
160     }
161   }
162 }