35 lines
918 B
C#
35 lines
918 B
C#
|
|
using System;
|
||
|
|
using System.Linq;
|
||
|
|
using System.Text.RegularExpressions;
|
||
|
|
using UnityEngine.TestTools;
|
||
|
|
|
||
|
|
namespace BracerLib.Utility
|
||
|
|
{
|
||
|
|
[ExcludeFromCoverage]
|
||
|
|
public static class RegexExtensions
|
||
|
|
{
|
||
|
|
public static T GrabValue<T>(this MatchCollection matches, string property)
|
||
|
|
{
|
||
|
|
var type = typeof(T);
|
||
|
|
var underlyingType = Nullable.GetUnderlyingType(type);
|
||
|
|
type = underlyingType ?? type;
|
||
|
|
|
||
|
|
foreach (var m in matches.Cast<Match>())
|
||
|
|
{
|
||
|
|
if (m.Groups[1].Value != property)
|
||
|
|
continue;
|
||
|
|
|
||
|
|
var value = m.Groups[2].Value;
|
||
|
|
|
||
|
|
if (type != typeof(bool))
|
||
|
|
return (T)Convert.ChangeType(value, type);
|
||
|
|
|
||
|
|
if (bool.TryParse(value, out var b))
|
||
|
|
return (T)Convert.ChangeType(b, type);
|
||
|
|
}
|
||
|
|
|
||
|
|
return default;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|