using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Text; namespace BizHawk { public struct Tuple : IEquatable> { private readonly T1 first; private readonly T2 second; public T1 First { get { return first; } } public T2 Second { get { return second; } } public Tuple(T1 o1, T2 o2) { first = o1; second = o2; } public bool Equals(Tuple other) { return first.Equals(other.first) && second.Equals(other.second); } public override bool Equals(object obj) { if (obj is Tuple) return this.Equals((Tuple)obj); else return false; } public override int GetHashCode() { return first.GetHashCode() ^ second.GetHashCode(); } } public static class Extensions { public static bool IsBinary(this string str) { for (int i = 0; i < str.Length; i++) { char c = str[i]; if (c == '0' || c == '1') continue; return false; } return true; } public static bool In(this string str, params string[] options) { foreach (string opt in options) { if (opt.Equals(str, StringComparison.CurrentCultureIgnoreCase)) return true; } return false; } public static bool In(this string str, IEnumerable options) { foreach (string opt in options) { if (opt.Equals(str,StringComparison.CurrentCultureIgnoreCase)) return true; } return false; } public static bool In(this string str, IEnumerable options, Func eval) { foreach (T opt in options) { if (eval(opt, str) == true) return true; } return false; } public static bool NotIn(this string str, params string[] options) { foreach (string opt in options) { if (opt.ToLower() == str.ToLower()) return false; } return true; } public static bool NotIn(this string str, IEnumerable options) { foreach (string opt in options) { if (opt.ToLower() == str.ToLower()) return false; } return true; } public static bool In(this int i, params int[] options) { foreach (int j in options) { if (i == j) return true; } return false; } public static bool In(this int i, IEnumerable options) { foreach (int j in options) { if (i == j) return true; } return false; } public static bool ContainsStartsWith(this IEnumerable options, string str) { foreach (string opt in options) { if (opt.StartsWith(str)) return true; } return false; } public static string GetOptionValue(this IEnumerable options, string str) { try { foreach (string opt in options) { if (opt.StartsWith(str)) { return opt.Split('=')[1]; } } } catch (Exception) { } return null; } public static bool IsValidRomExtentsion(this string str, params string[] romExtensions) { string strUpper = str.ToUpper(); foreach (string ext in romExtensions) { if (strUpper.EndsWith(ext.ToUpper())) return true; } return false; } public static string ToCommaSeparated(this List list) { var sb = new StringBuilder(); for (int i = 0; i < list.Count; i++) { if (i > 0) sb.Append(","); sb.Append(list[i]); } return sb.ToString(); } public static void SaveAsHex(this byte[] buffer, TextWriter writer) { for (int i=0; i= 0; j--) if (SaveRAM[j] != 0) return j + 1; return 0; } /// /// conerts bytes to an uppercase string of hex numbers in upper case without any spacing or anything /// public static string BytesToHexString(byte[] bytes) { StringBuilder sb = new StringBuilder(); foreach (byte b in bytes) sb.AppendFormat("{0:X2}", b); return sb.ToString(); } } }