Repository: dotnet/runtime
File: src/libraries/Common/src/System/Net/IPv4AddressHelper.Common.cs (C#)

Code before:
            {
                int ch = ToUShort(name[start]);

                if (allowIPv6)
                {
                    // For an IPv4 address nested inside an IPv6 address, the terminator is either the IPv6 address terminator (']'), prefix ('/') or ScopeId ('%')
                    if (ch == ']' || ch == '/' || ch == '%')
                    {
                        break;
                    }
                }
                else if (ch == '/' || ch == '\\' || (notImplicitFile && (ch == ':' || ch == '?' || ch == '#')))
                {
                    // For a normal IPv4 address, the terminator is the prefix ('/' or its counterpart, '\'). If notImplicitFile is set, the terminator
                    // is one of the characters which signify the start of the rest of the URI - the port number (':'), query string ('?') or fragment ('#')

                    break;
                }

                // An explicit cast to an unsigned integer forces character values preceding '0' to underflow, eliminating one comparison below.
                uint parsedCharacter = (uint)(ch - '0');

                if (parsedCharacter < IPv4AddressHelper.Decimal)
                {
                    if (!haveNumber && parsedCharacter == 0 &&
                        (uint)(start + 1) < (uint)name.Length && char.IsBetween((char)ToUShort(name[start + 1]), '0', '9'))
                    {
                        // Octal is not allowed in canonical format.
                        return false;
                    }

                    haveNumber = true;
                    number = number * IPv4AddressHelper.Decimal + parsedCharacter;
                    if (number > byte.MaxValue)
                    {
                        return false;
                    }
                }
                else if (ch == '.')
                {
                    // If the current character is not an integer, it may be the IPv4 component separator ('.')

                    if (!haveNumber)
                    {
                        return false;
                    }

                    ++dots;
                    haveNumber = false;
                    number = 0;
                }

Review comment (suggestion):
[nitpick] The octal detection logic uses `char.IsBetween((char)ToUShort(name[start + 1]), '0', '9')`, which is inconsistent with the validation pattern used throughout the rest of the method. The existing code uses `(uint)(ch - '0') < Decimal` to check if a character is a digit (see line 161).

For consistency and to match the pattern used elsewhere in this method, consider using:
```csharp
if (!haveNumber && parsedCharacter == 0 &&
    (uint)(start + 1) < (uint)name.Length && 
    (uint)(ToUShort(name[start + 1]) - '0') < Decimal)
```

While the current implementation works correctly for ASCII digits, using the established pattern improves code consistency and maintainability.
```suggestion
                        (uint)(start + 1) < (uint)name.Length && (uint)(ToUShort(name[start + 1]) - '0') < Decimal)
```

Code after:
            {
                int ch = ToUShort(name[start]);

                if (allowIPv6)
                {
                    // For an IPv4 address nested inside an IPv6 address, the terminator is either the IPv6 address terminator (']'), prefix ('/') or ScopeId ('%')
                    if (ch == ']' || ch == '/' || ch == '%')
                    {
                        break;
                    }
                }
                else if (ch == '/' || ch == '\\' || (notImplicitFile && (ch == ':' || ch == '?' || ch == '#')))
                {
                    // For a normal IPv4 address, the terminator is the prefix ('/' or its counterpart, '\'). If notImplicitFile is set, the terminator
                    // is one of the characters which signify the start of the rest of the URI - the port number (':'), query string ('?') or fragment ('#')

                    break;
                }

                // An explicit cast to an unsigned integer forces character values preceding '0' to underflow, eliminating one comparison below.
                uint parsedCharacter = (uint)(ch - '0');

                if (parsedCharacter < IPv4AddressHelper.Decimal)
                {
                    if (!haveNumber && parsedCharacter == 0 &&
                        (uint)(start + 1) < (uint)name.Length && char.IsAsciiDigit((char)ToUShort(name[start + 1])))
                    {
                        // Octal is not allowed in canonical format.
                        return false;
                    }

                    haveNumber = true;
                    number = number * IPv4AddressHelper.Decimal + parsedCharacter;
                    if (number > byte.MaxValue)
                    {
                        return false;
                    }
                }
                else if (ch == '.')
                {
                    // If the current character is not an integer, it may be the IPv4 component separator ('.')

                    if (!haveNumber)
                    {
                        return false;
                    }

                    ++dots;
                    haveNumber = false;
                    number = 0;
                }