• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lduchosal / ipnetwork / 808

17 Aug 2025 08:25AM UTC coverage: 93.223% (-1.0%) from 94.226%
808

push

appveyor

web-flow
Chore: cleanup, breaking changes, enum, tryparse, exception, static ListIPAddress (#363)

* Chore: huge cleanup, enum, tryparse, exception, static ListIPAddress, important changes : IPNetwork comparison and sort order have change to reflect expected behavoir
* Fix: obsolete enums
* Fix: network sorting and member comparison
* Chore: upgrade version number 3.3

1802 of 1933 relevant lines covered (93.22%)

726934.37 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.12
/src/System.Net.IPNetwork/IPNetwork2ToCidr.cs
1
// <copyright file="IPNetwork2ToCidr.cs" company="IPNetwork">
2
// Copyright (c) IPNetwork. All rights reserved.
3
// </copyright>
4

5
namespace System.Net;
6

7
using System.Net.Sockets;
8
using System.Numerics;
9

10
/// <summary>
11
/// ToCidr.
12
/// </summary>
13
public sealed partial class IPNetwork2
14
{
15
    /// <summary>
16
    /// Convert netmask to CIDR
17
    ///  255.255.255.0 -> 24
18
    ///  255.255.0.0   -> 16
19
    ///  255.0.0.0     -> 8.
20
    /// </summary>
21
    /// <param name="netmask">An IP Address representing the CIDR to convert.</param>
22
    /// <returns>A byte representing the CIDR converted from the netmask.</returns>
23
    public static byte ToCidr(IPAddress netmask)
24
    {
33✔
25
        bool cidrified = InternalToCidr(false, netmask, out byte cidr);
33✔
26
        if (!cidrified)
30✔
27
        {
×
28
            throw new ArgumentException(nameof(netmask));
×
29
        }
30
        return cidr;
30✔
31
    }
30✔
32

33
    /// <summary>
34
    /// Convert netmask to CIDR
35
    ///  255.255.255.0 -> 24
36
    ///  255.255.0.0   -> 16
37
    ///  255.0.0.0     -> 8.
38
    /// </summary>
39
    /// <param name="netmask">An IPAddress representing the CIDR to convert.</param>
40
    /// <param name="cidr">A byte representing the netmask in cidr format (/24).</param>
41
    /// <returns>true if netmask was converted successfully; otherwise, false.</returns>
42
    public static bool TryToCidr(IPAddress netmask, out byte cidr)
43
    {
4,001,108✔
44
        return InternalToCidr(true, netmask, out cidr);
4,001,108✔
45
    }
4,001,108✔
46

47
    private static bool InternalToCidr(bool tryParse, IPAddress netmask, out byte cidr)
48
    {
4,001,141✔
49
        if (netmask == null)
4,001,141✔
50
        {
2✔
51
            if (!tryParse)
2✔
52
            {
1✔
53
                throw new ArgumentNullException(nameof(netmask));
1✔
54
            }
55

56
            cidr = 0;
1✔
57
            return false;
1✔
58
        }
59

60
        TryToBigInteger(netmask, out BigInteger uintNetmask2);
4,001,139✔
61
        bool cidrified = InternalToCidr(tryParse, uintNetmask2, netmask.AddressFamily, out cidr);
4,001,139✔
62
        return cidrified;
4,001,137✔
63
    }
4,001,138✔
64

65
    /// <summary>
66
    /// Convert netmask to CIDR
67
    ///  255.255.255.0 -> 24
68
    ///  255.255.0.0   -> 16
69
    ///  255.0.0.0     -> 8.
70
    /// </summary>
71
    /// <param name="tryParse">Whether to throw exception or not during conversion.</param>
72
    /// <param name="netmask">A number representing the netmask to convert.</param>
73
    /// <param name="family">Either IPv4 or IPv6.</param>
74
    /// <param name="cidr">A byte representing the netmask in cidr format (/24).</param>
75
    private static bool InternalToCidr(bool tryParse, BigInteger netmask, AddressFamily family, out byte cidr)
76
    {
4,001,139✔
77
        if (!InternalValidNetmask(netmask, family))
4,001,139✔
78
        {
7✔
79
            if (!tryParse)
7✔
80
            {
2✔
81
                throw new ArgumentException("netmask");
2✔
82
            }
83

84
            cidr = 0;
5✔
85
            return false;
5✔
86
        }
87

88
        byte cidr2 = BitsSet(netmask);
4,001,132✔
89
        cidr = cidr2;
4,001,132✔
90
        return true;
4,001,132✔
91
    }
4,001,137✔
92
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc