Commit a7adc2fe authored by Brickner_cp's avatar Brickner_cp

Add TryParse() method to IpV4Address.

parent 81bf001d
...@@ -110,5 +110,27 @@ namespace PcapDotNet.Packets.Test ...@@ -110,5 +110,27 @@ namespace PcapDotNet.Packets.Test
Assert.IsNotNull(new IpV4Address(null)); Assert.IsNotNull(new IpV4Address(null));
Assert.Fail(); Assert.Fail();
} }
[TestMethod]
public void TryParseTest()
{
IpV4Address actual;
Assert.IsTrue(IpV4Address.TryParse("1.2.3.4", out actual));
Assert.AreEqual(new IpV4Address("1.2.3.4"), actual);
Assert.IsFalse(IpV4Address.TryParse(null, out actual));
Assert.IsFalse(IpV4Address.TryParse("1", out actual));
Assert.IsFalse(IpV4Address.TryParse("1.", out actual));
Assert.IsFalse(IpV4Address.TryParse("1.2", out actual));
Assert.IsFalse(IpV4Address.TryParse("1.2.", out actual));
Assert.IsFalse(IpV4Address.TryParse("1.2.3", out actual));
Assert.IsFalse(IpV4Address.TryParse("1.2.3.", out actual));
Assert.IsFalse(IpV4Address.TryParse("1.2.3.a", out actual));
Assert.IsFalse(IpV4Address.TryParse("a.2.3.4", out actual));
Assert.IsFalse(IpV4Address.TryParse("1.a.3.4", out actual));
Assert.IsFalse(IpV4Address.TryParse("1.2.a.4", out actual));
Assert.IsFalse(IpV4Address.TryParse("1.2.3.a", out actual));
Assert.IsFalse(IpV4Address.TryParse("256.2.3.4", out actual));
}
} }
} }
...@@ -59,6 +59,46 @@ namespace PcapDotNet.Packets.IpV4 ...@@ -59,6 +59,46 @@ namespace PcapDotNet.Packets.IpV4
byte.Parse(values[3], CultureInfo.InvariantCulture)); byte.Parse(values[3], CultureInfo.InvariantCulture));
} }
/// <summary>
/// Converts the string representation of an IPv4 address (1.2.3.4) to its IPv4 address equivalent.
/// A return value indicates whether the conversion succeeded.
/// </summary>
/// <param name="s">A string containing the IPv4 address to convert (1.2.3.4).</param>
/// <param name="result">
/// When this method returns, contains the IPv4 address value equivalent of the IPv4 address contained in s, if the conversion succeeded,
/// or zero IPv4 address if the conversion failed.
/// The conversion fails if the s parameter is null or String.Empty or is not of the correct format. This parameter is passed uninitialized.
/// </param>
/// <returns></returns>
public static bool TryParse(string s, out IpV4Address result)
{
if (s == null)
{
result = Zero;
return false;
}
string[] valuesStrings = s.Split('.');
if (valuesStrings.Length != 4)
{
result = Zero;
return false;
}
byte[] values = new byte[4];
for (int i = 0; i != 4; ++i)
{
if (!byte.TryParse(valuesStrings[i], NumberStyles.None, CultureInfo.InvariantCulture, out values[i]))
{
result = Zero;
return false;
}
}
result = new IpV4Address(BitSequence.Merge(values[0], values[1], values[2], values[3]));
return true;
}
/// <summary> /// <summary>
/// Gets the address value as a 32 bit integer. /// Gets the address value as a 32 bit integer.
/// </summary> /// </summary>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment