ant-design-blazor/components/core/CssSizeLength.cs
Alsein 82ef62b7a7 feat: add simple table component (#86)
* fix: add @commitlint/config-conventional to devDependencies

* feat: add basic table implementation

* feat: continue adding definitions

* feat: end of work in 20200414

* fix: add readonly to csssizelength

* fix: warnings from table

* fix: add ignorecase to csssizelength

* feat: refactor table

* feat: add simple table

Co-authored-by: ElderJames <shunjiey@hotmail.com>
2020-05-30 01:46:41 +08:00

93 lines
2.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
namespace AntDesign
{
internal enum CssSizeLengthUnit
{
Px,
Cm,
Mm,
In,
Pt,
Pc,
Em,
Rem,
Percent,
Vh,
Vw,
Vmin,
Vmax,
Fr,
}
public readonly struct CssSizeLength : IEquatable<CssSizeLength>
{
private readonly int _value;
private readonly CssSizeLengthUnit _unit;
public override string ToString() => _value.ToString(CultureInfo.InvariantCulture) + _unit switch
{
CssSizeLengthUnit.Percent => "%",
_ => Enum.GetName(typeof(CssSizeLengthUnit), _unit).ToLowerInvariant()
};
private CssSizeLength(int value, CssSizeLengthUnit unit)
{
_value = value;
_unit = unit;
}
public CssSizeLength(int value) : this(value, CssSizeLengthUnit.Px)
{
}
public CssSizeLength(string value)
{
value = value?.ToLowerInvariant() ?? throw new ArgumentNullException(nameof(value));
var index = value
.Select((c, i) => ((char c, int i)?)(c, i))
.FirstOrDefault(x => !(x.Value.c == '.' || x.Value.c >= '0' && x.Value.c <= '9'))
?.i
?? value.Length;
if (index == 0)
{
throw new FormatException();
}
_value = int.Parse(value.Substring(0, index), CultureInfo.InvariantCulture);
if (index == value.Length)
{
_unit = CssSizeLengthUnit.Px;
return;
}
if (!Enum.TryParse(value.Substring(index).Trim(), ignoreCase: true, out _unit))
{
throw new FormatException();
}
}
public static implicit operator CssSizeLength(int value) => new CssSizeLength(value);
public static implicit operator CssSizeLength(string value) => new CssSizeLength(value);
public override bool Equals(object obj) => obj is CssSizeLength other && Equals(other);
public override int GetHashCode() => unchecked(_value.GetHashCode() + _unit.GetHashCode());
public static bool operator ==(CssSizeLength left, CssSizeLength right) => left.Equals(right);
public static bool operator !=(CssSizeLength left, CssSizeLength right) => !(left == right);
public bool Equals(CssSizeLength other) => other._value == _value && other._unit == _unit;
}
}