Modified Ext.cs

This commit is contained in:
sta 2013-05-11 22:04:48 +09:00
parent b3f6aa2ebf
commit 79539f7638
10 changed files with 70 additions and 101 deletions

Binary file not shown.

View File

@ -1362,23 +1362,23 @@ namespace WebSocketSharp {
/// Converts the specified array of <see cref="byte"/> to the specified type data. /// Converts the specified array of <see cref="byte"/> to the specified type data.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// A T converted from the <paramref name="src"/>, or a default value of T /// A T converted from <paramref name="src"/>, or a default value of T
/// if the <paramref name="src"/> is an empty array of <see cref="byte"/> /// if <paramref name="src"/> is an empty array of <see cref="byte"/>
/// or if the types of T aren't the <see cref="bool"/>, <see cref="char"/>, <see cref="double"/>, /// or if the type of T isn't <see cref="bool"/>, <see cref="char"/>, <see cref="double"/>,
/// <see cref="float"/>, <see cref="int"/>, <see cref="long"/>, <see cref="short"/>, /// <see cref="float"/>, <see cref="int"/>, <see cref="long"/>, <see cref="short"/>,
/// <see cref="uint"/>, <see cref="ulong"/>, <see cref="ushort"/>. /// <see cref="uint"/>, <see cref="ulong"/> or <see cref="ushort"/>.
/// </returns> /// </returns>
/// <param name="src"> /// <param name="src">
/// An array of <see cref="byte"/> to convert. /// An array of <see cref="byte"/> to convert.
/// </param> /// </param>
/// <param name="srcOrder"> /// <param name="srcOrder">
/// A <see cref="WebSocketSharp.ByteOrder"/> that indicates the byte order of the <paramref name="src"/>. /// A <see cref="WebSocketSharp.ByteOrder"/> that indicates the byte order of <paramref name="src"/>.
/// </param> /// </param>
/// <typeparam name="T"> /// <typeparam name="T">
/// The type of the return value. The T must be a value type. /// The type of the return. The T must be a value type.
/// </typeparam> /// </typeparam>
/// <exception cref="ArgumentNullException"> /// <exception cref="ArgumentNullException">
/// Is thrown when the <paramref name="src"/> parameter passed to a method is invalid because it is <see langword="null"/>. /// <paramref name="src"/> is <see langword="null"/>.
/// </exception> /// </exception>
public static T To<T>(this byte[] src, ByteOrder srcOrder) public static T To<T>(this byte[] src, ByteOrder srcOrder)
where T : struct where T : struct
@ -1389,46 +1389,37 @@ namespace WebSocketSharp {
if (src.Length == 0) if (src.Length == 0)
return default(T); return default(T);
var type = typeof(T); var type = typeof(T);
var buffer = src.ToHostOrder(srcOrder); var buffer = src.ToHostOrder(srcOrder);
if (type == typeof(Boolean))
return (T)(object)BitConverter.ToBoolean(buffer, 0);
if (type == typeof(Char)) return type == typeof(Boolean)
return (T)(object)BitConverter.ToChar(buffer, 0); ? (T)(object)BitConverter.ToBoolean(buffer, 0)
: type == typeof(Char)
if (type == typeof(Double)) ? (T)(object)BitConverter.ToChar(buffer, 0)
return (T)(object)BitConverter.ToDouble(buffer, 0); : type == typeof(Double)
? (T)(object)BitConverter.ToDouble(buffer, 0)
if (type == typeof(Int16)) : type == typeof(Int16)
return (T)(object)BitConverter.ToInt16(buffer, 0); ? (T)(object)BitConverter.ToInt16(buffer, 0)
: type == typeof(Int32)
if (type == typeof(Int32)) ? (T)(object)BitConverter.ToInt32(buffer, 0)
return (T)(object)BitConverter.ToInt32(buffer, 0); : type == typeof(Int64)
? (T)(object)BitConverter.ToInt64(buffer, 0)
if (type == typeof(Int64)) : type == typeof(Single)
return (T)(object)BitConverter.ToInt64(buffer, 0); ? (T)(object)BitConverter.ToSingle(buffer, 0)
: type == typeof(UInt16)
if (type == typeof(Single)) ? (T)(object)BitConverter.ToUInt16(buffer, 0)
return (T)(object)BitConverter.ToSingle(buffer, 0); : type == typeof(UInt32)
? (T)(object)BitConverter.ToUInt32(buffer, 0)
if (type == typeof(UInt16)) : type == typeof(UInt64)
return (T)(object)BitConverter.ToUInt16(buffer, 0); ? (T)(object)BitConverter.ToUInt64(buffer, 0)
: default(T);
if (type == typeof(UInt32))
return (T)(object)BitConverter.ToUInt32(buffer, 0);
if (type == typeof(UInt64))
return (T)(object)BitConverter.ToUInt64(buffer, 0);
return default(T);
} }
/// <summary> /// <summary>
/// Converts the specified data to an array of <see cref="byte"/>. /// Converts the specified <paramref name="value"/> to an array of <see cref="byte"/>.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// An array of <see cref="byte"/> converted from the <paramref name="value"/>. /// An array of <see cref="byte"/> converted from <paramref name="value"/>.
/// </returns> /// </returns>
/// <param name="value"> /// <param name="value">
/// A T to convert. /// A T to convert.
@ -1437,59 +1428,37 @@ namespace WebSocketSharp {
/// A <see cref="WebSocketSharp.ByteOrder"/> that indicates the byte order of the return. /// A <see cref="WebSocketSharp.ByteOrder"/> that indicates the byte order of the return.
/// </param> /// </param>
/// <typeparam name="T"> /// <typeparam name="T">
/// The type of the <paramref name="value"/>. The T must be a value type. /// The type of <paramref name="value"/>. The T must be a value type.
/// </typeparam> /// </typeparam>
public static byte[] ToByteArray<T>(this T value, ByteOrder order) public static byte[] ToByteArray<T>(this T value, ByteOrder order)
where T : struct where T : struct
{ {
var type = typeof(T); var type = typeof(T);
byte[] buffer; var buffer = type == typeof(Boolean)
if (type == typeof(Boolean)) ? BitConverter.GetBytes((Boolean)(object)value)
{ : type == typeof(Byte)
buffer = BitConverter.GetBytes((Boolean)(object)value); ? new byte[]{ (Byte)(object)value }
} : type == typeof(Char)
else if (type == typeof(Char)) ? BitConverter.GetBytes((Char)(object)value)
{ : type == typeof(Double)
buffer = BitConverter.GetBytes((Char)(object)value); ? BitConverter.GetBytes((Double)(object)value)
} : type == typeof(Int16)
else if (type == typeof(Double)) ? BitConverter.GetBytes((Int16)(object)value)
{ : type == typeof(Int32)
buffer = BitConverter.GetBytes((Double)(object)value); ? BitConverter.GetBytes((Int32)(object)value)
} : type == typeof(Int64)
else if (type == typeof(Int16)) ? BitConverter.GetBytes((Int64)(object)value)
{ : type == typeof(Single)
buffer = BitConverter.GetBytes((Int16)(object)value); ? BitConverter.GetBytes((Single)(object)value)
} : type == typeof(UInt16)
else if (type == typeof(Int32)) ? BitConverter.GetBytes((UInt16)(object)value)
{ : type == typeof(UInt32)
buffer = BitConverter.GetBytes((Int32)(object)value); ? BitConverter.GetBytes((UInt32)(object)value)
} : type == typeof(UInt64)
else if (type == typeof(Int64)) ? BitConverter.GetBytes((UInt64)(object)value)
{ : new byte[]{};
buffer = BitConverter.GetBytes((Int64)(object)value);
}
else if (type == typeof(Single))
{
buffer = BitConverter.GetBytes((Single)(object)value);
}
else if (type == typeof(UInt16))
{
buffer = BitConverter.GetBytes((UInt16)(object)value);
}
else if (type == typeof(UInt32))
{
buffer = BitConverter.GetBytes((UInt32)(object)value);
}
else if (type == typeof(UInt64))
{
buffer = BitConverter.GetBytes((UInt64)(object)value);
}
else
{
buffer = new byte[]{};
}
return buffer.Length == 0 || order.IsHostOrder() return buffer.Length <= 1 || order.IsHostOrder()
? buffer ? buffer
: buffer.Reverse().ToArray(); : buffer.Reverse().ToArray();
} }
@ -1498,34 +1467,34 @@ namespace WebSocketSharp {
/// Converts the order of the specified array of <see cref="byte"/> to the host byte order. /// Converts the order of the specified array of <see cref="byte"/> to the host byte order.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// An array of <see cref="byte"/> converted from the <paramref name="src"/>. /// An array of <see cref="byte"/> converted from <paramref name="src"/>.
/// </returns> /// </returns>
/// <param name="src"> /// <param name="src">
/// An array of <see cref="byte"/> to convert. /// An array of <see cref="byte"/> to convert.
/// </param> /// </param>
/// <param name="srcOrder"> /// <param name="srcOrder">
/// A <see cref="WebSocketSharp.ByteOrder"/> that indicates the byte order of the <paramref name="src"/>. /// A <see cref="WebSocketSharp.ByteOrder"/> that indicates the byte order of <paramref name="src"/>.
/// </param> /// </param>
/// <exception cref="ArgumentNullException"> /// <exception cref="ArgumentNullException">
/// Is thrown when the <paramref name="src"/> parameter passed to a method is invalid because it is <see langword="null"/>. /// <paramref name="src"/> is <see langword="null"/>.
/// </exception> /// </exception>
public static byte[] ToHostOrder(this byte[] src, ByteOrder srcOrder) public static byte[] ToHostOrder(this byte[] src, ByteOrder srcOrder)
{ {
if (src == null) if (src == null)
throw new ArgumentNullException("src"); throw new ArgumentNullException("src");
return src.Length == 0 || srcOrder.IsHostOrder() return src.Length <= 1 || srcOrder.IsHostOrder()
? src ? src
: src.Reverse().ToArray(); : src.Reverse().ToArray();
} }
/// <summary> /// <summary>
/// Converts the specified array to a <see cref="string"/> concatenated the specified separator string /// Converts the specified <paramref name="array"/> to a <see cref="string"/> that concatenates
/// between each element of this array. /// the each element of <paramref name="array"/> across the specified <paramref name="separator"/>.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// A <see cref="string"/> converted from the <paramref name="array"/> parameter, or a <see cref="String.Empty"/> /// A <see cref="string"/> converted from <paramref name="array"/>, or a <see cref="String.Empty"/>
/// if the length of the <paramref name="array"/> is zero. /// if the length of <paramref name="array"/> is zero.
/// </returns> /// </returns>
/// <param name="array"> /// <param name="array">
/// An array of T to convert. /// An array of T to convert.
@ -1534,10 +1503,10 @@ namespace WebSocketSharp {
/// A <see cref="string"/> that contains a separator string. /// A <see cref="string"/> that contains a separator string.
/// </param> /// </param>
/// <typeparam name="T"> /// <typeparam name="T">
/// The type of elements in the <paramref name="array"/>. /// The type of elements in <paramref name="array"/>.
/// </typeparam> /// </typeparam>
/// <exception cref="ArgumentNullException"> /// <exception cref="ArgumentNullException">
/// Is thrown when the <paramref name="array"/> parameter passed to a method is invalid because it is <see langword="null"/>. /// <paramref name="array"/> is <see langword="null"/>.
/// </exception> /// </exception>
public static string ToString<T>(this T[] array, string separator) public static string ToString<T>(this T[] array, string separator)
{ {
@ -1551,13 +1520,13 @@ namespace WebSocketSharp {
if (separator == null) if (separator == null)
separator = String.Empty; separator = String.Empty;
var sb = new StringBuilder(); var buffer = new StringBuilder(64);
(len - 1).Times(i => (len - 1).Times(i =>
sb.AppendFormat("{0}{1}", array[i].ToString(), separator) buffer.AppendFormat("{0}{1}", array[i].ToString(), separator)
); );
sb.Append(array[len - 1].ToString()); buffer.Append(array[len - 1].ToString());
return sb.ToString(); return buffer.ToString();
} }
/// <summary> /// <summary>