using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Data;
using System.Data.Common;
using System.Web.Script.Serialization;
using System.Globalization;
using System.Collections;
using System.IO;
using System.Drawing;
using System.IO.Compression;
using System.Runtime.Serialization.Formatters.Binary;
namespace Pub.Class {
public static class ArrayExtensions {
public static bool IsNullEmpty( this Array array) { return array == null || array.Length == 0 ; }
public static bool IsNullEmpty( this ArrayList list) { return (list == null ) || (list.Count == 0 ); }
public static int GetInArrayID( this string [] stringArray, string strSearch, bool caseInsensetive) {
for ( int i = 0 ; i < stringArray.Length; i ++ ) {
if (caseInsensetive) {
if (strSearch.ToLower() == stringArray[i].ToLower()) return i;
} else {
if (strSearch == stringArray[i]) return i;
}
}
return - 1 ;
}
public static int GetInArrayID( this string [] stringArray, string strSearch) {
return GetInArrayID(stringArray, strSearch, true );
}
public static string ToDelimitedString < T > ( this T[] array, string format, string delimiter) {
if (array == null || array.Length == 0 ) return string .Empty;
if (format.IsNullEmpty()) format = " {0} " ;
StringBuilder builder = new StringBuilder();
for ( int index = 0 ; index < array.Length; ++ index) {
if (index != 0 ) builder.Append(delimiter);
builder.AppendFormat(format, array[index]);
}
return builder.ToString();
}
public static T[] RemoveDuplicates < T > ( this T[] array) {
ArrayList al = new ArrayList();
for ( int i = 0 ; i < array.Length; i ++ ) {
if ( ! al.Contains(array[i])) al.Add(array[i]);
}
return (T[])al.ToArray( typeof (T));
}
public static T[] Slice < T > ( this T[] array, int start, int end) {
if (start >= array.Length) { start = 0 ; end = 0 ; }
if (end < 0 ) end = array.Length - start - end;
if (end <= start) end = start;
if (end >= array.Length) end = array.Length - 1 ;
int len = end - start + 1 ;
T[] res = new T[len];
for ( int i = 0 ; i < len; i ++ ) res[i] = array[i + start];
return res;
}
public static string Join < T > ( this T[] array, string splitStr) {
StringBuilder sb = new StringBuilder();
foreach (T info in array) {
sb.AppendFormat( " {0}{1} " , info.ToString(), splitStr);
}
return sb.ToString().Left(sb.Length - splitStr.Length);
}
public static void Action < T > ( this T[] inArray, Action < T, Int32 > inAction) {
for ( int i0 = 0 ; i0 < inArray.GetLength( 0 ); i0 ++ ) {
inAction(inArray[i0], i0);
}
}
public static void Action < T > ( this T[,] inArray, Action < T, Int32, Int32 > inAction) {
for ( int i0 = 0 ; i0 < inArray.GetLength( 0 ); i0 ++ ) {
for ( int i1 = 0 ; i1 < inArray.GetLength( 1 ); i1 ++ ) inAction(inArray[i0, i1], i0, i1);
}
}
public static void Action < T > ( this T[,,] inArray, Action < T, Int32, Int32, Int32 > inAction) {
for ( int i0 = 0 ; i0 < inArray.GetLength( 0 ); i0 ++ ) {
for ( int i1 = 0 ; i1 < inArray.GetLength( 1 ); i1 ++ ) {
for ( int i2 = 0 ; i2 < inArray.GetLength( 2 ); i2 ++ ) inAction(inArray[i0, i1, i2], i0, i1, i2);
}
}
}
public static void Action < T > ( this T[,] inArray, Int32 inDimension, Int32 inIndex, Action < T, Int32 > inAction) {
if (inDimension == 0 ) {
for ( int i = 0 ; i < inArray.GetLength( 1 ); i ++ ) inAction(inArray[inIndex, i], i);
} else if (inDimension == 1 ) {
for ( int i = 0 ; i < inArray.GetLength( 0 ); i ++ ) inAction(inArray[i, inIndex], i);
} else {
throw new ArgumentException( " inDimension must be zero or one " );
}
}
public static void Action < T > ( this T[,,] inArray, Int32 inDimension, Int32 inIndex, Action < T, Int32, Int32 > inAction) {
if (inDimension == 0 ) {
for ( int i0 = 0 ; i0 < inArray.GetLength( 1 ); i0 ++ ) {
for ( int i1 = 0 ; i1 < inArray.GetLength( 2 ); i1 ++ ) inAction(inArray[inIndex, i0, i1], i0, i1);
}
} else if (inDimension == 1 ) {
for ( int i0 = 0 ; i0 < inArray.GetLength( 0 ); i0 ++ ){
for ( int i1 = 0 ; i1 < inArray.GetLength( 2 ); i1 ++ ) inAction(inArray[i0, inIndex, i1], i0, i1);
}
} else if (inDimension == 2 ) {
for ( int i0 = 0 ; i0 < inArray.GetLength( 0 ); i0 ++ ){
for ( int i1 = 0 ; i1 < inArray.GetLength( 1 ); i1 ++ ) inAction(inArray[i0, i1, inIndex], i0, i1);
}
} else {
throw new ArgumentException( " inDimension must be zero or one or two " );
}
}
public static Image ToImage( this byte [] bytes) {
if (bytes != null ) {
MemoryStream ms = new MemoryStream(bytes, 0 , bytes.Length);
ms.Write(bytes, 0 , bytes.Length);
return Image.FromStream(ms, true );
}
return null ;
}
public static bool ToFile( this byte [] bytes, string fileName, FileMode fileMode) {
bool returnValue = true ;
FileAccess fileAccess = FileAccess.ReadWrite;
if (fileMode == FileMode.Append) fileAccess = FileAccess.Write;
FileStream fs = new FileStream(fileName, fileMode, fileAccess);
BinaryWriter bw = new BinaryWriter(fs);
try { bw.Write(bytes); } catch (Exception) { returnValue = false ; } finally { fs.Close(); bw.Close(); }
return returnValue;
}
public static byte [] Compress( this byte [] data) {
using (MemoryStream output = new MemoryStream()) {
using (DeflateStream def = new DeflateStream(output, CompressionMode.Compress)) {
def.Write(data, 0 , data.Length);
}
return output.ToArray();
}
}
public static byte [] Decompress( this byte [] data) {
using (MemoryStream input = new MemoryStream()) {
input.Write(data, 0 , data.Length);
input.Position = 0 ;
using (DeflateStream def = new DeflateStream(input, CompressionMode.Decompress)) {
using (MemoryStream output = new MemoryStream()) {
byte [] buff = new byte [ 64 ];
int read = - 1 ;
read = def.Read(buff, 0 , buff.Length);
while (read > 0 ) {
output.Write(buff, 0 , read);
read = def.Read(buff, 0 , buff.Length);
}
def.Close();
return output.ToArray();
}
}
}
}
public static T Decompress < T > ( this byte [] compressedData) where T : class {
return compressedData.Decompress().Deserialize < T > ();
}
public static T Deserialize < T > ( this byte [] data) where T : class {
var formatter = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream(data)) return formatter.Deserialize(ms) as T;
}
public static bool IsInArray( this string [] stringArray, string strSearch, bool caseInsensetive) {
return stringArray.GetInArrayID(strSearch, caseInsensetive) >= 0 ;
}
public static bool IsInArray( this string [] stringarray, string str) {
return stringarray.IsInArray(str, false );
}
public static bool IsInIPArray( this string [] iparray, string ip) {
string [] userip = ip.Split( @" . " );
for ( int ipIndex = 0 ; ipIndex < iparray.Length; ipIndex ++ ) {
string [] tmpip = iparray[ipIndex].Split( @" . " );
int r = 0 ;
for ( int i = 0 ; i < tmpip.Length; i ++ ) {
if (tmpip[i] == " * " ) return true ;
if (userip.Length > i) {
if (tmpip[i] == userip[i]) r ++ ; else break ;
} else break ;
}
if (r == 4 ) return true ;
}
return false ;
}
// Action<string, int> MsgW3 = (s, i) => { Msg.Write(s + i); Msg.Write("<br />"); };
// "test1,test2,test3".Split(',').Action<string>(MsgW3);
}
}