You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
5.8 KiB

  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. using System;
  26. using System.Collections;
  27. using System.Collections.Generic;
  28. using System.ComponentModel;
  29. using System.Reflection;
  30. using System.Text;
  31. using System.Globalization;
  32. using System.Text.RegularExpressions;
  33. namespace Newtonsoft.Json.Utilities
  34. {
  35. internal delegate T Creator<T>();
  36. internal static class MiscellaneousUtils
  37. {
  38. public static bool ValueEquals(object objA, object objB)
  39. {
  40. if (objA == objB)
  41. {
  42. return true;
  43. }
  44. if (objA == null || objB == null)
  45. {
  46. return false;
  47. }
  48. // comparing an Int32 and Int64 both of the same value returns false
  49. // make types the same then compare
  50. if (objA.GetType() != objB.GetType())
  51. {
  52. if (ConvertUtils.IsInteger(objA) && ConvertUtils.IsInteger(objB))
  53. {
  54. return Convert.ToDecimal(objA, CultureInfo.CurrentCulture).Equals(Convert.ToDecimal(objB, CultureInfo.CurrentCulture));
  55. }
  56. else if ((objA is double || objA is float || objA is decimal) && (objB is double || objB is float || objB is decimal))
  57. {
  58. return MathUtils.ApproxEquals(Convert.ToDouble(objA, CultureInfo.CurrentCulture), Convert.ToDouble(objB, CultureInfo.CurrentCulture));
  59. }
  60. else
  61. {
  62. return false;
  63. }
  64. }
  65. return objA.Equals(objB);
  66. }
  67. public static ArgumentOutOfRangeException CreateArgumentOutOfRangeException(string paramName, object actualValue, string message)
  68. {
  69. string newMessage = message + Environment.NewLine + @"Actual value was {0}.".FormatWith(CultureInfo.InvariantCulture, actualValue);
  70. return new ArgumentOutOfRangeException(paramName, newMessage);
  71. }
  72. public static string ToString(object value)
  73. {
  74. if (value == null)
  75. {
  76. return "{null}";
  77. }
  78. return (value is string) ? @"""" + value.ToString() + @"""" : value.ToString();
  79. }
  80. public static int ByteArrayCompare(byte[] a1, byte[] a2)
  81. {
  82. int lengthCompare = a1.Length.CompareTo(a2.Length);
  83. if (lengthCompare != 0)
  84. {
  85. return lengthCompare;
  86. }
  87. for (int i = 0; i < a1.Length; i++)
  88. {
  89. int valueCompare = a1[i].CompareTo(a2[i]);
  90. if (valueCompare != 0)
  91. {
  92. return valueCompare;
  93. }
  94. }
  95. return 0;
  96. }
  97. public static string GetPrefix(string qualifiedName)
  98. {
  99. GetQualifiedNameParts(qualifiedName, out string prefix, out _);
  100. return prefix;
  101. }
  102. public static string GetLocalName(string qualifiedName)
  103. {
  104. GetQualifiedNameParts(qualifiedName, out _, out string localName);
  105. return localName;
  106. }
  107. public static void GetQualifiedNameParts(string qualifiedName, out string prefix, out string localName)
  108. {
  109. int colonPosition = qualifiedName.IndexOf(':');
  110. if ((colonPosition == -1 || colonPosition == 0) || (qualifiedName.Length - 1) == colonPosition)
  111. {
  112. prefix = null;
  113. localName = qualifiedName;
  114. }
  115. else
  116. {
  117. prefix = qualifiedName.Substring(0, colonPosition);
  118. localName = qualifiedName.Substring(colonPosition + 1);
  119. }
  120. }
  121. internal static string FormatValueForPrint(object value)
  122. {
  123. if (value == null)
  124. {
  125. return "{null}";
  126. }
  127. if (value is string)
  128. {
  129. return @"""" + value + @"""";
  130. }
  131. return value.ToString();
  132. }
  133. internal static RegexOptions GetRegexOptions(string optionsText)
  134. {
  135. RegexOptions options = RegexOptions.None;
  136. foreach (char c in optionsText)
  137. {
  138. switch (c)
  139. {
  140. case 'i':
  141. options |= RegexOptions.IgnoreCase;
  142. break;
  143. case 'm':
  144. options |= RegexOptions.Multiline;
  145. break;
  146. case 's':
  147. options |= RegexOptions.Singleline;
  148. break;
  149. case 'x':
  150. options |= RegexOptions.ExplicitCapture;
  151. break;
  152. }
  153. }
  154. return options;
  155. }
  156. }
  157. }