Turn a netmask/hostmask string into a prefix length.

        Args:
            ip_str: A netmask or hostmask, formatted as an IP address.

        Returns:
            The prefix length as an integer.

        Raises:
            NetmaskValueError: If the input is not a netmask or hostmask.

def _prefix_from_ip_string(self, ip_str):
        """Turn a netmask/hostmask string into a prefix length.

        Args:
            ip_str: A netmask or hostmask, formatted as an IP address.

        Returns:
            The prefix length as an integer.

        Raises:
            NetmaskValueError: If the input is not a netmask or hostmask.

        """
        # Parse the netmask/hostmask like an IP address.
        try:
            ip_int = self._ip_int_from_string(ip_str)
        except AddressValueError:
            raise NetmaskValueError('%s is not a valid netmask' % ip_str)

        # Try matching a netmask (this would be /1*0*/ as a bitwise regexp).
        # Note that the two ambiguous cases (all-ones and all-zeroes) are
        # treated as netmasks.
        try:
            return self._prefix_from_ip_int(ip_int)
        except NetmaskValueError:
            pass

        # Invert the bits, and try matching a /0+1+/ hostmask instead.
        ip_int ^= self._ALL_ONES
        try:
            return self._prefix_from_ip_int(ip_int)
        except NetmaskValueError:
            raise NetmaskValueError('%s is not a valid netmask' % ip_str)