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    /** Single Quote [']. */
037    public static final char QUOTE = '\'';
038
039    /** Space. */
040    public static final char SPACE = ' ';
041
042    /** Tab. */
043    public static final char TAB = '\t';
044
045    /**
046     * Converts a digit into an uppercase hexadecimal character or the null character if invalid.
047     *
048     * @param digit a number 0 - 15
049     * @return the hex character for that digit or '\0' if invalid
050     */
051    public static char getUpperCaseHex(final int digit) {
052        if (digit < 0 || digit >= 16) {
053            return '\0';
054        }
055        return digit < 10 ? getNumericalDigit(digit) : getUpperCaseAlphaDigit(digit);
056    }
057
058    /**
059     * Converts a digit into an lowercase hexadecimal character or the null character if invalid.
060     *
061     * @param digit a number 0 - 15
062     * @return the hex character for that digit or '\0' if invalid
063     */
064    public static char getLowerCaseHex(final int digit) {
065        if (digit < 0 || digit >= 16) {
066            return '\0';
067        }
068        return digit < 10 ? getNumericalDigit(digit) : getLowerCaseAlphaDigit(digit);
069    }
070
071    private static char getNumericalDigit(final int digit) {
072        return (char) ('0' + digit);
073    }
074
075    private static char getUpperCaseAlphaDigit(final int digit) {
076        return (char) ('A' + digit - 10);
077    }
078
079    private static char getLowerCaseAlphaDigit(final int digit) {
080        return (char) ('a' + digit - 10);
081    }
082
083    private Chars() {
084    }
085}