Click here to Skip to main content
15,896,912 members
Articles / DevOps / Unit Testing

Units of Measure Library for .NET

Rate me:
Please Sign up or sign in to vote.
4.93/5 (7 votes)
20 Jun 2012CPOL8 min read 60.5K   2K   35  
This article introduces a library for handling units of measure.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Globalization;

namespace HDLibrary.UnitsOfMeasure
{
    /// <summary>
    /// Parses the following formats: km/h; m^3; A^1*s^1*V^-1*m^-1; A * s / V * m.
    /// </summary>
    public class DerivedUnitParser : IUnitParser
    {
        IUnitParser abbreviatedUnitParser;

        /// <summary>
        /// Creates a new DerivedUnitParser.
        /// </summary>
        /// <param name="abbreviatedUnitParser">The parser to parse all abbreviated units, e.g. m or V.</param>
        public DerivedUnitParser(IUnitParser abbreviatedUnitParser)
        {
            if (abbreviatedUnitParser == null)
                throw new ArgumentNullException("abbreviatedUnitParser");
            this.abbreviatedUnitParser = abbreviatedUnitParser;
        }

        private UnitPart GetUnitPart(string str, bool negative = false)
        {
            string[] items = str.Split('^');
            if (items.Length < 3)
            {
                Unit u = abbreviatedUnitParser.ParseUnit(items[0], null, null, false);
                if (u != null)
                {
                    int exponent = 1;

                    if (items.Length == 1 || int.TryParse(items[1], out exponent))
                    {
                        if (negative)
                            exponent *= -1;

                        return new UnitPart(u, exponent);
                    }
                }
            }

            return null;
        }

        public Unit ParseUnit(string unitStr, string resultUnitAbbreviation, string resultUnitName, bool throwFormatException)
        {
            if (unitStr == null)
                throw new ArgumentNullException("unitStr");

            unitStr = unitStr.Replace("/", " / ").Replace("*", " * ");
            string[] items = unitStr.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            //km * m^2 / h^-1 * s^2
            if (items.Length % 2 == 0)
                return UnitParserExtension.ReturnNullOrThrowFormatException(string.Format("Invalid format: \"{0}\"", unitStr), throwFormatException);

            bool onDenominator = false;

            List<UnitPart> parts = new List<UnitPart>();

            for (int i = 0; i < items.Length; i += 2)
            {
                UnitPart part = GetUnitPart(items[i], onDenominator);
                if (part == null)
                    return UnitParserExtension.ReturnNullOrThrowFormatException(null, throwFormatException);

                if (i != items.Length - 1)
                {
                    string op = items[i + 1];

                    if (op != "*" && op != "/")
                        return UnitParserExtension.ReturnNullOrThrowFormatException(null, throwFormatException);

                    if (op == "/")
                    {
                        if (onDenominator)
                            return UnitParserExtension.ReturnNullOrThrowFormatException(null, throwFormatException);
                        else
                            onDenominator = true;
                    }
                }

                parts.Add(part);
            }

            return DerivedUnit.GetUnitFromParts(resultUnitAbbreviation, resultUnitName, parts.ToArray());
        }
    }

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student
Germany Germany
Presently I am a student of computer science at the Karlsruhe Institute of Technology in Germany.

Comments and Discussions