1 /////////////////////////////////////////////////////////////////////////////////////////////// 2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules. 3 // Copyright (C) 2001-2024 the original author or authors. 4 // 5 // This library is free software; you can redistribute it and/or 6 // modify it under the terms of the GNU Lesser General Public 7 // License as published by the Free Software Foundation; either 8 // version 2.1 of the License, or (at your option) any later version. 9 // 10 // This library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 // Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public 16 // License along with this library; if not, write to the Free Software 17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 /////////////////////////////////////////////////////////////////////////////////////////////// 19 20 package com.puppycrawl.tools.checkstyle.utils; 21 22 import java.util.Arrays; 23 24 /** 25 * Contains utility methods for code point. 26 */ 27 public final class CodePointUtil { 28 29 /** Stop instances being created. **/ 30 private CodePointUtil() { 31 } 32 33 /** 34 * Checks if given code point array is blank by either being empty, 35 * or contains only whitespace characters. 36 * 37 * @param codePoints The array of unicode code points of string to check. 38 * @return true if codePoints is blank. 39 */ 40 public static boolean isBlank(int... codePoints) { 41 return hasWhitespaceBefore(codePoints.length, codePoints); 42 } 43 44 /** 45 * Checks if the given code point array contains only whitespace up to specified index. 46 * 47 * @param codePoints 48 * array of Unicode code point of string to check 49 * @param index 50 * index to check up to (exclusive) 51 * @return true if all code points preceding given index are whitespace 52 */ 53 public static boolean hasWhitespaceBefore(int index, int... codePoints) { 54 return Arrays.stream(codePoints, 0, index) 55 .allMatch(Character::isWhitespace); 56 } 57 58 /** 59 * Removes trailing whitespaces. 60 * 61 * @param codePoints array of unicode code points 62 * @return unicode code points array with trailing whitespaces removed 63 */ 64 public static int[] stripTrailing(int... codePoints) { 65 int lastIndex = codePoints.length; 66 while (CommonUtil.isCodePointWhitespace(codePoints, lastIndex - 1)) { 67 lastIndex--; 68 } 69 return Arrays.copyOfRange(codePoints, 0, lastIndex); 70 } 71 72 /** 73 * Tests if the unicode code points array 74 * ends with the specified suffix. 75 * 76 * @param suffix the suffix 77 * @param codePoints the array of unicode code points to check 78 * @return {@code true}, if the unicode code points array ends with provided suffix 79 */ 80 public static boolean endsWith(int[] codePoints, String suffix) { 81 final int startIndex = codePoints.length - suffix.length(); 82 return startIndex > -1 && Arrays.equals(Arrays 83 .copyOfRange(codePoints, startIndex, codePoints.length), 84 suffix.codePoints().toArray()); 85 } 86 }