private string Format(Length length, IFormatProvider formatProvider, CustomLengthFormatInfo formatInfo) { var formattedLength = formatInfo.LengthConverter(length); string valueStr = formattedLength.Value.ToString(formatInfo.ValueFormat, ResolveValueFormatProvider(formatProvider)); var unitFormatter = formatProvider.GetFormat <ICustomFormatter <LengthUnit> >(); string unitStr = unitFormatter != null ? unitFormatter.Format(formatInfo.UnitFormat, formattedLength.Unit, ResolveUnitFormatProvider(formatProvider)) : formattedLength.Unit.ToString(); return(string.Concat(valueStr, " ", unitStr)); }
private bool TryGetFormatInfo(string format, IFormatProvider formatProvider, out CustomLengthFormatInfo formatInfo) { Converter <Length, Length> lengthConverter = e => e; string valueFormat = "0.########"; string unitFormat = "s"; int index = 0; while (index < format.Length) { string token; if (IsTokenSeparator(format, index, out string separator)) { index += separator.Length; continue; } else if (MatchesAnyFormattableUnitToken(format, index, out token, out var unit)) { lengthConverter = e => e.Convert(unit); } else if (MatchesTokenBegin(format, index, "v", CanReadTokenUntilNextSeparator, out token)) { valueFormat = token.Substring(1); } else if (MatchesTokenBegin(format, index, "u", CanReadTokenUntilNextSeparator, out token)) { unitFormat = token.Substring(1); } else { token = new string(format[index], 1); } index += token.Length; } formatInfo = new CustomLengthFormatInfo(lengthConverter, valueFormat, unitFormat); return(true); }