001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2018 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018//////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.checks; 021 022import java.nio.charset.StandardCharsets; 023import java.util.Arrays; 024 025/** 026 * Represents the options for line separator settings. 027 * 028 * @author lkuehne 029 * @see NewlineAtEndOfFileCheck 030 */ 031public enum LineSeparatorOption { 032 033 /** Windows-style line separators. **/ 034 CRLF("\r\n"), 035 036 /** Mac-style line separators. **/ 037 CR("\r"), 038 039 /** Unix-style line separators. **/ 040 LF("\n"), 041 042 /** 043 * Matches CR, LF and CRLF line separators. 044 * Only the length is used - the actual value is ignored. 045 */ 046 LF_CR_CRLF("##"), 047 048 /** System default line separators. **/ 049 SYSTEM(System.getProperty("line.separator")); 050 051 /** The line separator representation. */ 052 private final byte[] lineSeparator; 053 054 /** 055 * Creates a new {@code LineSeparatorOption} instance. 056 * @param sep the line separator, e.g. "\r\n" 057 */ 058 LineSeparatorOption(String sep) { 059 lineSeparator = sep.getBytes(StandardCharsets.US_ASCII); 060 } 061 062 /** 063 * Checks that bytes is equal to the byte representation of this line separator. 064 * @param bytes a bytes array to check 065 * @return if bytes is equal to the byte representation 066 * of this line separator 067 */ 068 public boolean matches(byte... bytes) { 069 final boolean result; 070 if (this == LF_CR_CRLF) { 071 // this silently assumes CRLF and ANY have the same length 072 // and LF and CR are of length 1 073 result = CRLF.matches(bytes) 074 || LF.matches(Arrays.copyOfRange(bytes, 1, 2)) 075 || CR.matches(Arrays.copyOfRange(bytes, 1, 2)); 076 } 077 else { 078 result = Arrays.equals(bytes, lineSeparator); 079 } 080 return result; 081 } 082 083 /** 084 * Returns length of file separator in bytes. 085 * @return the length of the file separator in bytes, 086 * e.g. 1 for CR, 2 for CRLF, ... 087 */ 088 public int length() { 089 return lineSeparator.length; 090 } 091 092}