Repository: netty/netty
File: codec-http/src/main/java/io/netty/handler/codec/http/HttpUtil.java (Java)

Code before:
    public static boolean isEncodingSafeStartLineToken(CharSequence token) {
        int i = 0;
        int lenBytes = token.length();
        int modulo = lenBytes % 4;
        int lenInts = modulo == 0 ? lenBytes : lenBytes - modulo;
        for (; i < lenInts; i += 4) {
            long chars = 1L << token.charAt(i) |
                    1L << token.charAt(i + 1) |
                    1L << token.charAt(i + 2) |
                    1L << token.charAt(i + 3);
            if ((chars & ILLEGAL_REQUEST_LINE_TOKEN_OCTET_MASK) != 0) {
                return false;
            }
        }
        for (; i < lenBytes; i++) {
            long ch = 1L << token.charAt(i);
            if ((ch & ILLEGAL_REQUEST_LINE_TOKEN_OCTET_MASK) != 0) {
                return false;
            }
        }
        return true;
    }

    /**
     * Returns {@code true} if and only if the connection can remain open and
     * thus 'kept alive'.  This methods respects the value of the.
     *
     * {@code "Connection"} header first and then the return value of
     * {@link HttpVersion#isKeepAliveDefault()}.
     */
    public static boolean isKeepAlive(HttpMessage message) {
        return !message.headers().containsValue(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE, true) &&
               (message.protocolVersion().isKeepAliveDefault() ||
                message.headers().containsValue(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE, true));
    }

    /**
     * Sets the value of the {@code "Connection"} header depending on the
     * protocol version of the specified message. This getMethod sets or removes
     * the {@code "Connection"} header depending on what the default keep alive
     * mode of the message's protocol version is, as specified by
     * {@link HttpVersion#isKeepAliveDefault()}.
     * <ul>
     * <li>If the connection is kept alive by default:
     *     <ul>
     *     <li>set to {@code "close"} if {@code keepAlive} is {@code false}.</li>
     *     <li>remove otherwise.</li>
     *     </ul></li>
     * <li>If the connection is closed by default:
     *     <ul>
     *     <li>set to {@code "keep-alive"} if {@code keepAlive} is {@code true}.</li>

Review comment (bug):
Why not using s lookup table for this or 
long mask = ((c - 64) >> 63);
return mask & (1L << c);

Assuming c to be >= 0.
I made it by phone so could be wrong but I think you got the idea; mask should use the msb to create a full 0s or 1s mask to produce the actual value. In this way there won't be any comparison.
A lookup table could be used similarly by computing an index to lookup into a table with N + 1 elements 


Code after:
    public static boolean isEncodingSafeStartLineToken(CharSequence token) {
        int i = 0;
        int lenBytes = token.length();
        int modulo = lenBytes % 4;
        int lenInts = modulo == 0 ? lenBytes : lenBytes - modulo;
        for (; i < lenInts; i += 4) {
            long chars = charMask(token, i) |
                    charMask(token, i + 1) |
                    charMask(token, i + 2) |
                    charMask(token, i + 3);
            if ((chars & ILLEGAL_REQUEST_LINE_TOKEN_OCTET_MASK) != 0) {
                return false;
            }
        }
        for (; i < lenBytes; i++) {
            long ch = charMask(token, i);
            if ((ch & ILLEGAL_REQUEST_LINE_TOKEN_OCTET_MASK) != 0) {
                return false;
            }
        }
        return true;
    }

    private static long charMask(CharSequence token, int i) {
        char c = token.charAt(i);
        return c < 64 ? 1L << c : 0;
    }

    /**
     * Returns {@code true} if and only if the connection can remain open and
     * thus 'kept alive'. This method respects the value of the
     * {@code "Connection"} header first and then the return value of
     * {@link HttpVersion#isKeepAliveDefault()}.
     */
    public static boolean isKeepAlive(HttpMessage message) {
        return !message.headers().containsValue(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE, true) &&
               (message.protocolVersion().isKeepAliveDefault() ||
                message.headers().containsValue(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE, true));
    }

    /**
     * Sets the value of the {@code "Connection"} header depending on the
     * protocol version of the specified message. This getMethod sets or removes
     * the {@code "Connection"} header depending on what the default keep alive
     * mode of the message's protocol version is, as specified by
     * {@link HttpVersion#isKeepAliveDefault()}.
     * <ul>
     * <li>If the connection is kept alive by default:
     *     <ul>
     *     <li>set to {@code "close"} if {@code keepAlive} is {@code false}.</li>
     *     <li>remove otherwise.</li>