Exemplo n.º 1
0
        private string Format(Weight weight, IFormatProvider formatProvider, CustomWeightFormatInfo formatInfo)
        {
            var    formattedWeight = formatInfo.WeightConverter(weight);
            string valueStr        = formattedWeight.Value.ToString(formatInfo.ValueFormat, ResolveValueFormatProvider(formatProvider));

            var    unitFormatter = formatProvider.GetFormat <ICustomFormatter <WeightUnit> >();
            string unitStr       = unitFormatter != null
                ? unitFormatter.Format(formatInfo.UnitFormat, formattedWeight.Unit, ResolveUnitFormatProvider(formatProvider))
                : formattedWeight.Unit.ToString();

            return(string.Concat(valueStr, " ", unitStr));
        }
Exemplo n.º 2
0
        private bool TryGetFormatInfo(string format, IFormatProvider formatProvider, out CustomWeightFormatInfo formatInfo)
        {
            Converter <Weight, Weight> weightConverter = 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 (MatchesAnyUnitToken(format, index, out token, out var unit))
                {
                    weightConverter = e => e.Convert(unit);
                }
                else if (MatchesToken(format, index, "lbs", out token))
                {
                    weightConverter = e => e.Convert(WeightUnit.Pound);
                }
                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 CustomWeightFormatInfo(weightConverter, valueFormat, unitFormat);
            return(true);
        }