private static void WriteUnsignedClass(StreamWriter writer, string typeName, int index) { string className = SupportedTypes.ToClassName(typeName); writer.Write(ClassPrefix .Replace("long", typeName) .Replace("Long", className)); // All signed types are unsafe for (int i = 0; i < SupportedTypes.SignedNumbersInOrder.Length; ++i) { WriteConversion(writer, typeName, SupportedTypes.SignedNumbersInOrder[i], false, true, i > index); } // Smaller unsigned types are safe for (int i = 0; i < SupportedTypes.UnsignedNumbersInOrder.Length; ++i) { WriteConversion(writer, typeName, SupportedTypes.UnsignedNumbersInOrder[i], i <= index, false, i > index); } // All float conversions are unsafe for (int i = 0; i < SupportedTypes.FloatingPointInOrder.Length; ++i) { WriteConversion(writer, typeName, SupportedTypes.FloatingPointInOrder[i], false); } writer.Write(ClassSuffix); }
private static void WriteFloatClass(StreamWriter writer, string typeName, int index) { string className = SupportedTypes.ToClassName(typeName); string prefix = ClassPrefix .Replace("long", typeName) .Replace("Long", className); if (typeName == "double") { prefix = prefix.Replace("\r\n private bool[] _couldNotConvert;", ""); } writer.Write(prefix); // All signed types are safe for (int i = 0; i < SupportedTypes.SignedNumbersInOrder.Length; ++i) { WriteConversion(writer, typeName, SupportedTypes.SignedNumbersInOrder[i], true); } // All unsigned types are safe for (int i = 0; i < SupportedTypes.UnsignedNumbersInOrder.Length; ++i) { WriteConversion(writer, typeName, SupportedTypes.UnsignedNumbersInOrder[i], true); } // Smaller float conversions are safe for (int i = 0; i < SupportedTypes.FloatingPointInOrder.Length; ++i) { WriteConversion(writer, typeName, SupportedTypes.FloatingPointInOrder[i], i <= index); } writer.Write(ClassSuffix); }
public static void Generate(string typeName, string outputPath) { string className = (typeName == "T" ? "ComparableComparer" : SupportedTypes.ToClassName(typeName) + "Comparer"); using (var writer = new StreamWriter(File.Open(Path.Combine(outputPath, $"{className}.cs"), FileMode.Create))) { string prefix = s_filePrefix .Replace("long", typeName) .Replace("LongComparer", className); // Differences for ComparableComparer if (typeName == "T") { prefix = prefix .Replace("ComparableComparer", "ComparableComparer<T>") .Replace("IXArrayComparer<T>", "IXArrayComparer<T> where T : System.IComparable<T>") .Replace(", 0));", ".GetHashCode(), 0));"); // Get the generic hash code and re-hash it } // Differences for String8Comparer if (typeName == "String8") { prefix = prefix .Replace("using System;", "using System;\r\n\r\nusing Microsoft.CodeAnalysis.Elfie.Model.Strings;") .Replace("IXArrayComparer<String8>", "IXArrayTextComparer, IXArrayComparer<String8>"); } writer.Write(prefix); WriteMethod(writer, typeName, "Equal", "=="); WriteMethod(writer, typeName, "NotEqual", "!="); WriteMethod(writer, typeName, "LessThan", "<"); WriteMethod(writer, typeName, "LessThanOrEqual", "<="); WriteMethod(writer, typeName, "GreaterThan", ">"); WriteMethod(writer, typeName, "GreaterThanOrEqual", ">="); if (typeName == "String8") { WriteMethod(writer, typeName, "Contains", ""); WriteMethod(writer, typeName, "ContainsExact", ""); WriteMethod(writer, typeName, "StartsWith", ""); } writer.Write(s_fileSuffix); } }
private static void WriteConversion(StreamWriter writer, string template, string toType, string fromType, bool checkLowerBound = true, bool checkUpperBound = true) { if (!checkLowerBound) { template = template.Replace("value < (fromType)toType.MinValue || ", ""); } if (!checkUpperBound) { template = template.Replace(" || value > (fromType)toType.MaxValue", ""); } template = template .Replace("toType", toType) .Replace("fromType", fromType) .Replace("FromType", SupportedTypes.ToClassName(fromType)); writer.Write(template); }
private static void WriteLinePerType(StreamWriter writer, string prefix, string lineTemplate, string suffix) { writer.Write(prefix); foreach (string typeName in SupportedTypes.PrimitiveTypes) { writer.Write(lineTemplate.Replace("typeName", typeName).Replace("TypeName", SupportedTypes.ToClassName(typeName))); } writer.Write(suffix); }