namespace Ubisoft
{
///
/// Utility class for dealing with bool type.
///
public static class HtBool
{
public const byte BOOL_AS_BYTE_FALSE = 0;
public const byte BOOL_AS_BYTE_TRUE = 1;
///
/// Convert the specified string representation of a logical value to its Boolean equivalent.
///
/// A string containing the value to convert.
/// Valid values: 'true', 'True', 'TRUE', '1', 'false', 'False', 'FALSE', '0'
/// true if s is 'true', 'True', 'True', or '1', otherwise false
/// true if s was converted successfully; otherwise false
public static bool TryParse(string s, out bool result)
{
bool returnValue = !string.IsNullOrEmpty(s);
if (returnValue)
{
if (s == "1")
{
result = true;
}
else if (s == "0")
{
result = false;
}
else
{
string normalisedValue = s.ToLower();
if (normalisedValue == "true")
{
result = true;
}
else if (normalisedValue == "false")
{
result = false;
}
else
{
result = false;
returnValue = false;
}
}
}
else
{
result = false;
}
return returnValue;
}
///
/// Convert the specified string representation of a byte value used as bool.
///
/// A string containing the value to convert.
/// Valid values: 'true', 'True', 'TRUE', '1', 'false', 'False', 'FALSE', '0'
/// 1 if s is 'true', 'True', 'True', or '1', otherwise 0
/// true if s was converted successfully; otherwise false
public static bool TryParseToByte(string s, out byte result)
{
bool returnValue = TryParse(s, out bool valueAsBool);
if (returnValue)
{
result = valueAsBool ? BOOL_AS_BYTE_TRUE : BOOL_AS_BYTE_FALSE;
}
else
{
result = BOOL_AS_BYTE_FALSE;
}
return returnValue;
}
///
/// Convert the specified string representation of a byte value used as bool.
///
/// A string containing the value to convert.
/// Valid values: 'true', 'True', 'TRUE', '1', 'false', 'False', 'FALSE', '0'
/// 1 if s is 'true', 'True', 'True', or '1', otherwise 0
public static byte ParseToByte(string s)
{
_ = TryParseToByte(s, out byte returnValue);
return returnValue;
}
}
}