1 /* 2 * Copyright (C) 2010, Google Inc. and others 3 * 4 * This program and the accompanying materials are made available under the 5 * terms of the Eclipse Distribution License v. 1.0 which is available at 6 * https://www.eclipse.org/org/documents/edl-v10.php. 7 * 8 * SPDX-License-Identifier: BSD-3-Clause 9 */ 10 11 package org.eclipse.jgit.util; 12 13 /** 14 * Utility class for character functions on raw bytes 15 * <p> 16 * Characters are assumed to be 8-bit US-ASCII. 17 */ 18 public class RawCharUtil { 19 private static final boolean[] WHITESPACE = new boolean[256]; 20 21 static { 22 WHITESPACE['\r'] = true; 23 WHITESPACE['\n'] = true; 24 WHITESPACE['\t'] = true; 25 WHITESPACE[' '] = true; 26 } 27 28 /** 29 * Determine if an 8-bit US-ASCII encoded character is represents whitespace 30 * 31 * @param c 32 * the 8-bit US-ASCII encoded character 33 * @return true if c represents a whitespace character in 8-bit US-ASCII 34 */ 35 public static boolean isWhitespace(byte c) { 36 return WHITESPACE[c & 0xff]; 37 } 38 39 /** 40 * Returns the new end point for the byte array passed in after trimming any 41 * trailing whitespace characters, as determined by the isWhitespace() 42 * function. start and end are assumed to be within the bounds of raw. 43 * 44 * @param raw 45 * the byte array containing the portion to trim whitespace for 46 * @param start 47 * the start of the section of bytes 48 * @param end 49 * the end of the section of bytes 50 * @return the new end point 51 */ 52 public static int trimTrailingWhitespace(byte[] raw, int start, int end) { 53 int ptr = end - 1; 54 while (start <= ptr && isWhitespace(raw[ptr])) 55 ptr--; 56 57 return ptr + 1; 58 } 59 60 /** 61 * Returns the new start point for the byte array passed in after trimming 62 * any leading whitespace characters, as determined by the isWhitespace() 63 * function. start and end are assumed to be within the bounds of raw. 64 * 65 * @param raw 66 * the byte array containing the portion to trim whitespace for 67 * @param start 68 * the start of the section of bytes 69 * @param end 70 * the end of the section of bytes 71 * @return the new start point 72 */ 73 public static int trimLeadingWhitespace(byte[] raw, int start, int end) { 74 while (start < end && isWhitespace(raw[start])) 75 start++; 76 77 return start; 78 } 79 80 private RawCharUtil() { 81 // This will never be called 82 } 83 }