internal static PaddedFormat GetOrCreate(string format, ref int pos) { _ = WhiteSpaceReader.TryRead(format, ref pos, out var prePadding); if (DoubleFormatReader.TryRead(format, ref pos, out var valueFormat)) { _ = WhiteSpaceReader.TryRead(format, ref pos, out var postPadding); return(new PaddedFormat(prePadding, valueFormat, postPadding)); } return(PaddedFormat.CreateUnknown(prePadding, format)); }
internal static bool TryParse(string?format, [MaybeNullWhen(false)] out QuantityFormat <TUnit> result) { if (string.IsNullOrWhiteSpace(format)) { result = QuantityFormat <TUnit> .CreateUnknown("{nameof(format) is null}", Unit <TUnit> .Default); return(false); } if (Cache.TryGetValue(format, out result)) { return(true); } var pos = 0; _ = WhiteSpaceReader.TryRead(format, ref pos); var end = format.Length; if (TryReadPrefix(format, ref pos)) { end = format.LastIndexOf('}'); if (end < 0) { result = QuantityFormat <TUnit> .CreateUnknown(format, Unit <TUnit> .Default); return(false); } if (!WhiteSpaceReader.IsRestWhiteSpace(format, end + 1)) { result = QuantityFormat <TUnit> .CreateUnknown(format, Unit <TUnit> .Default); return(false); } } var trimmedFormat = pos != end ? format.Substring(pos, end - pos) : format; var success = CompositeFormatParser.TryParse(trimmedFormat, out result); if (success) { Cache.Add(format, result); } return(success); }
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { var text = value as string; if (string.IsNullOrWhiteSpace(text)) { return(null); } var settings = Settings.Instance; var pos = 0; _ = WhiteSpaceReader.TryRead(text, ref pos); var indexOf = text.IndexOf("1/", pos, StringComparison.Ordinal); if (indexOf >= 0) { pos = indexOf + 2; } if (SymbolAndPowerReader.TryRead(text, ref pos, out IReadOnlyList <SymbolAndPower> result)) { if (WhiteSpaceReader.IsRestWhiteSpace(text, pos)) { var unitAndPowers = result.Select(sap => UnitAndPower.Create(settings.AllUnits.Single(x => x.Symbol == sap.Symbol), sap.Power)) .ToList(); var unitParts = new UnitParts(unitAndPowers); if (indexOf < 0) { return(unitParts); } return(unitParts.Inverse()); } } return(text); }
/// <inheritdoc /> public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { if (!this.initialized) { this.Initialize(); } var message = this.errorText.ToString(); if (!(targetType == typeof(VolumetricFlow) || targetType == typeof(VolumetricFlow?))) { message += $"{this.GetType().Name} does not support converting to {targetType.Name}"; } if (message != string.Empty) { message = message.TrimEnd('\r', '\n'); if (Is.DesignMode) { throw new InvalidOperationException(message); } return(message); } if (value == null) { return(null); } if (value is double) { return(new VolumetricFlow((double)value, this.unit.Value)); } var text = value as string; if (string.IsNullOrEmpty(text)) { return(null); } var unitInput = this.UnitInput ?? Wpf.UnitInput.ScalarOnly; switch (unitInput) { case Wpf.UnitInput.ScalarOnly: { double d; if (double.TryParse(text, NumberStyles.Float, culture, out d)) { return(new VolumetricFlow(d, this.unit.Value)); } VolumetricFlow result; if (VolumetricFlow.TryParse(text, NumberStyles.Float, culture, out result)) { return($"#{text}#"); // returning modified text so that TypeConverter fails and we get an error } return(text); // returning raw to trigger error } case Wpf.UnitInput.SymbolAllowed: { double d; int pos = 0; WhiteSpaceReader.TryRead(text, ref pos); if (DoubleReader.TryRead(text, ref pos, NumberStyles.Float, culture, out d)) { WhiteSpaceReader.TryRead(text, ref pos); if (pos == text.Length) { return(new VolumetricFlow(d, this.unit.Value)); } } goto case Wpf.UnitInput.SymbolRequired; } case Wpf.UnitInput.SymbolRequired: { VolumetricFlow result; if (VolumetricFlow.TryParse(text, NumberStyles.Float, culture, out result)) { return(result); } return(text); } default: throw new ArgumentOutOfRangeException(); } }