~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/Newtonsoft.Json/Src/Newtonsoft.Json/Utilities/MiscellaneousUtils.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#region License
 
2
// Copyright (c) 2007 James Newton-King
 
3
//
 
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
 
11
// conditions:
 
12
//
 
13
// The above copyright notice and this permission notice shall be
 
14
// included in all copies or substantial portions of the Software.
 
15
//
 
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.
 
24
#endregion
 
25
 
 
26
using System;
 
27
using System.Collections;
 
28
using System.Collections.Generic;
 
29
using System.ComponentModel;
 
30
using System.Reflection;
 
31
using System.Text;
 
32
using System.Globalization;
 
33
 
 
34
namespace Newtonsoft.Json.Utilities
 
35
{
 
36
  internal delegate T Creator<T>();
 
37
 
 
38
  internal static class MiscellaneousUtils
 
39
  {
 
40
    public static bool ValueEquals(object objA, object objB)
 
41
    {
 
42
      if (objA == null && objB == null)
 
43
        return true;
 
44
      if (objA != null && objB == null)
 
45
        return false;
 
46
      if (objA == null && objB != null)
 
47
        return false;
 
48
 
 
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())
 
52
      {
 
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));
 
57
        else
 
58
          return false;
 
59
      }
 
60
 
 
61
      return objA.Equals(objB);
 
62
    }
 
63
 
 
64
    public static ArgumentOutOfRangeException CreateArgumentOutOfRangeException(string paramName, object actualValue, string message)
 
65
    {
 
66
      string newMessage = message + Environment.NewLine + @"Actual value was {0}.".FormatWith(CultureInfo.InvariantCulture, actualValue);
 
67
 
 
68
      return new ArgumentOutOfRangeException(paramName, newMessage);
 
69
    }
 
70
 
 
71
    public static bool TryAction<T>(Creator<T> creator, out T output)
 
72
    {
 
73
      ValidationUtils.ArgumentNotNull(creator, "creator");
 
74
 
 
75
      try
 
76
      {
 
77
        output = creator();
 
78
        return true;
 
79
      }
 
80
      catch
 
81
      {
 
82
        output = default(T);
 
83
        return false;
 
84
      }
 
85
    }
 
86
 
 
87
    public static string ToString(object value)
 
88
    {
 
89
      if (value == null)
 
90
        return "{null}";
 
91
 
 
92
      return (value is string) ? @"""" + value.ToString() + @"""" : value.ToString();
 
93
    }
 
94
 
 
95
    public static byte[] HexToBytes(string hex)
 
96
    {
 
97
      string fixedHex = hex.Replace("-", string.Empty);
 
98
 
 
99
      // array to put the result in
 
100
      byte[] bytes = new byte[fixedHex.Length / 2];
 
101
      // variable to determine shift of high/low nibble
 
102
      int shift = 4;
 
103
      // offset of the current byte in the array
 
104
      int offset = 0;
 
105
      // loop the characters in the string
 
106
      foreach (char c in fixedHex)
 
107
      {
 
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
 
112
        if (b > 9) b -= 7;
 
113
        // store nibble (4 bits) in byte array
 
114
        bytes[offset] |= (byte)(b << shift);
 
115
        // toggle the shift variable between 0 and 4
 
116
        shift ^= 4;
 
117
        // move to next byte
 
118
        if (shift != 0) offset++;
 
119
      }
 
120
      return bytes;
 
121
    }
 
122
 
 
123
    public static string BytesToHex(byte[] bytes)
 
124
    {
 
125
      return BytesToHex(bytes, false);
 
126
    }
 
127
 
 
128
    public static string BytesToHex(byte[] bytes, bool removeDashes)
 
129
    {
 
130
      string hex = BitConverter.ToString(bytes);
 
131
      if (removeDashes)
 
132
        hex = hex.Replace("-", "");
 
133
 
 
134
      return hex;
 
135
    }
 
136
 
 
137
    public static int ByteArrayCompare(byte[] a1, byte[] a2)
 
138
    {
 
139
      int lengthCompare = a1.Length.CompareTo(a2.Length);
 
140
      if (lengthCompare != 0)
 
141
        return lengthCompare;
 
142
 
 
143
      for (int i = 0; i < a1.Length; i++)
 
144
      {
 
145
        int valueCompare = a1[i].CompareTo(a2[i]);
 
146
        if (valueCompare != 0)
 
147
          return valueCompare;
 
148
      }
 
149
 
 
150
      return 0;
 
151
    }
 
152
 
 
153
    public static string GetPrefix(string qualifiedName)
 
154
    {
 
155
      string prefix;
 
156
      string localName;
 
157
      GetQualifiedNameParts(qualifiedName, out prefix, out localName);
 
158
 
 
159
      return prefix;
 
160
    }
 
161
 
 
162
    public static string GetLocalName(string qualifiedName)
 
163
    {
 
164
      string prefix;
 
165
      string localName;
 
166
      GetQualifiedNameParts(qualifiedName, out prefix, out localName);
 
167
 
 
168
      return localName;
 
169
    }
 
170
 
 
171
    public static void GetQualifiedNameParts(string qualifiedName, out string prefix, out string localName)
 
172
    {
 
173
      int colonPosition = qualifiedName.IndexOf(':');
 
174
 
 
175
      if ((colonPosition == -1 || colonPosition == 0) || (qualifiedName.Length - 1) == colonPosition)
 
176
      {
 
177
        prefix = null;
 
178
        localName = qualifiedName;
 
179
      }
 
180
      else
 
181
      {
 
182
        prefix = qualifiedName.Substring(0, colonPosition);
 
183
        localName = qualifiedName.Substring(colonPosition + 1);
 
184
      }
 
185
    }
 
186
  }
 
187
}
 
 
b'\\ No newline at end of file'