PageRenderTime 71ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/DICK.B1/IronPython/Runtime/Operations/ByteOps.cs

https://bitbucket.org/williamybs/uidipythontool
C# | 200 lines | 162 code | 24 blank | 14 comment | 66 complexity | b7c9f8e6b4307a49c505b80ff40c31d5 MD5 | raw file
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Microsoft Public License. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Microsoft Public License, please send an email to
  8. * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Microsoft Public License.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. using System;
  16. using System.Collections;
  17. using System.Collections.Generic;
  18. using Microsoft.Scripting.Runtime;
  19. using Microsoft.Scripting.Utils;
  20. using IronPython.Runtime.Types;
  21. #if CLR2
  22. using Microsoft.Scripting.Math;
  23. #else
  24. using System.Numerics;
  25. #endif
  26. namespace IronPython.Runtime.Operations {
  27. public static partial class ByteOps {
  28. internal static byte ToByteChecked(this int item) {
  29. try {
  30. return checked((byte)item);
  31. } catch (OverflowException) {
  32. throw PythonOps.ValueError("byte must be in range(0, 256)");
  33. }
  34. }
  35. internal static byte ToByteChecked(this BigInteger item) {
  36. int val;
  37. if (item.AsInt32(out val)) {
  38. return ToByteChecked(val);
  39. }
  40. throw PythonOps.ValueError("byte must be in range(0, 256)");
  41. }
  42. internal static byte ToByteChecked(this double item) {
  43. try {
  44. return checked((byte)item);
  45. } catch (OverflowException) {
  46. throw PythonOps.ValueError("byte must be in range(0, 256)");
  47. }
  48. }
  49. internal static bool IsSign(this byte ch) {
  50. return ch == '+' || ch == '-';
  51. }
  52. internal static byte ToUpper(this byte p) {
  53. if (p >= 'a' && p <= 'z') {
  54. p -= ('a' - 'A');
  55. }
  56. return p;
  57. }
  58. internal static byte ToLower(this byte p) {
  59. if (p >= 'A' && p <= 'Z') {
  60. p += ('a' - 'A');
  61. }
  62. return p;
  63. }
  64. internal static bool IsLower(this byte p) {
  65. return p >= 'a' && p <= 'z';
  66. }
  67. internal static bool IsUpper(this byte p) {
  68. return p >= 'A' && p <= 'Z';
  69. }
  70. internal static bool IsDigit(this byte b) {
  71. return b >= '0' && b <= '9';
  72. }
  73. internal static bool IsLetter(this byte b) {
  74. return IsLower(b) || IsUpper(b);
  75. }
  76. internal static bool IsWhiteSpace(this byte b) {
  77. return b == ' ' ||
  78. b == '\t' ||
  79. b == '\n' ||
  80. b == '\r' ||
  81. b == '\f' ||
  82. b == 11;
  83. }
  84. internal static void AppendJoin(object value, int index, List<byte> byteList) {
  85. IList<byte> strVal;
  86. if ((strVal = value as IList<byte>) != null) {
  87. byteList.AddRange(strVal);
  88. } else {
  89. throw PythonOps.TypeError("sequence item {0}: expected bytes or byte array, {1} found", index.ToString(), PythonOps.GetPythonTypeName(value));
  90. }
  91. }
  92. internal static IList<byte> CoerceBytes(object obj) {
  93. IList<byte> ret = obj as IList<byte>;
  94. if (ret == null) {
  95. throw PythonOps.TypeError("expected string, got {0} Type", PythonTypeOps.GetName(obj));
  96. }
  97. return ret;
  98. }
  99. internal static List<byte> GetBytes(ICollection bytes) {
  100. return GetBytes(bytes, GetByte);
  101. }
  102. internal static List<byte> GetBytes(ICollection bytes, Func<object, byte> conversion) {
  103. List<byte> res = new List<byte>(bytes.Count);
  104. foreach (object o in bytes) {
  105. res.Add(conversion.Invoke(o));
  106. }
  107. return res;
  108. }
  109. internal static byte GetByteStringOk(object o) {
  110. string s;
  111. Extensible<string> es;
  112. if (!Object.ReferenceEquals(s = o as string, null)) {
  113. if (s.Length == 1) {
  114. return ((int)s[0]).ToByteChecked();
  115. } else {
  116. throw PythonOps.TypeError("an integer or string of size 1 is required");
  117. }
  118. } else if (!Object.ReferenceEquals(es = o as Extensible<string>, null)) {
  119. if (es.Value.Length == 1) {
  120. return ((int)es.Value[0]).ToByteChecked();
  121. } else {
  122. throw PythonOps.TypeError("an integer or string of size 1 is required");
  123. }
  124. } else {
  125. return GetByteListOk(o);
  126. }
  127. }
  128. internal static byte GetByteListOk(object o) {
  129. IList<byte> lbval = o as IList<byte>;
  130. if (lbval != null) {
  131. if (lbval.Count == 1) {
  132. return lbval[0];
  133. }
  134. throw PythonOps.ValueError("an integer or string of size 1 is required");
  135. }
  136. return GetByte(o);
  137. }
  138. internal static byte GetByte(object o) {
  139. Extensible<int> ei;
  140. Extensible<BigInteger> ebi;
  141. Extensible<double> ed;
  142. int i;
  143. if (o is int) {
  144. return ((int)o).ToByteChecked();
  145. } else if (o is BigInteger) {
  146. return ((BigInteger)o).ToByteChecked();
  147. } else if (o is double) {
  148. return ((double)o).ToByteChecked();
  149. } else if ((ei = o as Extensible<int>) != null) {
  150. return ei.Value.ToByteChecked();
  151. } else if (!Object.ReferenceEquals(ebi = o as Extensible<BigInteger>, null)) {
  152. return ebi.Value.ToByteChecked();
  153. } else if (!Object.ReferenceEquals(ed = o as Extensible<double>, null)) {
  154. return ed.Value.ToByteChecked();
  155. } else if (o is byte) {
  156. return (byte)o;
  157. } else if (o is sbyte) {
  158. return ((int)(sbyte)o).ToByteChecked();
  159. } else if (o is char) {
  160. return ((int)(char)o).ToByteChecked();
  161. } else if (o is short) {
  162. return ((int)(short)o).ToByteChecked();
  163. } else if (o is ushort) {
  164. return ((int)(ushort)o).ToByteChecked();
  165. } else if (o is uint) {
  166. return ((BigInteger)(uint)o).ToByteChecked();
  167. } else if (o is float) {
  168. return ((double)(float)o).ToByteChecked();
  169. } else if (Converter.TryConvertToIndex(o, out i)) {
  170. return i.ToByteChecked();
  171. } else {
  172. throw PythonOps.TypeError("an integer or string of size 1 is required");
  173. }
  174. }
  175. }
  176. }