/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.trino.sql.tree;

import com.google.common.io.BaseEncoding;
import io.trino.sql.parser.ParsingException;

import java.util.Arrays;
import java.util.Optional;
import java.util.regex.Pattern;

import static java.util.Locale.ENGLISH;
import static java.util.Objects.requireNonNull;

public class BinaryLiteral
        extends Literal
{
    // the grammar could possibly include whitespace in the value it passes to us
    private static final Pattern WHITESPACE_PATTERN = Pattern.compile("[ \\r\\n\\t]");
    private static final Pattern NOT_HEX_DIGIT_PATTERN = Pattern.compile(".*[^A-F0-9].*");

    private final byte[] value;

    public BinaryLiteral(String value)
    {
        this(Optional.empty(), value);
    }

    public BinaryLiteral(Optional<NodeLocation> location, String value)
    {
        super(location);
        requireNonNull(value, "value is null");
        String hexString = WHITESPACE_PATTERN.matcher(value).replaceAll("").toUpperCase(ENGLISH);
        if (NOT_HEX_DIGIT_PATTERN.matcher(hexString).matches()) {
            throw new ParsingException("Binary literal can only contain hexadecimal digits", location.get());
        }
        if (hexString.length() % 2 != 0) {
            throw new ParsingException("Binary literal must contain an even number of digits", location.get());
        }
        this.value = BaseEncoding.base16().decode(hexString);
    }

    public BinaryLiteral(NodeLocation location, String value)
    {
        this(Optional.of(location), value);
    }

    /**
     * Return the valued as a hex-formatted string with upper-case characters
     */
    public String toHexString()
    {
        return BaseEncoding.base16().encode(value);
    }

    public byte[] getValue()
    {
        return value.clone();
    }

    @Override
    public <R, C> R accept(AstVisitor<R, C> visitor, C context)
    {
        return visitor.visitBinaryLiteral(this, context);
    }

    @Override
    public boolean equals(Object o)
    {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        BinaryLiteral that = (BinaryLiteral) o;
        return Arrays.equals(value, that.value);
    }

    @Override
    public int hashCode()
    {
        return Arrays.hashCode(value);
    }

    @Override
    public boolean shallowEquals(Node other)
    {
        if (!sameClass(this, other)) {
            return false;
        }

        return Arrays.equals(value, ((BinaryLiteral) other).value);
    }
}

@@ -583,16 +583,59 @@
 .tree;%0A%0A
+import com.google.common.base.CharMatcher;%0A
 import c
@@ -668,16 +668,16 @@
 coding;%0A
-
 import i
@@ -769,40 +769,8 @@
 nal;
-%0Aimport java.util.regex.Pattern;
 %0A%0Aim
@@ -1016,23 +1016,27 @@
 c final 
-Patt
+CharMatch
 er
-n
  WHITESP
@@ -1043,48 +1043,41 @@
 ACE_
-PATT
+MATCH
 ER
-N
  = 
-Pattern.compile(%22%5B %5C%5Cr%5C%5Cn%5C%5Ct%5D%22
+CharMatcher.whitespace(
 );%0A 
@@ -1104,20 +1104,20 @@
 nal 
-Pattern NOT_
+CharMatcher 
 HEX_
@@ -1126,49 +1126,120 @@
 GIT_
-PATT
+MATCH
 ER
-N
  = 
-Pattern.compile(%22.*%5B%5EA-F0-9%5D.*%22
+CharMatcher.inRange('A', 'F')%0A            .or(CharMatcher.inRange('0', '9'))%0A            .precomputed(
 );%0A%0A
@@ -1555,44 +1555,32 @@
 ACE_
-PATTERN.matcher(value).replaceAll(%22%22
+MATCHER.removeFrom(value
 ).to
@@ -1615,12 +1615,9 @@
 if (
-NOT_
+!
 HEX_
@@ -1626,23 +1626,28 @@
 GIT_
-PATT
+MATCH
 ER
-N
 .matche
-r
+sAllOf
 (hex
@@ -1657,18 +1657,8 @@
 ing)
-.matches()
 ) %7B%0A

Replace Pattern usage with CharMatcher in BinaryLiteral