001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.util;
018
019/**
020 * <em>Consider this class private.</em>
021 */
022public final class Chars {
023
024    /** Carriage Return. */
025    public static final char CR = '\r';
026
027    /** Double Quote. */
028    public static final char DQUOTE = '\"';
029
030    /** Equals '='. */
031    public static final char EQ = '=';
032
033    /** Line Feed. */
034    public static final char LF = '\n';
035
036    /** NUL. */
037        public static final char NUL = 0;
038
039        /** Single Quote [']. */
040    public static final char QUOTE = '\'';
041
042    /** Space. */
043    public static final char SPACE = ' ';
044
045    /** Tab. */
046    public static final char TAB = '\t';
047
048    /**
049     * Converts a digit into an upper-case hexadecimal character or the null character if invalid.
050     *
051     * @param digit a number 0 - 15
052     * @return the hex character for that digit or '\0' if invalid
053     */
054    public static char getUpperCaseHex(final int digit) {
055        if (digit < 0 || digit >= 16) {
056            return '\0';
057        }
058        return digit < 10 ? getNumericalDigit(digit) : getUpperCaseAlphaDigit(digit);
059    }
060
061    /**
062     * Converts a digit into an lower-case hexadecimal character or the null character if invalid.
063     *
064     * @param digit a number 0 - 15
065     * @return the hex character for that digit or '\0' if invalid
066     */
067    public static char getLowerCaseHex(final int digit) {
068        if (digit < 0 || digit >= 16) {
069            return '\0';
070        }
071        return digit < 10 ? getNumericalDigit(digit) : getLowerCaseAlphaDigit(digit);
072    }
073
074    private static char getNumericalDigit(final int digit) {
075        return (char) ('0' + digit);
076    }
077
078    private static char getUpperCaseAlphaDigit(final int digit) {
079        return (char) ('A' + digit - 10);
080    }
081
082    private static char getLowerCaseAlphaDigit(final int digit) {
083        return (char) ('a' + digit - 10);
084    }
085
086    private Chars() {
087    }
088}