Exemple #1
0
 bool TryGetExistingExponent(string s, out ExponentMeasureUnit u)
 {
     u = null;
     if (_allUnits.TryGetValue(s, out var unit))
     {
         Debug.Assert(unit is ExponentMeasureUnit);
         u = (ExponentMeasureUnit)unit;
         return(true);
     }
     return(false);
 }
Exemple #2
0
        bool TryParseExponent(Match match, out ExponentMeasureUnit u, bool skipFirstLookup = false)
        {
            if (!skipFirstLookup && TryGetExistingExponent(match.ToString(), out u))
            {
                return(true);
            }
            u = null;
            // Handling dimensionless unit.
            var factor = match.Groups[4].Value;

            if (factor.Length > 0)
            {
                var f = ParseFactor(factor, ExpFactor.Neutral);
                u = RegisterPrefixed(f, MeasureStandardPrefix.None, MeasureUnit.None);
                return(true);
            }
            string sExp = match.Groups[3].Value;
            int    exp  = sExp.Length > 0 ? Int32.Parse(sExp) : 1;

            if (exp == 0)
            {
                u = MeasureUnit.None;
                return(true);
            }
            string withoutExp = match.Groups[1].Value + match.Groups[2].Value;

            if (_allUnits.TryGetValue(withoutExp, out var unit))
            {
                if (exp == 1 && unit is ExponentMeasureUnit direct)
                {
                    u = direct;
                    return(true);
                }
                if (!(unit is AtomicMeasureUnit atomic))
                {
                    return(false);
                }
                u = RegisterExponent(exp, atomic);
                return(true);
            }
            ExpFactor adjustment = ExpFactor.Neutral;

            foreach (Capture f in match.Groups[1].Captures)
            {
                adjustment = ParseFactor(f.Value, adjustment);
            }
            string withoutAdjustment = match.Groups[2].Value;

            if (_allUnits.TryGetValue(withoutAdjustment, out unit))
            {
                if (!(unit is AtomicMeasureUnit basic))
                {
                    return(false);
                }
                if (basic is PrefixedMeasureUnit prefix)
                {
                    adjustment = adjustment.Multiply(prefix.AdjustmentFactor).Multiply(prefix.Prefix.Factor);
                    basic      = prefix.AtomicMeasureUnit;
                }
                u = CreateExponentPrefixed(exp, adjustment, basic);
                return(true);
            }
            var p = MeasureStandardPrefix.FindPrefix(withoutAdjustment);

            if (p == MeasureStandardPrefix.None)
            {
                return(false);
            }
            withoutAdjustment = withoutAdjustment.Substring(p.Abbreviation.Length);
            if (withoutAdjustment.Length == 0)
            {
                return(false);
            }
            if (_allUnits.TryGetValue(withoutAdjustment, out unit))
            {
                if (!(unit is AtomicMeasureUnit basic))
                {
                    return(false);
                }
                if (basic is PrefixedMeasureUnit prefix)
                {
                    return(false);
                }
                adjustment = adjustment.Multiply(p.Factor);
                u          = CreateExponentPrefixed(exp, adjustment, basic);
                return(true);
            }
            return(false);
        }
        internal ExponentMeasureUnit RegisterExponent(int exp, AtomicMeasureUnit u)
        {
            var names = ExponentMeasureUnit.ComputeNames(exp, u);

            return(Register(names.A, names.N, () => new ExponentMeasureUnit(this, names, exp, u), null));
        }