1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache license, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the license for the specific language governing permissions and 15 * limitations under the license. 16 */ 17 package org.apache.logging.log4j.util; 18 19 /** 20 * <em>Consider this class private.</em> 21 */ 22 public final class Chars { 23 24 /** Carriage Return. */ 25 public static final char CR = '\r'; 26 27 /** Double Quote. */ 28 public static final char DQUOTE = '\"'; 29 30 /** Equals '='. */ 31 public static final char EQ = '='; 32 33 /** Line Feed. */ 34 public static final char LF = '\n'; 35 36 /** NUL. */ 37 public static final char NUL = 0; 38 39 /** Single Quote [']. */ 40 public static final char QUOTE = '\''; 41 42 /** Space. */ 43 public static final char SPACE = ' '; 44 45 /** Tab. */ 46 public static final char TAB = '\t'; 47 48 /** 49 * Converts a digit into an upper-case hexadecimal character or the null character if invalid. 50 * 51 * @param digit a number 0 - 15 52 * @return the hex character for that digit or '\0' if invalid 53 */ 54 public static char getUpperCaseHex(final int digit) { 55 if (digit < 0 || digit >= 16) { 56 return '\0'; 57 } 58 return digit < 10 ? getNumericalDigit(digit) : getUpperCaseAlphaDigit(digit); 59 } 60 61 /** 62 * Converts a digit into an lower-case hexadecimal character or the null character if invalid. 63 * 64 * @param digit a number 0 - 15 65 * @return the hex character for that digit or '\0' if invalid 66 */ 67 public static char getLowerCaseHex(final int digit) { 68 if (digit < 0 || digit >= 16) { 69 return '\0'; 70 } 71 return digit < 10 ? getNumericalDigit(digit) : getLowerCaseAlphaDigit(digit); 72 } 73 74 private static char getNumericalDigit(final int digit) { 75 return (char) ('0' + digit); 76 } 77 78 private static char getUpperCaseAlphaDigit(final int digit) { 79 return (char) ('A' + digit - 10); 80 } 81 82 private static char getLowerCaseAlphaDigit(final int digit) { 83 return (char) ('a' + digit - 10); 84 } 85 86 private Chars() { 87 } 88 }