internal static bool IsValidPart(char[] chars, bool allowLeadingZeros)
{
var result = true;
if (chars.Length == 0)
{
// empty labels are not allowed
result = false;
}
// 0 is fine, but 00 is not.
// 0A counts as an alpha numeric string where zeros are not counted
if (!allowLeadingZeros
&& chars.Length > 1
&& chars[0] == '0'
&& chars.All(c => Char.IsDigit(c)))
{
// no leading zeros in labels allowed
result = false;
}
else
{
result &= chars.All(c => IsLetterOrDigitOrDash(c));
}
return result;
}