|
| 1 | +using System.Globalization; |
| 2 | + |
| 3 | +namespace GitVersion.Formatting; |
| 4 | + |
| 5 | +internal class LegacyCompositeFormatter : IValueFormatter |
| 6 | +{ |
| 7 | + public int Priority => 1; |
| 8 | + |
| 9 | + public bool TryFormat(object? value, string format, out string result) => |
| 10 | + TryFormat(value, format, CultureInfo.InvariantCulture, out result); |
| 11 | + |
| 12 | + public bool TryFormat(object? value, string format, CultureInfo cultureInfo, out string result) |
| 13 | + { |
| 14 | + result = string.Empty; |
| 15 | + |
| 16 | + if (!HasLegacySyntax(format)) |
| 17 | + return false; |
| 18 | + |
| 19 | + var sections = ParseSections(format); |
| 20 | + var index = GetSectionIndex(value, sections.Length); |
| 21 | + |
| 22 | + if (index >= sections.Length) |
| 23 | + return true; |
| 24 | + |
| 25 | + var section = sections[index]; |
| 26 | + |
| 27 | + // Use absolute value for negative numbers to prevent double negatives |
| 28 | + var valueToFormat = (index == 1 && value != null && IsNumeric(value) && Convert.ToDouble(value) < 0) |
| 29 | + ? Math.Abs(Convert.ToDouble(value)) |
| 30 | + : value; |
| 31 | + |
| 32 | + result = IsQuotedLiteral(section) |
| 33 | + ? UnquoteString(section) |
| 34 | + : FormatWithSection(valueToFormat, section, cultureInfo, sections, index); |
| 35 | + |
| 36 | + return true; |
| 37 | + } |
| 38 | + |
| 39 | + private static bool HasLegacySyntax(string format) => |
| 40 | + !string.IsNullOrEmpty(format) && format.Contains(';') && !format.Contains("??"); |
| 41 | + |
| 42 | + private static string[] ParseSections(string format) |
| 43 | + { |
| 44 | + var sections = new List<string>(); |
| 45 | + var current = new StringBuilder(); |
| 46 | + var inQuotes = false; |
| 47 | + var quoteChar = '\0'; |
| 48 | + |
| 49 | + foreach (var c in format) |
| 50 | + { |
| 51 | + if (!inQuotes && (c == '\'' || c == '"')) |
| 52 | + { |
| 53 | + inQuotes = true; |
| 54 | + quoteChar = c; |
| 55 | + } |
| 56 | + else if (inQuotes && c == quoteChar) |
| 57 | + { |
| 58 | + inQuotes = false; |
| 59 | + } |
| 60 | + else if (!inQuotes && c == ';') |
| 61 | + { |
| 62 | + sections.Add(current.ToString()); |
| 63 | + current.Clear(); |
| 64 | + continue; |
| 65 | + } |
| 66 | + |
| 67 | + current.Append(c); |
| 68 | + } |
| 69 | + |
| 70 | + sections.Add(current.ToString()); |
| 71 | + return [.. sections]; |
| 72 | + } |
| 73 | + |
| 74 | + private static int GetSectionIndex(object? value, int sectionCount) |
| 75 | + { |
| 76 | + if (sectionCount == 1) return 0; |
| 77 | + if (value == null) return sectionCount >= 3 ? 2 : 0; |
| 78 | + |
| 79 | + if (!IsNumeric(value)) return 0; |
| 80 | + |
| 81 | + var num = Convert.ToDouble(value); |
| 82 | + return num switch |
| 83 | + { |
| 84 | + > 0 => 0, |
| 85 | + < 0 when sectionCount >= 2 => 1, |
| 86 | + 0 when sectionCount >= 3 => 2, |
| 87 | + _ => 0 |
| 88 | + }; |
| 89 | + } |
| 90 | + |
| 91 | + private static bool IsNumeric(object value) => |
| 92 | + value is byte or sbyte or short or ushort or int or uint or long or ulong or float or double or decimal; |
| 93 | + |
| 94 | + private static bool IsQuotedLiteral(string section) |
| 95 | + { |
| 96 | + if (string.IsNullOrEmpty(section)) return true; |
| 97 | + var trimmed = section.Trim(); |
| 98 | + return (trimmed.StartsWith('\'') && trimmed.EndsWith('\'')) || |
| 99 | + (trimmed.StartsWith('"') && trimmed.EndsWith('"')); |
| 100 | + } |
| 101 | + |
| 102 | + private static string UnquoteString(string section) |
| 103 | + { |
| 104 | + if (string.IsNullOrEmpty(section)) return string.Empty; |
| 105 | + var trimmed = section.Trim(); |
| 106 | + |
| 107 | + // Handle empty quoted strings like '' and "" |
| 108 | + if (trimmed == "''" || trimmed == "\"\"") |
| 109 | + return string.Empty; |
| 110 | + |
| 111 | + return IsQuoted(trimmed) && trimmed.Length > 2 |
| 112 | + ? trimmed[1..^1] |
| 113 | + : trimmed; |
| 114 | + |
| 115 | + static bool IsQuoted(string s) => |
| 116 | + (s.StartsWith('\'') && s.EndsWith('\'')) || (s.StartsWith('"') && s.EndsWith('"')); |
| 117 | + } |
| 118 | + |
| 119 | + private static string FormatWithSection(object? value, string section, IFormatProvider formatProvider, string[]? sections = null, int index = 0) |
| 120 | + { |
| 121 | + if (string.IsNullOrEmpty(section)) return string.Empty; |
| 122 | + if (IsQuotedLiteral(section)) return UnquoteString(section); |
| 123 | + |
| 124 | + try |
| 125 | + { |
| 126 | + return value switch |
| 127 | + { |
| 128 | + IFormattable formattable => formattable.ToString(section, formatProvider), |
| 129 | + not null when IsValidFormatString(section) => |
| 130 | + string.Format(formatProvider, "{0:" + section + "}", value), |
| 131 | + not null when index > 0 && sections != null && sections.Length > 0 && IsValidFormatString(sections[0]) => |
| 132 | + // For invalid formats in non-first sections, use first section format |
| 133 | + string.Format(formatProvider, "{0:" + sections[0] + "}", value), |
| 134 | + not null => value.ToString() ?? string.Empty, |
| 135 | + _ => section // Only for null values without valid format |
| 136 | + }; |
| 137 | + } |
| 138 | + catch (FormatException) |
| 139 | + { |
| 140 | + // On format exception, try first section format or return value string |
| 141 | + if (index > 0 && sections != null && sections.Length > 0 && IsValidFormatString(sections[0])) |
| 142 | + { |
| 143 | + try |
| 144 | + { |
| 145 | + return string.Format(formatProvider, "{0:" + sections[0] + "}", value); |
| 146 | + } |
| 147 | + catch |
| 148 | + { |
| 149 | + return value?.ToString() ?? section; |
| 150 | + } |
| 151 | + } |
| 152 | + return value?.ToString() ?? section; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + private static bool IsValidFormatString(string format) |
| 157 | + { |
| 158 | + if (string.IsNullOrEmpty(format)) return false; |
| 159 | + |
| 160 | + var firstChar = char.ToUpperInvariant(format[0]); |
| 161 | + return "CDEFGNPXR".Contains(firstChar) || |
| 162 | + format.All(c => "0123456789.,#".Contains(c)); |
| 163 | + } |
| 164 | +} |
0 commit comments