// -----------------------------------------------------------------------
//
// Copyright 2014 MIT Licence. See licence.md for more information.
//
// -----------------------------------------------------------------------
namespace Perspex.Controls.Parsers
{
using System;
using System.Collections.Generic;
using System.Linq;
///
/// Parses a string of s for and
/// .
///
public static class GridLengthsParser
{
///
/// Parses a string of s.
///
/// The string.
/// A collection of s.
public static IEnumerable Parse(string s)
{
var parts = s.Split(',').Select(x => x.ToUpperInvariant().Trim());
foreach (var part in parts)
{
if (part == "AUTO")
{
yield return GridLength.Auto;
}
else if (part.EndsWith("*"))
{
var valueString = part.Substring(0, part.Length - 1).Trim();
var value = valueString.Length > 0 ? double.Parse(valueString) : 1;
yield return new GridLength(value, GridUnitType.Star);
}
else if (part.EndsWith("PX"))
{
var value = double.Parse(part.Substring(0, part.Length - 2));
yield return new GridLength(value, GridUnitType.Pixel);
}
else
{
throw new FormatException("Invalid grid length: " + part);
}
}
}
}
}