RawCharUtil.java

  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. package org.eclipse.jgit.util;

  11. /**
  12.  * Utility class for character functions on raw bytes
  13.  * <p>
  14.  * Characters are assumed to be 8-bit US-ASCII.
  15.  */
  16. public class RawCharUtil {
  17.     private static final boolean[] WHITESPACE = new boolean[256];

  18.     static {
  19.         WHITESPACE['\r'] = true;
  20.         WHITESPACE['\n'] = true;
  21.         WHITESPACE['\t'] = true;
  22.         WHITESPACE[' '] = true;
  23.     }

  24.     /**
  25.      * Determine if an 8-bit US-ASCII encoded character is represents whitespace
  26.      *
  27.      * @param c
  28.      *            the 8-bit US-ASCII encoded character
  29.      * @return true if c represents a whitespace character in 8-bit US-ASCII
  30.      */
  31.     public static boolean isWhitespace(byte c) {
  32.         return WHITESPACE[c & 0xff];
  33.     }

  34.     /**
  35.      * Returns the new end point for the byte array passed in after trimming any
  36.      * trailing whitespace characters, as determined by the isWhitespace()
  37.      * function. start and end are assumed to be within the bounds of raw.
  38.      *
  39.      * @param raw
  40.      *            the byte array containing the portion to trim whitespace for
  41.      * @param start
  42.      *            the start of the section of bytes
  43.      * @param end
  44.      *            the end of the section of bytes
  45.      * @return the new end point
  46.      */
  47.     public static int trimTrailingWhitespace(byte[] raw, int start, int end) {
  48.         int ptr = end - 1;
  49.         while (start <= ptr && isWhitespace(raw[ptr]))
  50.             ptr--;

  51.         return ptr + 1;
  52.     }

  53.     /**
  54.      * Returns the new start point for the byte array passed in after trimming
  55.      * any leading whitespace characters, as determined by the isWhitespace()
  56.      * function. start and end are assumed to be within the bounds of raw.
  57.      *
  58.      * @param raw
  59.      *            the byte array containing the portion to trim whitespace for
  60.      * @param start
  61.      *            the start of the section of bytes
  62.      * @param end
  63.      *            the end of the section of bytes
  64.      * @return the new start point
  65.      */
  66.     public static int trimLeadingWhitespace(byte[] raw, int start, int end) {
  67.         while (start < end && isWhitespace(raw[start]))
  68.             start++;

  69.         return start;
  70.     }

  71.     private RawCharUtil() {
  72.         // This will never be called
  73.     }
  74. }